-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathwrapper.cpp
More file actions
232 lines (185 loc) · 5.97 KB
/
Copy pathwrapper.cpp
File metadata and controls
232 lines (185 loc) · 5.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <string>
#include <stdio.h>
#include <netinet/in.h>
#include "talk/app/webrtc/audiotrack.h"
#include "talk/app/webrtc/mediaconstraintsinterface.h"
#include "talk/app/webrtc/mediastreaminterface.h"
#include "talk/app/webrtc/peerconnectionfactory.h"
#include "talk/app/webrtc/peerconnectioninterface.h"
#include "talk/app/webrtc/videosourceinterface.h"
#include "talk/app/webrtc/videotrack.h"
#include "talk/base/logging.h"
#include "talk/base/ssladapter.h"
#include "wrapper.h"
#include "session_description.h"
#include "media_constraints_prv.h"
extern "C" {
#include "_cgo_export.h"
using talk_base::scoped_refptr;
using talk_base::RefCountedObject;
using namespace webrtc;
#define CreateRaw(T, ctor) \
scoped_refptr<T> _ptr = (ctor); \
T* _raw = _ptr.get(); \
_raw->AddRef(); \
return _raw;
#define CastPtr(T, ptr) ((T*)ptr)
const char* WebRTC_Version() {
return "hello";
}
int WebRTC_InitializeSSL() {
return talk_base::InitializeSSL();
}
int WebRTC_CleanupSSL() {
return talk_base::CleanupSSL();
}
Factory WebRTC_PeerConnectionFactory_Create() {
CreateRaw(PeerConnectionFactoryInterface, webrtc::CreatePeerConnectionFactory());
}
void WebRTC_PeerConnectionFactory_Free(Factory ptr) {
if (ptr == NULL) return;
CastPtr(PeerConnectionFactoryInterface, ptr)->Release();
}
// RTCMediaStream
MediaStream WebRTC_CreateMediaStreamWithLabel(Factory ptr, char* clabel) {
PeerConnectionFactoryInterface* factory = CastPtr(PeerConnectionFactoryInterface, ptr);
std::string label = clabel;
CreateRaw(MediaStreamInterface, factory->CreateLocalMediaStream(label));
}
void WebRTC_MediaStream_Free(MediaStream ptr) {
if (ptr == NULL) return;
CastPtr(MediaStreamInterface, ptr)->Release();
}
class RTCPeerConnectionObserver : public PeerConnectionObserver {
public:
RTCPeerConnectionObserver(Ref ref) {
_ref = ref;
}
void OnError() {
c_RTCPeerConnectionObserver_OnError(_ref);
}
// Triggered when the SignalingState changed.
void OnSignalingChange(
PeerConnectionInterface::SignalingState new_state)
{
c_RTCPeerConnectionObserver_OnSignalingChange(_ref, int(new_state));
}
// Triggered when media is received on a new stream from remote peer.
void OnAddStream(MediaStreamInterface* stream)
{
c_RTCPeerConnectionObserver_OnAddStream(_ref);
}
// Triggered when a remote peer close a stream.
void OnRemoveStream(MediaStreamInterface* stream)
{
c_RTCPeerConnectionObserver_OnRemoveStream(_ref);
}
// Triggered when a remote peer open a data channel.
// TODO(perkj): Make pure
void OnDataChannel(DataChannelInterface* data_channel)
{
// keep the channel
data_channel->AddRef();
c_RTCPeerConnectionObserver_OnDataChannel(_ref, data_channel);
}
// Triggered when renegotiation is needed, for example the ICE has restarted.
void OnRenegotiationNeeded()
{
c_RTCPeerConnectionObserver_OnRenegotiationNeeded(_ref);
}
// Called any time the IceConnectionState changes
void OnIceConnectionChange(
PeerConnectionInterface::IceConnectionState new_state)
{
c_RTCPeerConnectionObserver_OnIceConnectionChange(_ref, (int)new_state);
}
// Called any time the IceGatheringState changes
void OnIceGatheringChange(
PeerConnectionInterface::IceGatheringState new_state)
{
c_RTCPeerConnectionObserver_OnIceGatheringChange(_ref, (int)new_state);
}
// New Ice candidate have been found.
void OnIceCandidate(const IceCandidateInterface* candidate)
{
std::string sdp_mid = candidate->sdp_mid();
int sdp_mline_index = candidate->sdp_mline_index();
std::string sdp;
if (!candidate->ToString(&sdp)) {
return;
}
void* _candidate = go_IceCandidate_ToGo(
(char*)sdp_mid.c_str(),
sdp_mline_index,
(char*)sdp.c_str()
);
c_RTCPeerConnectionObserver_OnIceCandidate(_ref, _candidate);
}
~RTCPeerConnectionObserver() {
go_Ref_Unregister(_ref);
}
private:
Ref _ref;
};
// PeerConnection
PeerConnection WebRTC_PeerConnection_Create(
Factory factory,
void* servers, int nservers,
void* constraints, int nconstraints,
Ref observerRef)
{
PeerConnectionInterface::IceServers iceServers;
void** rservers = (void**)servers;
void** rconstraints = (void**)constraints;
// collect servers
for (int i = 0; i < nservers; i++) {
PeerConnectionInterface::IceServer iceServer;
iceServer.uri = c_ICEServer_URL(rservers[i]);
iceServer.username = c_ICEServer_Username(rservers[i]);
iceServer.password = c_ICEServer_Password(rservers[i]);
iceServers.push_back(iceServer);
}
// setup observer
RTCPeerConnectionObserver* observer = new RTCPeerConnectionObserver(observerRef);
MediaConstraintsInterface* mconstraints = new MediaConstraints(rconstraints, nconstraints);
CreateRaw(
PeerConnectionInterface,
CastPtr(PeerConnectionFactoryInterface, factory)->CreatePeerConnection(
iceServers,
mconstraints,
NULL,
NULL,
observer
)
);
}
int WebRTC_PeerConnection_UpdateICE(
PeerConnection ptr,
void* servers, int nservers,
void* constraints, int nconstraints)
{
if (ptr == NULL) return 0;
PeerConnectionInterface* pc = CastPtr(PeerConnectionInterface, ptr);
PeerConnectionInterface::IceServers iceServers;
void** rservers = (void**)servers;
void** rconstraints = (void**)constraints;
// collect servers
for (int i = 0; i < nservers; i++) {
PeerConnectionInterface::IceServer iceServer;
iceServer.uri = c_ICEServer_URL(rservers[i]);
iceServer.username = c_ICEServer_Username(rservers[i]);
iceServer.password = c_ICEServer_Password(rservers[i]);
iceServers.push_back(iceServer);
}
MediaConstraintsInterface* mconstraints = new MediaConstraints(rconstraints, nconstraints);
return pc->UpdateIce(iceServers, mconstraints);
}
void WebRTC_PeerConnection_Free(PeerConnection ptr) {
if (ptr == NULL) return;
CastPtr(PeerConnectionInterface, ptr)->Release();
}
} // extern "C"