| 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 | |
| 443 DCHECK(!request_info->request.isNull()); | 435 DCHECK(!request_info->request.isNull()); |
| 444 blink::WebVector<blink::WebMediaStreamTrack> audio_track_vector( | 436 blink::WebVector<blink::WebMediaStreamTrack> audio_track_vector( |
| 445 audio_array.size()); | 437 audio_array.size()); |
| 446 CreateAudioTracks(audio_array, request_info->request.audioConstraints(), | 438 CreateAudioTracks(audio_array, request_info->request.audioConstraints(), |
| 447 &audio_track_vector, request_info); | 439 &audio_track_vector, request_info); |
| 448 | 440 |
| 449 blink::WebVector<blink::WebMediaStreamTrack> video_track_vector( | 441 blink::WebVector<blink::WebMediaStreamTrack> video_track_vector( |
| 450 video_array.size()); | 442 video_array.size()); |
| 451 CreateVideoTracks(video_array, request_info->request.videoConstraints(), | 443 CreateVideoTracks(video_array, request_info->request.videoConstraints(), |
| 452 &video_track_vector, request_info); | 444 &video_track_vector, request_info); |
| 453 | 445 |
| 454 blink::WebString webkit_id = blink::WebString::fromUTF8(label); | 446 blink::WebString webkit_id = blink::WebString::fromUTF8(label); |
| 455 blink::WebMediaStream* web_stream = &(request_info->web_stream); | 447 blink::WebMediaStream* web_stream = &(request_info->web_stream); |
| 456 | 448 |
| 457 web_stream->initialize(webkit_id, audio_track_vector, video_track_vector); | 449 web_stream->initialize(webkit_id, audio_track_vector, |
| 450 video_track_vector); |
| 458 web_stream->setExtraData(new MediaStream()); | 451 web_stream->setExtraData(new MediaStream()); |
| 459 | 452 |
| 460 // Wait for the tracks to be started successfully or to fail. | 453 // Wait for the tracks to be started successfully or to fail. |
| 461 request_info->CallbackOnTracksStarted( | 454 request_info->CallbackOnTracksStarted( |
| 462 base::Bind(&UserMediaClientImpl::OnCreateNativeTracksCompleted, | 455 base::Bind(&UserMediaClientImpl::OnCreateNativeTracksCompleted, |
| 463 weak_factory_.GetWeakPtr())); | 456 weak_factory_.GetWeakPtr())); |
| 464 } | 457 } |
| 465 | 458 |
| 466 void UserMediaClientImpl::OnStreamGeneratedForCancelledRequest( | 459 void UserMediaClientImpl::OnStreamGeneratedForCancelledRequest( |
| 467 const StreamDeviceInfoArray& audio_array, | 460 const StreamDeviceInfoArray& audio_array, |
| 468 const StreamDeviceInfoArray& video_array) { | 461 const StreamDeviceInfoArray& video_array) { |
| 469 // Only stop the device if the device is not used in another MediaStream. | 462 // Only stop the device if the device is not used in another MediaStream. |
| 470 for (StreamDeviceInfoArray::const_iterator device_it = audio_array.begin(); | 463 for (StreamDeviceInfoArray::const_iterator device_it = audio_array.begin(); |
| 471 device_it != audio_array.end(); ++device_it) { | 464 device_it != audio_array.end(); ++device_it) { |
| 472 if (!FindLocalSource(*device_it)) | 465 if (!FindLocalSource(*device_it)) |
| 473 media_stream_dispatcher_->StopStreamDevice(*device_it); | 466 media_stream_dispatcher_->StopStreamDevice(*device_it); |
| 474 } | 467 } |
| 475 | 468 |
| 476 for (StreamDeviceInfoArray::const_iterator device_it = video_array.begin(); | 469 for (StreamDeviceInfoArray::const_iterator device_it = video_array.begin(); |
| 477 device_it != video_array.end(); ++device_it) { | 470 device_it != video_array.end(); ++device_it) { |
| 478 if (!FindLocalSource(*device_it)) | 471 if (!FindLocalSource(*device_it)) |
| 479 media_stream_dispatcher_->StopStreamDevice(*device_it); | 472 media_stream_dispatcher_->StopStreamDevice(*device_it); |
| 480 } | 473 } |
| 481 } | 474 } |
| 482 | 475 |
| 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 | |
| 517 void UserMediaClientImpl::FinalizeEnumerateDevices( | 476 void UserMediaClientImpl::FinalizeEnumerateDevices( |
| 518 blink::WebMediaDevicesRequest request, | 477 blink::WebMediaDevicesRequest request, |
| 519 const EnumerationResult& result) { | 478 const EnumerationResult& result) { |
| 520 DCHECK_EQ(static_cast<size_t>(NUM_MEDIA_DEVICE_TYPES), result.size()); | 479 DCHECK_EQ(static_cast<size_t>(NUM_MEDIA_DEVICE_TYPES), result.size()); |
| 521 | 480 |
| 522 blink::WebVector<blink::WebMediaDeviceInfo> devices( | 481 blink::WebVector<blink::WebMediaDeviceInfo> devices( |
| 523 result[MEDIA_DEVICE_TYPE_AUDIO_INPUT].size() + | 482 result[MEDIA_DEVICE_TYPE_AUDIO_INPUT].size() + |
| 524 result[MEDIA_DEVICE_TYPE_VIDEO_INPUT].size() + | 483 result[MEDIA_DEVICE_TYPE_VIDEO_INPUT].size() + |
| 525 result[MEDIA_DEVICE_TYPE_AUDIO_OUTPUT].size()); | 484 result[MEDIA_DEVICE_TYPE_AUDIO_OUTPUT].size()); |
| 526 size_t index = 0; | 485 size_t index = 0; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 574 // as the underlying media device is unplugged from the system. | 533 // as the underlying media device is unplugged from the system. |
| 575 return; | 534 return; |
| 576 } | 535 } |
| 577 // By creating |source| it is guaranteed that the blink::WebMediaStreamSource | 536 // By creating |source| it is guaranteed that the blink::WebMediaStreamSource |
| 578 // object is valid during the cleanup. | 537 // object is valid during the cleanup. |
| 579 blink::WebMediaStreamSource source(*source_ptr); | 538 blink::WebMediaStreamSource source(*source_ptr); |
| 580 StopLocalSource(source, false); | 539 StopLocalSource(source, false); |
| 581 RemoveLocalSource(source); | 540 RemoveLocalSource(source); |
| 582 } | 541 } |
| 583 | 542 |
| 584 void UserMediaClientImpl::InitializeVideoSourceObject( | 543 void UserMediaClientImpl::InitializeSourceObject( |
| 585 const StreamDeviceInfo& device, | 544 const StreamDeviceInfo& device, |
| 545 blink::WebMediaStreamSource::Type type, |
| 586 const blink::WebMediaConstraints& constraints, | 546 const blink::WebMediaConstraints& constraints, |
| 587 blink::WebMediaStreamSource* webkit_source) { | 547 blink::WebMediaStreamSource* webkit_source) { |
| 588 DCHECK(CalledOnValidThread()); | 548 const blink::WebMediaStreamSource* existing_source = |
| 589 | 549 FindLocalSource(device); |
| 590 *webkit_source = FindOrInitializeSourceObject(device); | 550 if (existing_source) { |
| 591 if (webkit_source->getExtraData()) | 551 *webkit_source = *existing_source; |
| 592 return; | 552 DVLOG(1) << "Source already exist. Reusing source with id " |
| 593 | 553 << webkit_source->id().utf8(); |
| 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; | |
| 610 return; | 554 return; |
| 611 } | 555 } |
| 612 | 556 |
| 613 *source_initialized = false; | 557 webkit_source->initialize(blink::WebString::fromUTF8(device.device.id), type, |
| 558 blink::WebString::fromUTF8(device.device.name), |
| 559 false /* remote */); |
| 614 | 560 |
| 615 // See if the source is already being initialized. | 561 DVLOG(1) << "Initialize source object :" |
| 616 auto* pending = FindPendingLocalSource(device); | 562 << "id = " << webkit_source->id().utf8() |
| 617 if (pending) { | 563 << ", name = " << webkit_source->name().utf8(); |
| 618 *webkit_source = *pending; | 564 |
| 619 return; | 565 if (type == blink::WebMediaStreamSource::TypeVideo) { |
| 566 webkit_source->setExtraData( |
| 567 CreateVideoSource( |
| 568 device, |
| 569 base::Bind(&UserMediaClientImpl::OnLocalSourceStopped, |
| 570 weak_factory_.GetWeakPtr()))); |
| 571 } else { |
| 572 DCHECK_EQ(blink::WebMediaStreamSource::TypeAudio, type); |
| 573 MediaStreamAudioSource* const audio_source = |
| 574 CreateAudioSource(device, constraints); |
| 575 audio_source->SetStopCallback( |
| 576 base::Bind(&UserMediaClientImpl::OnLocalSourceStopped, |
| 577 weak_factory_.GetWeakPtr())); |
| 578 webkit_source->setExtraData(audio_source); // Takes ownership. |
| 620 } | 579 } |
| 621 | 580 local_sources_.push_back(*webkit_source); |
| 622 // While sources are being initialized, keep them in a separate array. | |
| 623 // Once they've finished initialized, they'll be moved over to local_sources_. | |
| 624 // See OnAudioSourceStarted for more details. | |
| 625 pending_local_sources_.push_back(*webkit_source); | |
| 626 | |
| 627 MediaStreamSource::ConstraintsCallback source_ready = base::Bind( | |
| 628 &UserMediaClientImpl::OnAudioSourceStartedOnAudioThread, | |
| 629 base::ThreadTaskRunnerHandle::Get(), weak_factory_.GetWeakPtr()); | |
| 630 | |
| 631 MediaStreamAudioSource* const audio_source = | |
| 632 CreateAudioSource(device, constraints, source_ready); | |
| 633 audio_source->SetStopCallback(base::Bind( | |
| 634 &UserMediaClientImpl::OnLocalSourceStopped, weak_factory_.GetWeakPtr())); | |
| 635 webkit_source->setExtraData(audio_source); // Takes ownership. | |
| 636 } | 581 } |
| 637 | 582 |
| 638 MediaStreamAudioSource* UserMediaClientImpl::CreateAudioSource( | 583 MediaStreamAudioSource* UserMediaClientImpl::CreateAudioSource( |
| 639 const StreamDeviceInfo& device, | 584 const StreamDeviceInfo& device, |
| 640 const blink::WebMediaConstraints& constraints, | 585 const blink::WebMediaConstraints& constraints) { |
| 641 const MediaStreamSource::ConstraintsCallback& source_ready) { | |
| 642 DCHECK(CalledOnValidThread()); | |
| 643 // If the audio device is a loopback device (for screen capture), or if the | 586 // If the audio device is a loopback device (for screen capture), or if the |
| 644 // constraints/effects parameters indicate no audio processing is needed, | 587 // constraints/effects parameters indicate no audio processing is needed, |
| 645 // create an efficient, direct-path MediaStreamAudioSource instance. | 588 // create an efficient, direct-path MediaStreamAudioSource instance. |
| 646 if (IsScreenCaptureMediaType(device.device.type) || | 589 if (IsScreenCaptureMediaType(device.device.type) || |
| 647 !MediaStreamAudioProcessor::WouldModifyAudio( | 590 !MediaStreamAudioProcessor::WouldModifyAudio( |
| 648 constraints, device.device.input.effects)) { | 591 constraints, device.device.input.effects)) { |
| 649 return new LocalMediaStreamAudioSource(RenderFrameObserver::routing_id(), | 592 return new LocalMediaStreamAudioSource(RenderFrameObserver::routing_id(), |
| 650 device, source_ready); | 593 device); |
| 651 } | 594 } |
| 652 | 595 |
| 653 // The audio device is not associated with screen capture and also requires | 596 // The audio device is not associated with screen capture and also requires |
| 654 // processing. | 597 // processing. |
| 655 ProcessedLocalAudioSource* source = new ProcessedLocalAudioSource( | 598 ProcessedLocalAudioSource* source = new ProcessedLocalAudioSource( |
| 656 RenderFrameObserver::routing_id(), device, constraints, source_ready, | 599 RenderFrameObserver::routing_id(), device, dependency_factory_); |
| 657 dependency_factory_); | 600 source->SetSourceConstraints(constraints); |
| 658 return source; | 601 return source; |
| 659 } | 602 } |
| 660 | 603 |
| 661 MediaStreamVideoSource* UserMediaClientImpl::CreateVideoSource( | 604 MediaStreamVideoSource* UserMediaClientImpl::CreateVideoSource( |
| 662 const StreamDeviceInfo& device, | 605 const StreamDeviceInfo& device, |
| 663 const MediaStreamSource::SourceStoppedCallback& stop_callback) { | 606 const MediaStreamSource::SourceStoppedCallback& stop_callback) { |
| 664 DCHECK(CalledOnValidThread()); | |
| 665 content::MediaStreamVideoCapturerSource* ret = | 607 content::MediaStreamVideoCapturerSource* ret = |
| 666 new content::MediaStreamVideoCapturerSource(stop_callback, device, | 608 new content::MediaStreamVideoCapturerSource(stop_callback, device, |
| 667 render_frame()); | 609 render_frame()); |
| 668 return ret; | 610 return ret; |
| 669 } | 611 } |
| 670 | 612 |
| 671 void UserMediaClientImpl::CreateVideoTracks( | 613 void UserMediaClientImpl::CreateVideoTracks( |
| 672 const StreamDeviceInfoArray& devices, | 614 const StreamDeviceInfoArray& devices, |
| 673 const blink::WebMediaConstraints& constraints, | 615 const blink::WebMediaConstraints& constraints, |
| 674 blink::WebVector<blink::WebMediaStreamTrack>* webkit_tracks, | 616 blink::WebVector<blink::WebMediaStreamTrack>* webkit_tracks, |
| 675 UserMediaRequestInfo* request) { | 617 UserMediaRequestInfo* request) { |
| 676 DCHECK(CalledOnValidThread()); | |
| 677 DCHECK_EQ(devices.size(), webkit_tracks->size()); | 618 DCHECK_EQ(devices.size(), webkit_tracks->size()); |
| 678 | 619 |
| 679 for (size_t i = 0; i < devices.size(); ++i) { | 620 for (size_t i = 0; i < devices.size(); ++i) { |
| 680 blink::WebMediaStreamSource webkit_source; | 621 blink::WebMediaStreamSource webkit_source; |
| 681 InitializeVideoSourceObject(devices[i], constraints, &webkit_source); | 622 InitializeSourceObject(devices[i], |
| 623 blink::WebMediaStreamSource::TypeVideo, |
| 624 constraints, |
| 625 &webkit_source); |
| 682 (*webkit_tracks)[i] = | 626 (*webkit_tracks)[i] = |
| 683 request->CreateAndStartVideoTrack(webkit_source, constraints); | 627 request->CreateAndStartVideoTrack(webkit_source, constraints); |
| 684 } | 628 } |
| 685 } | 629 } |
| 686 | 630 |
| 687 void UserMediaClientImpl::CreateAudioTracks( | 631 void UserMediaClientImpl::CreateAudioTracks( |
| 688 const StreamDeviceInfoArray& devices, | 632 const StreamDeviceInfoArray& devices, |
| 689 const blink::WebMediaConstraints& constraints, | 633 const blink::WebMediaConstraints& constraints, |
| 690 blink::WebVector<blink::WebMediaStreamTrack>* webkit_tracks, | 634 blink::WebVector<blink::WebMediaStreamTrack>* webkit_tracks, |
| 691 UserMediaRequestInfo* request) { | 635 UserMediaRequestInfo* request) { |
| 692 DCHECK(CalledOnValidThread()); | |
| 693 DCHECK_EQ(devices.size(), webkit_tracks->size()); | 636 DCHECK_EQ(devices.size(), webkit_tracks->size()); |
| 694 | 637 |
| 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 |
| 695 StreamDeviceInfoArray overridden_audio_array = devices; | 648 StreamDeviceInfoArray overridden_audio_array = devices; |
| 696 if (!request->enable_automatic_output_device_selection) { | 649 if (!request->enable_automatic_output_device_selection) { |
| 697 // If the GetUserMedia request did not explicitly set the constraint | 650 // If the GetUserMedia request did not explicitly set the constraint |
| 698 // kMediaStreamRenderToAssociatedSink, the output device parameters must | 651 // kMediaStreamRenderToAssociatedSink, the output device parameters must |
| 699 // be removed. | 652 // be removed. |
| 700 for (auto& device_info : overridden_audio_array) { | 653 for (StreamDeviceInfoArray::iterator it = overridden_audio_array.begin(); |
| 701 device_info.device.matched_output_device_id = ""; | 654 it != overridden_audio_array.end(); ++it) { |
| 702 device_info.device.matched_output = | 655 it->device.matched_output_device_id = ""; |
| 703 MediaStreamDevice::AudioDeviceParameters(); | 656 it->device.matched_output = MediaStreamDevice::AudioDeviceParameters(); |
| 704 } | 657 } |
| 705 } | 658 } |
| 706 | 659 |
| 707 for (size_t i = 0; i < overridden_audio_array.size(); ++i) { | 660 for (size_t i = 0; i < overridden_audio_array.size(); ++i) { |
| 708 blink::WebMediaStreamSource webkit_source; | 661 blink::WebMediaStreamSource webkit_source; |
| 709 bool source_initialized = true; | 662 InitializeSourceObject(overridden_audio_array[i], |
| 710 InitializeAudioSourceObject(overridden_audio_array[i], constraints, | 663 blink::WebMediaStreamSource::TypeAudio, |
| 711 &webkit_source, &source_initialized); | 664 constraints, |
| 665 &webkit_source); |
| 712 (*webkit_tracks)[i].initialize(webkit_source); | 666 (*webkit_tracks)[i].initialize(webkit_source); |
| 713 request->StartAudioTrack((*webkit_tracks)[i], source_initialized); | 667 request->StartAudioTrack((*webkit_tracks)[i]); |
| 714 } | 668 } |
| 715 } | 669 } |
| 716 | 670 |
| 717 void UserMediaClientImpl::OnCreateNativeTracksCompleted( | 671 void UserMediaClientImpl::OnCreateNativeTracksCompleted( |
| 718 UserMediaRequestInfo* request, | 672 UserMediaRequestInfo* request, |
| 719 MediaStreamRequestResult result, | 673 MediaStreamRequestResult result, |
| 720 const blink::WebString& result_name) { | 674 const blink::WebString& result_name) { |
| 721 DVLOG(1) << "UserMediaClientImpl::OnCreateNativeTracksComplete(" | 675 DVLOG(1) << "UserMediaClientImpl::OnCreateNativeTracksComplete(" |
| 722 << "{request_id = " << request->request_id << "} " | 676 << "{request_id = " << request->request_id << "} " |
| 723 << "{result = " << result << "})"; | 677 << "{result = " << result << "})"; |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 857 request_info.requestFailed(); | 811 request_info.requestFailed(); |
| 858 } | 812 } |
| 859 | 813 |
| 860 void UserMediaClientImpl::EnumerateDevicesSucceded( | 814 void UserMediaClientImpl::EnumerateDevicesSucceded( |
| 861 blink::WebMediaDevicesRequest* request, | 815 blink::WebMediaDevicesRequest* request, |
| 862 blink::WebVector<blink::WebMediaDeviceInfo>& devices) { | 816 blink::WebVector<blink::WebMediaDeviceInfo>& devices) { |
| 863 request->requestSucceeded(devices); | 817 request->requestSucceeded(devices); |
| 864 } | 818 } |
| 865 | 819 |
| 866 const blink::WebMediaStreamSource* UserMediaClientImpl::FindLocalSource( | 820 const blink::WebMediaStreamSource* UserMediaClientImpl::FindLocalSource( |
| 867 const LocalStreamSources& sources, | |
| 868 const StreamDeviceInfo& device) const { | 821 const StreamDeviceInfo& device) const { |
| 869 for (const auto& local_source : sources) { | 822 for (LocalStreamSources::const_iterator it = local_sources_.begin(); |
| 823 it != local_sources_.end(); ++it) { |
| 870 MediaStreamSource* const source = | 824 MediaStreamSource* const source = |
| 871 static_cast<MediaStreamSource*>(local_source.getExtraData()); | 825 static_cast<MediaStreamSource*>(it->getExtraData()); |
| 872 const StreamDeviceInfo& active_device = source->device_info(); | 826 const StreamDeviceInfo& active_device = source->device_info(); |
| 873 if (IsSameDevice(active_device, device)) | 827 if (IsSameDevice(active_device, device)) { |
| 874 return &local_source; | 828 return &(*it); |
| 829 } |
| 875 } | 830 } |
| 876 return nullptr; | 831 return NULL; |
| 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; | |
| 902 } | 832 } |
| 903 | 833 |
| 904 bool UserMediaClientImpl::RemoveLocalSource( | 834 bool UserMediaClientImpl::RemoveLocalSource( |
| 905 const blink::WebMediaStreamSource& source) { | 835 const blink::WebMediaStreamSource& source) { |
| 836 bool device_found = false; |
| 906 for (LocalStreamSources::iterator device_it = local_sources_.begin(); | 837 for (LocalStreamSources::iterator device_it = local_sources_.begin(); |
| 907 device_it != local_sources_.end(); ++device_it) { | 838 device_it != local_sources_.end(); ++device_it) { |
| 908 if (IsSameSource(*device_it, source)) { | 839 if (IsSameSource(*device_it, source)) { |
| 840 device_found = true; |
| 909 local_sources_.erase(device_it); | 841 local_sources_.erase(device_it); |
| 910 return true; | 842 break; |
| 911 } | 843 } |
| 912 } | 844 } |
| 913 | 845 return device_found; |
| 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; | |
| 931 } | 846 } |
| 932 | 847 |
| 933 UserMediaClientImpl::UserMediaRequestInfo* | 848 UserMediaClientImpl::UserMediaRequestInfo* |
| 934 UserMediaClientImpl::FindUserMediaRequestInfo(int request_id) { | 849 UserMediaClientImpl::FindUserMediaRequestInfo(int request_id) { |
| 935 UserMediaRequests::iterator it = user_media_requests_.begin(); | 850 UserMediaRequests::iterator it = user_media_requests_.begin(); |
| 936 for (; it != user_media_requests_.end(); ++it) { | 851 for (; it != user_media_requests_.end(); ++it) { |
| 937 if ((*it)->request_id == request_id) | 852 if ((*it)->request_id == request_id) |
| 938 return (*it); | 853 return (*it); |
| 939 } | 854 } |
| 940 return NULL; | 855 return NULL; |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1053 request(request), | 968 request(request), |
| 1054 request_result_(MEDIA_DEVICE_OK), | 969 request_result_(MEDIA_DEVICE_OK), |
| 1055 request_result_name_("") { | 970 request_result_name_("") { |
| 1056 } | 971 } |
| 1057 | 972 |
| 1058 UserMediaClientImpl::UserMediaRequestInfo::~UserMediaRequestInfo() { | 973 UserMediaClientImpl::UserMediaRequestInfo::~UserMediaRequestInfo() { |
| 1059 DVLOG(1) << "~UserMediaRequestInfo"; | 974 DVLOG(1) << "~UserMediaRequestInfo"; |
| 1060 } | 975 } |
| 1061 | 976 |
| 1062 void UserMediaClientImpl::UserMediaRequestInfo::StartAudioTrack( | 977 void UserMediaClientImpl::UserMediaRequestInfo::StartAudioTrack( |
| 1063 const blink::WebMediaStreamTrack& track, | 978 const blink::WebMediaStreamTrack& track) { |
| 1064 bool source_initialized) { | |
| 1065 DCHECK(track.source().getType() == blink::WebMediaStreamSource::TypeAudio); | 979 DCHECK(track.source().getType() == blink::WebMediaStreamSource::TypeAudio); |
| 1066 MediaStreamAudioSource* native_source = | 980 MediaStreamAudioSource* native_source = |
| 1067 MediaStreamAudioSource::From(track.source()); | 981 MediaStreamAudioSource::From(track.source()); |
| 1068 // Add the source as pending since OnTrackStarted will expect it to be there. | 982 DCHECK(native_source); |
| 1069 sources_waiting_for_callback_.push_back(native_source); | |
| 1070 | 983 |
| 1071 sources_.push_back(track.source()); | 984 sources_.push_back(track.source()); |
| 1072 bool connected = native_source->ConnectToTrack(track); | 985 sources_waiting_for_callback_.push_back(native_source); |
| 1073 if (source_initialized) { | 986 if (native_source->ConnectToTrack(track)) |
| 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 | |
| 1083 OnTrackStarted(native_source, MEDIA_DEVICE_OK, ""); | 987 OnTrackStarted(native_source, MEDIA_DEVICE_OK, ""); |
| 1084 #endif | 988 else |
| 1085 } | 989 OnTrackStarted(native_source, MEDIA_DEVICE_TRACK_START_FAILURE, ""); |
| 1086 } | 990 } |
| 1087 | 991 |
| 1088 blink::WebMediaStreamTrack | 992 blink::WebMediaStreamTrack |
| 1089 UserMediaClientImpl::UserMediaRequestInfo::CreateAndStartVideoTrack( | 993 UserMediaClientImpl::UserMediaRequestInfo::CreateAndStartVideoTrack( |
| 1090 const blink::WebMediaStreamSource& source, | 994 const blink::WebMediaStreamSource& source, |
| 1091 const blink::WebMediaConstraints& constraints) { | 995 const blink::WebMediaConstraints& constraints) { |
| 1092 DCHECK(source.getType() == blink::WebMediaStreamSource::TypeVideo); | 996 DCHECK(source.getType() == blink::WebMediaStreamSource::TypeVideo); |
| 1093 MediaStreamVideoSource* native_source = | 997 MediaStreamVideoSource* native_source = |
| 1094 MediaStreamVideoSource::GetVideoSource(source); | 998 MediaStreamVideoSource::GetVideoSource(source); |
| 1095 DCHECK(native_source); | 999 DCHECK(native_source); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1129 | 1033 |
| 1130 CheckAllTracksStarted(); | 1034 CheckAllTracksStarted(); |
| 1131 } | 1035 } |
| 1132 | 1036 |
| 1133 void UserMediaClientImpl::UserMediaRequestInfo::CheckAllTracksStarted() { | 1037 void UserMediaClientImpl::UserMediaRequestInfo::CheckAllTracksStarted() { |
| 1134 if (!ready_callback_.is_null() && sources_waiting_for_callback_.empty()) { | 1038 if (!ready_callback_.is_null() && sources_waiting_for_callback_.empty()) { |
| 1135 ready_callback_.Run(this, request_result_, request_result_name_); | 1039 ready_callback_.Run(this, request_result_, request_result_name_); |
| 1136 } | 1040 } |
| 1137 } | 1041 } |
| 1138 | 1042 |
| 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 |
| 1139 bool UserMediaClientImpl::UserMediaRequestInfo::HasPendingSources() const { | 1066 bool UserMediaClientImpl::UserMediaRequestInfo::HasPendingSources() const { |
| 1140 return !sources_waiting_for_callback_.empty(); | 1067 return !sources_waiting_for_callback_.empty(); |
| 1141 } | 1068 } |
| 1142 | 1069 |
| 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 | |
| 1155 void UserMediaClientImpl::OnDestruct() { | 1070 void UserMediaClientImpl::OnDestruct() { |
| 1156 delete this; | 1071 delete this; |
| 1157 } | 1072 } |
| 1158 | 1073 |
| 1159 } // namespace content | 1074 } // namespace content |
| OLD | NEW |