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
|
// Copyright (c) 2012 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/media_stream_dispatcher.h"
#include "base/logging.h"
#include "content/common/media/media_stream_messages.h"
#include "content/renderer/media/media_stream_dispatcher_eventhandler.h"
#include "content/renderer/render_view_impl.h"
#include "googleurl/src/gurl.h"
struct MediaStreamDispatcher::Request {
Request(const base::WeakPtr<MediaStreamDispatcherEventHandler>& handler,
int request_id,
int ipc_request)
: handler(handler),
request_id(request_id),
ipc_request(ipc_request) {
}
base::WeakPtr<MediaStreamDispatcherEventHandler> handler;
int request_id;
int ipc_request;
};
struct MediaStreamDispatcher::Stream {
Stream() {}
~Stream() {}
base::WeakPtr<MediaStreamDispatcherEventHandler> handler;
media_stream::StreamDeviceInfoArray audio_array;
media_stream::StreamDeviceInfoArray video_array;
};
MediaStreamDispatcher::EnumerationRequest::EnumerationRequest(
const base::WeakPtr<MediaStreamDispatcherEventHandler>& handler,
int request_id)
: handler(handler),
request_id(request_id) {
}
MediaStreamDispatcher::EnumerationRequest::~EnumerationRequest() {}
MediaStreamDispatcher::EnumerationState::EnumerationState()
: ipc_id(-1) {
}
MediaStreamDispatcher::EnumerationState::~EnumerationState() {}
struct MediaStreamDispatcher::EnumerationState::CachedDevices {
CachedDevices(const std::string& label,
const media_stream::StreamDeviceInfoArray& device_array)
: label(label),
devices(device_array) {
}
~CachedDevices() {}
std::string label;
media_stream::StreamDeviceInfoArray devices;
};
MediaStreamDispatcher::MediaStreamDispatcher(RenderViewImpl* render_view)
: content::RenderViewObserver(render_view),
main_loop_(base::MessageLoopProxy::current()),
next_ipc_id_(0) {
}
MediaStreamDispatcher::~MediaStreamDispatcher() {}
void MediaStreamDispatcher::GenerateStream(
int request_id,
const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler,
const media_stream::StreamOptions& components,
const GURL& security_origin) {
DCHECK(main_loop_->BelongsToCurrentThread());
DVLOG(1) << "MediaStreamDispatcher::GenerateStream(" << request_id << ")";
requests_.push_back(Request(event_handler, request_id, next_ipc_id_));
Send(new MediaStreamHostMsg_GenerateStream(routing_id(),
next_ipc_id_++,
components,
security_origin));
}
void MediaStreamDispatcher::CancelGenerateStream(int request_id) {
DCHECK(main_loop_->BelongsToCurrentThread());
DVLOG(1) << "MediaStreamDispatcher::CancelGenerateStream"
<< ", {request_id = " << request_id << "}";
RequestList::iterator it = requests_.begin();
for (; it != requests_.end(); ++it) {
Request& request = *it;
if (request.request_id == request_id) {
requests_.erase(it);
Send(new MediaStreamHostMsg_CancelGenerateStream(routing_id(),
request_id));
break;
}
}
}
void MediaStreamDispatcher::StopStream(const std::string& label) {
DCHECK(main_loop_->BelongsToCurrentThread());
DVLOG(1) << "MediaStreamDispatcher::StopStream"
<< ", {label = " << label << "}";
LabelStreamMap::iterator it = label_stream_map_.find(label);
if (it == label_stream_map_.end())
return;
Send(new MediaStreamHostMsg_StopGeneratedStream(routing_id(), label));
label_stream_map_.erase(it);
}
void MediaStreamDispatcher::EnumerateDevices(
int request_id,
const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler,
media_stream::MediaStreamType type,
const GURL& security_origin) {
DCHECK(main_loop_->BelongsToCurrentThread());
DCHECK(type == content::MEDIA_DEVICE_AUDIO_CAPTURE ||
type == content::MEDIA_DEVICE_VIDEO_CAPTURE);
DVLOG(1) << "MediaStreamDispatcher::EnumerateDevices("
<< request_id << ")";
EnumerationState* state =
(type == content::MEDIA_DEVICE_AUDIO_CAPTURE ?
&audio_enumeration_state_ : &video_enumeration_state_);
state->requests.push_back(EnumerationRequest(event_handler, request_id));
if (state->cached_devices.get()) {
event_handler->OnDevicesEnumerated(
request_id, state->cached_devices->devices);
} else if (state->ipc_id < 0) {
Send(new MediaStreamHostMsg_EnumerateDevices(routing_id(),
next_ipc_id_,
type,
security_origin));
state->ipc_id = next_ipc_id_++;
}
}
void MediaStreamDispatcher::StopEnumerateDevices(
int request_id,
const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler) {
DCHECK(main_loop_->BelongsToCurrentThread());
DVLOG(1) << "MediaStreamDispatcher::StopEnumerateDevices("
<< request_id << ")";
// Remove the request.
RemoveEnumerationRequest(
request_id, event_handler, &audio_enumeration_state_);
RemoveEnumerationRequest(
request_id, event_handler, &video_enumeration_state_);
}
void MediaStreamDispatcher::RemoveEnumerationRequest(
int request_id,
const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler,
EnumerationState* state) {
EnumerationRequestList* requests = &state->requests;
for (EnumerationRequestList::iterator it = requests->begin();
it != requests->end(); ++it) {
if (it->request_id == request_id && it->handler == event_handler) {
requests->erase(it);
if (requests->empty() && !state->cached_devices.get()) {
// No more request and has a label, try to stop the label
// and invalidate the state.
Send(new MediaStreamHostMsg_StopGeneratedStream(
routing_id(), state->cached_devices->label));
state->ipc_id = -1;
state->cached_devices.reset();
}
return;
}
}
}
void MediaStreamDispatcher::OpenDevice(
int request_id,
const base::WeakPtr<MediaStreamDispatcherEventHandler>& event_handler,
const std::string& device_id,
media_stream::MediaStreamType type,
const GURL& security_origin) {
DCHECK(main_loop_->BelongsToCurrentThread());
DVLOG(1) << "MediaStreamDispatcher::OpenDevice(" << request_id << ")";
requests_.push_back(Request(event_handler, request_id, next_ipc_id_));
Send(new MediaStreamHostMsg_OpenDevice(routing_id(),
next_ipc_id_++,
device_id,
type,
security_origin));
}
void MediaStreamDispatcher::CloseDevice(const std::string& label) {
DCHECK(main_loop_->BelongsToCurrentThread());
DVLOG(1) << "MediaStreamDispatcher::CloseDevice"
<< ", {label = " << label << "}";
StopStream(label);
}
bool MediaStreamDispatcher::OnMessageReceived(const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(MediaStreamDispatcher, message)
IPC_MESSAGE_HANDLER(MediaStreamMsg_StreamGenerated,
OnStreamGenerated)
IPC_MESSAGE_HANDLER(MediaStreamMsg_StreamGenerationFailed,
OnStreamGenerationFailed)
IPC_MESSAGE_HANDLER(MediaStreamMsg_DevicesEnumerated,
OnDevicesEnumerated)
IPC_MESSAGE_HANDLER(MediaStreamMsg_DevicesEnumerationFailed,
OnDevicesEnumerationFailed)
IPC_MESSAGE_HANDLER(MediaStreamMsg_DeviceOpened,
OnDeviceOpened)
IPC_MESSAGE_HANDLER(MediaStreamMsg_DeviceOpenFailed,
OnDeviceOpenFailed)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
void MediaStreamDispatcher::OnStreamGenerated(
int request_id,
const std::string& label,
const media_stream::StreamDeviceInfoArray& audio_array,
const media_stream::StreamDeviceInfoArray& video_array) {
DCHECK(main_loop_->BelongsToCurrentThread());
for (RequestList::iterator it = requests_.begin();
it != requests_.end(); ++it) {
Request& request = *it;
if (request.ipc_request == request_id) {
Stream new_stream;
new_stream.handler = request.handler;
new_stream.audio_array = audio_array;
new_stream.video_array = video_array;
label_stream_map_[label] = new_stream;
if (request.handler) {
request.handler->OnStreamGenerated(request.request_id, label,
audio_array, video_array);
DVLOG(1) << "MediaStreamDispatcher::OnStreamGenerated("
<< request.request_id << ", " << label << ")";
}
requests_.erase(it);
break;
}
}
}
void MediaStreamDispatcher::OnStreamGenerationFailed(int request_id) {
DCHECK(main_loop_->BelongsToCurrentThread());
for (RequestList::iterator it = requests_.begin();
it != requests_.end(); ++it) {
Request& request = *it;
if (request.ipc_request == request_id) {
if (request.handler) {
request.handler->OnStreamGenerationFailed(request.request_id);
DVLOG(1) << "MediaStreamDispatcher::OnStreamGenerationFailed("
<< request.request_id << ")\n";
}
requests_.erase(it);
break;
}
}
}
void MediaStreamDispatcher::OnDevicesEnumerated(
int request_id,
const std::string& label,
const media_stream::StreamDeviceInfoArray& device_array) {
DCHECK(main_loop_->BelongsToCurrentThread());
DCHECK_GE(request_id, 0);
EnumerationState* state;
if (request_id == audio_enumeration_state_.ipc_id) {
state = &audio_enumeration_state_;
} else if (request_id == video_enumeration_state_.ipc_id) {
state = &video_enumeration_state_;
} else {
// This could happen when requester has stopped enumeration while some
// enumerated response is on the way. Have to stop the |label| because
// this might be the first enumerated device list is received. This also
// lead to same label being stopped multiple times.
Send(new MediaStreamHostMsg_StopGeneratedStream(routing_id(), label));
return;
}
DCHECK(!label.empty());
state->cached_devices.reset(new EnumerationState::CachedDevices(
label, device_array));
for (EnumerationRequestList::iterator it = state->requests.begin();
it != state->requests.end(); ++it) {
if (it->handler) {
it->handler->OnDevicesEnumerated(it->request_id, device_array);
DVLOG(1) << "MediaStreamDispatcher::OnDevicesEnumerated("
<< it->request_id << ")";
}
}
}
void MediaStreamDispatcher::OnDevicesEnumerationFailed(int request_id) {
DCHECK(main_loop_->BelongsToCurrentThread());
for (RequestList::iterator it = requests_.begin();
it != requests_.end(); ++it) {
Request& request = *it;
if (request.ipc_request == request_id) {
if (request.handler) {
request.handler->OnDevicesEnumerationFailed(request.request_id);
DVLOG(1) << "MediaStreamDispatcher::OnDevicesEnumerationFailed("
<< request.request_id << ")\n";
}
requests_.erase(it);
break;
}
}
}
void MediaStreamDispatcher::OnDeviceOpened(
int request_id,
const std::string& label,
const media_stream::StreamDeviceInfo& device_info) {
DCHECK(main_loop_->BelongsToCurrentThread());
for (RequestList::iterator it = requests_.begin();
it != requests_.end(); ++it) {
Request& request = *it;
if (request.ipc_request == request_id) {
Stream new_stream;
new_stream.handler = request.handler;
if (content::IsAudioMediaType(device_info.stream_type)) {
new_stream.audio_array.push_back(device_info);
} else if (content::IsVideoMediaType(device_info.stream_type)) {
new_stream.video_array.push_back(device_info);
} else {
NOTREACHED();
}
label_stream_map_[label] = new_stream;
if (request.handler) {
request.handler->OnDeviceOpened(request.request_id, label,
device_info);
DVLOG(1) << "MediaStreamDispatcher::OnDeviceOpened("
<< request.request_id << ", " << label << ")";
}
requests_.erase(it);
break;
}
}
}
void MediaStreamDispatcher::OnDeviceOpenFailed(int request_id) {
DCHECK(main_loop_->BelongsToCurrentThread());
for (RequestList::iterator it = requests_.begin();
it != requests_.end(); ++it) {
Request& request = *it;
if (request.ipc_request == request_id) {
if (request.handler) {
request.handler->OnDeviceOpenFailed(request.request_id);
DVLOG(1) << "MediaStreamDispatcher::OnDeviceOpenFailed("
<< request.request_id << ")\n";
}
requests_.erase(it);
break;
}
}
}
int MediaStreamDispatcher::audio_session_id(const std::string& label,
int index) {
LabelStreamMap::iterator it = label_stream_map_.find(label);
if (it == label_stream_map_.end())
return media_stream::StreamDeviceInfo::kNoId;
DCHECK_GT(it->second.audio_array.size(), static_cast<size_t>(index));
return it->second.audio_array[index].session_id;
}
bool MediaStreamDispatcher::IsStream(const std::string& label) {
return label_stream_map_.find(label) != label_stream_map_.end();
}
int MediaStreamDispatcher::video_session_id(const std::string& label,
int index) {
LabelStreamMap::iterator it = label_stream_map_.find(label);
if (it == label_stream_map_.end())
return media_stream::StreamDeviceInfo::kNoId;
DCHECK_GT(it->second.video_array.size(), static_cast<size_t>(index));
return it->second.video_array[index].session_id;
}
|