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

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

Issue 2886303002: [Mojo Video Capture] Add unit tests for ServiceVideoCaptureDeviceLauncher (Closed)
Patch Set: Incorporated suggestions from Patch Set 2 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" 7 #include "content/browser/renderer_host/media/service_launched_video_capture_dev ice.h"
8 #include "content/public/browser/browser_thread.h" 8 #include "content/public/browser/browser_thread.h"
9 #include "media/capture/video/video_frame_receiver_on_task_runner.h" 9 #include "media/capture/video/video_frame_receiver_on_task_runner.h"
10 #include "mojo/public/cpp/bindings/strong_binding.h" 10 #include "mojo/public/cpp/bindings/strong_binding.h"
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 callbacks->OnDeviceLaunchAborted(); 50 callbacks->OnDeviceLaunchAborted();
51 else 51 else
52 callbacks->OnDeviceLaunchFailed(); 52 callbacks->OnDeviceLaunchFailed();
53 base::ResetAndReturn(&done_cb).Run(); 53 base::ResetAndReturn(&done_cb).Run();
54 } 54 }
55 55
56 } // anonymous namespace 56 } // anonymous namespace
57 57
58 ServiceVideoCaptureDeviceLauncher::ServiceVideoCaptureDeviceLauncher( 58 ServiceVideoCaptureDeviceLauncher::ServiceVideoCaptureDeviceLauncher(
59 video_capture::mojom::DeviceFactoryPtr* device_factory) 59 video_capture::mojom::DeviceFactoryPtr* device_factory)
60 : device_factory_(device_factory), state_(State::READY_TO_LAUNCH) {} 60 : device_factory_(device_factory),
61 state_(State::READY_TO_LAUNCH),
62 callbacks_(nullptr) {}
61 63
62 ServiceVideoCaptureDeviceLauncher::~ServiceVideoCaptureDeviceLauncher() { 64 ServiceVideoCaptureDeviceLauncher::~ServiceVideoCaptureDeviceLauncher() {
63 DCHECK(sequence_checker_.CalledOnValidSequence()); 65 DCHECK(sequence_checker_.CalledOnValidSequence());
64 DCHECK(state_ == State::READY_TO_LAUNCH); 66 DCHECK(state_ == State::READY_TO_LAUNCH);
65 } 67 }
66 68
67 void ServiceVideoCaptureDeviceLauncher::LaunchDeviceAsync( 69 void ServiceVideoCaptureDeviceLauncher::LaunchDeviceAsync(
68 const std::string& device_id, 70 const std::string& device_id,
69 MediaStreamType stream_type, 71 MediaStreamType stream_type,
70 const media::VideoCaptureParams& params, 72 const media::VideoCaptureParams& params,
(...skipping 10 matching lines...) Expand all
81 } 83 }
82 84
83 if (!device_factory_->is_bound()) { 85 if (!device_factory_->is_bound()) {
84 // This can happen when the ServiceVideoCaptureProvider owning 86 // This can happen when the ServiceVideoCaptureProvider owning
85 // |device_factory_| loses connection to the service process and resets 87 // |device_factory_| loses connection to the service process and resets
86 // |device_factory_|. 88 // |device_factory_|.
87 ConcludeLaunchDeviceWithFailure(false, callbacks, std::move(done_cb)); 89 ConcludeLaunchDeviceWithFailure(false, callbacks, std::move(done_cb));
88 return; 90 return;
89 } 91 }
90 video_capture::mojom::DevicePtr device; 92 video_capture::mojom::DevicePtr device;
91 // We need the temporary variable |device_request| in order to guarantee that
92 // mojo::MakeRequest(&device) happens before base::Passed(&device).
93 auto device_request = mojo::MakeRequest(&device); 93 auto device_request = mojo::MakeRequest(&device);
94 // Ownership of |done_cb| is moved to |this|. It is not sufficient to attach
95 // it to the callback passed to |(*device_factory_)->CreateDevice()|, because
96 // |device_factory_| may get torn down before the callback is invoked.
97 done_cb_ = std::move(done_cb);
98 callbacks_ = callbacks;
99 // Use of Unretained(this) is safe, because |done_cb_| guarantees that |this|
100 // stays alive.
101 device.set_connection_error_handler(
102 base::Bind(&ServiceVideoCaptureDeviceLauncher::
103 OnConnectionLostWhileWaitingForCallback,
104 base::Unretained(this)));
94 (*device_factory_) 105 (*device_factory_)
95 ->CreateDevice( 106 ->CreateDevice(
96 device_id, std::move(device_request), 107 device_id, std::move(device_request),
97 base::Bind( 108 base::Bind(
98 // Use of Unretained |this| is safe, because |done_cb| guarantees 109 // Use of Unretained |this| is safe, because |done_cb_| guarantees
99 // that |this| stays alive. 110 // that |this| stays alive.
100 &ServiceVideoCaptureDeviceLauncher::OnCreateDeviceCallback, 111 &ServiceVideoCaptureDeviceLauncher::OnCreateDeviceCallback,
101 base::Unretained(this), params, base::Passed(&device), 112 base::Unretained(this), params, base::Passed(&device),
102 std::move(receiver), callbacks, base::Passed(&done_cb))); 113 std::move(receiver)));
103 state_ = State::DEVICE_START_IN_PROGRESS; 114 state_ = State::DEVICE_START_IN_PROGRESS;
104 } 115 }
105 116
106 void ServiceVideoCaptureDeviceLauncher::AbortLaunch() { 117 void ServiceVideoCaptureDeviceLauncher::AbortLaunch() {
107 DCHECK(sequence_checker_.CalledOnValidSequence()); 118 DCHECK(sequence_checker_.CalledOnValidSequence());
108 if (state_ == State::DEVICE_START_IN_PROGRESS) 119 if (state_ == State::DEVICE_START_IN_PROGRESS)
109 state_ = State::DEVICE_START_ABORTING; 120 state_ = State::DEVICE_START_ABORTING;
110 } 121 }
111 122
112 void ServiceVideoCaptureDeviceLauncher::OnCreateDeviceCallback( 123 void ServiceVideoCaptureDeviceLauncher::OnCreateDeviceCallback(
113 const media::VideoCaptureParams& params, 124 const media::VideoCaptureParams& params,
114 video_capture::mojom::DevicePtr device, 125 video_capture::mojom::DevicePtr device,
115 base::WeakPtr<media::VideoFrameReceiver> receiver, 126 base::WeakPtr<media::VideoFrameReceiver> receiver,
116 Callbacks* callbacks,
117 base::OnceClosure done_cb,
118 video_capture::mojom::DeviceAccessResultCode result_code) { 127 video_capture::mojom::DeviceAccessResultCode result_code) {
119 DCHECK(sequence_checker_.CalledOnValidSequence()); 128 DCHECK(sequence_checker_.CalledOnValidSequence());
129 DCHECK(callbacks_);
130 DCHECK(done_cb_);
131 device.set_connection_error_handler(base::Bind(&base::DoNothing));
120 const bool abort_requested = (state_ == State::DEVICE_START_ABORTING); 132 const bool abort_requested = (state_ == State::DEVICE_START_ABORTING);
121 state_ = State::READY_TO_LAUNCH; 133 state_ = State::READY_TO_LAUNCH;
134 Callbacks* callbacks = callbacks_;
135 callbacks_ = nullptr;
122 switch (result_code) { 136 switch (result_code) {
123 case video_capture::mojom::DeviceAccessResultCode::SUCCESS: 137 case video_capture::mojom::DeviceAccessResultCode::SUCCESS:
124 ConcludeLaunchDeviceWithSuccess(abort_requested, params, 138 ConcludeLaunchDeviceWithSuccess(abort_requested, params,
125 std::move(device), std::move(receiver), 139 std::move(device), std::move(receiver),
126 callbacks, std::move(done_cb)); 140 callbacks, std::move(done_cb_));
127 return; 141 return;
128 case video_capture::mojom::DeviceAccessResultCode::ERROR_DEVICE_NOT_FOUND: 142 case video_capture::mojom::DeviceAccessResultCode::ERROR_DEVICE_NOT_FOUND:
129 case video_capture::mojom::DeviceAccessResultCode::NOT_INITIALIZED: 143 case video_capture::mojom::DeviceAccessResultCode::NOT_INITIALIZED:
130 ConcludeLaunchDeviceWithFailure(abort_requested, callbacks, 144 ConcludeLaunchDeviceWithFailure(abort_requested, callbacks,
131 std::move(done_cb)); 145 std::move(done_cb_));
132 return; 146 return;
133 } 147 }
134 } 148 }
135 149
150 void ServiceVideoCaptureDeviceLauncher::
151 OnConnectionLostWhileWaitingForCallback() {
152 DCHECK(sequence_checker_.CalledOnValidSequence());
153 DCHECK(callbacks_);
154 const bool abort_requested = (state_ == State::DEVICE_START_ABORTING);
155 state_ = State::READY_TO_LAUNCH;
156 Callbacks* callbacks = callbacks_;
157 callbacks_ = nullptr;
158 ConcludeLaunchDeviceWithFailure(abort_requested, callbacks,
159 std::move(done_cb_));
160 }
161
136 } // namespace content 162 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698