gyzhou | ad7cad8 | 2017-05-11 16:10:03 -0700 | [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 06:47:31 +0200 | [diff] [blame^] | 11 | #include "examples/unityplugin/unity_plugin_apis.h" |
gyzhou | ad7cad8 | 2017-05-11 16:10:03 -0700 | [diff] [blame] | 12 | |
| 13 | #include <map> |
| 14 | #include <string> |
| 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 16 | #include "examples/unityplugin/simple_peer_connection.h" |
gyzhou | ad7cad8 | 2017-05-11 16:10:03 -0700 | [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 16:04:31 -0700 | [diff] [blame] | 24 | int CreatePeerConnection(const char** turn_urls, |
| 25 | const int no_of_urls, |
| 26 | const char* username, |
| 27 | const char* credential) { |
gyzhou | ad7cad8 | 2017-05-11 16:10:03 -0700 | [diff] [blame] | 28 | 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( |
gyzhou | b38f386 | 2017-07-25 16:04:31 -0700 | [diff] [blame] | 32 | turn_urls, no_of_urls, username, credential, false)) |
gyzhou | ad7cad8 | 2017-05-11 16:10:03 -0700 | [diff] [blame] | 33 | return -1; |
| 34 | |
| 35 | return g_peer_connection_id++; |
| 36 | } |
| 37 | |
| 38 | bool 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 | |
| 47 | bool 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 | |
| 55 | bool 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 | |
| 62 | bool 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 | |
| 69 | bool 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 | |
| 76 | bool 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 | |
| 86 | bool 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 | |
gyzhou | b38f386 | 2017-07-25 16:04:31 -0700 | [diff] [blame] | 95 | bool SetRemoteDescription(int peer_connection_id, |
| 96 | const char* type, |
| 97 | const char* sdp) { |
gyzhou | ad7cad8 | 2017-05-11 16:10:03 -0700 | [diff] [blame] | 98 | if (!g_peer_connection_map.count(peer_connection_id)) |
| 99 | return false; |
| 100 | |
gyzhou | b38f386 | 2017-07-25 16:04:31 -0700 | [diff] [blame] | 101 | return g_peer_connection_map[peer_connection_id]->SetRemoteDescription(type, |
| 102 | sdp); |
| 103 | } |
| 104 | |
| 105 | bool 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. |
| 117 | bool 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 | |
| 127 | bool 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); |
gyzhou | ad7cad8 | 2017-05-11 16:10:03 -0700 | [diff] [blame] | 134 | return true; |
| 135 | } |
| 136 | |
| 137 | bool 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 | |
| 147 | bool 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 | |
| 158 | bool 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 | |
| 166 | bool 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. |
| 176 | bool 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 | |
| 186 | bool 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 | } |