Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(45)

Side by Side Diff: content/browser/renderer_host/media/service_video_capture_device_launcher.cc

Issue 2857303002: [Mojo Video Capture] Implement a VideoCaptureProvider using the Mojo service (part 2) (Closed)
Patch Set: Rebase to May 5th Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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/browser/renderer_host/media/service_video_capture_device_launc her.h" 5 #include "content/browser/renderer_host/media/service_video_capture_device_launc her.h"
6 6
7 #include "content/browser/renderer_host/media/service_launched_video_capture_dev ice.h"
8 #include "content/public/browser/browser_thread.h"
9 #include "media/capture/video/video_frame_receiver_on_task_runner.h"
10 #include "mojo/public/cpp/bindings/strong_binding.h"
11 #include "services/video_capture/public/cpp/receiver_media_to_mojo_adapter.h"
12
7 namespace content { 13 namespace content {
8 14
9 ServiceVideoCaptureDeviceLauncher::ServiceVideoCaptureDeviceLauncher( 15 ServiceVideoCaptureDeviceLauncher::ServiceVideoCaptureDeviceLauncher(
10 video_capture::mojom::DeviceFactoryPtr* device_factory) 16 video_capture::mojom::DeviceFactoryPtr* device_factory)
11 : device_factory_(device_factory) {} 17 : device_factory_(device_factory), state_(State::READY_TO_LAUNCH) {}
12 18
13 ServiceVideoCaptureDeviceLauncher::~ServiceVideoCaptureDeviceLauncher() {} 19 ServiceVideoCaptureDeviceLauncher::~ServiceVideoCaptureDeviceLauncher() {
20 DCHECK(thread_checker_.CalledOnValidThread());
21 DCHECK(state_ == State::READY_TO_LAUNCH);
22 }
14 23
15 void ServiceVideoCaptureDeviceLauncher::LaunchDeviceAsync( 24 void ServiceVideoCaptureDeviceLauncher::LaunchDeviceAsync(
16 const std::string& device_id, 25 const std::string& device_id,
17 MediaStreamType stream_type, 26 MediaStreamType stream_type,
18 const media::VideoCaptureParams& params, 27 const media::VideoCaptureParams& params,
19 base::WeakPtr<media::VideoFrameReceiver> receiver, 28 base::WeakPtr<media::VideoFrameReceiver> receiver,
20 Callbacks* callbacks, 29 Callbacks* callbacks,
21 base::OnceClosure done_cb) { 30 base::OnceClosure done_cb) {
22 NOTIMPLEMENTED(); 31 DCHECK(thread_checker_.CalledOnValidThread());
32 DCHECK(state_ == State::READY_TO_LAUNCH);
33
34 if (stream_type != content::MEDIA_DEVICE_VIDEO_CAPTURE) {
35 // This launcher only supports MEDIA_DEVICE_VIDEO_CAPTURE.
36 NOTREACHED();
37 return;
38 }
39
40 if (!device_factory_->is_bound()) {
41 // This can happen when the ServiceVideoCaptureProvider owning
42 // |device_factory_| loses connection to the service process and resets
43 // |device_factory_|.
44 OnDeviceCreationFailed(callbacks, std::move(done_cb));
45 return;
46 }
47 video_capture::mojom::DevicePtr device;
48 (*device_factory_)
49 ->CreateDevice(
50 device_id, mojo::MakeRequest(&device),
51 base::Bind(
52 // Use of Unretained |this| is safe, because |done_cb| guarantees
53 // that |this| stays alive.
54 &ServiceVideoCaptureDeviceLauncher::OnCreateDeviceCallback,
55 base::Unretained(this), params, base::Passed(&device),
56 std::move(receiver), callbacks, base::Passed(&done_cb)));
57 state_ = State::DEVICE_START_IN_PROGRESS;
23 } 58 }
24 59
25 void ServiceVideoCaptureDeviceLauncher::AbortLaunch() { 60 void ServiceVideoCaptureDeviceLauncher::AbortLaunch() {
26 NOTIMPLEMENTED(); 61 DCHECK(thread_checker_.CalledOnValidThread());
62 if (state_ == State::DEVICE_START_IN_PROGRESS)
63 state_ = State::DEVICE_START_ABORTING;
64 }
65
66 void ServiceVideoCaptureDeviceLauncher::OnCreateDeviceCallback(
67 const media::VideoCaptureParams& params,
68 video_capture::mojom::DevicePtr device,
69 base::WeakPtr<media::VideoFrameReceiver> receiver,
70 Callbacks* callbacks,
71 base::OnceClosure done_cb,
72 video_capture::mojom::DeviceAccessResultCode result_code) {
73 DCHECK(thread_checker_.CalledOnValidThread());
74 switch (result_code) {
75 case video_capture::mojom::DeviceAccessResultCode::SUCCESS:
76 OnDeviceCreatedSuccessfully(params, std::move(device),
77 std::move(receiver), callbacks,
78 std::move(done_cb));
79 return;
80 case video_capture::mojom::DeviceAccessResultCode::ERROR_DEVICE_NOT_FOUND:
81 case video_capture::mojom::DeviceAccessResultCode::NOT_INITIALIZED:
82 OnDeviceCreationFailed(callbacks, std::move(done_cb));
83 return;
84 }
85 }
86
87 void ServiceVideoCaptureDeviceLauncher::OnDeviceCreatedSuccessfully(
88 const media::VideoCaptureParams& params,
89 video_capture::mojom::DevicePtr device,
90 base::WeakPtr<media::VideoFrameReceiver> receiver,
91 Callbacks* callbacks,
92 base::OnceClosure done_cb) {
93 DCHECK(thread_checker_.CalledOnValidThread());
94 State state_copy = state_;
miu 2017/05/08 20:21:11 naming: How about |prior_state|? Actually, you do
chfremer 2017/05/08 20:51:46 Agreed that this is probably better than |state_co
95 state_ = State::READY_TO_LAUNCH;
96
97 if (state_copy == State::DEVICE_START_ABORTING) {
98 device.reset();
99 callbacks->OnDeviceLaunchAborted();
100 base::ResetAndReturn(&done_cb).Run();
101 return;
102 }
103
104 auto receiver_adapter =
105 base::MakeUnique<video_capture::ReceiverMediaToMojoAdapter>(
106 base::MakeUnique<media::VideoFrameReceiverOnTaskRunner>(
107 std::move(receiver),
108 BrowserThread::GetTaskRunnerForThread(BrowserThread::IO)));
109 video_capture::mojom::ReceiverPtr receiver_proxy;
110 mojo::MakeStrongBinding<video_capture::mojom::Receiver>(
111 std::move(receiver_adapter), mojo::MakeRequest(&receiver_proxy));
112 device->Start(params, std::move(receiver_proxy));
113 callbacks->OnDeviceLaunched(
114 base::MakeUnique<ServiceLaunchedVideoCaptureDevice>(std::move(device)));
115 base::ResetAndReturn(&done_cb).Run();
116 }
117
118 void ServiceVideoCaptureDeviceLauncher::OnDeviceCreationFailed(
119 Callbacks* callbacks,
120 base::OnceClosure done_cb) {
121 DCHECK(thread_checker_.CalledOnValidThread());
122 State state_copy = state_;
miu 2017/05/08 20:21:11 ditto: I think you could simplify by not mutating
chfremer 2017/05/08 20:51:46 see other reply
123 state_ = State::READY_TO_LAUNCH;
124 if (state_copy == State::DEVICE_START_ABORTING)
125 callbacks->OnDeviceLaunchAborted();
126 else
127 callbacks->OnDeviceLaunchFailed();
128 base::ResetAndReturn(&done_cb).Run();
27 } 129 }
28 130
29 } // namespace content 131 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698