blob: 6e34d7e1e02d5edebdc0a38e224dc4de1237e376 [file] [log] [blame]
gyzhouad7cad82017-05-11 23:10:031/*
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
Mirko Bonadei92ea95e2017-09-15 04:47:3111#include "examples/unityplugin/unity_plugin_apis.h"
gyzhouad7cad82017-05-11 23:10:0312
13#include <map>
14#include <string>
15
Mirko Bonadei92ea95e2017-09-15 04:47:3116#include "examples/unityplugin/simple_peer_connection.h"
gyzhouad7cad82017-05-11 23:10:0317
18namespace {
19static int g_peer_connection_id = 1;
20static std::map<int, rtc::scoped_refptr<SimplePeerConnection>>
21 g_peer_connection_map;
22} // namespace
23
gyzhoub38f3862017-07-25 23:04:3124int CreatePeerConnection(const char** turn_urls,
25 const int no_of_urls,
26 const char* username,
Qiang Chen43fb9122017-12-20 18:47:3627 const char* credential,
28 bool mandatory_receive_video) {
gyzhouad7cad82017-05-11 23:10:0329 g_peer_connection_map[g_peer_connection_id] =
Niels Möller027c7932022-01-25 12:56:0730 rtc::make_ref_counted<SimplePeerConnection>();
gyzhouad7cad82017-05-11 23:10:0331
32 if (!g_peer_connection_map[g_peer_connection_id]->InitializePeerConnection(
Qiang Chen43fb9122017-12-20 18:47:3633 turn_urls, no_of_urls, username, credential, mandatory_receive_video))
gyzhouad7cad82017-05-11 23:10:0334 return -1;
35
36 return g_peer_connection_id++;
37}
38
39bool 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
48bool 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
56bool 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
63bool 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
70bool 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
77bool 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
87bool 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
gyzhoub38f3862017-07-25 23:04:3196bool SetRemoteDescription(int peer_connection_id,
97 const char* type,
98 const char* sdp) {
gyzhouad7cad82017-05-11 23:10:0399 if (!g_peer_connection_map.count(peer_connection_id))
100 return false;
101
gyzhoub38f3862017-07-25 23:04:31102 return g_peer_connection_map[peer_connection_id]->SetRemoteDescription(type,
103 sdp);
104}
105
106bool 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.
118bool 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
128bool 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);
gyzhouad7cad82017-05-11 23:10:03135 return true;
136}
137
138bool 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
148bool 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
159bool 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
167bool 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.
177bool 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
David Sanders60c588d2022-03-19 08:43:10187bool RegisterOnIceCandidateReadytoSend(
gyzhouad7cad82017-05-11 23:10:03188 int peer_connection_id,
189 ICECANDIDATEREADYTOSEND_CALLBACK callback) {
190 if (!g_peer_connection_map.count(peer_connection_id))
191 return false;
192
David Sanders60c588d2022-03-19 08:43:10193 g_peer_connection_map[peer_connection_id]->RegisterOnIceCandidateReadytoSend(
gyzhouad7cad82017-05-11 23:10:03194 callback);
195 return true;
196}