1 /*
2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "examples/unityplugin/unity_plugin_apis.h"
12
13 #include <map>
14 #include <string>
15
16 #include "examples/unityplugin/simple_peer_connection.h"
17
18 namespace {
19 static int g_peer_connection_id = 1;
20 static std::map<int, rtc::scoped_refptr<SimplePeerConnection>>
21 g_peer_connection_map;
22 } // namespace
23
CreatePeerConnection(const char ** turn_urls,const int no_of_urls,const char * username,const char * credential,bool mandatory_receive_video)24 int CreatePeerConnection(const char** turn_urls,
25 const int no_of_urls,
26 const char* username,
27 const char* credential,
28 bool mandatory_receive_video) {
29 g_peer_connection_map[g_peer_connection_id] =
30 rtc::make_ref_counted<SimplePeerConnection>();
31
32 if (!g_peer_connection_map[g_peer_connection_id]->InitializePeerConnection(
33 turn_urls, no_of_urls, username, credential, mandatory_receive_video))
34 return -1;
35
36 return g_peer_connection_id++;
37 }
38
ClosePeerConnection(int peer_connection_id)39 bool ClosePeerConnection(int peer_connection_id) {
40 if (!g_peer_connection_map.count(peer_connection_id))
41 return false;
42
43 g_peer_connection_map[peer_connection_id]->DeletePeerConnection();
44 g_peer_connection_map.erase(peer_connection_id);
45 return true;
46 }
47
AddStream(int peer_connection_id,bool audio_only)48 bool AddStream(int peer_connection_id, bool audio_only) {
49 if (!g_peer_connection_map.count(peer_connection_id))
50 return false;
51
52 g_peer_connection_map[peer_connection_id]->AddStreams(audio_only);
53 return true;
54 }
55
AddDataChannel(int peer_connection_id)56 bool AddDataChannel(int peer_connection_id) {
57 if (!g_peer_connection_map.count(peer_connection_id))
58 return false;
59
60 return g_peer_connection_map[peer_connection_id]->CreateDataChannel();
61 }
62
CreateOffer(int peer_connection_id)63 bool CreateOffer(int peer_connection_id) {
64 if (!g_peer_connection_map.count(peer_connection_id))
65 return false;
66
67 return g_peer_connection_map[peer_connection_id]->CreateOffer();
68 }
69
CreateAnswer(int peer_connection_id)70 bool CreateAnswer(int peer_connection_id) {
71 if (!g_peer_connection_map.count(peer_connection_id))
72 return false;
73
74 return g_peer_connection_map[peer_connection_id]->CreateAnswer();
75 }
76
SendDataViaDataChannel(int peer_connection_id,const char * data)77 bool SendDataViaDataChannel(int peer_connection_id, const char* data) {
78 if (!g_peer_connection_map.count(peer_connection_id))
79 return false;
80
81 std::string s(data);
82 g_peer_connection_map[peer_connection_id]->SendDataViaDataChannel(s);
83
84 return true;
85 }
86
SetAudioControl(int peer_connection_id,bool is_mute,bool is_record)87 bool SetAudioControl(int peer_connection_id, bool is_mute, bool is_record) {
88 if (!g_peer_connection_map.count(peer_connection_id))
89 return false;
90
91 g_peer_connection_map[peer_connection_id]->SetAudioControl(is_mute,
92 is_record);
93 return true;
94 }
95
SetRemoteDescription(int peer_connection_id,const char * type,const char * sdp)96 bool SetRemoteDescription(int peer_connection_id,
97 const char* type,
98 const char* sdp) {
99 if (!g_peer_connection_map.count(peer_connection_id))
100 return false;
101
102 return g_peer_connection_map[peer_connection_id]->SetRemoteDescription(type,
103 sdp);
104 }
105
AddIceCandidate(const int peer_connection_id,const char * candidate,const int sdp_mlineindex,const char * sdp_mid)106 bool AddIceCandidate(const int peer_connection_id,
107 const char* candidate,
108 const int sdp_mlineindex,
109 const char* sdp_mid) {
110 if (!g_peer_connection_map.count(peer_connection_id))
111 return false;
112
113 return g_peer_connection_map[peer_connection_id]->AddIceCandidate(
114 candidate, sdp_mlineindex, sdp_mid);
115 }
116
117 // Register callback functions.
RegisterOnLocalI420FrameReady(int peer_connection_id,I420FRAMEREADY_CALLBACK callback)118 bool RegisterOnLocalI420FrameReady(int peer_connection_id,
119 I420FRAMEREADY_CALLBACK callback) {
120 if (!g_peer_connection_map.count(peer_connection_id))
121 return false;
122
123 g_peer_connection_map[peer_connection_id]->RegisterOnLocalI420FrameReady(
124 callback);
125 return true;
126 }
127
RegisterOnRemoteI420FrameReady(int peer_connection_id,I420FRAMEREADY_CALLBACK callback)128 bool RegisterOnRemoteI420FrameReady(int peer_connection_id,
129 I420FRAMEREADY_CALLBACK callback) {
130 if (!g_peer_connection_map.count(peer_connection_id))
131 return false;
132
133 g_peer_connection_map[peer_connection_id]->RegisterOnRemoteI420FrameReady(
134 callback);
135 return true;
136 }
137
RegisterOnLocalDataChannelReady(int peer_connection_id,LOCALDATACHANNELREADY_CALLBACK callback)138 bool RegisterOnLocalDataChannelReady(int peer_connection_id,
139 LOCALDATACHANNELREADY_CALLBACK callback) {
140 if (!g_peer_connection_map.count(peer_connection_id))
141 return false;
142
143 g_peer_connection_map[peer_connection_id]->RegisterOnLocalDataChannelReady(
144 callback);
145 return true;
146 }
147
RegisterOnDataFromDataChannelReady(int peer_connection_id,DATAFROMEDATECHANNELREADY_CALLBACK callback)148 bool RegisterOnDataFromDataChannelReady(
149 int peer_connection_id,
150 DATAFROMEDATECHANNELREADY_CALLBACK callback) {
151 if (!g_peer_connection_map.count(peer_connection_id))
152 return false;
153
154 g_peer_connection_map[peer_connection_id]->RegisterOnDataFromDataChannelReady(
155 callback);
156 return true;
157 }
158
RegisterOnFailure(int peer_connection_id,FAILURE_CALLBACK callback)159 bool RegisterOnFailure(int peer_connection_id, FAILURE_CALLBACK callback) {
160 if (!g_peer_connection_map.count(peer_connection_id))
161 return false;
162
163 g_peer_connection_map[peer_connection_id]->RegisterOnFailure(callback);
164 return true;
165 }
166
RegisterOnAudioBusReady(int peer_connection_id,AUDIOBUSREADY_CALLBACK callback)167 bool RegisterOnAudioBusReady(int peer_connection_id,
168 AUDIOBUSREADY_CALLBACK callback) {
169 if (!g_peer_connection_map.count(peer_connection_id))
170 return false;
171
172 g_peer_connection_map[peer_connection_id]->RegisterOnAudioBusReady(callback);
173 return true;
174 }
175
176 // Singnaling channel related functions.
RegisterOnLocalSdpReadytoSend(int peer_connection_id,LOCALSDPREADYTOSEND_CALLBACK callback)177 bool RegisterOnLocalSdpReadytoSend(int peer_connection_id,
178 LOCALSDPREADYTOSEND_CALLBACK callback) {
179 if (!g_peer_connection_map.count(peer_connection_id))
180 return false;
181
182 g_peer_connection_map[peer_connection_id]->RegisterOnLocalSdpReadytoSend(
183 callback);
184 return true;
185 }
186
RegisterOnIceCandidateReadytoSend(int peer_connection_id,ICECANDIDATEREADYTOSEND_CALLBACK callback)187 bool RegisterOnIceCandidateReadytoSend(
188 int peer_connection_id,
189 ICECANDIDATEREADYTOSEND_CALLBACK callback) {
190 if (!g_peer_connection_map.count(peer_connection_id))
191 return false;
192
193 g_peer_connection_map[peer_connection_id]->RegisterOnIceCandidateReadytoSend(
194 callback);
195 return true;
196 }
197