blob: ae98a833f5db1d9cfc440f51662cbf7e9bcf0258 [file] [log] [blame]
gyzhouad7cad82017-05-11 16:10:03 -07001/*
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 06:47:31 +020011#include "examples/unityplugin/unity_plugin_apis.h"
gyzhouad7cad82017-05-11 16:10:03 -070012
13#include <map>
14#include <string>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "examples/unityplugin/simple_peer_connection.h"
gyzhouad7cad82017-05-11 16:10:03 -070017
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 16:04:31 -070024int CreatePeerConnection(const char** turn_urls,
25 const int no_of_urls,
26 const char* username,
27 const char* credential) {
gyzhouad7cad82017-05-11 16:10:03 -070028 g_peer_connection_map[g_peer_connection_id] =
29 new rtc::RefCountedObject<SimplePeerConnection>();
30
31 if (!g_peer_connection_map[g_peer_connection_id]->InitializePeerConnection(
gyzhoub38f3862017-07-25 16:04:31 -070032 turn_urls, no_of_urls, username, credential, false))
gyzhouad7cad82017-05-11 16:10:03 -070033 return -1;
34
35 return g_peer_connection_id++;
36}
37
38bool ClosePeerConnection(int peer_connection_id) {
39 if (!g_peer_connection_map.count(peer_connection_id))
40 return false;
41
42 g_peer_connection_map[peer_connection_id]->DeletePeerConnection();
43 g_peer_connection_map.erase(peer_connection_id);
44 return true;
45}
46
47bool AddStream(int peer_connection_id, bool audio_only) {
48 if (!g_peer_connection_map.count(peer_connection_id))
49 return false;
50
51 g_peer_connection_map[peer_connection_id]->AddStreams(audio_only);
52 return true;
53}
54
55bool AddDataChannel(int peer_connection_id) {
56 if (!g_peer_connection_map.count(peer_connection_id))
57 return false;
58
59 return g_peer_connection_map[peer_connection_id]->CreateDataChannel();
60}
61
62bool CreateOffer(int peer_connection_id) {
63 if (!g_peer_connection_map.count(peer_connection_id))
64 return false;
65
66 return g_peer_connection_map[peer_connection_id]->CreateOffer();
67}
68
69bool CreateAnswer(int peer_connection_id) {
70 if (!g_peer_connection_map.count(peer_connection_id))
71 return false;
72
73 return g_peer_connection_map[peer_connection_id]->CreateAnswer();
74}
75
76bool SendDataViaDataChannel(int peer_connection_id, const char* data) {
77 if (!g_peer_connection_map.count(peer_connection_id))
78 return false;
79
80 std::string s(data);
81 g_peer_connection_map[peer_connection_id]->SendDataViaDataChannel(s);
82
83 return true;
84}
85
86bool SetAudioControl(int peer_connection_id, bool is_mute, bool is_record) {
87 if (!g_peer_connection_map.count(peer_connection_id))
88 return false;
89
90 g_peer_connection_map[peer_connection_id]->SetAudioControl(is_mute,
91 is_record);
92 return true;
93}
94
gyzhoub38f3862017-07-25 16:04:31 -070095bool SetRemoteDescription(int peer_connection_id,
96 const char* type,
97 const char* sdp) {
gyzhouad7cad82017-05-11 16:10:03 -070098 if (!g_peer_connection_map.count(peer_connection_id))
99 return false;
100
gyzhoub38f3862017-07-25 16:04:31 -0700101 return g_peer_connection_map[peer_connection_id]->SetRemoteDescription(type,
102 sdp);
103}
104
105bool AddIceCandidate(const int peer_connection_id,
106 const char* candidate,
107 const int sdp_mlineindex,
108 const char* sdp_mid) {
109 if (!g_peer_connection_map.count(peer_connection_id))
110 return false;
111
112 return g_peer_connection_map[peer_connection_id]->AddIceCandidate(
113 candidate, sdp_mlineindex, sdp_mid);
114}
115
116// Register callback functions.
117bool RegisterOnLocalI420FrameReady(int peer_connection_id,
118 I420FRAMEREADY_CALLBACK callback) {
119 if (!g_peer_connection_map.count(peer_connection_id))
120 return false;
121
122 g_peer_connection_map[peer_connection_id]->RegisterOnLocalI420FrameReady(
123 callback);
124 return true;
125}
126
127bool RegisterOnRemoteI420FrameReady(int peer_connection_id,
128 I420FRAMEREADY_CALLBACK callback) {
129 if (!g_peer_connection_map.count(peer_connection_id))
130 return false;
131
132 g_peer_connection_map[peer_connection_id]->RegisterOnRemoteI420FrameReady(
133 callback);
gyzhouad7cad82017-05-11 16:10:03 -0700134 return true;
135}
136
137bool RegisterOnLocalDataChannelReady(int peer_connection_id,
138 LOCALDATACHANNELREADY_CALLBACK callback) {
139 if (!g_peer_connection_map.count(peer_connection_id))
140 return false;
141
142 g_peer_connection_map[peer_connection_id]->RegisterOnLocalDataChannelReady(
143 callback);
144 return true;
145}
146
147bool RegisterOnDataFromDataChannelReady(
148 int peer_connection_id,
149 DATAFROMEDATECHANNELREADY_CALLBACK callback) {
150 if (!g_peer_connection_map.count(peer_connection_id))
151 return false;
152
153 g_peer_connection_map[peer_connection_id]->RegisterOnDataFromDataChannelReady(
154 callback);
155 return true;
156}
157
158bool RegisterOnFailure(int peer_connection_id, FAILURE_CALLBACK callback) {
159 if (!g_peer_connection_map.count(peer_connection_id))
160 return false;
161
162 g_peer_connection_map[peer_connection_id]->RegisterOnFailure(callback);
163 return true;
164}
165
166bool RegisterOnAudioBusReady(int peer_connection_id,
167 AUDIOBUSREADY_CALLBACK callback) {
168 if (!g_peer_connection_map.count(peer_connection_id))
169 return false;
170
171 g_peer_connection_map[peer_connection_id]->RegisterOnAudioBusReady(callback);
172 return true;
173}
174
175// Singnaling channel related functions.
176bool RegisterOnLocalSdpReadytoSend(int peer_connection_id,
177 LOCALSDPREADYTOSEND_CALLBACK callback) {
178 if (!g_peer_connection_map.count(peer_connection_id))
179 return false;
180
181 g_peer_connection_map[peer_connection_id]->RegisterOnLocalSdpReadytoSend(
182 callback);
183 return true;
184}
185
186bool RegisterOnIceCandiateReadytoSend(
187 int peer_connection_id,
188 ICECANDIDATEREADYTOSEND_CALLBACK callback) {
189 if (!g_peer_connection_map.count(peer_connection_id))
190 return false;
191
192 g_peer_connection_map[peer_connection_id]->RegisterOnIceCandiateReadytoSend(
193 callback);
194 return true;
195}