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/user_media_client_impl.h" | 5 #include "content/renderer/media/user_media_client_impl.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <utility> | 10 #include <utility> |
(...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
425 UserMediaRequestInfo* request_info = FindUserMediaRequestInfo(request_id); | 425 UserMediaRequestInfo* request_info = FindUserMediaRequestInfo(request_id); |
426 if (!request_info) { | 426 if (!request_info) { |
427 // This can happen if the request is canceled or the frame reloads while | 427 // This can happen if the request is canceled or the frame reloads while |
428 // MediaStreamDispatcher is processing the request. | 428 // MediaStreamDispatcher is processing the request. |
429 DVLOG(1) << "Request ID not found"; | 429 DVLOG(1) << "Request ID not found"; |
430 OnStreamGeneratedForCancelledRequest(audio_array, video_array); | 430 OnStreamGeneratedForCancelledRequest(audio_array, video_array); |
431 return; | 431 return; |
432 } | 432 } |
433 request_info->generated = true; | 433 request_info->generated = true; |
434 | 434 |
435 for (const auto* array : {&audio_array, &video_array}) { | |
436 for (const auto& info : *array) { | |
437 WebRtcLogMessage(base::StringPrintf("Request %d for device \"%s\"", | |
438 request_id, | |
439 info.device.name.c_str())); | |
440 } | |
441 } | |
442 | |
435 DCHECK(!request_info->request.isNull()); | 443 DCHECK(!request_info->request.isNull()); |
436 blink::WebVector<blink::WebMediaStreamTrack> audio_track_vector( | 444 blink::WebVector<blink::WebMediaStreamTrack> audio_track_vector( |
437 audio_array.size()); | 445 audio_array.size()); |
438 CreateAudioTracks(audio_array, request_info->request.audioConstraints(), | 446 CreateAudioTracks(audio_array, request_info->request.audioConstraints(), |
439 &audio_track_vector, request_info); | 447 &audio_track_vector, request_info); |
440 | 448 |
441 blink::WebVector<blink::WebMediaStreamTrack> video_track_vector( | 449 blink::WebVector<blink::WebMediaStreamTrack> video_track_vector( |
442 video_array.size()); | 450 video_array.size()); |
443 CreateVideoTracks(video_array, request_info->request.videoConstraints(), | 451 CreateVideoTracks(video_array, request_info->request.videoConstraints(), |
444 &video_track_vector, request_info); | 452 &video_track_vector, request_info); |
445 | 453 |
446 blink::WebString webkit_id = blink::WebString::fromUTF8(label); | 454 blink::WebString webkit_id = blink::WebString::fromUTF8(label); |
447 blink::WebMediaStream* web_stream = &(request_info->web_stream); | 455 blink::WebMediaStream* web_stream = &(request_info->web_stream); |
448 | 456 |
449 web_stream->initialize(webkit_id, audio_track_vector, | 457 web_stream->initialize(webkit_id, audio_track_vector, video_track_vector); |
450 video_track_vector); | |
451 web_stream->setExtraData(new MediaStream()); | 458 web_stream->setExtraData(new MediaStream()); |
452 | 459 |
453 // Wait for the tracks to be started successfully or to fail. | 460 // Wait for the tracks to be started successfully or to fail. |
454 request_info->CallbackOnTracksStarted( | 461 request_info->CallbackOnTracksStarted( |
455 base::Bind(&UserMediaClientImpl::OnCreateNativeTracksCompleted, | 462 base::Bind(&UserMediaClientImpl::OnCreateNativeTracksCompleted, |
456 weak_factory_.GetWeakPtr())); | 463 weak_factory_.GetWeakPtr())); |
457 } | 464 } |
458 | 465 |
459 void UserMediaClientImpl::OnStreamGeneratedForCancelledRequest( | 466 void UserMediaClientImpl::OnStreamGeneratedForCancelledRequest( |
460 const StreamDeviceInfoArray& audio_array, | 467 const StreamDeviceInfoArray& audio_array, |
461 const StreamDeviceInfoArray& video_array) { | 468 const StreamDeviceInfoArray& video_array) { |
462 // Only stop the device if the device is not used in another MediaStream. | 469 // Only stop the device if the device is not used in another MediaStream. |
463 for (StreamDeviceInfoArray::const_iterator device_it = audio_array.begin(); | 470 for (StreamDeviceInfoArray::const_iterator device_it = audio_array.begin(); |
464 device_it != audio_array.end(); ++device_it) { | 471 device_it != audio_array.end(); ++device_it) { |
465 if (!FindLocalSource(*device_it)) | 472 if (!FindLocalSource(*device_it)) |
466 media_stream_dispatcher_->StopStreamDevice(*device_it); | 473 media_stream_dispatcher_->StopStreamDevice(*device_it); |
467 } | 474 } |
468 | 475 |
469 for (StreamDeviceInfoArray::const_iterator device_it = video_array.begin(); | 476 for (StreamDeviceInfoArray::const_iterator device_it = video_array.begin(); |
470 device_it != video_array.end(); ++device_it) { | 477 device_it != video_array.end(); ++device_it) { |
471 if (!FindLocalSource(*device_it)) | 478 if (!FindLocalSource(*device_it)) |
472 media_stream_dispatcher_->StopStreamDevice(*device_it); | 479 media_stream_dispatcher_->StopStreamDevice(*device_it); |
473 } | 480 } |
474 } | 481 } |
475 | 482 |
483 // static | |
484 void UserMediaClientImpl::OnAudioSourceStartedOnAudioThread( | |
485 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | |
486 base::WeakPtr<UserMediaClientImpl> weak_ptr, | |
487 MediaStreamSource* source, | |
488 MediaStreamRequestResult result, | |
489 const blink::WebString& result_name) { | |
490 task_runner->PostTask(FROM_HERE, | |
491 base::Bind(&UserMediaClientImpl::OnAudioSourceStarted, | |
492 weak_ptr, source, result, result_name)); | |
493 } | |
494 | |
495 void UserMediaClientImpl::OnAudioSourceStarted( | |
496 MediaStreamSource* source, | |
497 MediaStreamRequestResult result, | |
498 const blink::WebString& result_name) { | |
499 DCHECK(CalledOnValidThread()); | |
500 | |
501 for (auto it = pending_local_sources_.begin(); | |
502 it != pending_local_sources_.end(); ++it) { | |
503 MediaStreamSource* const source_extra_data = | |
504 static_cast<MediaStreamSource*>((*it).getExtraData()); | |
505 if (source_extra_data == source) { | |
miu
2017/01/11 20:51:11
style nit:
if (source_extra_data != source)
tommi (sloooow) - chröme
2017/01/11 22:40:58
Done.
| |
506 if (result == MEDIA_DEVICE_OK) | |
507 local_sources_.push_back((*it)); | |
508 pending_local_sources_.erase(it); | |
509 // Since a request object might get deleted and removed from the | |
miu
2017/01/11 20:51:11
This comment leads me to believe the pointer may b
tommi (sloooow) - chröme
2017/01/11 22:40:58
Indeed, the pointer to the request object will bec
| |
510 // |user_media_requests_| array while we iterate through it, we need | |
511 // to jump through this hoop here, copy pointers to the objects we're | |
512 // notifying and avoid using user_media_requests_ as we iterate+notify. | |
513 std::vector<UserMediaRequestInfo*> requests; | |
miu
2017/01/11 20:51:11
Suggest simplifying by just using the vector copy
tommi (sloooow) - chröme
2017/01/11 22:40:58
Unfortunately user_media_requests_ is a ScopedVect
miu
2017/01/12 21:25:37
Ah! This might be a good time to change the type (
tommi (sloooow) - chröme
2017/01/13 18:43:52
Type changed.
I gave the conversion a try and the
| |
514 requests.reserve(user_media_requests_.size()); | |
515 for (const auto& request : user_media_requests_) | |
516 requests.push_back(request); | |
517 for (const auto& request : requests) | |
miu
2017/01/11 20:51:11
nit: The elements are pointers (plain old data typ
tommi (sloooow) - chröme
2017/01/11 22:40:58
Done.
| |
518 request->OnAudioSourceStarted(source, result, result_name); | |
519 return; | |
520 } | |
521 } | |
522 NOTREACHED(); | |
523 } | |
524 | |
476 void UserMediaClientImpl::FinalizeEnumerateDevices( | 525 void UserMediaClientImpl::FinalizeEnumerateDevices( |
477 blink::WebMediaDevicesRequest request, | 526 blink::WebMediaDevicesRequest request, |
478 const EnumerationResult& result) { | 527 const EnumerationResult& result) { |
479 DCHECK_EQ(static_cast<size_t>(NUM_MEDIA_DEVICE_TYPES), result.size()); | 528 DCHECK_EQ(static_cast<size_t>(NUM_MEDIA_DEVICE_TYPES), result.size()); |
480 | 529 |
481 blink::WebVector<blink::WebMediaDeviceInfo> devices( | 530 blink::WebVector<blink::WebMediaDeviceInfo> devices( |
482 result[MEDIA_DEVICE_TYPE_AUDIO_INPUT].size() + | 531 result[MEDIA_DEVICE_TYPE_AUDIO_INPUT].size() + |
483 result[MEDIA_DEVICE_TYPE_VIDEO_INPUT].size() + | 532 result[MEDIA_DEVICE_TYPE_VIDEO_INPUT].size() + |
484 result[MEDIA_DEVICE_TYPE_AUDIO_OUTPUT].size()); | 533 result[MEDIA_DEVICE_TYPE_AUDIO_OUTPUT].size()); |
485 size_t index = 0; | 534 size_t index = 0; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
533 // as the underlying media device is unplugged from the system. | 582 // as the underlying media device is unplugged from the system. |
534 return; | 583 return; |
535 } | 584 } |
536 // By creating |source| it is guaranteed that the blink::WebMediaStreamSource | 585 // By creating |source| it is guaranteed that the blink::WebMediaStreamSource |
537 // object is valid during the cleanup. | 586 // object is valid during the cleanup. |
538 blink::WebMediaStreamSource source(*source_ptr); | 587 blink::WebMediaStreamSource source(*source_ptr); |
539 StopLocalSource(source, false); | 588 StopLocalSource(source, false); |
540 RemoveLocalSource(source); | 589 RemoveLocalSource(source); |
541 } | 590 } |
542 | 591 |
543 void UserMediaClientImpl::InitializeSourceObject( | 592 void UserMediaClientImpl::InitializeVideoSourceObject( |
544 const StreamDeviceInfo& device, | 593 const StreamDeviceInfo& device, |
545 blink::WebMediaStreamSource::Type type, | |
546 const blink::WebMediaConstraints& constraints, | 594 const blink::WebMediaConstraints& constraints, |
547 blink::WebMediaStreamSource* webkit_source) { | 595 blink::WebMediaStreamSource* webkit_source) { |
548 const blink::WebMediaStreamSource* existing_source = | 596 DCHECK(CalledOnValidThread()); |
549 FindLocalSource(device); | 597 |
550 if (existing_source) { | 598 *webkit_source = FindOrInitializeSourceObject(device); |
551 *webkit_source = *existing_source; | 599 if (webkit_source->getExtraData()) |
552 DVLOG(1) << "Source already exist. Reusing source with id " | 600 return; |
553 << webkit_source->id().utf8(); | 601 |
602 webkit_source->setExtraData(CreateVideoSource( | |
603 device, base::Bind(&UserMediaClientImpl::OnLocalSourceStopped, | |
604 weak_factory_.GetWeakPtr()))); | |
605 local_sources_.push_back(*webkit_source); | |
606 } | |
607 | |
608 void UserMediaClientImpl::InitializeAudioSourceObject( | |
609 const StreamDeviceInfo& device, | |
610 const blink::WebMediaConstraints& constraints, | |
611 blink::WebMediaStreamSource* webkit_source, | |
612 bool* source_initialized) { | |
613 DCHECK(CalledOnValidThread()); | |
614 | |
615 *webkit_source = FindOrInitializeSourceObject(device); | |
616 if (webkit_source->getExtraData()) { | |
617 *source_initialized = true; | |
554 return; | 618 return; |
555 } | 619 } |
556 | 620 |
557 webkit_source->initialize(blink::WebString::fromUTF8(device.device.id), type, | 621 *source_initialized = false; |
558 blink::WebString::fromUTF8(device.device.name), | |
559 false /* remote */); | |
560 | 622 |
561 DVLOG(1) << "Initialize source object :" | 623 // See if the source is already being initialized. |
562 << "id = " << webkit_source->id().utf8() | 624 auto* pending = FindPendingLocalSource(device); |
563 << ", name = " << webkit_source->name().utf8(); | 625 if (pending) { |
626 *webkit_source = *pending; | |
627 return; | |
628 } | |
564 | 629 |
565 if (type == blink::WebMediaStreamSource::TypeVideo) { | 630 // While sources are being initialized, keep them in a separate array. |
566 webkit_source->setExtraData( | 631 // Once they've finished initialized, they'll be moved over to local_sources_. |
567 CreateVideoSource( | 632 // See OnAudioSourceStarted for more details. |
568 device, | 633 pending_local_sources_.push_back(*webkit_source); |
569 base::Bind(&UserMediaClientImpl::OnLocalSourceStopped, | 634 |
570 weak_factory_.GetWeakPtr()))); | 635 MediaStreamSource::ConstraintsCallback source_ready = base::Bind( |
571 } else { | 636 &UserMediaClientImpl::OnAudioSourceStartedOnAudioThread, |
572 DCHECK_EQ(blink::WebMediaStreamSource::TypeAudio, type); | 637 base::ThreadTaskRunnerHandle::Get(), weak_factory_.GetWeakPtr()); |
573 MediaStreamAudioSource* const audio_source = | 638 |
574 CreateAudioSource(device, constraints); | 639 MediaStreamAudioSource* const audio_source = |
575 audio_source->SetStopCallback( | 640 CreateAudioSource(device, constraints, source_ready); |
576 base::Bind(&UserMediaClientImpl::OnLocalSourceStopped, | 641 audio_source->SetStopCallback(base::Bind( |
577 weak_factory_.GetWeakPtr())); | 642 &UserMediaClientImpl::OnLocalSourceStopped, weak_factory_.GetWeakPtr())); |
578 webkit_source->setExtraData(audio_source); // Takes ownership. | 643 webkit_source->setExtraData(audio_source); // Takes ownership. |
579 } | |
580 local_sources_.push_back(*webkit_source); | |
581 } | 644 } |
582 | 645 |
583 MediaStreamAudioSource* UserMediaClientImpl::CreateAudioSource( | 646 MediaStreamAudioSource* UserMediaClientImpl::CreateAudioSource( |
584 const StreamDeviceInfo& device, | 647 const StreamDeviceInfo& device, |
585 const blink::WebMediaConstraints& constraints) { | 648 const blink::WebMediaConstraints& constraints, |
649 const MediaStreamSource::ConstraintsCallback& source_ready) { | |
650 DCHECK(CalledOnValidThread()); | |
586 // If the audio device is a loopback device (for screen capture), or if the | 651 // If the audio device is a loopback device (for screen capture), or if the |
587 // constraints/effects parameters indicate no audio processing is needed, | 652 // constraints/effects parameters indicate no audio processing is needed, |
588 // create an efficient, direct-path MediaStreamAudioSource instance. | 653 // create an efficient, direct-path MediaStreamAudioSource instance. |
589 if (IsScreenCaptureMediaType(device.device.type) || | 654 if (IsScreenCaptureMediaType(device.device.type) || |
590 !MediaStreamAudioProcessor::WouldModifyAudio( | 655 !MediaStreamAudioProcessor::WouldModifyAudio( |
591 constraints, device.device.input.effects)) { | 656 constraints, device.device.input.effects)) { |
592 return new LocalMediaStreamAudioSource(RenderFrameObserver::routing_id(), | 657 return new LocalMediaStreamAudioSource(RenderFrameObserver::routing_id(), |
593 device); | 658 device, source_ready); |
594 } | 659 } |
595 | 660 |
596 // The audio device is not associated with screen capture and also requires | 661 // The audio device is not associated with screen capture and also requires |
597 // processing. | 662 // processing. |
598 ProcessedLocalAudioSource* source = new ProcessedLocalAudioSource( | 663 ProcessedLocalAudioSource* source = new ProcessedLocalAudioSource( |
599 RenderFrameObserver::routing_id(), device, dependency_factory_); | 664 RenderFrameObserver::routing_id(), device, constraints, source_ready, |
600 source->SetSourceConstraints(constraints); | 665 dependency_factory_); |
601 return source; | 666 return source; |
602 } | 667 } |
603 | 668 |
604 MediaStreamVideoSource* UserMediaClientImpl::CreateVideoSource( | 669 MediaStreamVideoSource* UserMediaClientImpl::CreateVideoSource( |
605 const StreamDeviceInfo& device, | 670 const StreamDeviceInfo& device, |
606 const MediaStreamSource::SourceStoppedCallback& stop_callback) { | 671 const MediaStreamSource::SourceStoppedCallback& stop_callback) { |
672 DCHECK(CalledOnValidThread()); | |
607 content::MediaStreamVideoCapturerSource* ret = | 673 content::MediaStreamVideoCapturerSource* ret = |
608 new content::MediaStreamVideoCapturerSource(stop_callback, device, | 674 new content::MediaStreamVideoCapturerSource(stop_callback, device, |
609 render_frame()); | 675 render_frame()); |
610 return ret; | 676 return ret; |
611 } | 677 } |
612 | 678 |
613 void UserMediaClientImpl::CreateVideoTracks( | 679 void UserMediaClientImpl::CreateVideoTracks( |
614 const StreamDeviceInfoArray& devices, | 680 const StreamDeviceInfoArray& devices, |
615 const blink::WebMediaConstraints& constraints, | 681 const blink::WebMediaConstraints& constraints, |
616 blink::WebVector<blink::WebMediaStreamTrack>* webkit_tracks, | 682 blink::WebVector<blink::WebMediaStreamTrack>* webkit_tracks, |
617 UserMediaRequestInfo* request) { | 683 UserMediaRequestInfo* request) { |
684 DCHECK(CalledOnValidThread()); | |
618 DCHECK_EQ(devices.size(), webkit_tracks->size()); | 685 DCHECK_EQ(devices.size(), webkit_tracks->size()); |
619 | 686 |
620 for (size_t i = 0; i < devices.size(); ++i) { | 687 for (size_t i = 0; i < devices.size(); ++i) { |
621 blink::WebMediaStreamSource webkit_source; | 688 blink::WebMediaStreamSource webkit_source; |
622 InitializeSourceObject(devices[i], | 689 InitializeVideoSourceObject(devices[i], constraints, &webkit_source); |
623 blink::WebMediaStreamSource::TypeVideo, | |
624 constraints, | |
625 &webkit_source); | |
626 (*webkit_tracks)[i] = | 690 (*webkit_tracks)[i] = |
627 request->CreateAndStartVideoTrack(webkit_source, constraints); | 691 request->CreateAndStartVideoTrack(webkit_source, constraints); |
628 } | 692 } |
629 } | 693 } |
630 | 694 |
631 void UserMediaClientImpl::CreateAudioTracks( | 695 void UserMediaClientImpl::CreateAudioTracks( |
632 const StreamDeviceInfoArray& devices, | 696 const StreamDeviceInfoArray& devices, |
633 const blink::WebMediaConstraints& constraints, | 697 const blink::WebMediaConstraints& constraints, |
634 blink::WebVector<blink::WebMediaStreamTrack>* webkit_tracks, | 698 blink::WebVector<blink::WebMediaStreamTrack>* webkit_tracks, |
635 UserMediaRequestInfo* request) { | 699 UserMediaRequestInfo* request) { |
700 DCHECK(CalledOnValidThread()); | |
636 DCHECK_EQ(devices.size(), webkit_tracks->size()); | 701 DCHECK_EQ(devices.size(), webkit_tracks->size()); |
637 | 702 |
638 // Log the device names for this request. | |
639 for (StreamDeviceInfoArray::const_iterator it = devices.begin(); | |
640 it != devices.end(); ++it) { | |
641 WebRtcLogMessage(base::StringPrintf( | |
642 "Generated media stream for request id %d contains audio device name" | |
643 " \"%s\"", | |
644 request->request_id, | |
645 it->device.name.c_str())); | |
646 } | |
647 | |
648 StreamDeviceInfoArray overridden_audio_array = devices; | 703 StreamDeviceInfoArray overridden_audio_array = devices; |
649 if (!request->enable_automatic_output_device_selection) { | 704 if (!request->enable_automatic_output_device_selection) { |
650 // If the GetUserMedia request did not explicitly set the constraint | 705 // If the GetUserMedia request did not explicitly set the constraint |
651 // kMediaStreamRenderToAssociatedSink, the output device parameters must | 706 // kMediaStreamRenderToAssociatedSink, the output device parameters must |
652 // be removed. | 707 // be removed. |
653 for (StreamDeviceInfoArray::iterator it = overridden_audio_array.begin(); | 708 for (auto& device_info : overridden_audio_array) { |
654 it != overridden_audio_array.end(); ++it) { | 709 device_info.device.matched_output_device_id = ""; |
655 it->device.matched_output_device_id = ""; | 710 device_info.device.matched_output = |
656 it->device.matched_output = MediaStreamDevice::AudioDeviceParameters(); | 711 MediaStreamDevice::AudioDeviceParameters(); |
657 } | 712 } |
658 } | 713 } |
659 | 714 |
660 for (size_t i = 0; i < overridden_audio_array.size(); ++i) { | 715 for (size_t i = 0; i < overridden_audio_array.size(); ++i) { |
661 blink::WebMediaStreamSource webkit_source; | 716 blink::WebMediaStreamSource webkit_source; |
662 InitializeSourceObject(overridden_audio_array[i], | 717 bool source_initialized = true; |
663 blink::WebMediaStreamSource::TypeAudio, | 718 InitializeAudioSourceObject(overridden_audio_array[i], constraints, |
664 constraints, | 719 &webkit_source, &source_initialized); |
665 &webkit_source); | |
666 (*webkit_tracks)[i].initialize(webkit_source); | 720 (*webkit_tracks)[i].initialize(webkit_source); |
667 request->StartAudioTrack((*webkit_tracks)[i]); | 721 request->StartAudioTrack((*webkit_tracks)[i], source_initialized); |
miu
2017/01/11 20:51:11
It's a bit confusing to pass in a bool turning "St
tommi (sloooow) - chröme
2017/01/11 22:40:58
The variable name I chose caused the confusion. I
| |
668 } | 722 } |
669 } | 723 } |
670 | 724 |
671 void UserMediaClientImpl::OnCreateNativeTracksCompleted( | 725 void UserMediaClientImpl::OnCreateNativeTracksCompleted( |
672 UserMediaRequestInfo* request, | 726 UserMediaRequestInfo* request, |
673 MediaStreamRequestResult result, | 727 MediaStreamRequestResult result, |
674 const blink::WebString& result_name) { | 728 const blink::WebString& result_name) { |
729 DCHECK(CalledOnValidThread()); | |
675 DVLOG(1) << "UserMediaClientImpl::OnCreateNativeTracksComplete(" | 730 DVLOG(1) << "UserMediaClientImpl::OnCreateNativeTracksComplete(" |
676 << "{request_id = " << request->request_id << "} " | 731 << "{request_id = " << request->request_id << "} " |
677 << "{result = " << result << "})"; | 732 << "{result = " << result << "})"; |
678 | 733 |
679 if (result == content::MEDIA_DEVICE_OK) { | 734 if (result == content::MEDIA_DEVICE_OK) { |
680 GetUserMediaRequestSucceeded(request->web_stream, request->request); | 735 GetUserMediaRequestSucceeded(request->web_stream, request->request); |
681 } else { | 736 } else { |
682 GetUserMediaRequestFailed(request->request, result, result_name); | 737 GetUserMediaRequestFailed(request->request, result, result_name); |
683 | 738 |
684 blink::WebVector<blink::WebMediaStreamTrack> tracks; | 739 blink::WebVector<blink::WebMediaStreamTrack> tracks; |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
811 request_info.requestFailed(); | 866 request_info.requestFailed(); |
812 } | 867 } |
813 | 868 |
814 void UserMediaClientImpl::EnumerateDevicesSucceded( | 869 void UserMediaClientImpl::EnumerateDevicesSucceded( |
815 blink::WebMediaDevicesRequest* request, | 870 blink::WebMediaDevicesRequest* request, |
816 blink::WebVector<blink::WebMediaDeviceInfo>& devices) { | 871 blink::WebVector<blink::WebMediaDeviceInfo>& devices) { |
817 request->requestSucceeded(devices); | 872 request->requestSucceeded(devices); |
818 } | 873 } |
819 | 874 |
820 const blink::WebMediaStreamSource* UserMediaClientImpl::FindLocalSource( | 875 const blink::WebMediaStreamSource* UserMediaClientImpl::FindLocalSource( |
876 const LocalStreamSources& sources, | |
821 const StreamDeviceInfo& device) const { | 877 const StreamDeviceInfo& device) const { |
822 for (LocalStreamSources::const_iterator it = local_sources_.begin(); | 878 for (const auto& local_source : sources) { |
823 it != local_sources_.end(); ++it) { | |
824 MediaStreamSource* const source = | 879 MediaStreamSource* const source = |
825 static_cast<MediaStreamSource*>(it->getExtraData()); | 880 static_cast<MediaStreamSource*>(local_source.getExtraData()); |
826 const StreamDeviceInfo& active_device = source->device_info(); | 881 const StreamDeviceInfo& active_device = source->device_info(); |
827 if (IsSameDevice(active_device, device)) { | 882 if (IsSameDevice(active_device, device)) |
828 return &(*it); | 883 return &local_source; |
829 } | |
830 } | 884 } |
831 return NULL; | 885 return nullptr; |
886 } | |
887 | |
888 blink::WebMediaStreamSource UserMediaClientImpl::FindOrInitializeSourceObject( | |
889 const StreamDeviceInfo& device) { | |
890 const blink::WebMediaStreamSource* existing_source = FindLocalSource(device); | |
891 if (existing_source) { | |
892 DVLOG(1) << "Source already exists. Reusing source with id " | |
893 << existing_source->id().utf8(); | |
894 return *existing_source; | |
895 } | |
896 | |
897 blink::WebMediaStreamSource::Type type = | |
898 IsAudioInputMediaType(device.device.type) | |
899 ? blink::WebMediaStreamSource::TypeAudio | |
900 : blink::WebMediaStreamSource::TypeVideo; | |
901 | |
902 blink::WebMediaStreamSource source; | |
903 source.initialize(blink::WebString::fromUTF8(device.device.id), type, | |
904 blink::WebString::fromUTF8(device.device.name), | |
905 false /* remote */); | |
906 | |
907 DVLOG(1) << "Initialize source object :" | |
908 << "id = " << source.id().utf8() | |
909 << ", name = " << source.name().utf8(); | |
910 return source; | |
832 } | 911 } |
833 | 912 |
834 bool UserMediaClientImpl::RemoveLocalSource( | 913 bool UserMediaClientImpl::RemoveLocalSource( |
835 const blink::WebMediaStreamSource& source) { | 914 const blink::WebMediaStreamSource& source) { |
836 bool device_found = false; | 915 DCHECK(CalledOnValidThread()); |
916 | |
837 for (LocalStreamSources::iterator device_it = local_sources_.begin(); | 917 for (LocalStreamSources::iterator device_it = local_sources_.begin(); |
838 device_it != local_sources_.end(); ++device_it) { | 918 device_it != local_sources_.end(); ++device_it) { |
839 if (IsSameSource(*device_it, source)) { | 919 if (IsSameSource(*device_it, source)) { |
840 device_found = true; | |
841 local_sources_.erase(device_it); | 920 local_sources_.erase(device_it); |
842 break; | 921 return true; |
843 } | 922 } |
844 } | 923 } |
845 return device_found; | 924 |
925 // Check if the source was pending. | |
926 for (LocalStreamSources::iterator device_it = pending_local_sources_.begin(); | |
927 device_it != pending_local_sources_.end(); ++device_it) { | |
928 if (IsSameSource(*device_it, source)) { | |
929 MediaStreamSource* const source_extra_data = | |
930 static_cast<MediaStreamSource*>(source.getExtraData()); | |
931 for (const auto& request : user_media_requests_) { | |
miu
2017/01/11 20:51:11
Does this suffer from the same problem as above, w
tommi (sloooow) - chröme
2017/01/11 22:40:58
Good catch - it must. I factored out the code that
| |
932 request->OnAudioSourceStarted(source_extra_data, | |
933 MEDIA_DEVICE_TRACK_START_FAILURE, | |
934 "Failed to access audio capture device"); | |
935 } | |
936 pending_local_sources_.erase(device_it); | |
937 return true; | |
938 } | |
939 } | |
940 | |
941 return false; | |
846 } | 942 } |
847 | 943 |
848 UserMediaClientImpl::UserMediaRequestInfo* | 944 UserMediaClientImpl::UserMediaRequestInfo* |
849 UserMediaClientImpl::FindUserMediaRequestInfo(int request_id) { | 945 UserMediaClientImpl::FindUserMediaRequestInfo(int request_id) { |
946 DCHECK(CalledOnValidThread()); | |
850 UserMediaRequests::iterator it = user_media_requests_.begin(); | 947 UserMediaRequests::iterator it = user_media_requests_.begin(); |
851 for (; it != user_media_requests_.end(); ++it) { | 948 for (; it != user_media_requests_.end(); ++it) { |
852 if ((*it)->request_id == request_id) | 949 if ((*it)->request_id == request_id) |
853 return (*it); | 950 return (*it); |
854 } | 951 } |
855 return NULL; | 952 return NULL; |
856 } | 953 } |
857 | 954 |
858 UserMediaClientImpl::UserMediaRequestInfo* | 955 UserMediaClientImpl::UserMediaRequestInfo* |
859 UserMediaClientImpl::FindUserMediaRequestInfo( | 956 UserMediaClientImpl::FindUserMediaRequestInfo( |
860 const blink::WebUserMediaRequest& request) { | 957 const blink::WebUserMediaRequest& request) { |
958 DCHECK(CalledOnValidThread()); | |
861 UserMediaRequests::iterator it = user_media_requests_.begin(); | 959 UserMediaRequests::iterator it = user_media_requests_.begin(); |
862 for (; it != user_media_requests_.end(); ++it) { | 960 for (; it != user_media_requests_.end(); ++it) { |
863 if ((*it)->request == request) | 961 if ((*it)->request == request) |
864 return (*it); | 962 return (*it); |
865 } | 963 } |
866 return NULL; | 964 return NULL; |
867 } | 965 } |
868 | 966 |
869 void UserMediaClientImpl::DeleteUserMediaRequestInfo( | 967 void UserMediaClientImpl::DeleteUserMediaRequestInfo( |
870 UserMediaRequestInfo* request) { | 968 UserMediaRequestInfo* request) { |
969 DCHECK(CalledOnValidThread()); | |
871 UserMediaRequests::iterator it = user_media_requests_.begin(); | 970 UserMediaRequests::iterator it = user_media_requests_.begin(); |
872 for (; it != user_media_requests_.end(); ++it) { | 971 for (; it != user_media_requests_.end(); ++it) { |
873 if ((*it) == request) { | 972 if ((*it) == request) { |
874 user_media_requests_.erase(it); | 973 user_media_requests_.erase(it); |
875 return; | 974 return; |
876 } | 975 } |
877 } | 976 } |
878 NOTREACHED(); | 977 NOTREACHED(); |
879 } | 978 } |
880 | 979 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
968 request(request), | 1067 request(request), |
969 request_result_(MEDIA_DEVICE_OK), | 1068 request_result_(MEDIA_DEVICE_OK), |
970 request_result_name_("") { | 1069 request_result_name_("") { |
971 } | 1070 } |
972 | 1071 |
973 UserMediaClientImpl::UserMediaRequestInfo::~UserMediaRequestInfo() { | 1072 UserMediaClientImpl::UserMediaRequestInfo::~UserMediaRequestInfo() { |
974 DVLOG(1) << "~UserMediaRequestInfo"; | 1073 DVLOG(1) << "~UserMediaRequestInfo"; |
975 } | 1074 } |
976 | 1075 |
977 void UserMediaClientImpl::UserMediaRequestInfo::StartAudioTrack( | 1076 void UserMediaClientImpl::UserMediaRequestInfo::StartAudioTrack( |
978 const blink::WebMediaStreamTrack& track) { | 1077 const blink::WebMediaStreamTrack& track, |
1078 bool source_initialized) { | |
979 DCHECK(track.source().getType() == blink::WebMediaStreamSource::TypeAudio); | 1079 DCHECK(track.source().getType() == blink::WebMediaStreamSource::TypeAudio); |
980 MediaStreamAudioSource* native_source = | 1080 MediaStreamAudioSource* native_source = |
981 MediaStreamAudioSource::From(track.source()); | 1081 MediaStreamAudioSource::From(track.source()); |
982 DCHECK(native_source); | 1082 // Add the source as pending since OnTrackStarted will expect it to be there. |
1083 sources_waiting_for_callback_.push_back(native_source); | |
983 | 1084 |
984 sources_.push_back(track.source()); | 1085 sources_.push_back(track.source()); |
985 sources_waiting_for_callback_.push_back(native_source); | 1086 bool connected = native_source->ConnectToTrack(track); |
986 if (native_source->ConnectToTrack(track)) | 1087 if (source_initialized) { |
1088 OnTrackStarted( | |
1089 native_source, | |
1090 connected ? MEDIA_DEVICE_OK : MEDIA_DEVICE_TRACK_START_FAILURE, ""); | |
1091 #if defined(OS_ANDROID) | |
1092 } else if (connected) { | |
1093 CHECK(native_source->is_local_source()); | |
miu
2017/01/11 20:51:11
Suggest moving the Android "readiness" state hack
tommi (sloooow) - chröme
2017/01/11 22:40:58
I think I've addressed this in the other comments.
| |
1094 // On Android, we won't get the callback indicating the device readyness. | |
1095 // TODO(tommi): Update the android implementation to support the | |
1096 // OnAudioSourceStarted notification. http://crbug.com/679302 | |
987 OnTrackStarted(native_source, MEDIA_DEVICE_OK, ""); | 1097 OnTrackStarted(native_source, MEDIA_DEVICE_OK, ""); |
988 else | 1098 #endif |
989 OnTrackStarted(native_source, MEDIA_DEVICE_TRACK_START_FAILURE, ""); | 1099 } |
miu
2017/01/11 20:51:11
So, basically, with all the other comments applied
tommi (sloooow) - chröme
2017/01/11 22:40:58
Same here and I think StartAudioTrack still needs
| |
990 } | 1100 } |
991 | 1101 |
992 blink::WebMediaStreamTrack | 1102 blink::WebMediaStreamTrack |
993 UserMediaClientImpl::UserMediaRequestInfo::CreateAndStartVideoTrack( | 1103 UserMediaClientImpl::UserMediaRequestInfo::CreateAndStartVideoTrack( |
994 const blink::WebMediaStreamSource& source, | 1104 const blink::WebMediaStreamSource& source, |
995 const blink::WebMediaConstraints& constraints) { | 1105 const blink::WebMediaConstraints& constraints) { |
996 DCHECK(source.getType() == blink::WebMediaStreamSource::TypeVideo); | 1106 DCHECK(source.getType() == blink::WebMediaStreamSource::TypeVideo); |
997 MediaStreamVideoSource* native_source = | 1107 MediaStreamVideoSource* native_source = |
998 MediaStreamVideoSource::GetVideoSource(source); | 1108 MediaStreamVideoSource::GetVideoSource(source); |
999 DCHECK(native_source); | 1109 DCHECK(native_source); |
(...skipping 30 matching lines...) Expand all Loading... | |
1030 request_result_ = result; | 1140 request_result_ = result; |
1031 request_result_name_ = result_name; | 1141 request_result_name_ = result_name; |
1032 } | 1142 } |
1033 | 1143 |
1034 CheckAllTracksStarted(); | 1144 CheckAllTracksStarted(); |
1035 } | 1145 } |
1036 | 1146 |
1037 void UserMediaClientImpl::UserMediaRequestInfo::CheckAllTracksStarted() { | 1147 void UserMediaClientImpl::UserMediaRequestInfo::CheckAllTracksStarted() { |
1038 if (!ready_callback_.is_null() && sources_waiting_for_callback_.empty()) { | 1148 if (!ready_callback_.is_null() && sources_waiting_for_callback_.empty()) { |
1039 ready_callback_.Run(this, request_result_, request_result_name_); | 1149 ready_callback_.Run(this, request_result_, request_result_name_); |
1150 // NOTE: |this| might now be deleted. | |
1040 } | 1151 } |
1041 } | 1152 } |
1042 | 1153 |
1043 bool UserMediaClientImpl::UserMediaRequestInfo::IsSourceUsed( | |
1044 const blink::WebMediaStreamSource& source) const { | |
1045 for (std::vector<blink::WebMediaStreamSource>::const_iterator source_it = | |
1046 sources_.begin(); | |
1047 source_it != sources_.end(); ++source_it) { | |
1048 if (source_it->id() == source.id()) | |
1049 return true; | |
1050 } | |
1051 return false; | |
1052 } | |
1053 | |
1054 void UserMediaClientImpl::UserMediaRequestInfo::RemoveSource( | |
1055 const blink::WebMediaStreamSource& source) { | |
1056 for (std::vector<blink::WebMediaStreamSource>::iterator it = | |
1057 sources_.begin(); | |
1058 it != sources_.end(); ++it) { | |
1059 if (source.id() == it->id()) { | |
1060 sources_.erase(it); | |
1061 return; | |
1062 } | |
1063 } | |
1064 } | |
1065 | |
1066 bool UserMediaClientImpl::UserMediaRequestInfo::HasPendingSources() const { | 1154 bool UserMediaClientImpl::UserMediaRequestInfo::HasPendingSources() const { |
1067 return !sources_waiting_for_callback_.empty(); | 1155 return !sources_waiting_for_callback_.empty(); |
1068 } | 1156 } |
1069 | 1157 |
1158 void UserMediaClientImpl::UserMediaRequestInfo::OnAudioSourceStarted( | |
1159 MediaStreamSource* source, | |
1160 MediaStreamRequestResult result, | |
1161 const blink::WebString& result_name) { | |
1162 // Check if we're waiting to be notified of this source. If not, then we'll | |
1163 // ignore the notification. | |
1164 auto found = std::find(sources_waiting_for_callback_.begin(), | |
1165 sources_waiting_for_callback_.end(), source); | |
1166 if (found != sources_waiting_for_callback_.end()) | |
1167 OnTrackStarted(source, result, result_name); | |
1168 } | |
1169 | |
1070 void UserMediaClientImpl::OnDestruct() { | 1170 void UserMediaClientImpl::OnDestruct() { |
1071 delete this; | 1171 delete this; |
1072 } | 1172 } |
1073 | 1173 |
1074 } // namespace content | 1174 } // namespace content |
OLD | NEW |