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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/renderer/media/peer_connection_tracker.h"
#include "base/strings/utf_string_conversions.h"
#include "content/common/media/peer_connection_tracker_messages.h"
#include "content/renderer/media/rtc_media_constraints.h"
#include "content/renderer/media/rtc_peer_connection_handler.h"
#include "content/renderer/render_thread_impl.h"
#include "third_party/WebKit/public/platform/WebMediaStream.h"
#include "third_party/WebKit/public/platform/WebMediaStreamSource.h"
#include "third_party/WebKit/public/platform/WebMediaStreamTrack.h"
#include "third_party/WebKit/public/platform/WebRTCICECandidate.h"
#include "third_party/WebKit/public/platform/WebRTCPeerConnectionHandlerClient.h"
#include "third_party/WebKit/public/web/WebDocument.h"
#include "third_party/WebKit/public/web/WebFrame.h"
using std::string;
using webrtc::MediaConstraintsInterface;
using blink::WebRTCPeerConnectionHandlerClient;
namespace content {
static string SerializeServers(
const std::vector<webrtc::PeerConnectionInterface::IceServer>& servers) {
string result = "[";
for (size_t i = 0; i < servers.size(); ++i) {
result += servers[i].uri;
if (i != servers.size() - 1)
result += ", ";
}
result += "]";
return result;
}
static string SerializeMediaConstraints(
const RTCMediaConstraints& constraints) {
string result;
MediaConstraintsInterface::Constraints mandatory = constraints.GetMandatory();
if (!mandatory.empty()) {
result += "mandatory: {";
for (size_t i = 0; i < mandatory.size(); ++i) {
result += mandatory[i].key + ":" + mandatory[i].value;
if (i != mandatory.size() - 1)
result += ", ";
}
result += "}";
}
MediaConstraintsInterface::Constraints optional = constraints.GetOptional();
if (!optional.empty()) {
if (!result.empty())
result += ", ";
result += "optional: {";
for (size_t i = 0; i < optional.size(); ++i) {
result += optional[i].key + ":" + optional[i].value;
if (i != optional.size() - 1)
result += ", ";
}
result += "}";
}
return result;
}
static string SerializeMediaStreamComponent(
const blink::WebMediaStreamTrack component) {
string id = UTF16ToUTF8(component.source().id());
return id;
}
static string SerializeMediaDescriptor(
const blink::WebMediaStream& stream) {
string label = UTF16ToUTF8(stream.id());
string result = "label: " + label;
blink::WebVector<blink::WebMediaStreamTrack> tracks;
stream.audioTracks(tracks);
if (!tracks.isEmpty()) {
result += ", audio: [";
for (size_t i = 0; i < tracks.size(); ++i) {
result += SerializeMediaStreamComponent(tracks[i]);
if (i != tracks.size() - 1)
result += ", ";
}
result += "]";
}
stream.videoTracks(tracks);
if (!tracks.isEmpty()) {
result += ", video: [";
for (size_t i = 0; i < tracks.size(); ++i) {
result += SerializeMediaStreamComponent(tracks[i]);
if (i != tracks.size() - 1)
result += ", ";
}
result += "]";
}
return result;
}
#define GET_STRING_OF_STATE(state) \
case WebRTCPeerConnectionHandlerClient::state: \
result = #state; \
break;
static string GetSignalingStateString(
WebRTCPeerConnectionHandlerClient::SignalingState state) {
string result;
switch (state) {
GET_STRING_OF_STATE(SignalingStateStable)
GET_STRING_OF_STATE(SignalingStateHaveLocalOffer)
GET_STRING_OF_STATE(SignalingStateHaveRemoteOffer)
GET_STRING_OF_STATE(SignalingStateHaveLocalPrAnswer)
GET_STRING_OF_STATE(SignalingStateHaveRemotePrAnswer)
GET_STRING_OF_STATE(SignalingStateClosed)
default:
NOTREACHED();
break;
}
return result;
}
static string GetIceConnectionStateString(
WebRTCPeerConnectionHandlerClient::ICEConnectionState state) {
string result;
switch (state) {
GET_STRING_OF_STATE(ICEConnectionStateStarting)
GET_STRING_OF_STATE(ICEConnectionStateChecking)
GET_STRING_OF_STATE(ICEConnectionStateConnected)
GET_STRING_OF_STATE(ICEConnectionStateCompleted)
GET_STRING_OF_STATE(ICEConnectionStateFailed)
GET_STRING_OF_STATE(ICEConnectionStateDisconnected)
GET_STRING_OF_STATE(ICEConnectionStateClosed)
default:
NOTREACHED();
break;
}
return result;
}
static string GetIceGatheringStateString(
WebRTCPeerConnectionHandlerClient::ICEGatheringState state) {
string result;
switch (state) {
GET_STRING_OF_STATE(ICEGatheringStateNew)
GET_STRING_OF_STATE(ICEGatheringStateGathering)
GET_STRING_OF_STATE(ICEGatheringStateComplete)
default:
NOTREACHED();
break;
}
return result;
}
// Builds a DictionaryValue from the StatsReport.
// The caller takes the ownership of the returned value.
// Note:
// The format must be consistent with what webrtc_internals.js expects.
// If you change it here, you must change webrtc_internals.js as well.
static base::DictionaryValue* GetDictValueStats(
const webrtc::StatsReport& report) {
if (report.values.empty())
return NULL;
DictionaryValue* dict = new base::DictionaryValue();
dict->SetDouble("timestamp", report.timestamp);
base::ListValue* values = new base::ListValue();
dict->Set("values", values);
for (size_t i = 0; i < report.values.size(); ++i) {
values->AppendString(report.values[i].name);
values->AppendString(report.values[i].value);
}
return dict;
}
// Builds a DictionaryValue from the StatsReport.
// The caller takes the ownership of the returned value.
static base::DictionaryValue* GetDictValue(const webrtc::StatsReport& report) {
scoped_ptr<base::DictionaryValue> stats, result;
stats.reset(GetDictValueStats(report));
if (!stats)
return NULL;
result.reset(new base::DictionaryValue());
// Note:
// The format must be consistent with what webrtc_internals.js expects.
// If you change it here, you must change webrtc_internals.js as well.
result->Set("stats", stats.release());
result->SetString("id", report.id);
result->SetString("type", report.type);
return result.release();
}
class InternalStatsObserver : public webrtc::StatsObserver {
public:
InternalStatsObserver(int lid)
: lid_(lid){}
virtual void OnComplete(
const std::vector<webrtc::StatsReport>& reports) OVERRIDE {
base::ListValue list;
for (size_t i = 0; i < reports.size(); ++i) {
base::DictionaryValue* report = GetDictValue(reports[i]);
if (report)
list.Append(report);
}
if (!list.empty())
RenderThreadImpl::current()->Send(
new PeerConnectionTrackerHost_AddStats(lid_, list));
}
protected:
virtual ~InternalStatsObserver() {}
private:
int lid_;
};
PeerConnectionTracker::PeerConnectionTracker() : next_lid_(1) {
}
PeerConnectionTracker::~PeerConnectionTracker() {
}
bool PeerConnectionTracker::OnControlMessageReceived(
const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(PeerConnectionTracker, message)
IPC_MESSAGE_HANDLER(PeerConnectionTracker_GetAllStats, OnGetAllStats)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
void PeerConnectionTracker::OnGetAllStats() {
for (PeerConnectionIdMap::iterator it = peer_connection_id_map_.begin();
it != peer_connection_id_map_.end(); ++it) {
talk_base::scoped_refptr<InternalStatsObserver> observer(
new talk_base::RefCountedObject<InternalStatsObserver>(it->second));
it->first->GetStats(observer, NULL);
}
}
void PeerConnectionTracker::RegisterPeerConnection(
RTCPeerConnectionHandler* pc_handler,
const std::vector<webrtc::PeerConnectionInterface::IceServer>& servers,
const RTCMediaConstraints& constraints,
const blink::WebFrame* frame) {
DVLOG(1) << "PeerConnectionTracker::RegisterPeerConnection()";
PeerConnectionInfo info;
info.lid = GetNextLocalID();
info.servers = SerializeServers(servers);
info.constraints = SerializeMediaConstraints(constraints);
info.url = frame->document().url().spec();
RenderThreadImpl::current()->Send(
new PeerConnectionTrackerHost_AddPeerConnection(info));
DCHECK(peer_connection_id_map_.find(pc_handler) ==
peer_connection_id_map_.end());
peer_connection_id_map_[pc_handler] = info.lid;
}
void PeerConnectionTracker::UnregisterPeerConnection(
RTCPeerConnectionHandler* pc_handler) {
DVLOG(1) << "PeerConnectionTracker::UnregisterPeerConnection()";
std::map<RTCPeerConnectionHandler*, int>::iterator it =
peer_connection_id_map_.find(pc_handler);
if (it == peer_connection_id_map_.end()) {
// The PeerConnection might not have been registered if its initilization
// failed.
return;
}
RenderThreadImpl::current()->Send(
new PeerConnectionTrackerHost_RemovePeerConnection(it->second));
peer_connection_id_map_.erase(it);
}
void PeerConnectionTracker::TrackCreateOffer(
RTCPeerConnectionHandler* pc_handler,
const RTCMediaConstraints& constraints) {
SendPeerConnectionUpdate(
pc_handler, "createOffer",
"constraints: {" + SerializeMediaConstraints(constraints) + "}");
}
void PeerConnectionTracker::TrackCreateAnswer(
RTCPeerConnectionHandler* pc_handler,
const RTCMediaConstraints& constraints) {
SendPeerConnectionUpdate(
pc_handler, "createAnswer",
"constraints: {" + SerializeMediaConstraints(constraints) + "}");
}
void PeerConnectionTracker::TrackSetSessionDescription(
RTCPeerConnectionHandler* pc_handler,
const blink::WebRTCSessionDescription& desc,
Source source) {
string sdp = UTF16ToUTF8(desc.sdp());
string type = UTF16ToUTF8(desc.type());
string value = "type: " + type + ", sdp: " + sdp;
SendPeerConnectionUpdate(
pc_handler,
source == SOURCE_LOCAL ? "setLocalDescription" : "setRemoteDescription",
value);
}
void PeerConnectionTracker::TrackUpdateIce(
RTCPeerConnectionHandler* pc_handler,
const std::vector<webrtc::PeerConnectionInterface::IceServer>& servers,
const RTCMediaConstraints& options) {
string servers_string = "servers: " + SerializeServers(servers);
string constraints =
"constraints: {" + SerializeMediaConstraints(options) + "}";
SendPeerConnectionUpdate(
pc_handler, "updateIce", servers_string + ", " + constraints);
}
void PeerConnectionTracker::TrackAddIceCandidate(
RTCPeerConnectionHandler* pc_handler,
const blink::WebRTCICECandidate& candidate,
Source source) {
string value = "mid: " + UTF16ToUTF8(candidate.sdpMid()) + ", " +
"candidate: " + UTF16ToUTF8(candidate.candidate());
SendPeerConnectionUpdate(
pc_handler,
source == SOURCE_LOCAL ? "onIceCandidate" : "addIceCandidate", value);
}
void PeerConnectionTracker::TrackAddStream(
RTCPeerConnectionHandler* pc_handler,
const blink::WebMediaStream& stream,
Source source){
SendPeerConnectionUpdate(
pc_handler, source == SOURCE_LOCAL ? "addStream" : "onAddStream",
SerializeMediaDescriptor(stream));
}
void PeerConnectionTracker::TrackRemoveStream(
RTCPeerConnectionHandler* pc_handler,
const blink::WebMediaStream& stream,
Source source){
SendPeerConnectionUpdate(
pc_handler, source == SOURCE_LOCAL ? "removeStream" : "onRemoveStream",
SerializeMediaDescriptor(stream));
}
void PeerConnectionTracker::TrackCreateDataChannel(
RTCPeerConnectionHandler* pc_handler,
const webrtc::DataChannelInterface* data_channel,
Source source) {
string value = "label: " + data_channel->label() +
", reliable: " + (data_channel->reliable() ? "true" : "false");
SendPeerConnectionUpdate(
pc_handler,
source == SOURCE_LOCAL ? "createLocalDataChannel" : "onRemoteDataChannel",
value);
}
void PeerConnectionTracker::TrackStop(RTCPeerConnectionHandler* pc_handler) {
SendPeerConnectionUpdate(pc_handler, "stop", std::string());
}
void PeerConnectionTracker::TrackSignalingStateChange(
RTCPeerConnectionHandler* pc_handler,
WebRTCPeerConnectionHandlerClient::SignalingState state) {
SendPeerConnectionUpdate(
pc_handler, "signalingStateChange", GetSignalingStateString(state));
}
void PeerConnectionTracker::TrackIceConnectionStateChange(
RTCPeerConnectionHandler* pc_handler,
WebRTCPeerConnectionHandlerClient::ICEConnectionState state) {
SendPeerConnectionUpdate(
pc_handler, "iceConnectionStateChange",
GetIceConnectionStateString(state));
}
void PeerConnectionTracker::TrackIceGatheringStateChange(
RTCPeerConnectionHandler* pc_handler,
WebRTCPeerConnectionHandlerClient::ICEGatheringState state) {
SendPeerConnectionUpdate(
pc_handler, "iceGatheringStateChange",
GetIceGatheringStateString(state));
}
void PeerConnectionTracker::TrackSessionDescriptionCallback(
RTCPeerConnectionHandler* pc_handler, Action action,
const string& callback_type, const string& value) {
string update_type;
switch (action) {
case ACTION_SET_LOCAL_DESCRIPTION:
update_type = "setLocalDescription";
break;
case ACTION_SET_REMOTE_DESCRIPTION:
update_type = "setRemoteDescription";
break;
case ACTION_CREATE_OFFER:
update_type = "createOffer";
break;
case ACTION_CREATE_ANSWER:
update_type = "createAnswer";
break;
default:
NOTREACHED();
break;
}
update_type += callback_type;
SendPeerConnectionUpdate(pc_handler, update_type, value);
}
void PeerConnectionTracker::TrackOnRenegotiationNeeded(
RTCPeerConnectionHandler* pc_handler) {
SendPeerConnectionUpdate(pc_handler, "onRenegotiationNeeded", std::string());
}
void PeerConnectionTracker::TrackCreateDTMFSender(
RTCPeerConnectionHandler* pc_handler,
const blink::WebMediaStreamTrack& track) {
SendPeerConnectionUpdate(pc_handler, "createDTMFSender",
UTF16ToUTF8(track.id()));
}
int PeerConnectionTracker::GetNextLocalID() {
return next_lid_++;
}
void PeerConnectionTracker::SendPeerConnectionUpdate(
RTCPeerConnectionHandler* pc_handler,
const std::string& type,
const std::string& value) {
if (peer_connection_id_map_.find(pc_handler) == peer_connection_id_map_.end())
return;
RenderThreadImpl::current()->Send(
new PeerConnectionTrackerHost_UpdatePeerConnection(
peer_connection_id_map_[pc_handler], type, value));
}
} // namespace content
|