Chromium Code Reviews| Index: content/browser/renderer_host/media/in_process_video_capture_device_launcher.cc |
| diff --git a/content/browser/renderer_host/media/in_process_buildable_video_capture_device.cc b/content/browser/renderer_host/media/in_process_video_capture_device_launcher.cc |
| similarity index 42% |
| rename from content/browser/renderer_host/media/in_process_buildable_video_capture_device.cc |
| rename to content/browser/renderer_host/media/in_process_video_capture_device_launcher.cc |
| index f562158b46376cdc93c5fc21de6863969e247ae4..ac574e7bade5dfe18f09312d86e2bbb56721030c 100644 |
| --- a/content/browser/renderer_host/media/in_process_buildable_video_capture_device.cc |
| +++ b/content/browser/renderer_host/media/in_process_video_capture_device_launcher.cc |
| @@ -2,12 +2,13 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| -#include "content/browser/renderer_host/media/in_process_buildable_video_capture_device.h" |
| +#include "content/browser/renderer_host/media/in_process_video_capture_device_launcher.h" |
| #include "base/metrics/histogram_macros.h" |
| #include "base/strings/stringprintf.h" |
| #include "content/browser/media/capture/desktop_capture_device_uma_types.h" |
| #include "content/browser/media/capture/web_contents_video_capture_device.h" |
| +#include "content/browser/renderer_host/media/in_process_launched_video_capture_device.h" |
| #include "content/browser/renderer_host/media/video_capture_controller.h" |
| #include "content/browser/renderer_host/media/video_capture_gpu_jpeg_decoder.h" |
| #include "content/public/browser/browser_thread.h" |
| @@ -17,6 +18,7 @@ |
| #include "media/capture/video/video_capture_buffer_pool_impl.h" |
| #include "media/capture/video/video_capture_buffer_tracker_factory_impl.h" |
| #include "media/capture/video/video_capture_device_client.h" |
| +#include "media/capture/video/video_frame_receiver.h" |
| #include "media/capture/video/video_frame_receiver_on_task_runner.h" |
| #if defined(ENABLE_SCREEN_CAPTURE) && !defined(OS_ANDROID) |
| @@ -32,41 +34,11 @@ |
| namespace { |
| -class VideoFrameConsumerFeedbackObserverOnTaskRunner |
| - : public media::VideoFrameConsumerFeedbackObserver { |
| - public: |
| - VideoFrameConsumerFeedbackObserverOnTaskRunner( |
| - media::VideoFrameConsumerFeedbackObserver* observer, |
| - scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| - : observer_(observer), task_runner_(std::move(task_runner)) {} |
| - |
| - void OnUtilizationReport(int frame_feedback_id, double utilization) override { |
| - task_runner_->PostTask( |
| - FROM_HERE, |
| - base::Bind( |
| - &media::VideoFrameConsumerFeedbackObserver::OnUtilizationReport, |
| - base::Unretained(observer_), frame_feedback_id, utilization)); |
| - } |
| - |
| - private: |
| - media::VideoFrameConsumerFeedbackObserver* const observer_; |
| - const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| -}; |
| - |
| std::unique_ptr<media::VideoCaptureJpegDecoder> CreateGpuJpegDecoder( |
| const media::VideoCaptureJpegDecoder::DecodeDoneCB& decode_done_cb) { |
| return base::MakeUnique<content::VideoCaptureGpuJpegDecoder>(decode_done_cb); |
| } |
| -void StopAndReleaseDeviceOnDeviceThread(media::VideoCaptureDevice* device, |
| - base::OnceClosure done_cb) { |
| - SCOPED_UMA_HISTOGRAM_TIMER("Media.VideoCaptureManager.StopDeviceTime"); |
| - device->StopAndDeAllocate(); |
| - DVLOG(3) << "StopAndReleaseDeviceOnDeviceThread"; |
| - delete device; |
| - base::ResetAndReturn(&done_cb).Run(); |
| -} |
| - |
| // The maximum number of video frame buffers in-flight at any one time. This |
| // value should be based on the logical capacity of the capture pipeline, and |
| // not on hardware performance. For example, tab capture requires more buffers |
| @@ -81,64 +53,64 @@ const int kMaxNumberOfBuffersForTabCapture = 10; |
| namespace content { |
| -InProcessBuildableVideoCaptureDevice::InProcessBuildableVideoCaptureDevice( |
| +InProcessVideoCaptureDeviceLauncher::InProcessVideoCaptureDeviceLauncher( |
| scoped_refptr<base::SingleThreadTaskRunner> device_task_runner, |
| media::VideoCaptureSystem* video_capture_system) |
| : device_task_runner_(std::move(device_task_runner)), |
| - video_capture_system_(video_capture_system) {} |
| + video_capture_system_(video_capture_system), |
| + state_(State::READY_TO_LAUNCH) {} |
| -InProcessBuildableVideoCaptureDevice::~InProcessBuildableVideoCaptureDevice() { |
| +InProcessVideoCaptureDeviceLauncher::~InProcessVideoCaptureDeviceLauncher() { |
| DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| - DCHECK(!device_); |
| + DCHECK(state_ == State::READY_TO_LAUNCH); |
| } |
| -void InProcessBuildableVideoCaptureDevice::CreateAndStartDeviceAsync( |
| - VideoCaptureController* controller, |
| +void InProcessVideoCaptureDeviceLauncher::LaunchDeviceAsync( |
| + const std::string& device_id, |
| + MediaStreamType stream_type, |
| const media::VideoCaptureParams& params, |
| + base::WeakPtr<media::VideoFrameReceiver> receiver, |
| Callbacks* callbacks, |
| base::OnceClosure done_cb) { |
| DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| - DCHECK_EQ(State::NO_DEVICE, state_); |
| + DCHECK(state_ == State::READY_TO_LAUNCH); |
| - const int max_buffers = (controller->stream_type() == MEDIA_TAB_VIDEO_CAPTURE |
| - ? kMaxNumberOfBuffersForTabCapture |
| - : kMaxNumberOfBuffers); |
| + const int max_buffers = |
| + (stream_type == MEDIA_TAB_VIDEO_CAPTURE ? kMaxNumberOfBuffersForTabCapture |
| + : kMaxNumberOfBuffers); |
| - auto device_client = |
| - CreateDeviceClient(max_buffers, controller->GetWeakPtrForIOThread()); |
| + auto device_client = CreateDeviceClient(max_buffers, std::move(receiver)); |
| base::Closure start_capture_closure; |
| - // Use of Unretained() is safe, because |done_cb| guarantees that |
| - // |this| stays alive. |
| + // Use of |this| is safe, because |context_reference| guarantees that |this| |
|
emircan
2017/04/14 17:19:38
Where is |context_reference|?
chfremer
2017/04/14 20:00:30
Sorry about the outdated comment. Changed to |done
|
| + // stays alive. |
| ReceiveDeviceCallback after_start_capture_callback = media::BindToCurrentLoop( |
| - base::Bind(&InProcessBuildableVideoCaptureDevice::OnDeviceStarted, |
| - base::Unretained(this), controller, callbacks, |
| - base::Passed(&done_cb))); |
| + base::Bind(&InProcessVideoCaptureDeviceLauncher::OnDeviceStarted, |
| + base::Unretained(this), callbacks, base::Passed(&done_cb))); |
| - switch (controller->stream_type()) { |
| + switch (stream_type) { |
| case MEDIA_DEVICE_VIDEO_CAPTURE: { |
| start_capture_closure = |
| - base::Bind(&InProcessBuildableVideoCaptureDevice:: |
| + base::Bind(&InProcessVideoCaptureDeviceLauncher:: |
| DoStartDeviceCaptureOnDeviceThread, |
| - base::Unretained(this), controller->device_id(), params, |
| + base::Unretained(this), device_id, params, |
| base::Passed(std::move(device_client)), |
| std::move(after_start_capture_callback)); |
| break; |
| } |
| case MEDIA_TAB_VIDEO_CAPTURE: |
| - start_capture_closure = |
| - base::Bind(&InProcessBuildableVideoCaptureDevice:: |
| - DoStartTabCaptureOnDeviceThread, |
| - base::Unretained(this), controller->device_id(), params, |
| - base::Passed(std::move(device_client)), |
| - std::move(after_start_capture_callback)); |
| + start_capture_closure = base::Bind( |
| + &InProcessVideoCaptureDeviceLauncher::DoStartTabCaptureOnDeviceThread, |
| + base::Unretained(this), device_id, params, |
| + base::Passed(std::move(device_client)), |
| + std::move(after_start_capture_callback)); |
| break; |
| case MEDIA_DESKTOP_VIDEO_CAPTURE: |
| start_capture_closure = |
| - base::Bind(&InProcessBuildableVideoCaptureDevice:: |
| + base::Bind(&InProcessVideoCaptureDeviceLauncher:: |
| DoStartDesktopCaptureOnDeviceThread, |
| - base::Unretained(this), controller->device_id(), params, |
| + base::Unretained(this), device_id, params, |
| base::Passed(std::move(device_client)), |
| std::move(after_start_capture_callback)); |
| break; |
| @@ -153,188 +125,72 @@ void InProcessBuildableVideoCaptureDevice::CreateAndStartDeviceAsync( |
| state_ = State::DEVICE_START_IN_PROGRESS; |
| } |
| -void InProcessBuildableVideoCaptureDevice::ReleaseDeviceAsync( |
| - VideoCaptureController* controller, |
| - base::OnceClosure done_cb) { |
| - DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| - controller->SetConsumerFeedbackObserver(nullptr); |
| - switch (state_) { |
| - case State::DEVICE_START_IN_PROGRESS: |
| - state_ = State::DEVICE_START_ABORTING; |
| - return; |
| - case State::NO_DEVICE: |
| - case State::DEVICE_START_ABORTING: |
| - return; |
| - case State::DEVICE_STARTED: |
| - media::VideoCaptureDevice* device_ptr = device_.release(); |
| - bool posting_task_succeeded = device_task_runner_->PostTask( |
| - FROM_HERE, |
| - base::Bind( |
| - &StopAndReleaseDeviceOnDeviceThread, device_ptr, |
| - base::Bind([](scoped_refptr<base::SingleThreadTaskRunner>) {}, |
| - device_task_runner_))); |
| - if (posting_task_succeeded == false) { |
| - // Since posting to the task runner has failed, we attempt doing it on |
| - // the calling thread instead. |
| - StopAndReleaseDeviceOnDeviceThread(device_ptr, base::Bind([]() {})); |
| - } |
| - state_ = State::NO_DEVICE; |
| - return; |
| - } |
| - base::ResetAndReturn(&done_cb).Run(); |
| -} |
| - |
| -bool InProcessBuildableVideoCaptureDevice::IsDeviceAlive() const { |
| - DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| - return device_ != nullptr; |
| -} |
| - |
| -void InProcessBuildableVideoCaptureDevice::GetPhotoCapabilities( |
| - media::VideoCaptureDevice::GetPhotoCapabilitiesCallback callback) const { |
| +void InProcessVideoCaptureDeviceLauncher::AbortLaunch() { |
| DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| - // Unretained() is safe to use here because |device| would be null if it |
| - // was scheduled for shutdown and destruction, and because this task is |
| - // guaranteed to run before the task that destroys the |device|. |
| - device_task_runner_->PostTask( |
| - FROM_HERE, |
| - base::Bind(&media::VideoCaptureDevice::GetPhotoCapabilities, |
| - base::Unretained(device_.get()), base::Passed(&callback))); |
| -} |
| - |
| -void InProcessBuildableVideoCaptureDevice::SetPhotoOptions( |
| - media::mojom::PhotoSettingsPtr settings, |
| - media::VideoCaptureDevice::SetPhotoOptionsCallback callback) { |
| - DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| - // Unretained() is safe to use here because |device| would be null if it |
| - // was scheduled for shutdown and destruction, and because this task is |
| - // guaranteed to run before the task that destroys the |device|. |
| - device_task_runner_->PostTask( |
| - FROM_HERE, base::Bind(&media::VideoCaptureDevice::SetPhotoOptions, |
| - base::Unretained(device_.get()), |
| - base::Passed(&settings), base::Passed(&callback))); |
| -} |
| - |
| -void InProcessBuildableVideoCaptureDevice::TakePhoto( |
| - media::VideoCaptureDevice::TakePhotoCallback callback) { |
| - DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| - // Unretained() is safe to use here because |device| would be null if it |
| - // was scheduled for shutdown and destruction, and because this task is |
| - // guaranteed to run before the task that destroys the |device|. |
| - device_task_runner_->PostTask( |
| - FROM_HERE, |
| - base::Bind(&media::VideoCaptureDevice::TakePhoto, |
| - base::Unretained(device_.get()), base::Passed(&callback))); |
| -} |
| - |
| -void InProcessBuildableVideoCaptureDevice::MaybeSuspendDevice() { |
| - DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| - // Unretained() is safe to use here because |device| would be null if it |
| - // was scheduled for shutdown and destruction, and because this task is |
| - // guaranteed to run before the task that destroys the |device|. |
| - device_task_runner_->PostTask( |
| - FROM_HERE, base::Bind(&media::VideoCaptureDevice::MaybeSuspend, |
| - base::Unretained(device_.get()))); |
| -} |
| - |
| -void InProcessBuildableVideoCaptureDevice::ResumeDevice() { |
| - DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| - // Unretained() is safe to use here because |device| would be null if it |
| - // was scheduled for shutdown and destruction, and because this task is |
| - // guaranteed to run before the task that destroys the |device|. |
| - device_task_runner_->PostTask(FROM_HERE, |
| - base::Bind(&media::VideoCaptureDevice::Resume, |
| - base::Unretained(device_.get()))); |
| -} |
| - |
| -void InProcessBuildableVideoCaptureDevice::RequestRefreshFrame() { |
| - DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| - // Unretained() is safe to use here because |device| would be null if it |
| - // was scheduled for shutdown and destruction, and because this task is |
| - // guaranteed to run before the task that destroys the |device|. |
| - device_task_runner_->PostTask( |
| - FROM_HERE, base::Bind(&media::VideoCaptureDevice::RequestRefreshFrame, |
| - base::Unretained(device_.get()))); |
| -} |
| - |
| -void InProcessBuildableVideoCaptureDevice::SetDesktopCaptureWindowIdAsync( |
| - gfx::NativeViewId window_id, |
| - base::OnceClosure done_cb) { |
| - DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| - // Post |device_| to the the |device_task_runner_|. This is safe since the |
| - // device is destroyed on the |device_task_runner_| and |done_cb| guarantees |
| - // that |this| stays alive. |
| - device_task_runner_->PostTask( |
| - FROM_HERE, base::Bind(&InProcessBuildableVideoCaptureDevice:: |
| - SetDesktopCaptureWindowIdOnDeviceThread, |
| - base::Unretained(this), device_.get(), window_id, |
| - base::Passed(&done_cb))); |
| + if (state_ == State::DEVICE_START_IN_PROGRESS) |
| + state_ = State::DEVICE_START_ABORTING; |
| } |
| std::unique_ptr<media::VideoCaptureDeviceClient> |
| -InProcessBuildableVideoCaptureDevice::CreateDeviceClient( |
| +InProcessVideoCaptureDeviceLauncher::CreateDeviceClient( |
| int buffer_pool_max_buffer_count, |
| base::WeakPtr<media::VideoFrameReceiver> receiver) { |
| DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + scoped_refptr<media::VideoCaptureBufferPool> buffer_pool = |
| + new media::VideoCaptureBufferPoolImpl( |
| + base::MakeUnique<media::VideoCaptureBufferTrackerFactoryImpl>(), |
| + buffer_pool_max_buffer_count); |
| + |
| return base::MakeUnique<media::VideoCaptureDeviceClient>( |
| base::MakeUnique<media::VideoFrameReceiverOnTaskRunner>( |
| receiver, BrowserThread::GetTaskRunnerForThread(BrowserThread::IO)), |
| - new media::VideoCaptureBufferPoolImpl( |
| - base::MakeUnique<media::VideoCaptureBufferTrackerFactoryImpl>(), |
| - buffer_pool_max_buffer_count), |
| + std::move(buffer_pool), |
| base::Bind(&CreateGpuJpegDecoder, |
| base::Bind(&media::VideoFrameReceiver::OnFrameReadyInBuffer, |
| receiver))); |
| } |
| -void InProcessBuildableVideoCaptureDevice::OnDeviceStarted( |
| - VideoCaptureController* controller, |
| +void InProcessVideoCaptureDeviceLauncher::OnDeviceStarted( |
| Callbacks* callbacks, |
| base::OnceClosure done_cb, |
| std::unique_ptr<media::VideoCaptureDevice> device) { |
| DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| - switch (state_) { |
| - case State::DEVICE_START_IN_PROGRESS: |
| - if (!device) { |
| - state_ = State::NO_DEVICE; |
| - callbacks->OnDeviceStartFailed(controller); |
| - base::ResetAndReturn(&done_cb).Run(); |
| + State state_copy = state_; |
| + state_ = State::READY_TO_LAUNCH; |
| + if (!device) { |
| + switch (state_copy) { |
| + case State::DEVICE_START_IN_PROGRESS: |
| + callbacks->OnDeviceLaunchFailed(); |
| + return; |
| + case State::DEVICE_START_ABORTING: |
| + callbacks->OnDeviceLaunchAborted(); |
| return; |
| - } |
| - // Passing raw pointer |device.get()| to the controller is safe, |
| - // because we take ownership of |device| and we call |
| - // controller->SetConsumerFeedbackObserver(nullptr) before releasing |
| - // |device|. |
| - controller->SetConsumerFeedbackObserver( |
| - base::MakeUnique<VideoFrameConsumerFeedbackObserverOnTaskRunner>( |
| - device.get(), device_task_runner_)); |
| - device_ = std::move(device); |
| - state_ = State::DEVICE_STARTED; |
| - callbacks->OnDeviceStarted(controller); |
| - base::ResetAndReturn(&done_cb).Run(); |
| + case State::READY_TO_LAUNCH: |
| + NOTREACHED(); |
| + return; |
| + } |
| + } |
| + |
| + auto launched_device = base::MakeUnique<InProcessLaunchedVideoCaptureDevice>( |
| + std::move(device), device_task_runner_); |
| + |
| + switch (state_copy) { |
| + case State::DEVICE_START_IN_PROGRESS: |
| + callbacks->OnDeviceLaunched(std::move(launched_device)); |
| return; |
| case State::DEVICE_START_ABORTING: |
| - if (device) { |
| - device_ = std::move(device); |
| - state_ = State::DEVICE_STARTED; |
| - // We do not move our |done_cb| to this invocation, because |
| - // we still need it to stay alive for the remainder of this method |
| - // execution. Our implementation of ReleaseDeviceAsync() does not |
| - // actually need the context while releasing the device. |
| - ReleaseDeviceAsync(controller, base::Bind([]() {})); |
| - } |
| - state_ = State::NO_DEVICE; |
| - callbacks->OnDeviceStartAborted(); |
| - base::ResetAndReturn(&done_cb).Run(); |
| + launched_device.reset(); |
| + callbacks->OnDeviceLaunchAborted(); |
| return; |
| - case State::NO_DEVICE: |
| - case State::DEVICE_STARTED: |
| + case State::READY_TO_LAUNCH: |
| NOTREACHED(); |
| return; |
| } |
| + base::ResetAndReturn(&done_cb).Run(); |
| } |
| -void InProcessBuildableVideoCaptureDevice::DoStartDeviceCaptureOnDeviceThread( |
| +void InProcessVideoCaptureDeviceLauncher::DoStartDeviceCaptureOnDeviceThread( |
| const std::string& device_id, |
| const media::VideoCaptureParams& params, |
| std::unique_ptr<media::VideoCaptureDeviceClient> device_client, |
| @@ -354,7 +210,7 @@ void InProcessBuildableVideoCaptureDevice::DoStartDeviceCaptureOnDeviceThread( |
| result_callback.Run(std::move(video_capture_device)); |
| } |
| -void InProcessBuildableVideoCaptureDevice::DoStartTabCaptureOnDeviceThread( |
| +void InProcessVideoCaptureDeviceLauncher::DoStartTabCaptureOnDeviceThread( |
| const std::string& id, |
| const media::VideoCaptureParams& params, |
| std::unique_ptr<media::VideoCaptureDeviceClient> device_client, |
| @@ -376,7 +232,7 @@ void InProcessBuildableVideoCaptureDevice::DoStartTabCaptureOnDeviceThread( |
| result_callback.Run(std::move(video_capture_device)); |
| } |
| -void InProcessBuildableVideoCaptureDevice::DoStartDesktopCaptureOnDeviceThread( |
| +void InProcessVideoCaptureDeviceLauncher::DoStartDesktopCaptureOnDeviceThread( |
| const std::string& id, |
| const media::VideoCaptureParams& params, |
| std::unique_ptr<media::VideoCaptureDeviceClient> device_client, |
| @@ -397,10 +253,11 @@ void InProcessBuildableVideoCaptureDevice::DoStartDesktopCaptureOnDeviceThread( |
| #if defined(OS_LINUX) || defined(OS_MACOSX) || defined(OS_WIN) |
| video_capture_device = WebContentsVideoCaptureDevice::Create(id); |
| IncrementDesktopCaptureCounter(TAB_VIDEO_CAPTURER_CREATED); |
| - if (desktop_id.audio_share) |
| + if (desktop_id.audio_share) { |
| IncrementDesktopCaptureCounter(TAB_VIDEO_CAPTURER_CREATED_WITH_AUDIO); |
| - else |
| + } else { |
| IncrementDesktopCaptureCounter(TAB_VIDEO_CAPTURER_CREATED_WITHOUT_AUDIO); |
| + } |
| #endif |
| } else { |
| #if defined(OS_ANDROID) |
| @@ -426,19 +283,4 @@ void InProcessBuildableVideoCaptureDevice::DoStartDesktopCaptureOnDeviceThread( |
| result_callback.Run(std::move(video_capture_device)); |
| } |
| -void InProcessBuildableVideoCaptureDevice:: |
| - SetDesktopCaptureWindowIdOnDeviceThread(media::VideoCaptureDevice* device, |
| - gfx::NativeViewId window_id, |
| - base::OnceClosure done_cb) { |
| - DCHECK(device_task_runner_->BelongsToCurrentThread()); |
| -#if defined(ENABLE_SCREEN_CAPTURE) && BUILDFLAG(ENABLE_WEBRTC) && \ |
| - !defined(OS_ANDROID) |
| - DesktopCaptureDevice* desktop_device = |
| - static_cast<DesktopCaptureDevice*>(device); |
| - desktop_device->SetNotificationWindowId(window_id); |
| - VLOG(2) << "Screen capture notification window passed on device thread."; |
| -#endif |
| - base::ResetAndReturn(&done_cb).Run(); |
| -} |
| - |
| } // namespace content |