gyzhou | ad7cad8 | 2017-05-11 23:10:03 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #include "examples/unityplugin/unity_plugin_apis.h" |
gyzhou | ad7cad8 | 2017-05-11 23:10:03 | [diff] [blame] | 12 | |
| 13 | #include <map> |
| 14 | #include <string> |
| 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 16 | #include "examples/unityplugin/simple_peer_connection.h" |
gyzhou | ad7cad8 | 2017-05-11 23:10:03 | [diff] [blame] | 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 | |
gyzhou | b38f386 | 2017-07-25 23:04:31 | [diff] [blame] | 24 | int CreatePeerConnection(const char** turn_urls, |
| 25 | const int no_of_urls, |
| 26 | const char* username, |
Qiang Chen | 43fb912 | 2017-12-20 18:47:36 | [diff] [blame] | 27 | const char* credential, |
| 28 | bool mandatory_receive_video) { |
gyzhou | ad7cad8 | 2017-05-11 23:10:03 | [diff] [blame] | 29 | g_peer_connection_map[g_peer_connection_id] = |
Niels Möller | 027c793 | 2022-01-25 12:56:07 | [diff] [blame] | 30 | rtc::make_ref_counted<SimplePeerConnection>(); |
gyzhou | ad7cad8 | 2017-05-11 23:10:03 | [diff] [blame] | 31 | |
| 32 | if (!g_peer_connection_map[g_peer_connection_id]->InitializePeerConnection( |
Qiang Chen | 43fb912 | 2017-12-20 18:47:36 | [diff] [blame] | 33 | turn_urls, no_of_urls, username, credential, mandatory_receive_video)) |
gyzhou | ad7cad8 | 2017-05-11 23:10:03 | [diff] [blame] | 34 | return -1; |
| 35 | |
| 36 | return g_peer_connection_id++; |
| 37 | } |
| 38 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | |
gyzhou | b38f386 | 2017-07-25 23:04:31 | [diff] [blame] | 96 | bool SetRemoteDescription(int peer_connection_id, |
| 97 | const char* type, |
| 98 | const char* sdp) { |
gyzhou | ad7cad8 | 2017-05-11 23:10:03 | [diff] [blame] | 99 | if (!g_peer_connection_map.count(peer_connection_id)) |
| 100 | return false; |
| 101 | |
gyzhou | b38f386 | 2017-07-25 23:04:31 | [diff] [blame] | 102 | return g_peer_connection_map[peer_connection_id]->SetRemoteDescription(type, |
| 103 | sdp); |
| 104 | } |
| 105 | |
| 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. |
| 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 | |
| 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); |
gyzhou | ad7cad8 | 2017-05-11 23:10:03 | [diff] [blame] | 135 | return true; |
| 136 | } |
| 137 | |
| 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 | |
| 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 | |
| 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 | |
| 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. |
| 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 | |
David Sanders | 60c588d | 2022-03-19 08:43:10 | [diff] [blame] | 187 | bool RegisterOnIceCandidateReadytoSend( |
gyzhou | ad7cad8 | 2017-05-11 23:10:03 | [diff] [blame] | 188 | int peer_connection_id, |
| 189 | ICECANDIDATEREADYTOSEND_CALLBACK callback) { |
| 190 | if (!g_peer_connection_map.count(peer_connection_id)) |
| 191 | return false; |
| 192 | |
David Sanders | 60c588d | 2022-03-19 08:43:10 | [diff] [blame] | 193 | g_peer_connection_map[peer_connection_id]->RegisterOnIceCandidateReadytoSend( |
gyzhou | ad7cad8 | 2017-05-11 23:10:03 | [diff] [blame] | 194 | callback); |
| 195 | return true; |
| 196 | } |