| 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) { |
| 506 if (result == MEDIA_DEVICE_OK) |
| 507 local_sources_.push_back((*it)); |
| 508 pending_local_sources_.erase(it); |
| 509 for (const auto& request : user_media_requests_) |
| 510 request->OnAudioSourceStarted(source, result, result_name); |
| 511 return; |
| 512 } |
| 513 } |
| 514 NOTREACHED(); |
| 515 } |
| 516 |
| 476 void UserMediaClientImpl::FinalizeEnumerateDevices( | 517 void UserMediaClientImpl::FinalizeEnumerateDevices( |
| 477 blink::WebMediaDevicesRequest request, | 518 blink::WebMediaDevicesRequest request, |
| 478 const EnumerationResult& result) { | 519 const EnumerationResult& result) { |
| 479 DCHECK_EQ(static_cast<size_t>(NUM_MEDIA_DEVICE_TYPES), result.size()); | 520 DCHECK_EQ(static_cast<size_t>(NUM_MEDIA_DEVICE_TYPES), result.size()); |
| 480 | 521 |
| 481 blink::WebVector<blink::WebMediaDeviceInfo> devices( | 522 blink::WebVector<blink::WebMediaDeviceInfo> devices( |
| 482 result[MEDIA_DEVICE_TYPE_AUDIO_INPUT].size() + | 523 result[MEDIA_DEVICE_TYPE_AUDIO_INPUT].size() + |
| 483 result[MEDIA_DEVICE_TYPE_VIDEO_INPUT].size() + | 524 result[MEDIA_DEVICE_TYPE_VIDEO_INPUT].size() + |
| 484 result[MEDIA_DEVICE_TYPE_AUDIO_OUTPUT].size()); | 525 result[MEDIA_DEVICE_TYPE_AUDIO_OUTPUT].size()); |
| 485 size_t index = 0; | 526 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. | 574 // as the underlying media device is unplugged from the system. |
| 534 return; | 575 return; |
| 535 } | 576 } |
| 536 // By creating |source| it is guaranteed that the blink::WebMediaStreamSource | 577 // By creating |source| it is guaranteed that the blink::WebMediaStreamSource |
| 537 // object is valid during the cleanup. | 578 // object is valid during the cleanup. |
| 538 blink::WebMediaStreamSource source(*source_ptr); | 579 blink::WebMediaStreamSource source(*source_ptr); |
| 539 StopLocalSource(source, false); | 580 StopLocalSource(source, false); |
| 540 RemoveLocalSource(source); | 581 RemoveLocalSource(source); |
| 541 } | 582 } |
| 542 | 583 |
| 543 void UserMediaClientImpl::InitializeSourceObject( | 584 void UserMediaClientImpl::InitializeVideoSourceObject( |
| 544 const StreamDeviceInfo& device, | 585 const StreamDeviceInfo& device, |
| 545 blink::WebMediaStreamSource::Type type, | |
| 546 const blink::WebMediaConstraints& constraints, | 586 const blink::WebMediaConstraints& constraints, |
| 547 blink::WebMediaStreamSource* webkit_source) { | 587 blink::WebMediaStreamSource* webkit_source) { |
| 548 const blink::WebMediaStreamSource* existing_source = | 588 DCHECK(CalledOnValidThread()); |
| 549 FindLocalSource(device); | 589 |
| 550 if (existing_source) { | 590 *webkit_source = FindOrInitializeSourceObject(device); |
| 551 *webkit_source = *existing_source; | 591 if (webkit_source->getExtraData()) |
| 552 DVLOG(1) << "Source already exist. Reusing source with id " | 592 return; |
| 553 << webkit_source->id().utf8(); | 593 |
| 594 webkit_source->setExtraData(CreateVideoSource( |
| 595 device, base::Bind(&UserMediaClientImpl::OnLocalSourceStopped, |
| 596 weak_factory_.GetWeakPtr()))); |
| 597 local_sources_.push_back(*webkit_source); |
| 598 } |
| 599 |
| 600 void UserMediaClientImpl::InitializeAudioSourceObject( |
| 601 const StreamDeviceInfo& device, |
| 602 const blink::WebMediaConstraints& constraints, |
| 603 blink::WebMediaStreamSource* webkit_source, |
| 604 bool* source_initialized) { |
| 605 DCHECK(CalledOnValidThread()); |
| 606 |
| 607 *webkit_source = FindOrInitializeSourceObject(device); |
| 608 if (webkit_source->getExtraData()) { |
| 609 *source_initialized = true; |
| 554 return; | 610 return; |
| 555 } | 611 } |
| 556 | 612 |
| 557 webkit_source->initialize(blink::WebString::fromUTF8(device.device.id), type, | 613 *source_initialized = false; |
| 558 blink::WebString::fromUTF8(device.device.name), | |
| 559 false /* remote */); | |
| 560 | 614 |
| 561 DVLOG(1) << "Initialize source object :" | 615 // See if the source is already being initialized. |
| 562 << "id = " << webkit_source->id().utf8() | 616 auto* pending = FindPendingLocalSource(device); |
| 563 << ", name = " << webkit_source->name().utf8(); | 617 if (pending) { |
| 618 *webkit_source = *pending; |
| 619 return; |
| 620 } |
| 564 | 621 |
| 565 if (type == blink::WebMediaStreamSource::TypeVideo) { | 622 // While sources are being initialized, keep them in a separate array. |
| 566 webkit_source->setExtraData( | 623 // Once they've finished initialized, they'll be moved over to local_sources_. |
| 567 CreateVideoSource( | 624 // See OnAudioSourceStarted for more details. |
| 568 device, | 625 pending_local_sources_.push_back(*webkit_source); |
| 569 base::Bind(&UserMediaClientImpl::OnLocalSourceStopped, | 626 |
| 570 weak_factory_.GetWeakPtr()))); | 627 MediaStreamSource::ConstraintsCallback source_ready = base::Bind( |
| 571 } else { | 628 &UserMediaClientImpl::OnAudioSourceStartedOnAudioThread, |
| 572 DCHECK_EQ(blink::WebMediaStreamSource::TypeAudio, type); | 629 base::ThreadTaskRunnerHandle::Get(), weak_factory_.GetWeakPtr()); |
| 573 MediaStreamAudioSource* const audio_source = | 630 |
| 574 CreateAudioSource(device, constraints); | 631 MediaStreamAudioSource* const audio_source = |
| 575 audio_source->SetStopCallback( | 632 CreateAudioSource(device, constraints, source_ready); |
| 576 base::Bind(&UserMediaClientImpl::OnLocalSourceStopped, | 633 audio_source->SetStopCallback(base::Bind( |
| 577 weak_factory_.GetWeakPtr())); | 634 &UserMediaClientImpl::OnLocalSourceStopped, weak_factory_.GetWeakPtr())); |
| 578 webkit_source->setExtraData(audio_source); // Takes ownership. | 635 webkit_source->setExtraData(audio_source); // Takes ownership. |
| 579 } | |
| 580 local_sources_.push_back(*webkit_source); | |
| 581 } | 636 } |
| 582 | 637 |
| 583 MediaStreamAudioSource* UserMediaClientImpl::CreateAudioSource( | 638 MediaStreamAudioSource* UserMediaClientImpl::CreateAudioSource( |
| 584 const StreamDeviceInfo& device, | 639 const StreamDeviceInfo& device, |
| 585 const blink::WebMediaConstraints& constraints) { | 640 const blink::WebMediaConstraints& constraints, |
| 641 const MediaStreamSource::ConstraintsCallback& source_ready) { |
| 642 DCHECK(CalledOnValidThread()); |
| 586 // If the audio device is a loopback device (for screen capture), or if the | 643 // If the audio device is a loopback device (for screen capture), or if the |
| 587 // constraints/effects parameters indicate no audio processing is needed, | 644 // constraints/effects parameters indicate no audio processing is needed, |
| 588 // create an efficient, direct-path MediaStreamAudioSource instance. | 645 // create an efficient, direct-path MediaStreamAudioSource instance. |
| 589 if (IsScreenCaptureMediaType(device.device.type) || | 646 if (IsScreenCaptureMediaType(device.device.type) || |
| 590 !MediaStreamAudioProcessor::WouldModifyAudio( | 647 !MediaStreamAudioProcessor::WouldModifyAudio( |
| 591 constraints, device.device.input.effects)) { | 648 constraints, device.device.input.effects)) { |
| 592 return new LocalMediaStreamAudioSource(RenderFrameObserver::routing_id(), | 649 return new LocalMediaStreamAudioSource(RenderFrameObserver::routing_id(), |
| 593 device); | 650 device, source_ready); |
| 594 } | 651 } |
| 595 | 652 |
| 596 // The audio device is not associated with screen capture and also requires | 653 // The audio device is not associated with screen capture and also requires |
| 597 // processing. | 654 // processing. |
| 598 ProcessedLocalAudioSource* source = new ProcessedLocalAudioSource( | 655 ProcessedLocalAudioSource* source = new ProcessedLocalAudioSource( |
| 599 RenderFrameObserver::routing_id(), device, dependency_factory_); | 656 RenderFrameObserver::routing_id(), device, constraints, source_ready, |
| 600 source->SetSourceConstraints(constraints); | 657 dependency_factory_); |
| 601 return source; | 658 return source; |
| 602 } | 659 } |
| 603 | 660 |
| 604 MediaStreamVideoSource* UserMediaClientImpl::CreateVideoSource( | 661 MediaStreamVideoSource* UserMediaClientImpl::CreateVideoSource( |
| 605 const StreamDeviceInfo& device, | 662 const StreamDeviceInfo& device, |
| 606 const MediaStreamSource::SourceStoppedCallback& stop_callback) { | 663 const MediaStreamSource::SourceStoppedCallback& stop_callback) { |
| 664 DCHECK(CalledOnValidThread()); |
| 607 content::MediaStreamVideoCapturerSource* ret = | 665 content::MediaStreamVideoCapturerSource* ret = |
| 608 new content::MediaStreamVideoCapturerSource(stop_callback, device, | 666 new content::MediaStreamVideoCapturerSource(stop_callback, device, |
| 609 render_frame()); | 667 render_frame()); |
| 610 return ret; | 668 return ret; |
| 611 } | 669 } |
| 612 | 670 |
| 613 void UserMediaClientImpl::CreateVideoTracks( | 671 void UserMediaClientImpl::CreateVideoTracks( |
| 614 const StreamDeviceInfoArray& devices, | 672 const StreamDeviceInfoArray& devices, |
| 615 const blink::WebMediaConstraints& constraints, | 673 const blink::WebMediaConstraints& constraints, |
| 616 blink::WebVector<blink::WebMediaStreamTrack>* webkit_tracks, | 674 blink::WebVector<blink::WebMediaStreamTrack>* webkit_tracks, |
| 617 UserMediaRequestInfo* request) { | 675 UserMediaRequestInfo* request) { |
| 676 DCHECK(CalledOnValidThread()); |
| 618 DCHECK_EQ(devices.size(), webkit_tracks->size()); | 677 DCHECK_EQ(devices.size(), webkit_tracks->size()); |
| 619 | 678 |
| 620 for (size_t i = 0; i < devices.size(); ++i) { | 679 for (size_t i = 0; i < devices.size(); ++i) { |
| 621 blink::WebMediaStreamSource webkit_source; | 680 blink::WebMediaStreamSource webkit_source; |
| 622 InitializeSourceObject(devices[i], | 681 InitializeVideoSourceObject(devices[i], constraints, &webkit_source); |
| 623 blink::WebMediaStreamSource::TypeVideo, | |
| 624 constraints, | |
| 625 &webkit_source); | |
| 626 (*webkit_tracks)[i] = | 682 (*webkit_tracks)[i] = |
| 627 request->CreateAndStartVideoTrack(webkit_source, constraints); | 683 request->CreateAndStartVideoTrack(webkit_source, constraints); |
| 628 } | 684 } |
| 629 } | 685 } |
| 630 | 686 |
| 631 void UserMediaClientImpl::CreateAudioTracks( | 687 void UserMediaClientImpl::CreateAudioTracks( |
| 632 const StreamDeviceInfoArray& devices, | 688 const StreamDeviceInfoArray& devices, |
| 633 const blink::WebMediaConstraints& constraints, | 689 const blink::WebMediaConstraints& constraints, |
| 634 blink::WebVector<blink::WebMediaStreamTrack>* webkit_tracks, | 690 blink::WebVector<blink::WebMediaStreamTrack>* webkit_tracks, |
| 635 UserMediaRequestInfo* request) { | 691 UserMediaRequestInfo* request) { |
| 692 DCHECK(CalledOnValidThread()); |
| 636 DCHECK_EQ(devices.size(), webkit_tracks->size()); | 693 DCHECK_EQ(devices.size(), webkit_tracks->size()); |
| 637 | 694 |
| 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; | 695 StreamDeviceInfoArray overridden_audio_array = devices; |
| 649 if (!request->enable_automatic_output_device_selection) { | 696 if (!request->enable_automatic_output_device_selection) { |
| 650 // If the GetUserMedia request did not explicitly set the constraint | 697 // If the GetUserMedia request did not explicitly set the constraint |
| 651 // kMediaStreamRenderToAssociatedSink, the output device parameters must | 698 // kMediaStreamRenderToAssociatedSink, the output device parameters must |
| 652 // be removed. | 699 // be removed. |
| 653 for (StreamDeviceInfoArray::iterator it = overridden_audio_array.begin(); | 700 for (auto& device_info : overridden_audio_array) { |
| 654 it != overridden_audio_array.end(); ++it) { | 701 device_info.device.matched_output_device_id = ""; |
| 655 it->device.matched_output_device_id = ""; | 702 device_info.device.matched_output = |
| 656 it->device.matched_output = MediaStreamDevice::AudioDeviceParameters(); | 703 MediaStreamDevice::AudioDeviceParameters(); |
| 657 } | 704 } |
| 658 } | 705 } |
| 659 | 706 |
| 660 for (size_t i = 0; i < overridden_audio_array.size(); ++i) { | 707 for (size_t i = 0; i < overridden_audio_array.size(); ++i) { |
| 661 blink::WebMediaStreamSource webkit_source; | 708 blink::WebMediaStreamSource webkit_source; |
| 662 InitializeSourceObject(overridden_audio_array[i], | 709 bool source_initialized = true; |
| 663 blink::WebMediaStreamSource::TypeAudio, | 710 InitializeAudioSourceObject(overridden_audio_array[i], constraints, |
| 664 constraints, | 711 &webkit_source, &source_initialized); |
| 665 &webkit_source); | |
| 666 (*webkit_tracks)[i].initialize(webkit_source); | 712 (*webkit_tracks)[i].initialize(webkit_source); |
| 667 request->StartAudioTrack((*webkit_tracks)[i]); | 713 request->StartAudioTrack((*webkit_tracks)[i], source_initialized); |
| 668 } | 714 } |
| 669 } | 715 } |
| 670 | 716 |
| 671 void UserMediaClientImpl::OnCreateNativeTracksCompleted( | 717 void UserMediaClientImpl::OnCreateNativeTracksCompleted( |
| 672 UserMediaRequestInfo* request, | 718 UserMediaRequestInfo* request, |
| 673 MediaStreamRequestResult result, | 719 MediaStreamRequestResult result, |
| 674 const blink::WebString& result_name) { | 720 const blink::WebString& result_name) { |
| 675 DVLOG(1) << "UserMediaClientImpl::OnCreateNativeTracksComplete(" | 721 DVLOG(1) << "UserMediaClientImpl::OnCreateNativeTracksComplete(" |
| 676 << "{request_id = " << request->request_id << "} " | 722 << "{request_id = " << request->request_id << "} " |
| 677 << "{result = " << result << "})"; | 723 << "{result = " << result << "})"; |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 811 request_info.requestFailed(); | 857 request_info.requestFailed(); |
| 812 } | 858 } |
| 813 | 859 |
| 814 void UserMediaClientImpl::EnumerateDevicesSucceded( | 860 void UserMediaClientImpl::EnumerateDevicesSucceded( |
| 815 blink::WebMediaDevicesRequest* request, | 861 blink::WebMediaDevicesRequest* request, |
| 816 blink::WebVector<blink::WebMediaDeviceInfo>& devices) { | 862 blink::WebVector<blink::WebMediaDeviceInfo>& devices) { |
| 817 request->requestSucceeded(devices); | 863 request->requestSucceeded(devices); |
| 818 } | 864 } |
| 819 | 865 |
| 820 const blink::WebMediaStreamSource* UserMediaClientImpl::FindLocalSource( | 866 const blink::WebMediaStreamSource* UserMediaClientImpl::FindLocalSource( |
| 867 const LocalStreamSources& sources, |
| 821 const StreamDeviceInfo& device) const { | 868 const StreamDeviceInfo& device) const { |
| 822 for (LocalStreamSources::const_iterator it = local_sources_.begin(); | 869 for (const auto& local_source : sources) { |
| 823 it != local_sources_.end(); ++it) { | |
| 824 MediaStreamSource* const source = | 870 MediaStreamSource* const source = |
| 825 static_cast<MediaStreamSource*>(it->getExtraData()); | 871 static_cast<MediaStreamSource*>(local_source.getExtraData()); |
| 826 const StreamDeviceInfo& active_device = source->device_info(); | 872 const StreamDeviceInfo& active_device = source->device_info(); |
| 827 if (IsSameDevice(active_device, device)) { | 873 if (IsSameDevice(active_device, device)) |
| 828 return &(*it); | 874 return &local_source; |
| 829 } | |
| 830 } | 875 } |
| 831 return NULL; | 876 return nullptr; |
| 877 } |
| 878 |
| 879 blink::WebMediaStreamSource UserMediaClientImpl::FindOrInitializeSourceObject( |
| 880 const StreamDeviceInfo& device) { |
| 881 const blink::WebMediaStreamSource* existing_source = FindLocalSource(device); |
| 882 if (existing_source) { |
| 883 DVLOG(1) << "Source already exists. Reusing source with id " |
| 884 << existing_source->id().utf8(); |
| 885 return *existing_source; |
| 886 } |
| 887 |
| 888 blink::WebMediaStreamSource::Type type = |
| 889 IsAudioInputMediaType(device.device.type) |
| 890 ? blink::WebMediaStreamSource::TypeAudio |
| 891 : blink::WebMediaStreamSource::TypeVideo; |
| 892 |
| 893 blink::WebMediaStreamSource source; |
| 894 source.initialize(blink::WebString::fromUTF8(device.device.id), type, |
| 895 blink::WebString::fromUTF8(device.device.name), |
| 896 false /* remote */); |
| 897 |
| 898 DVLOG(1) << "Initialize source object :" |
| 899 << "id = " << source.id().utf8() |
| 900 << ", name = " << source.name().utf8(); |
| 901 return source; |
| 832 } | 902 } |
| 833 | 903 |
| 834 bool UserMediaClientImpl::RemoveLocalSource( | 904 bool UserMediaClientImpl::RemoveLocalSource( |
| 835 const blink::WebMediaStreamSource& source) { | 905 const blink::WebMediaStreamSource& source) { |
| 836 bool device_found = false; | |
| 837 for (LocalStreamSources::iterator device_it = local_sources_.begin(); | 906 for (LocalStreamSources::iterator device_it = local_sources_.begin(); |
| 838 device_it != local_sources_.end(); ++device_it) { | 907 device_it != local_sources_.end(); ++device_it) { |
| 839 if (IsSameSource(*device_it, source)) { | 908 if (IsSameSource(*device_it, source)) { |
| 840 device_found = true; | |
| 841 local_sources_.erase(device_it); | 909 local_sources_.erase(device_it); |
| 842 break; | 910 return true; |
| 843 } | 911 } |
| 844 } | 912 } |
| 845 return device_found; | 913 |
| 914 // Check if the source was pending. |
| 915 for (LocalStreamSources::iterator device_it = pending_local_sources_.begin(); |
| 916 device_it != pending_local_sources_.end(); ++device_it) { |
| 917 if (IsSameSource(*device_it, source)) { |
| 918 MediaStreamSource* const source_extra_data = |
| 919 static_cast<MediaStreamSource*>(source.getExtraData()); |
| 920 for (const auto& request : user_media_requests_) { |
| 921 request->OnAudioSourceStarted(source_extra_data, |
| 922 MEDIA_DEVICE_TRACK_START_FAILURE, |
| 923 "Failed to access audio capture device"); |
| 924 } |
| 925 pending_local_sources_.erase(device_it); |
| 926 return true; |
| 927 } |
| 928 } |
| 929 |
| 930 return false; |
| 846 } | 931 } |
| 847 | 932 |
| 848 UserMediaClientImpl::UserMediaRequestInfo* | 933 UserMediaClientImpl::UserMediaRequestInfo* |
| 849 UserMediaClientImpl::FindUserMediaRequestInfo(int request_id) { | 934 UserMediaClientImpl::FindUserMediaRequestInfo(int request_id) { |
| 850 UserMediaRequests::iterator it = user_media_requests_.begin(); | 935 UserMediaRequests::iterator it = user_media_requests_.begin(); |
| 851 for (; it != user_media_requests_.end(); ++it) { | 936 for (; it != user_media_requests_.end(); ++it) { |
| 852 if ((*it)->request_id == request_id) | 937 if ((*it)->request_id == request_id) |
| 853 return (*it); | 938 return (*it); |
| 854 } | 939 } |
| 855 return NULL; | 940 return NULL; |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 968 request(request), | 1053 request(request), |
| 969 request_result_(MEDIA_DEVICE_OK), | 1054 request_result_(MEDIA_DEVICE_OK), |
| 970 request_result_name_("") { | 1055 request_result_name_("") { |
| 971 } | 1056 } |
| 972 | 1057 |
| 973 UserMediaClientImpl::UserMediaRequestInfo::~UserMediaRequestInfo() { | 1058 UserMediaClientImpl::UserMediaRequestInfo::~UserMediaRequestInfo() { |
| 974 DVLOG(1) << "~UserMediaRequestInfo"; | 1059 DVLOG(1) << "~UserMediaRequestInfo"; |
| 975 } | 1060 } |
| 976 | 1061 |
| 977 void UserMediaClientImpl::UserMediaRequestInfo::StartAudioTrack( | 1062 void UserMediaClientImpl::UserMediaRequestInfo::StartAudioTrack( |
| 978 const blink::WebMediaStreamTrack& track) { | 1063 const blink::WebMediaStreamTrack& track, |
| 1064 bool source_initialized) { |
| 979 DCHECK(track.source().getType() == blink::WebMediaStreamSource::TypeAudio); | 1065 DCHECK(track.source().getType() == blink::WebMediaStreamSource::TypeAudio); |
| 980 MediaStreamAudioSource* native_source = | 1066 MediaStreamAudioSource* native_source = |
| 981 MediaStreamAudioSource::From(track.source()); | 1067 MediaStreamAudioSource::From(track.source()); |
| 982 DCHECK(native_source); | 1068 // Add the source as pending since OnTrackStarted will expect it to be there. |
| 1069 sources_waiting_for_callback_.push_back(native_source); |
| 983 | 1070 |
| 984 sources_.push_back(track.source()); | 1071 sources_.push_back(track.source()); |
| 985 sources_waiting_for_callback_.push_back(native_source); | 1072 bool connected = native_source->ConnectToTrack(track); |
| 986 if (native_source->ConnectToTrack(track)) | 1073 if (source_initialized) { |
| 1074 OnTrackStarted( |
| 1075 native_source, |
| 1076 connected ? MEDIA_DEVICE_OK : MEDIA_DEVICE_TRACK_START_FAILURE, ""); |
| 1077 #if defined(OS_ANDROID) |
| 1078 } else if (connected) { |
| 1079 CHECK(native_source->is_local_source()); |
| 1080 // On Android, we won't get the callback indicating the device readyness. |
| 1081 // TODO(tommi): Update the android implementation to support the |
| 1082 // OnAudioSourceStarted notification. http://crbug.com/679302 |
| 987 OnTrackStarted(native_source, MEDIA_DEVICE_OK, ""); | 1083 OnTrackStarted(native_source, MEDIA_DEVICE_OK, ""); |
| 988 else | 1084 #endif |
| 989 OnTrackStarted(native_source, MEDIA_DEVICE_TRACK_START_FAILURE, ""); | 1085 } |
| 990 } | 1086 } |
| 991 | 1087 |
| 992 blink::WebMediaStreamTrack | 1088 blink::WebMediaStreamTrack |
| 993 UserMediaClientImpl::UserMediaRequestInfo::CreateAndStartVideoTrack( | 1089 UserMediaClientImpl::UserMediaRequestInfo::CreateAndStartVideoTrack( |
| 994 const blink::WebMediaStreamSource& source, | 1090 const blink::WebMediaStreamSource& source, |
| 995 const blink::WebMediaConstraints& constraints) { | 1091 const blink::WebMediaConstraints& constraints) { |
| 996 DCHECK(source.getType() == blink::WebMediaStreamSource::TypeVideo); | 1092 DCHECK(source.getType() == blink::WebMediaStreamSource::TypeVideo); |
| 997 MediaStreamVideoSource* native_source = | 1093 MediaStreamVideoSource* native_source = |
| 998 MediaStreamVideoSource::GetVideoSource(source); | 1094 MediaStreamVideoSource::GetVideoSource(source); |
| 999 DCHECK(native_source); | 1095 DCHECK(native_source); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1033 | 1129 |
| 1034 CheckAllTracksStarted(); | 1130 CheckAllTracksStarted(); |
| 1035 } | 1131 } |
| 1036 | 1132 |
| 1037 void UserMediaClientImpl::UserMediaRequestInfo::CheckAllTracksStarted() { | 1133 void UserMediaClientImpl::UserMediaRequestInfo::CheckAllTracksStarted() { |
| 1038 if (!ready_callback_.is_null() && sources_waiting_for_callback_.empty()) { | 1134 if (!ready_callback_.is_null() && sources_waiting_for_callback_.empty()) { |
| 1039 ready_callback_.Run(this, request_result_, request_result_name_); | 1135 ready_callback_.Run(this, request_result_, request_result_name_); |
| 1040 } | 1136 } |
| 1041 } | 1137 } |
| 1042 | 1138 |
| 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 { | 1139 bool UserMediaClientImpl::UserMediaRequestInfo::HasPendingSources() const { |
| 1067 return !sources_waiting_for_callback_.empty(); | 1140 return !sources_waiting_for_callback_.empty(); |
| 1068 } | 1141 } |
| 1069 | 1142 |
| 1143 void UserMediaClientImpl::UserMediaRequestInfo::OnAudioSourceStarted( |
| 1144 MediaStreamSource* source, |
| 1145 MediaStreamRequestResult result, |
| 1146 const blink::WebString& result_name) { |
| 1147 // Check if we're waiting to be notified of this source. If not, then we'll |
| 1148 // ignore the notification. |
| 1149 auto found = std::find(sources_waiting_for_callback_.begin(), |
| 1150 sources_waiting_for_callback_.end(), source); |
| 1151 if (found != sources_waiting_for_callback_.end()) |
| 1152 OnTrackStarted(source, result, result_name); |
| 1153 } |
| 1154 |
| 1070 void UserMediaClientImpl::OnDestruct() { | 1155 void UserMediaClientImpl::OnDestruct() { |
| 1071 delete this; | 1156 delete this; |
| 1072 } | 1157 } |
| 1073 | 1158 |
| 1074 } // namespace content | 1159 } // namespace content |
| OLD | NEW |