-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpeer_manager_imp.cpp
More file actions
299 lines (244 loc) · 10.5 KB
/
peer_manager_imp.cpp
File metadata and controls
299 lines (244 loc) · 10.5 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include "peer_manager_imp.h"
#include <iostream>
#include "glib.h"
#include "talk/app/webrtc/test/fakeperiodicvideocapturer.h"
#include "talk/app/webrtc/videosourceinterface.h"
#include "codecs/codec_factory.h"
#include "json_parser/json_parser.h"
#include "observers/set_sdp_observer.h"
#include "observers/create_sdp_observer.h"
#include "observers/peer_connection_observer.h"
namespace h264webrtc
{
const std::string AUDIO_LABEL = "audio_label";
const std::string VIDEO_LABEL = "video_label";
const std::string STREAM_LABEL = "stream_label";
// Names used for a IceCandidate JSON object.
const std::string CANDIDATE_SDP_MID_NAME = "sdpMid";
const std::string CANDIDATE_SDP_MLINE_INDEX_NAME = "sdpMLineIndex";
const std::string CANDIDATE_SDP_NAME = "candidate";
// Names used for a SessionDescription JSON object.
const std::string SDP_TYPE_NAME = "type";
const std::string SDP_NAME = "sdp";
class VideoCapturerListener : public sigslot::has_slots<>
{
public:
VideoCapturerListener(cricket::VideoCapturer *capturer)
{
capturer->SignalFrameCaptured.connect(this, &VideoCapturerListener::OnFrameCaptured);
}
void OnFrameCaptured(cricket::VideoCapturer *capturer, const cricket::CapturedFrame *frame) {
g_debug("%s", __FUNCTION__);
}
};
PeerManager *PeerManager::Create(const std::string &stunurl) {
return new PeerManagerImp(stunurl);
}
PeerManagerImp::PeerManagerImp(const std::string &stunurl) :
stunurl(stunurl)
{
signaling_thread = new rtc::Thread();
worker_thread = new rtc::Thread();
signaling_thread->Start();
worker_thread->Start();
cricket::WebRtcVideoEncoderFactory *encoder_factory = H264EncoderFactory::Create();
g_assert(encoder_factory);
cricket::WebRtcVideoDecoderFactory *decoder_factory = H264DecoderFactory::Create();
g_assert(decoder_factory);
peer_connection_factory = webrtc::CreatePeerConnectionFactory(
worker_thread,
signaling_thread,
NULL,
encoder_factory,
decoder_factory
);
if (!peer_connection_factory.get()) {
g_critical("Failed to initialize PeerConnectionFactory");
}
}
PeerManagerImp::~PeerManagerImp()
{
peer_connection_factory = NULL;
}
void PeerManagerImp::setOffser(const std::string &peerid, const Json::Value &sdp)
{
g_debug("setOffser -> peerid: %s", peerid.c_str());
std::string sdp_type, sdp_offer;
if (!GetStringFromJsonObject(sdp, SDP_TYPE_NAME, &sdp_type) ||
!GetStringFromJsonObject(sdp, SDP_NAME, &sdp_offer)) {
g_warning("setOffser <- Can't parse received message.");
return;
}
webrtc::SdpParseError sdp_parse_error;
webrtc::SessionDescriptionInterface *session_description = webrtc::CreateSessionDescription(sdp_type, sdp_offer, &sdp_parse_error);
if (!session_description) {
g_warning("setOffser <- Can't parse received session description message: %s", sdp_parse_error.description.c_str());
return;
}
g_debug("From peerid: %s, sdp sdp_type: %s", peerid.c_str(), session_description->type().c_str());
std::pair<rtc::scoped_refptr<webrtc::PeerConnectionInterface>, webrtc::PeerConnectionObserver *> peer_connection = CreatePeerConnection();
if (!peer_connection.first) {
g_warning("setOffser <- Fail to initialize peer connection");
return;
}
g_debug("Success to create peer connection");
// Set SDP offer to the PeerConnection
rtc::scoped_refptr<webrtc::PeerConnectionInterface> pc = peer_connection.first;
pc->SetRemoteDescription(SetSDPObserver::Create(), session_description);
// Register this peer
peer_connection_map.insert(std::pair<std::string, rtc::scoped_refptr<webrtc::PeerConnectionInterface> >(peerid, peer_connection.first));
peer_connectionobs_map.insert(std::pair<std::string, webrtc::PeerConnectionObserver *>(peerid, peer_connection.second));
// Create SDP answer
webrtc::FakeConstraints constraints;
constraints.SetMandatoryReceiveAudio(false);
constraints.SetMandatoryReceiveVideo(false);
pc->CreateAnswer(CreateSDPObserver::Create(pc, signal_sdp_feedback), &constraints);
g_debug("setOffser <- peerid: %s", peerid.c_str());
}
std::pair<rtc::scoped_refptr<webrtc::PeerConnectionInterface>, webrtc::PeerConnectionObserver *> PeerManagerImp::CreatePeerConnection()
{
webrtc::PeerConnectionInterface::IceServers servers;
webrtc::PeerConnectionInterface::IceServer server;
server.uri = "stun:" + stunurl;
servers.push_back(server);
PeerConnectionObserver *obs = PeerConnectionObserver::Create(signal_candidate_feedback);
rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection =
peer_connection_factory->CreatePeerConnection(servers, NULL, NULL, NULL, obs);
if (!peer_connection.get()) {
g_warning("CreatePeerConnection failed");
delete obs;
return std::pair<rtc::scoped_refptr<webrtc::PeerConnectionInterface>, webrtc::PeerConnectionObserver *>(
NULL, NULL);
}
AddStreams(peer_connection);
return std::pair<rtc::scoped_refptr<webrtc::PeerConnectionInterface>, webrtc::PeerConnectionObserver *>(
peer_connection, obs);
}
bool PeerManagerImp::AddStreams(webrtc::PeerConnectionInterface *peer_connection)
{
if (media_stream.get() == NULL) {
cricket::VideoCapturer *capturer = OpenVideoCaptureDevice();
if (!capturer) {
g_warning("Cannot create capturer");
return false;
}
// Register video capturer listener
//VideoCapturerListener listener(capturer);
// Create media stream
media_stream = peer_connection_factory->CreateLocalMediaStream(STREAM_LABEL);
if (!media_stream.get()) {
g_warning("Fail to create stream");
return false;
}
// Create video track
/*webrtc::FakeConstraints video_constraints;
video_constraints.SetMandatoryMinWidth(320);
video_constraints.SetMandatoryMinHeight(480);*/
rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
peer_connection_factory->CreateVideoTrack(VIDEO_LABEL,
peer_connection_factory->CreateVideoSource(capturer,
NULL))
);
// Create audio track
rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
peer_connection_factory->CreateAudioTrack(AUDIO_LABEL,
peer_connection_factory->CreateAudioSource(NULL))
);
if (!media_stream->AddTrack(video_track)) {
g_warning("Fail to add video track");
return false;
}
if (!media_stream->AddTrack(audio_track)) {
g_warning("Fail to add audio track");
return false;
}
}
if (!peer_connection->AddStream(media_stream)) {
g_warning("Fail to add media stream to PeerConnection");
return false;
}
return true;
}
cricket::VideoCapturer *PeerManagerImp::OpenVideoCaptureDevice()
{
cricket::VideoCapturer *capturer = NULL;
rtc::scoped_ptr<cricket::DeviceManagerInterface> dev_manager(cricket::DeviceManagerFactory::Create());
if (!dev_manager.get() || !dev_manager->Init()) {
g_warning("Fail to create device manager");
return NULL;
}
std::vector<cricket::Device> devs;
if (!dev_manager->GetVideoCaptureDevices(&devs)) {
g_warning("Fail to enumerate video devices");
return NULL;
}
// Try to use the normal camera device
for (std::vector<cricket::Device>::iterator it = devs.begin(); it != devs.end(); ++it) {
cricket::Device device = *it;
capturer = dev_manager->CreateVideoCapturer(device);
if (capturer) {
break;
} else {
g_warning("Fail to create video capture device: %s", it->id.c_str());
}
}
// Use the fake yuvframegenerator
if (capturer == NULL) {
cricket::Device device;
if (dev_manager->GetVideoCaptureDevice("YuvFramesGenerator", &device)) {
capturer = dev_manager->CreateVideoCapturer(device);
}
if (capturer == NULL) {
g_warning("Fail to get fake video devices");
} else {
g_warning("Success to create fake video devices");
}
}
if (capturer != NULL)
g_debug("Create capturer device: %s", capturer->GetId().c_str());
return capturer;
}
void PeerManagerImp::deletePeerConnection(const std::string &peerid)
{
std::map<std::string, rtc::scoped_refptr<webrtc::PeerConnectionInterface> >::iterator it = peer_connection_map.find(
peerid);
if (it != peer_connection_map.end()) {
it->second = NULL;
peer_connection_map.erase(it);
}
std::map<std::string, webrtc::PeerConnectionObserver *>::iterator obs_it = peer_connectionobs_map.find(peerid);
if (obs_it == peer_connectionobs_map.end()) {
PeerConnectionObserver *obs = dynamic_cast<PeerConnectionObserver *>(obs_it->second);
delete obs;
peer_connectionobs_map.erase(obs_it);
}
}
void PeerManagerImp::addIceCandidate(const std::string &peerid, const Json::Value &candidate)
{
g_debug("addIceCandidate -> peerid: %s", peerid.c_str());
std::map<std::string, rtc::scoped_refptr<webrtc::PeerConnectionInterface> >::iterator it = peer_connection_map.find(peerid);
if (it == peer_connection_map.end()) {
g_warning("addIceCandidate <- Fail to find the existed peer connection.");
return;
}
std::string sdp_mid, sdp;
int sdp_mlineindex = 0;
if (!GetStringFromJsonObject(candidate, CANDIDATE_SDP_MID_NAME, &sdp_mid) ||
!GetIntFromJsonObject(candidate, CANDIDATE_SDP_MLINE_INDEX_NAME, &sdp_mlineindex) ||
!GetStringFromJsonObject(candidate, CANDIDATE_SDP_NAME, &sdp)) {
g_warning("addIceCandidate <- Fail to parse received message.");
return;
}
rtc::scoped_ptr<webrtc::IceCandidateInterface> ice_candidate(webrtc::CreateIceCandidate(sdp_mid, sdp_mlineindex, sdp));
if (!ice_candidate.get()) {
g_warning("addIceCandidate <- Fail to parse received candidate message.");
return;
}
rtc::scoped_refptr<webrtc::PeerConnectionInterface> pc = it->second;
if (!pc->AddIceCandidate(ice_candidate.get())) {
g_warning("addIceCandidate <- Failed to apply the received candidate");
return;
}
g_debug("addIceCandidate <- peerid: %s", peerid.c_str());
}
}