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

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: Rebase to May 19th 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 device.set_connection_error_handler(
95 base::Bind(&ServiceVideoCaptureDeviceLauncher::
96 OnConnectionLostWhileWaitingForCallback,
97 base::Unretained(this)));
emircan 2017/05/23 23:05:40 Can you add a comment explaining why Unretained is
chfremer 2017/05/23 23:39:18 Done.
98 // Ownership of |done_cb| is moved to |this|. It is not sufficient to attach
99 // it to the callback passed to |(*device_factory_)->CreateDevice()|, because
100 // |device_factory_| may get torn down before the callback is invoked.
101 done_cb_ = std::move(done_cb);
102 callbacks_ = callbacks;
94 (*device_factory_) 103 (*device_factory_)
95 ->CreateDevice( 104 ->CreateDevice(
96 device_id, std::move(device_request), 105 device_id, std::move(device_request),
97 base::Bind( 106 base::Bind(
98 // Use of Unretained |this| is safe, because |done_cb| guarantees 107 // Use of Unretained |this| is safe, because |done_cb_| guarantees
99 // that |this| stays alive. 108 // that |this| stays alive.
100 &ServiceVideoCaptureDeviceLauncher::OnCreateDeviceCallback, 109 &ServiceVideoCaptureDeviceLauncher::OnCreateDeviceCallback,
101 base::Unretained(this), params, base::Passed(&device), 110 base::Unretained(this), params, base::Passed(&device),
102 std::move(receiver), callbacks, base::Passed(&done_cb))); 111 std::move(receiver)));
103 state_ = State::DEVICE_START_IN_PROGRESS; 112 state_ = State::DEVICE_START_IN_PROGRESS;
104 } 113 }
105 114
106 void ServiceVideoCaptureDeviceLauncher::AbortLaunch() { 115 void ServiceVideoCaptureDeviceLauncher::AbortLaunch() {
107 DCHECK(sequence_checker_.CalledOnValidSequence()); 116 DCHECK(sequence_checker_.CalledOnValidSequence());
108 if (state_ == State::DEVICE_START_IN_PROGRESS) 117 if (state_ == State::DEVICE_START_IN_PROGRESS)
109 state_ = State::DEVICE_START_ABORTING; 118 state_ = State::DEVICE_START_ABORTING;
110 } 119 }
111 120
112 void ServiceVideoCaptureDeviceLauncher::OnCreateDeviceCallback( 121 void ServiceVideoCaptureDeviceLauncher::OnCreateDeviceCallback(
113 const media::VideoCaptureParams& params, 122 const media::VideoCaptureParams& params,
114 video_capture::mojom::DevicePtr device, 123 video_capture::mojom::DevicePtr device,
115 base::WeakPtr<media::VideoFrameReceiver> receiver, 124 base::WeakPtr<media::VideoFrameReceiver> receiver,
116 Callbacks* callbacks,
117 base::OnceClosure done_cb,
118 video_capture::mojom::DeviceAccessResultCode result_code) { 125 video_capture::mojom::DeviceAccessResultCode result_code) {
119 DCHECK(sequence_checker_.CalledOnValidSequence()); 126 DCHECK(sequence_checker_.CalledOnValidSequence());
127 DCHECK(callbacks_);
128 DCHECK(done_cb_);
129 device.set_connection_error_handler(base::Bind(&base::DoNothing));
120 const bool abort_requested = (state_ == State::DEVICE_START_ABORTING); 130 const bool abort_requested = (state_ == State::DEVICE_START_ABORTING);
121 state_ = State::READY_TO_LAUNCH; 131 state_ = State::READY_TO_LAUNCH;
132 Callbacks* callbacks = callbacks_;
133 callbacks_ = nullptr;
122 switch (result_code) { 134 switch (result_code) {
123 case video_capture::mojom::DeviceAccessResultCode::SUCCESS: 135 case video_capture::mojom::DeviceAccessResultCode::SUCCESS:
124 ConcludeLaunchDeviceWithSuccess(abort_requested, params, 136 ConcludeLaunchDeviceWithSuccess(abort_requested, params,
125 std::move(device), std::move(receiver), 137 std::move(device), std::move(receiver),
126 callbacks, std::move(done_cb)); 138 callbacks, std::move(done_cb_));
127 return; 139 return;
128 case video_capture::mojom::DeviceAccessResultCode::ERROR_DEVICE_NOT_FOUND: 140 case video_capture::mojom::DeviceAccessResultCode::ERROR_DEVICE_NOT_FOUND:
129 case video_capture::mojom::DeviceAccessResultCode::NOT_INITIALIZED: 141 case video_capture::mojom::DeviceAccessResultCode::NOT_INITIALIZED:
130 ConcludeLaunchDeviceWithFailure(abort_requested, callbacks, 142 ConcludeLaunchDeviceWithFailure(abort_requested, callbacks,
131 std::move(done_cb)); 143 std::move(done_cb_));
132 return; 144 return;
133 } 145 }
134 } 146 }
135 147
148 void ServiceVideoCaptureDeviceLauncher::
149 OnConnectionLostWhileWaitingForCallback() {
150 DCHECK(sequence_checker_.CalledOnValidSequence());
151 const bool abort_requested = (state_ == State::DEVICE_START_ABORTING);
152 state_ = State::READY_TO_LAUNCH;
153 Callbacks* callbacks = callbacks_;
emircan 2017/05/23 23:05:40 DCHECK(callbacks_)
chfremer 2017/05/23 23:39:18 Done.
154 callbacks_ = nullptr;
155 ConcludeLaunchDeviceWithFailure(abort_requested, callbacks,
156 std::move(done_cb_));
157 }
158
136 } // namespace content 159 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698