2 * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <SinkStreamManager.h>
20 #include <SrcStreamManager.h>
21 #include <flatbuffers/flexbuffers.h>
26 #include "AittException.h"
27 #include "aitt_internal.h"
29 namespace AittWebRTCNamespace {
31 Module::Module(AittDiscovery &discovery, const std::string &topic, AittStreamRole role)
32 : is_source_(role == AittStreamRole::AITT_STREAM_ROLE_PUBLISHER),
33 discovery_(discovery),
34 state_cb_user_data_(nullptr),
35 receive_cb_user_data_(nullptr)
37 std::stringstream s_stream;
38 s_stream << std::this_thread::get_id();
41 stream_manager_ = new SrcStreamManager(topic, discovery_.GetId(), s_stream.str());
43 stream_manager_ = new SinkStreamManager(topic, discovery_.GetId(), s_stream.str());
45 discovery_cb_ = discovery_.AddDiscoveryCB(stream_manager_->GetWatchingTopic(),
46 std::bind(&Module::DiscoveryMessageCallback, this, std::placeholders::_1,
47 std::placeholders::_2, std::placeholders::_3, std::placeholders::_4));
52 discovery_.RemoveDiscoveryCB(discovery_cb_);
54 delete stream_manager_;
57 void Module::SetConfig(const std::string &key, const std::string &value)
61 void Module::SetConfig(const std::string &key, void *obj)
65 void Module::Start(void)
67 stream_manager_->SetIceCandidateAddedCallback(std::bind(&Module::OnIceCandidateAdded, this));
68 stream_manager_->SetStreamStartCallback(std::bind(&Module::OnStreamStarted, this));
69 stream_manager_->SetStreamStopCallback(std::bind(&Module::OnStreamStopped, this));
71 stream_manager_->Start();
74 void Module::Stop(void)
78 void Module::OnIceCandidateAdded(void)
80 DBG("OnIceCandidateAdded");
81 auto msg = stream_manager_->GetDiscoveryMessage();
82 discovery_.UpdateDiscoveryMsg(stream_manager_->GetTopic(), msg.data(), msg.size());
85 void Module::OnStreamStarted(void)
87 std::vector<uint8_t> msg;
89 flexbuffers::Builder fbb;
93 msg = fbb.GetBuffer();
94 discovery_.UpdateDiscoveryMsg(stream_manager_->GetTopic(), msg.data(), msg.size());
97 void Module::OnStreamStopped(void)
99 std::vector<uint8_t> msg;
101 flexbuffers::Builder fbb;
105 msg = fbb.GetBuffer();
106 discovery_.UpdateDiscoveryMsg(stream_manager_->GetTopic(), msg.data(), msg.size());
109 void Module::SetStateCallback(StateCallback cb, void *user_data)
111 state_callback_ = cb;
112 state_cb_user_data_ = user_data;
115 void Module::SetReceiveCallback(ReceiveCallback cb, void *user_data)
117 receive_callback_ = cb;
118 receive_cb_user_data_ = user_data;
121 void Module::DiscoveryMessageCallback(const std::string &clientId, const std::string &status,
122 const void *msg, const int szmsg)
124 if (!status.compare(AittDiscovery::WILL_LEAVE_NETWORK)) {
125 stream_manager_->HandleRemovedClient(clientId);
129 stream_manager_->HandleMsg(clientId, std::vector<uint8_t>(static_cast<const uint8_t *>(msg),
130 static_cast<const uint8_t *>(msg) + szmsg));
133 } // namespace AittWebRTCNamespace