StreamManager refactoring
[platform/core/ml/aitt.git] / modules / webrtc / StreamManager.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 "StreamManager.h"
18
19 #include "aitt_internal.h"
20
21 namespace AittWebRTCNamespace {
22
23 StreamManager::StreamManager(const std::string &topic, const std::string &watching_topic,
24       const std::string &aitt_id, const std::string &thread_id)
25       : topic_(topic), watching_topic_(watching_topic), aitt_id_(aitt_id), thread_id_(thread_id)
26 {
27 }
28
29 void StreamManager::Start(void)
30 {
31     DBG("%s %s", __func__, GetTopic().c_str());
32     if (stream_start_cb_)
33         stream_start_cb_();
34 }
35
36 void StreamManager::Stop(void)
37 {
38     DBG("%s %s", __func__, GetTopic().c_str());
39     // TODO: You should take care about stream resource
40     for (auto itr = streams_.begin(); itr != streams_.end(); ++itr)
41         itr->second->Destroy();
42     streams_.clear();
43
44     if (stream_stop_cb_)
45         stream_stop_cb_();
46 }
47
48 void StreamManager::HandleRemovedClient(const std::string &discovery_id)
49 {
50     auto stream_itr = streams_.find(discovery_id);
51     if (stream_itr == streams_.end()) {
52         DBG("There's no stream %s", discovery_id.c_str());
53         return;
54     }
55
56     // TODO: You should take care about stream resource
57     stream_itr->second->Destroy();
58     streams_.erase(stream_itr);
59
60     return;
61 }
62
63 void StreamManager::HandleMsg(const std::string &discovery_id, const std::vector<uint8_t> &message)
64 {
65     if (flexbuffers::GetRoot(message).IsString())
66         HandleStreamState(discovery_id, message);
67     else if (flexbuffers::GetRoot(message).IsVector())
68         HandleStreamInfo(discovery_id, message);
69 }
70
71 std::string StreamManager::GetTopic(void) const
72 {
73     return topic_;
74 }
75
76 std::string StreamManager::GetWatchingTopic(void) const
77 {
78     return watching_topic_;
79 }
80
81 void StreamManager::SetIceCandidateAddedCallback(IceCandidateAddedCallback cb)
82 {
83     ice_candidate_added_cb_ = cb;
84 }
85
86 void StreamManager::SetStreamStartCallback(StreamStartCallback cb)
87 {
88     stream_start_cb_ = cb;
89 }
90
91 void StreamManager::SetStreamStopCallback(StreamStopCallback cb)
92 {
93     stream_stop_cb_ = cb;
94 }
95
96 }  // namespace AittWebRTCNamespace