OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/media/media_stream_impl.h" | 5 #include "content/renderer/media/media_stream_impl.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/hash.h" | 9 #include "base/hash.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "content/public/renderer/render_frame.h" |
15 #include "content/renderer/media/media_stream.h" | 16 #include "content/renderer/media/media_stream.h" |
16 #include "content/renderer/media/media_stream_audio_source.h" | 17 #include "content/renderer/media/media_stream_audio_source.h" |
17 #include "content/renderer/media/media_stream_dispatcher.h" | 18 #include "content/renderer/media/media_stream_dispatcher.h" |
18 #include "content/renderer/media/media_stream_video_capturer_source.h" | 19 #include "content/renderer/media/media_stream_video_capturer_source.h" |
19 #include "content/renderer/media/media_stream_video_track.h" | 20 #include "content/renderer/media/media_stream_video_track.h" |
20 #include "content/renderer/media/peer_connection_tracker.h" | 21 #include "content/renderer/media/peer_connection_tracker.h" |
21 #include "content/renderer/media/webrtc/webrtc_video_capturer_adapter.h" | 22 #include "content/renderer/media/webrtc/webrtc_video_capturer_adapter.h" |
22 #include "content/renderer/media/webrtc_audio_capturer.h" | 23 #include "content/renderer/media/webrtc_audio_capturer.h" |
23 #include "content/renderer/media/webrtc_logging.h" | 24 #include "content/renderer/media/webrtc_logging.h" |
24 #include "content/renderer/media/webrtc_uma_histograms.h" | 25 #include "content/renderer/media/webrtc_uma_histograms.h" |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 int audio_output_request_id; | 76 int audio_output_request_id; |
76 bool has_audio_input_returned; | 77 bool has_audio_input_returned; |
77 bool has_video_input_returned; | 78 bool has_video_input_returned; |
78 bool has_audio_output_returned; | 79 bool has_audio_output_returned; |
79 StreamDeviceInfoArray audio_input_devices; | 80 StreamDeviceInfoArray audio_input_devices; |
80 StreamDeviceInfoArray video_input_devices; | 81 StreamDeviceInfoArray video_input_devices; |
81 StreamDeviceInfoArray audio_output_devices; | 82 StreamDeviceInfoArray audio_output_devices; |
82 }; | 83 }; |
83 | 84 |
84 MediaStreamImpl::MediaStreamImpl( | 85 MediaStreamImpl::MediaStreamImpl( |
85 RenderView* render_view, | 86 RenderFrame* render_frame, |
86 MediaStreamDispatcher* media_stream_dispatcher, | 87 PeerConnectionDependencyFactory* dependency_factory, |
87 PeerConnectionDependencyFactory* dependency_factory) | 88 scoped_ptr<MediaStreamDispatcher> media_stream_dispatcher) |
88 : RenderViewObserver(render_view), | 89 : RenderFrameObserver(render_frame), |
89 dependency_factory_(dependency_factory), | 90 dependency_factory_(dependency_factory), |
90 media_stream_dispatcher_(media_stream_dispatcher) { | 91 media_stream_dispatcher_(media_stream_dispatcher.Pass()), |
| 92 weak_factory_(this) { |
| 93 DCHECK(dependency_factory_); |
| 94 DCHECK(media_stream_dispatcher_.get()); |
91 } | 95 } |
92 | 96 |
93 MediaStreamImpl::~MediaStreamImpl() { | 97 MediaStreamImpl::~MediaStreamImpl() { |
| 98 // Force-close all outstanding user media requests and local sources here, |
| 99 // before the outstanding WeakPtrs are invalidated, to ensure a clean |
| 100 // shutdown. |
| 101 FrameWillClose(); |
94 } | 102 } |
95 | 103 |
96 void MediaStreamImpl::requestUserMedia( | 104 void MediaStreamImpl::requestUserMedia( |
97 const blink::WebUserMediaRequest& user_media_request) { | 105 const blink::WebUserMediaRequest& user_media_request) { |
98 // Save histogram data so we can see how much GetUserMedia is used. | 106 // Save histogram data so we can see how much GetUserMedia is used. |
99 // The histogram counts the number of calls to the JS API | 107 // The histogram counts the number of calls to the JS API |
100 // webGetUserMedia. | 108 // webGetUserMedia. |
101 UpdateWebRTCMethodCount(WEBKIT_GET_USER_MEDIA); | 109 UpdateWebRTCMethodCount(WEBKIT_GET_USER_MEDIA); |
102 DCHECK(CalledOnValidThread()); | 110 DCHECK(CalledOnValidThread()); |
103 | 111 |
104 if (RenderThreadImpl::current()) { | 112 if (RenderThreadImpl::current()) { |
105 RenderThreadImpl::current()->peer_connection_tracker()->TrackGetUserMedia( | 113 RenderThreadImpl::current()->peer_connection_tracker()->TrackGetUserMedia( |
106 user_media_request); | 114 user_media_request); |
107 } | 115 } |
108 | 116 |
109 int request_id = g_next_request_id++; | 117 int request_id = g_next_request_id++; |
110 StreamOptions options; | 118 StreamOptions options; |
111 blink::WebLocalFrame* frame = NULL; | |
112 GURL security_origin; | 119 GURL security_origin; |
113 bool enable_automatic_output_device_selection = false; | 120 bool enable_automatic_output_device_selection = false; |
114 | 121 |
115 // |user_media_request| can't be mocked. So in order to test at all we check | 122 // |user_media_request| can't be mocked. So in order to test at all we check |
116 // if it isNull. | 123 // if it isNull. |
117 if (user_media_request.isNull()) { | 124 if (user_media_request.isNull()) { |
118 // We are in a test. | 125 // We are in a test. |
119 options.audio_requested = true; | 126 options.audio_requested = true; |
120 options.video_requested = true; | 127 options.video_requested = true; |
121 } else { | 128 } else { |
(...skipping 13 matching lines...) Expand all Loading... |
135 } | 142 } |
136 } | 143 } |
137 if (user_media_request.video()) { | 144 if (user_media_request.video()) { |
138 options.video_requested = true; | 145 options.video_requested = true; |
139 CopyStreamConstraints(user_media_request.videoConstraints(), | 146 CopyStreamConstraints(user_media_request.videoConstraints(), |
140 &options.mandatory_video, | 147 &options.mandatory_video, |
141 &options.optional_video); | 148 &options.optional_video); |
142 } | 149 } |
143 | 150 |
144 security_origin = GURL(user_media_request.securityOrigin().toString()); | 151 security_origin = GURL(user_media_request.securityOrigin().toString()); |
145 // Get the WebFrame that requested a MediaStream. | 152 DCHECK(render_frame()->GetWebFrame() == |
146 // The frame is needed to tell the MediaStreamDispatcher when a stream goes | 153 static_cast<blink::WebFrame*>( |
147 // out of scope. | 154 user_media_request.ownerDocument().frame())); |
148 frame = user_media_request.ownerDocument().frame(); | |
149 DCHECK(frame); | |
150 } | 155 } |
151 | 156 |
152 DVLOG(1) << "MediaStreamImpl::requestUserMedia(" << request_id << ", [ " | 157 DVLOG(1) << "MediaStreamImpl::requestUserMedia(" << request_id << ", [ " |
153 << "audio=" << (options.audio_requested) | 158 << "audio=" << (options.audio_requested) |
154 << " select associated sink: " | 159 << " select associated sink: " |
155 << enable_automatic_output_device_selection | 160 << enable_automatic_output_device_selection |
156 << ", video=" << (options.video_requested) << " ], " | 161 << ", video=" << (options.video_requested) << " ], " |
157 << security_origin.spec() << ")"; | 162 << security_origin.spec() << ")"; |
158 | 163 |
159 std::string audio_device_id; | 164 std::string audio_device_id; |
160 bool mandatory_audio; | 165 bool mandatory_audio; |
161 options.GetFirstAudioConstraintByName(kMediaStreamSourceInfoId, | 166 options.GetFirstAudioConstraintByName(kMediaStreamSourceInfoId, |
162 &audio_device_id, &mandatory_audio); | 167 &audio_device_id, &mandatory_audio); |
163 std::string video_device_id; | 168 std::string video_device_id; |
164 bool mandatory_video; | 169 bool mandatory_video; |
165 options.GetFirstVideoConstraintByName(kMediaStreamSourceInfoId, | 170 options.GetFirstVideoConstraintByName(kMediaStreamSourceInfoId, |
166 &video_device_id, &mandatory_video); | 171 &video_device_id, &mandatory_video); |
167 | 172 |
168 WebRtcLogMessage(base::StringPrintf( | 173 WebRtcLogMessage(base::StringPrintf( |
169 "MSI::requestUserMedia. request_id=%d" | 174 "MSI::requestUserMedia. request_id=%d" |
170 ", audio source id=%s mandatory= %s " | 175 ", audio source id=%s mandatory= %s " |
171 ", video source id=%s mandatory= %s", | 176 ", video source id=%s mandatory= %s", |
172 request_id, | 177 request_id, |
173 audio_device_id.c_str(), | 178 audio_device_id.c_str(), |
174 mandatory_audio ? "true":"false", | 179 mandatory_audio ? "true":"false", |
175 video_device_id.c_str(), | 180 video_device_id.c_str(), |
176 mandatory_video ? "true":"false")); | 181 mandatory_video ? "true":"false")); |
177 | 182 |
178 user_media_requests_.push_back( | 183 user_media_requests_.push_back( |
179 new UserMediaRequestInfo(request_id, frame, user_media_request, | 184 new UserMediaRequestInfo(request_id, user_media_request, |
180 enable_automatic_output_device_selection)); | 185 enable_automatic_output_device_selection)); |
181 | 186 |
182 media_stream_dispatcher_->GenerateStream( | 187 media_stream_dispatcher_->GenerateStream( |
183 request_id, | 188 request_id, |
184 AsWeakPtr(), | 189 weak_factory_.GetWeakPtr(), |
185 options, | 190 options, |
186 security_origin); | 191 security_origin); |
187 } | 192 } |
188 | 193 |
189 void MediaStreamImpl::cancelUserMediaRequest( | 194 void MediaStreamImpl::cancelUserMediaRequest( |
190 const blink::WebUserMediaRequest& user_media_request) { | 195 const blink::WebUserMediaRequest& user_media_request) { |
191 DCHECK(CalledOnValidThread()); | 196 DCHECK(CalledOnValidThread()); |
192 UserMediaRequestInfo* request = FindUserMediaRequestInfo(user_media_request); | 197 UserMediaRequestInfo* request = FindUserMediaRequestInfo(user_media_request); |
193 if (request) { | 198 if (request) { |
194 // We can't abort the stream generation process. | 199 // We can't abort the stream generation process. |
(...skipping 24 matching lines...) Expand all Loading... |
219 << ", " << security_origin.spec() << ")"; | 224 << ", " << security_origin.spec() << ")"; |
220 | 225 |
221 media_devices_requests_.push_back(new MediaDevicesRequestInfo( | 226 media_devices_requests_.push_back(new MediaDevicesRequestInfo( |
222 media_devices_request, | 227 media_devices_request, |
223 audio_input_request_id, | 228 audio_input_request_id, |
224 video_input_request_id, | 229 video_input_request_id, |
225 audio_output_request_id)); | 230 audio_output_request_id)); |
226 | 231 |
227 media_stream_dispatcher_->EnumerateDevices( | 232 media_stream_dispatcher_->EnumerateDevices( |
228 audio_input_request_id, | 233 audio_input_request_id, |
229 AsWeakPtr(), | 234 weak_factory_.GetWeakPtr(), |
230 MEDIA_DEVICE_AUDIO_CAPTURE, | 235 MEDIA_DEVICE_AUDIO_CAPTURE, |
231 security_origin, | 236 security_origin, |
232 true); | 237 true); |
233 | 238 |
234 media_stream_dispatcher_->EnumerateDevices( | 239 media_stream_dispatcher_->EnumerateDevices( |
235 video_input_request_id, | 240 video_input_request_id, |
236 AsWeakPtr(), | 241 weak_factory_.GetWeakPtr(), |
237 MEDIA_DEVICE_VIDEO_CAPTURE, | 242 MEDIA_DEVICE_VIDEO_CAPTURE, |
238 security_origin, | 243 security_origin, |
239 true); | 244 true); |
240 | 245 |
241 media_stream_dispatcher_->EnumerateDevices( | 246 media_stream_dispatcher_->EnumerateDevices( |
242 audio_output_request_id, | 247 audio_output_request_id, |
243 AsWeakPtr(), | 248 weak_factory_.GetWeakPtr(), |
244 MEDIA_DEVICE_AUDIO_OUTPUT, | 249 MEDIA_DEVICE_AUDIO_OUTPUT, |
245 security_origin, | 250 security_origin, |
246 true); | 251 true); |
247 } | 252 } |
248 | 253 |
249 void MediaStreamImpl::cancelMediaDevicesRequest( | 254 void MediaStreamImpl::cancelMediaDevicesRequest( |
250 const blink::WebMediaDevicesRequest& media_devices_request) { | 255 const blink::WebMediaDevicesRequest& media_devices_request) { |
251 DCHECK(CalledOnValidThread()); | 256 DCHECK(CalledOnValidThread()); |
252 MediaDevicesRequestInfo* request = | 257 MediaDevicesRequestInfo* request = |
253 FindMediaDevicesRequestInfo(media_devices_request); | 258 FindMediaDevicesRequestInfo(media_devices_request); |
254 if (!request) | 259 if (!request) |
255 return; | 260 return; |
256 | 261 CancelAndDeleteMediaDevicesRequest(request); |
257 // Cancel device enumeration. | |
258 media_stream_dispatcher_->StopEnumerateDevices( | |
259 request->audio_input_request_id, | |
260 AsWeakPtr()); | |
261 media_stream_dispatcher_->StopEnumerateDevices( | |
262 request->video_input_request_id, | |
263 AsWeakPtr()); | |
264 media_stream_dispatcher_->StopEnumerateDevices( | |
265 request->audio_output_request_id, | |
266 AsWeakPtr()); | |
267 DeleteMediaDevicesRequestInfo(request); | |
268 } | 262 } |
269 | 263 |
270 // Callback from MediaStreamDispatcher. | 264 // Callback from MediaStreamDispatcher. |
271 // The requested stream have been generated by the MediaStreamDispatcher. | 265 // The requested stream have been generated by the MediaStreamDispatcher. |
272 void MediaStreamImpl::OnStreamGenerated( | 266 void MediaStreamImpl::OnStreamGenerated( |
273 int request_id, | 267 int request_id, |
274 const std::string& label, | 268 const std::string& label, |
275 const StreamDeviceInfoArray& audio_array, | 269 const StreamDeviceInfoArray& audio_array, |
276 const StreamDeviceInfoArray& video_array) { | 270 const StreamDeviceInfoArray& video_array) { |
277 DCHECK(CalledOnValidThread()); | 271 DCHECK(CalledOnValidThread()); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 blink::WebMediaStream* web_stream = &(request_info->web_stream); | 321 blink::WebMediaStream* web_stream = &(request_info->web_stream); |
328 | 322 |
329 web_stream->initialize(webkit_id, audio_track_vector, | 323 web_stream->initialize(webkit_id, audio_track_vector, |
330 video_track_vector); | 324 video_track_vector); |
331 web_stream->setExtraData( | 325 web_stream->setExtraData( |
332 new MediaStream( | 326 new MediaStream( |
333 *web_stream)); | 327 *web_stream)); |
334 | 328 |
335 // Wait for the tracks to be started successfully or to fail. | 329 // Wait for the tracks to be started successfully or to fail. |
336 request_info->CallbackOnTracksStarted( | 330 request_info->CallbackOnTracksStarted( |
337 base::Bind(&MediaStreamImpl::OnCreateNativeTracksCompleted, AsWeakPtr())); | 331 base::Bind(&MediaStreamImpl::OnCreateNativeTracksCompleted, |
| 332 weak_factory_.GetWeakPtr())); |
338 } | 333 } |
339 | 334 |
340 // Callback from MediaStreamDispatcher. | 335 // Callback from MediaStreamDispatcher. |
341 // The requested stream failed to be generated. | 336 // The requested stream failed to be generated. |
342 void MediaStreamImpl::OnStreamGenerationFailed( | 337 void MediaStreamImpl::OnStreamGenerationFailed( |
343 int request_id, | 338 int request_id, |
344 content::MediaStreamRequestResult result) { | 339 content::MediaStreamRequestResult result) { |
345 DCHECK(CalledOnValidThread()); | 340 DCHECK(CalledOnValidThread()); |
346 DVLOG(1) << "MediaStreamImpl::OnStreamGenerationFailed(" | 341 DVLOG(1) << "MediaStreamImpl::OnStreamGenerationFailed(" |
347 << request_id << ")"; | 342 << request_id << ")"; |
(...skipping 25 matching lines...) Expand all Loading... |
373 // as the underlying media device is unplugged from the system. | 368 // as the underlying media device is unplugged from the system. |
374 return; | 369 return; |
375 } | 370 } |
376 // By creating |source| it is guaranteed that the blink::WebMediaStreamSource | 371 // By creating |source| it is guaranteed that the blink::WebMediaStreamSource |
377 // object is valid during the cleanup. | 372 // object is valid during the cleanup. |
378 blink::WebMediaStreamSource source(*source_ptr); | 373 blink::WebMediaStreamSource source(*source_ptr); |
379 StopLocalSource(source, false); | 374 StopLocalSource(source, false); |
380 | 375 |
381 for (LocalStreamSources::iterator device_it = local_sources_.begin(); | 376 for (LocalStreamSources::iterator device_it = local_sources_.begin(); |
382 device_it != local_sources_.end(); ++device_it) { | 377 device_it != local_sources_.end(); ++device_it) { |
383 if (device_it->source.id() == source.id()) { | 378 if (device_it->id() == source.id()) { |
384 local_sources_.erase(device_it); | 379 local_sources_.erase(device_it); |
385 break; | 380 break; |
386 } | 381 } |
387 } | 382 } |
388 } | 383 } |
389 | 384 |
390 void MediaStreamImpl::InitializeSourceObject( | 385 void MediaStreamImpl::InitializeSourceObject( |
391 const StreamDeviceInfo& device, | 386 const StreamDeviceInfo& device, |
392 blink::WebMediaStreamSource::Type type, | 387 blink::WebMediaStreamSource::Type type, |
393 const blink::WebMediaConstraints& constraints, | 388 const blink::WebMediaConstraints& constraints, |
394 blink::WebFrame* frame, | |
395 blink::WebMediaStreamSource* webkit_source) { | 389 blink::WebMediaStreamSource* webkit_source) { |
396 const blink::WebMediaStreamSource* existing_source = | 390 const blink::WebMediaStreamSource* existing_source = |
397 FindLocalSource(device); | 391 FindLocalSource(device); |
398 if (existing_source) { | 392 if (existing_source) { |
399 *webkit_source = *existing_source; | 393 *webkit_source = *existing_source; |
400 DVLOG(1) << "Source already exist. Reusing source with id " | 394 DVLOG(1) << "Source already exist. Reusing source with id " |
401 << webkit_source->id().utf8(); | 395 << webkit_source->id().utf8(); |
402 return; | 396 return; |
403 } | 397 } |
404 | 398 |
405 webkit_source->initialize( | 399 webkit_source->initialize( |
406 base::UTF8ToUTF16(device.device.id), | 400 base::UTF8ToUTF16(device.device.id), |
407 type, | 401 type, |
408 base::UTF8ToUTF16(device.device.name)); | 402 base::UTF8ToUTF16(device.device.name)); |
409 | 403 |
410 DVLOG(1) << "Initialize source object :" | 404 DVLOG(1) << "Initialize source object :" |
411 << "id = " << webkit_source->id().utf8() | 405 << "id = " << webkit_source->id().utf8() |
412 << ", name = " << webkit_source->name().utf8(); | 406 << ", name = " << webkit_source->name().utf8(); |
413 | 407 |
414 if (type == blink::WebMediaStreamSource::TypeVideo) { | 408 if (type == blink::WebMediaStreamSource::TypeVideo) { |
415 webkit_source->setExtraData( | 409 webkit_source->setExtraData( |
416 CreateVideoSource( | 410 CreateVideoSource( |
417 device, | 411 device, |
418 base::Bind(&MediaStreamImpl::OnLocalSourceStopped, AsWeakPtr()))); | 412 base::Bind(&MediaStreamImpl::OnLocalSourceStopped, |
| 413 weak_factory_.GetWeakPtr()))); |
419 } else { | 414 } else { |
420 DCHECK_EQ(blink::WebMediaStreamSource::TypeAudio, type); | 415 DCHECK_EQ(blink::WebMediaStreamSource::TypeAudio, type); |
421 MediaStreamAudioSource* audio_source( | 416 MediaStreamAudioSource* audio_source( |
422 new MediaStreamAudioSource( | 417 new MediaStreamAudioSource( |
423 RenderViewObserver::routing_id(), | 418 RenderFrameObserver::routing_id(), |
424 device, | 419 device, |
425 base::Bind(&MediaStreamImpl::OnLocalSourceStopped, AsWeakPtr()), | 420 base::Bind(&MediaStreamImpl::OnLocalSourceStopped, |
| 421 weak_factory_.GetWeakPtr()), |
426 dependency_factory_)); | 422 dependency_factory_)); |
427 webkit_source->setExtraData(audio_source); | 423 webkit_source->setExtraData(audio_source); |
428 } | 424 } |
429 local_sources_.push_back(LocalStreamSource(frame, *webkit_source)); | 425 local_sources_.push_back(*webkit_source); |
430 } | 426 } |
431 | 427 |
432 MediaStreamVideoSource* MediaStreamImpl::CreateVideoSource( | 428 MediaStreamVideoSource* MediaStreamImpl::CreateVideoSource( |
433 const StreamDeviceInfo& device, | 429 const StreamDeviceInfo& device, |
434 const MediaStreamSource::SourceStoppedCallback& stop_callback) { | 430 const MediaStreamSource::SourceStoppedCallback& stop_callback) { |
435 return new content::MediaStreamVideoCapturerSource( | 431 return new content::MediaStreamVideoCapturerSource( |
436 device, | 432 device, |
437 stop_callback, | 433 stop_callback, |
438 new VideoCapturerDelegate(device)); | 434 new VideoCapturerDelegate(device)); |
439 } | 435 } |
440 | 436 |
441 void MediaStreamImpl::CreateVideoTracks( | 437 void MediaStreamImpl::CreateVideoTracks( |
442 const StreamDeviceInfoArray& devices, | 438 const StreamDeviceInfoArray& devices, |
443 const blink::WebMediaConstraints& constraints, | 439 const blink::WebMediaConstraints& constraints, |
444 blink::WebVector<blink::WebMediaStreamTrack>* webkit_tracks, | 440 blink::WebVector<blink::WebMediaStreamTrack>* webkit_tracks, |
445 UserMediaRequestInfo* request) { | 441 UserMediaRequestInfo* request) { |
446 DCHECK_EQ(devices.size(), webkit_tracks->size()); | 442 DCHECK_EQ(devices.size(), webkit_tracks->size()); |
447 | 443 |
448 for (size_t i = 0; i < devices.size(); ++i) { | 444 for (size_t i = 0; i < devices.size(); ++i) { |
449 blink::WebMediaStreamSource webkit_source; | 445 blink::WebMediaStreamSource webkit_source; |
450 InitializeSourceObject(devices[i], | 446 InitializeSourceObject(devices[i], |
451 blink::WebMediaStreamSource::TypeVideo, | 447 blink::WebMediaStreamSource::TypeVideo, |
452 constraints, | 448 constraints, |
453 request->frame, | |
454 &webkit_source); | 449 &webkit_source); |
455 (*webkit_tracks)[i] = | 450 (*webkit_tracks)[i] = |
456 request->CreateAndStartVideoTrack(webkit_source, constraints); | 451 request->CreateAndStartVideoTrack(webkit_source, constraints); |
457 } | 452 } |
458 } | 453 } |
459 | 454 |
460 void MediaStreamImpl::CreateAudioTracks( | 455 void MediaStreamImpl::CreateAudioTracks( |
461 const StreamDeviceInfoArray& devices, | 456 const StreamDeviceInfoArray& devices, |
462 const blink::WebMediaConstraints& constraints, | 457 const blink::WebMediaConstraints& constraints, |
463 blink::WebVector<blink::WebMediaStreamTrack>* webkit_tracks, | 458 blink::WebVector<blink::WebMediaStreamTrack>* webkit_tracks, |
(...skipping 20 matching lines...) Expand all Loading... |
484 it->device.matched_output_device_id = ""; | 479 it->device.matched_output_device_id = ""; |
485 it->device.matched_output = MediaStreamDevice::AudioDeviceParameters(); | 480 it->device.matched_output = MediaStreamDevice::AudioDeviceParameters(); |
486 } | 481 } |
487 } | 482 } |
488 | 483 |
489 for (size_t i = 0; i < overridden_audio_array.size(); ++i) { | 484 for (size_t i = 0; i < overridden_audio_array.size(); ++i) { |
490 blink::WebMediaStreamSource webkit_source; | 485 blink::WebMediaStreamSource webkit_source; |
491 InitializeSourceObject(overridden_audio_array[i], | 486 InitializeSourceObject(overridden_audio_array[i], |
492 blink::WebMediaStreamSource::TypeAudio, | 487 blink::WebMediaStreamSource::TypeAudio, |
493 constraints, | 488 constraints, |
494 request->frame, | |
495 &webkit_source); | 489 &webkit_source); |
496 (*webkit_tracks)[i].initialize(webkit_source); | 490 (*webkit_tracks)[i].initialize(webkit_source); |
497 request->StartAudioTrack((*webkit_tracks)[i], constraints); | 491 request->StartAudioTrack((*webkit_tracks)[i], constraints); |
498 } | 492 } |
499 } | 493 } |
500 | 494 |
501 void MediaStreamImpl::OnCreateNativeTracksCompleted( | 495 void MediaStreamImpl::OnCreateNativeTracksCompleted( |
502 UserMediaRequestInfo* request, | 496 UserMediaRequestInfo* request, |
503 content::MediaStreamRequestResult result) { | 497 content::MediaStreamRequestResult result) { |
504 DVLOG(1) << "MediaStreamImpl::OnCreateNativeTracksComplete(" | 498 DVLOG(1) << "MediaStreamImpl::OnCreateNativeTracksComplete(" |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
578 const MediaStreamDevice& device = request->audio_output_devices[i].device; | 572 const MediaStreamDevice& device = request->audio_output_devices[i].device; |
579 DCHECK_EQ(device.type, MEDIA_DEVICE_AUDIO_OUTPUT); | 573 DCHECK_EQ(device.type, MEDIA_DEVICE_AUDIO_OUTPUT); |
580 devices[offset + i].initialize( | 574 devices[offset + i].initialize( |
581 blink::WebString::fromUTF8(device.id), | 575 blink::WebString::fromUTF8(device.id), |
582 blink::WebMediaDeviceInfo::MediaDeviceKindAudioOutput, | 576 blink::WebMediaDeviceInfo::MediaDeviceKindAudioOutput, |
583 blink::WebString::fromUTF8(device.name), | 577 blink::WebString::fromUTF8(device.name), |
584 blink::WebString::fromUTF8(base::UintToString(base::Hash(device.id)))); | 578 blink::WebString::fromUTF8(base::UintToString(base::Hash(device.id)))); |
585 } | 579 } |
586 | 580 |
587 EnumerateDevicesSucceded(&request->request, devices); | 581 EnumerateDevicesSucceded(&request->request, devices); |
588 | 582 CancelAndDeleteMediaDevicesRequest(request); |
589 // Cancel device enumeration. | |
590 media_stream_dispatcher_->StopEnumerateDevices( | |
591 request->audio_input_request_id, | |
592 AsWeakPtr()); | |
593 media_stream_dispatcher_->StopEnumerateDevices( | |
594 request->video_input_request_id, | |
595 AsWeakPtr()); | |
596 media_stream_dispatcher_->StopEnumerateDevices( | |
597 request->audio_output_request_id, | |
598 AsWeakPtr()); | |
599 | |
600 DeleteMediaDevicesRequestInfo(request); | |
601 } | 583 } |
602 | 584 |
603 void MediaStreamImpl::OnDeviceOpened( | 585 void MediaStreamImpl::OnDeviceOpened( |
604 int request_id, | 586 int request_id, |
605 const std::string& label, | 587 const std::string& label, |
606 const StreamDeviceInfo& video_device) { | 588 const StreamDeviceInfo& video_device) { |
607 DVLOG(1) << "MediaStreamImpl::OnDeviceOpened(" | 589 DVLOG(1) << "MediaStreamImpl::OnDeviceOpened(" |
608 << request_id << ", " << label << ")"; | 590 << request_id << ", " << label << ")"; |
609 NOTIMPLEMENTED(); | 591 NOTIMPLEMENTED(); |
610 } | 592 } |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
665 void MediaStreamImpl::EnumerateDevicesSucceded( | 647 void MediaStreamImpl::EnumerateDevicesSucceded( |
666 blink::WebMediaDevicesRequest* request, | 648 blink::WebMediaDevicesRequest* request, |
667 blink::WebVector<blink::WebMediaDeviceInfo>& devices) { | 649 blink::WebVector<blink::WebMediaDeviceInfo>& devices) { |
668 request->requestSucceeded(devices); | 650 request->requestSucceeded(devices); |
669 } | 651 } |
670 | 652 |
671 const blink::WebMediaStreamSource* MediaStreamImpl::FindLocalSource( | 653 const blink::WebMediaStreamSource* MediaStreamImpl::FindLocalSource( |
672 const StreamDeviceInfo& device) const { | 654 const StreamDeviceInfo& device) const { |
673 for (LocalStreamSources::const_iterator it = local_sources_.begin(); | 655 for (LocalStreamSources::const_iterator it = local_sources_.begin(); |
674 it != local_sources_.end(); ++it) { | 656 it != local_sources_.end(); ++it) { |
675 MediaStreamSource* source = | 657 MediaStreamSource* const source = |
676 static_cast<MediaStreamSource*>(it->source.extraData()); | 658 static_cast<MediaStreamSource*>(it->extraData()); |
677 const StreamDeviceInfo& active_device = source->device_info(); | 659 const StreamDeviceInfo& active_device = source->device_info(); |
678 if (active_device.device.id == device.device.id && | 660 if (active_device.device.id == device.device.id && |
679 active_device.device.type == device.device.type && | 661 active_device.device.type == device.device.type && |
680 active_device.session_id == device.session_id) { | 662 active_device.session_id == device.session_id) { |
681 return &it->source; | 663 return &(*it); |
682 } | 664 } |
683 } | 665 } |
684 return NULL; | 666 return NULL; |
685 } | 667 } |
686 | 668 |
687 MediaStreamImpl::UserMediaRequestInfo* | 669 MediaStreamImpl::UserMediaRequestInfo* |
688 MediaStreamImpl::FindUserMediaRequestInfo(int request_id) { | 670 MediaStreamImpl::FindUserMediaRequestInfo(int request_id) { |
689 UserMediaRequests::iterator it = user_media_requests_.begin(); | 671 UserMediaRequests::iterator it = user_media_requests_.begin(); |
690 for (; it != user_media_requests_.end(); ++it) { | 672 for (; it != user_media_requests_.end(); ++it) { |
691 if ((*it)->request_id == request_id) | 673 if ((*it)->request_id == request_id) |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
735 MediaStreamImpl::FindMediaDevicesRequestInfo( | 717 MediaStreamImpl::FindMediaDevicesRequestInfo( |
736 const blink::WebMediaDevicesRequest& request) { | 718 const blink::WebMediaDevicesRequest& request) { |
737 MediaDevicesRequests::iterator it = media_devices_requests_.begin(); | 719 MediaDevicesRequests::iterator it = media_devices_requests_.begin(); |
738 for (; it != media_devices_requests_.end(); ++it) { | 720 for (; it != media_devices_requests_.end(); ++it) { |
739 if ((*it)->request == request) | 721 if ((*it)->request == request) |
740 return (*it); | 722 return (*it); |
741 } | 723 } |
742 return NULL; | 724 return NULL; |
743 } | 725 } |
744 | 726 |
745 void MediaStreamImpl::DeleteMediaDevicesRequestInfo( | 727 void MediaStreamImpl::CancelAndDeleteMediaDevicesRequest( |
746 MediaDevicesRequestInfo* request) { | 728 MediaDevicesRequestInfo* request) { |
747 MediaDevicesRequests::iterator it = media_devices_requests_.begin(); | 729 MediaDevicesRequests::iterator it = media_devices_requests_.begin(); |
748 for (; it != media_devices_requests_.end(); ++it) { | 730 for (; it != media_devices_requests_.end(); ++it) { |
749 if ((*it) == request) { | 731 if ((*it) == request) { |
| 732 // Cancel device enumeration. |
| 733 media_stream_dispatcher_->StopEnumerateDevices( |
| 734 request->audio_input_request_id, weak_factory_.GetWeakPtr()); |
| 735 media_stream_dispatcher_->StopEnumerateDevices( |
| 736 request->video_input_request_id, weak_factory_.GetWeakPtr()); |
| 737 media_stream_dispatcher_->StopEnumerateDevices( |
| 738 request->audio_output_request_id, weak_factory_.GetWeakPtr()); |
| 739 |
750 media_devices_requests_.erase(it); | 740 media_devices_requests_.erase(it); |
751 return; | 741 return; |
752 } | 742 } |
753 } | 743 } |
754 NOTREACHED(); | 744 NOTREACHED(); |
755 } | 745 } |
756 | 746 |
757 void MediaStreamImpl::FrameDetached(blink::WebFrame* frame) { | 747 void MediaStreamImpl::FrameWillClose() { |
758 // Do same thing as FrameWillClose. | 748 // Cancel all outstanding UserMediaRequests. |
759 FrameWillClose(frame); | |
760 } | |
761 | |
762 void MediaStreamImpl::FrameWillClose(blink::WebFrame* frame) { | |
763 // Loop through all UserMediaRequests and find the requests that belong to the | |
764 // frame that is being closed. | |
765 UserMediaRequests::iterator request_it = user_media_requests_.begin(); | 749 UserMediaRequests::iterator request_it = user_media_requests_.begin(); |
766 while (request_it != user_media_requests_.end()) { | 750 while (request_it != user_media_requests_.end()) { |
767 if ((*request_it)->frame == frame) { | 751 DVLOG(1) << "MediaStreamImpl@" << this << "::FrameWillClose: " |
768 DVLOG(1) << "MediaStreamImpl::FrameWillClose: " | 752 << "Cancel user media request " << (*request_it)->request_id; |
769 << "Cancel user media request " << (*request_it)->request_id; | 753 // If the request is not generated, it means that a request |
770 // If the request is not generated, it means that a request | 754 // has been sent to the MediaStreamDispatcher to generate a stream |
771 // has been sent to the MediaStreamDispatcher to generate a stream | 755 // but MediaStreamDispatcher has not yet responded and we need to cancel |
772 // but MediaStreamDispatcher has not yet responded and we need to cancel | 756 // the request. |
773 // the request. | 757 if (!(*request_it)->generated) { |
774 if (!(*request_it)->generated) { | 758 media_stream_dispatcher_->CancelGenerateStream( |
775 media_stream_dispatcher_->CancelGenerateStream( | 759 (*request_it)->request_id, weak_factory_.GetWeakPtr()); |
776 (*request_it)->request_id, AsWeakPtr()); | |
777 } | |
778 request_it = user_media_requests_.erase(request_it); | |
779 } else { | |
780 ++request_it; | |
781 } | 760 } |
| 761 request_it = user_media_requests_.erase(request_it); |
782 } | 762 } |
783 | 763 |
784 // Loop through all current local sources and stop the sources that were | 764 // Loop through all current local sources and stop the sources. |
785 // created by the frame that will be closed. | |
786 LocalStreamSources::iterator sources_it = local_sources_.begin(); | 765 LocalStreamSources::iterator sources_it = local_sources_.begin(); |
787 while (sources_it != local_sources_.end()) { | 766 while (sources_it != local_sources_.end()) { |
788 if (sources_it->frame == frame) { | 767 StopLocalSource(*sources_it, true); |
789 StopLocalSource(sources_it->source, true); | 768 sources_it = local_sources_.erase(sources_it); |
790 sources_it = local_sources_.erase(sources_it); | |
791 } else { | |
792 ++sources_it; | |
793 } | |
794 } | 769 } |
795 } | 770 } |
796 | 771 |
797 void MediaStreamImpl::OnLocalSourceStopped( | 772 void MediaStreamImpl::OnLocalSourceStopped( |
798 const blink::WebMediaStreamSource& source) { | 773 const blink::WebMediaStreamSource& source) { |
799 DCHECK(CalledOnValidThread()); | 774 DCHECK(CalledOnValidThread()); |
800 DVLOG(1) << "MediaStreamImpl::OnLocalSourceStopped"; | 775 DVLOG(1) << "MediaStreamImpl::OnLocalSourceStopped"; |
801 | 776 |
802 bool device_found = false; | 777 bool device_found = false; |
803 for (LocalStreamSources::iterator device_it = local_sources_.begin(); | 778 for (LocalStreamSources::iterator device_it = local_sources_.begin(); |
804 device_it != local_sources_.end(); ++device_it) { | 779 device_it != local_sources_.end(); ++device_it) { |
805 if (device_it->source.id() == source.id()) { | 780 if (device_it->id() == source.id()) { |
806 device_found = true; | 781 device_found = true; |
807 local_sources_.erase(device_it); | 782 local_sources_.erase(device_it); |
808 break; | 783 break; |
809 } | 784 } |
810 } | 785 } |
811 CHECK(device_found); | 786 CHECK(device_found); |
812 | 787 |
813 MediaStreamSource* source_impl = | 788 MediaStreamSource* source_impl = |
814 static_cast<MediaStreamSource*> (source.extraData()); | 789 static_cast<MediaStreamSource*>(source.extraData()); |
815 media_stream_dispatcher_->StopStreamDevice(source_impl->device_info()); | 790 media_stream_dispatcher_->StopStreamDevice(source_impl->device_info()); |
816 } | 791 } |
817 | 792 |
818 void MediaStreamImpl::StopLocalSource( | 793 void MediaStreamImpl::StopLocalSource( |
819 const blink::WebMediaStreamSource& source, | 794 const blink::WebMediaStreamSource& source, |
820 bool notify_dispatcher) { | 795 bool notify_dispatcher) { |
821 MediaStreamSource* source_impl = | 796 MediaStreamSource* source_impl = |
822 static_cast<MediaStreamSource*> (source.extraData()); | 797 static_cast<MediaStreamSource*>(source.extraData()); |
823 DVLOG(1) << "MediaStreamImpl::StopLocalSource(" | 798 DVLOG(1) << "MediaStreamImpl::StopLocalSource(" |
824 << "{device_id = " << source_impl->device_info().device.id << "})"; | 799 << "{device_id = " << source_impl->device_info().device.id << "})"; |
825 | 800 |
826 if (notify_dispatcher) | 801 if (notify_dispatcher) |
827 media_stream_dispatcher_->StopStreamDevice(source_impl->device_info()); | 802 media_stream_dispatcher_->StopStreamDevice(source_impl->device_info()); |
828 | 803 |
829 source_impl->ResetSourceStoppedCallback(); | 804 source_impl->ResetSourceStoppedCallback(); |
830 source_impl->StopSource(); | 805 source_impl->StopSource(); |
831 } | 806 } |
832 | 807 |
833 MediaStreamImpl::UserMediaRequestInfo::UserMediaRequestInfo( | 808 MediaStreamImpl::UserMediaRequestInfo::UserMediaRequestInfo( |
834 int request_id, | 809 int request_id, |
835 blink::WebFrame* frame, | |
836 const blink::WebUserMediaRequest& request, | 810 const blink::WebUserMediaRequest& request, |
837 bool enable_automatic_output_device_selection) | 811 bool enable_automatic_output_device_selection) |
838 : request_id(request_id), | 812 : request_id(request_id), |
839 generated(false), | 813 generated(false), |
840 enable_automatic_output_device_selection( | 814 enable_automatic_output_device_selection( |
841 enable_automatic_output_device_selection), | 815 enable_automatic_output_device_selection), |
842 frame(frame), | |
843 request(request), | 816 request(request), |
844 request_failed_(false) { | 817 request_failed_(false) { |
845 } | 818 } |
846 | 819 |
847 MediaStreamImpl::UserMediaRequestInfo::~UserMediaRequestInfo() { | 820 MediaStreamImpl::UserMediaRequestInfo::~UserMediaRequestInfo() { |
848 DVLOG(1) << "~UserMediaRequestInfo"; | 821 DVLOG(1) << "~UserMediaRequestInfo"; |
849 } | 822 } |
850 | 823 |
851 void MediaStreamImpl::UserMediaRequestInfo::StartAudioTrack( | 824 void MediaStreamImpl::UserMediaRequestInfo::StartAudioTrack( |
852 const blink::WebMediaStreamTrack& track, | 825 const blink::WebMediaStreamTrack& track, |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
929 sources_.begin(); | 902 sources_.begin(); |
930 it != sources_.end(); ++it) { | 903 it != sources_.end(); ++it) { |
931 if (source.id() == it->id()) { | 904 if (source.id() == it->id()) { |
932 sources_.erase(it); | 905 sources_.erase(it); |
933 return; | 906 return; |
934 } | 907 } |
935 } | 908 } |
936 } | 909 } |
937 | 910 |
938 } // namespace content | 911 } // namespace content |
OLD | NEW |