StreamManager refactoring
[platform/core/ml/aitt.git] / modules / webrtc / Module.cc
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "Module.h"
18
19 #include <SinkStreamManager.h>
20 #include <SrcStreamManager.h>
21 #include <flatbuffers/flexbuffers.h>
22
23 #include <sstream>
24 #include <thread>
25
26 #include "AittException.h"
27 #include "aitt_internal.h"
28
29 namespace AittWebRTCNamespace {
30
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)
36 {
37     std::stringstream s_stream;
38     s_stream << std::this_thread::get_id();
39
40     if (is_source_)
41         stream_manager_ = new SrcStreamManager(topic, discovery_.GetId(), s_stream.str());
42     else
43         stream_manager_ = new SinkStreamManager(topic, discovery_.GetId(), s_stream.str());
44
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));
48 }
49
50 Module::~Module(void)
51 {
52     discovery_.RemoveDiscoveryCB(discovery_cb_);
53     if (stream_manager_)
54         delete stream_manager_;
55 }
56
57 void Module::SetConfig(const std::string &key, const std::string &value)
58 {
59 }
60
61 void Module::SetConfig(const std::string &key, void *obj)
62 {
63 }
64
65 void Module::Start(void)
66 {
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));
70
71     stream_manager_->Start();
72 }
73
74 void Module::Stop(void)
75 {
76 }
77
78 void Module::OnIceCandidateAdded(void)
79 {
80     DBG("OnIceCandidateAdded");
81     auto msg = stream_manager_->GetDiscoveryMessage();
82     discovery_.UpdateDiscoveryMsg(stream_manager_->GetTopic(), msg.data(), msg.size());
83 }
84
85 void Module::OnStreamStarted(void)
86 {
87     std::vector<uint8_t> msg;
88
89     flexbuffers::Builder fbb;
90     fbb.String("START");
91     fbb.Finish();
92
93     msg = fbb.GetBuffer();
94     discovery_.UpdateDiscoveryMsg(stream_manager_->GetTopic(), msg.data(), msg.size());
95 }
96
97 void Module::OnStreamStopped(void)
98 {
99     std::vector<uint8_t> msg;
100
101     flexbuffers::Builder fbb;
102     fbb.String("STOP");
103     fbb.Finish();
104
105     msg = fbb.GetBuffer();
106     discovery_.UpdateDiscoveryMsg(stream_manager_->GetTopic(), msg.data(), msg.size());
107 }
108
109 void Module::SetStateCallback(StateCallback cb, void *user_data)
110 {
111     state_callback_ = cb;
112     state_cb_user_data_ = user_data;
113 }
114
115 void Module::SetReceiveCallback(ReceiveCallback cb, void *user_data)
116 {
117     receive_callback_ = cb;
118     receive_cb_user_data_ = user_data;
119 }
120
121 void Module::DiscoveryMessageCallback(const std::string &clientId, const std::string &status,
122       const void *msg, const int szmsg)
123 {
124     if (!status.compare(AittDiscovery::WILL_LEAVE_NETWORK)) {
125         stream_manager_->HandleRemovedClient(clientId);
126         return;
127     }
128
129     stream_manager_->HandleMsg(clientId, std::vector<uint8_t>(static_cast<const uint8_t *>(msg),
130                                                static_cast<const uint8_t *>(msg) + szmsg));
131 }
132
133 }  // namespace AittWebRTCNamespace