| 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/browser/renderer_host/media/video_capture_manager.h" | 5 #include "content/browser/renderer_host/media/video_capture_manager.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
| 13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 14 #include "base/location.h" | 14 #include "base/location.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/metrics/histogram_macros.h" | 16 #include "base/metrics/histogram_macros.h" |
| 17 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
| 18 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
| 19 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
| 20 #include "base/task_runner_util.h" | 20 #include "base/task_runner_util.h" |
| 21 #include "base/threading/sequenced_worker_pool.h" | 21 #include "base/threading/sequenced_worker_pool.h" |
| 22 #include "base/threading/thread_task_runner_handle.h" | 22 #include "base/threading/thread_task_runner_handle.h" |
| 23 #include "build/build_config.h" | 23 #include "build/build_config.h" |
| 24 #include "content/browser/media/capture/desktop_capture_device_uma_types.h" | 24 #include "content/browser/media/capture/desktop_capture_device_uma_types.h" |
| 25 #include "content/browser/media/capture/web_contents_video_capture_device.h" | 25 #include "content/browser/media/capture/web_contents_video_capture_device.h" |
| 26 #include "content/browser/media/media_internals.h" | 26 #include "content/browser/media/media_internals.h" |
| 27 #include "content/browser/renderer_host/media/video_capture_controller.h" | 27 #include "content/browser/renderer_host/media/video_capture_controller.h" |
| 28 #include "content/browser/renderer_host/media/video_capture_controller_event_han
dler.h" | 28 #include "content/browser/renderer_host/media/video_capture_controller_event_han
dler.h" |
| 29 #include "content/browser/renderer_host/media/video_capture_gpu_jpeg_decoder.h" |
| 30 #include "content/browser/renderer_host/media/video_frame_receiver_on_io_thread.
h" |
| 29 #include "content/public/browser/browser_thread.h" | 31 #include "content/public/browser/browser_thread.h" |
| 30 #include "content/public/browser/desktop_media_id.h" | 32 #include "content/public/browser/desktop_media_id.h" |
| 31 #include "content/public/common/media_stream_request.h" | 33 #include "content/public/common/media_stream_request.h" |
| 32 #include "media/base/bind_to_current_loop.h" | 34 #include "media/base/bind_to_current_loop.h" |
| 33 #include "media/base/media_switches.h" | 35 #include "media/base/media_switches.h" |
| 36 #include "media/capture/video/video_capture_buffer_pool_impl.h" |
| 37 #include "media/capture/video/video_capture_buffer_tracker_factory_impl.h" |
| 34 #include "media/capture/video/video_capture_device.h" | 38 #include "media/capture/video/video_capture_device.h" |
| 39 #include "media/capture/video/video_capture_device_client.h" |
| 35 #include "media/capture/video/video_capture_device_factory.h" | 40 #include "media/capture/video/video_capture_device_factory.h" |
| 36 | 41 |
| 37 #if defined(ENABLE_SCREEN_CAPTURE) && !defined(OS_ANDROID) | 42 #if defined(ENABLE_SCREEN_CAPTURE) && !defined(OS_ANDROID) |
| 38 #include "content/browser/media/capture/desktop_capture_device.h" | 43 #include "content/browser/media/capture/desktop_capture_device.h" |
| 39 #if defined(USE_AURA) | 44 #if defined(USE_AURA) |
| 40 #include "content/browser/media/capture/desktop_capture_device_aura.h" | 45 #include "content/browser/media/capture/desktop_capture_device_aura.h" |
| 41 #endif | 46 #endif |
| 42 #endif | 47 #endif |
| 43 | 48 |
| 44 #if defined(ENABLE_SCREEN_CAPTURE) && defined(OS_ANDROID) | 49 #if defined(ENABLE_SCREEN_CAPTURE) && defined(OS_ANDROID) |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 formats->erase(last, formats->end()); | 111 formats->erase(last, formats->end()); |
| 107 // Mark all formats as I420, since this is what the renderer side will get | 112 // Mark all formats as I420, since this is what the renderer side will get |
| 108 // anyhow: the actual pixel format is decided at the device level. | 113 // anyhow: the actual pixel format is decided at the device level. |
| 109 // Don't do this for Y16 format as it is handled separatelly. | 114 // Don't do this for Y16 format as it is handled separatelly. |
| 110 for (auto& format : *formats) { | 115 for (auto& format : *formats) { |
| 111 if (format.pixel_format != media::PIXEL_FORMAT_Y16) | 116 if (format.pixel_format != media::PIXEL_FORMAT_Y16) |
| 112 format.pixel_format = media::PIXEL_FORMAT_I420; | 117 format.pixel_format = media::PIXEL_FORMAT_I420; |
| 113 } | 118 } |
| 114 } | 119 } |
| 115 | 120 |
| 116 // The maximum number of buffers in the capture pipeline. See | 121 // The maximum number of video frame buffers in-flight at any one time. This |
| 117 // VideoCaptureController ctor comments for more details. | 122 // value should be based on the logical capacity of the capture pipeline, and |
| 123 // not on hardware performance. For example, tab capture requires more buffers |
| 124 // than webcam capture because the pipeline is longer (it includes read-backs |
| 125 // pending in the GPU pipeline). |
| 118 const int kMaxNumberOfBuffers = 3; | 126 const int kMaxNumberOfBuffers = 3; |
| 119 // TODO(miu): The value for tab capture should be determined programmatically. | 127 // TODO(miu): The value for tab capture should be determined programmatically. |
| 120 // http://crbug.com/460318 | 128 // http://crbug.com/460318 |
| 121 const int kMaxNumberOfBuffersForTabCapture = 10; | 129 const int kMaxNumberOfBuffersForTabCapture = 10; |
| 122 | 130 |
| 123 // Used for logging capture events. | 131 // Used for logging capture events. |
| 124 // Elements in this enum should not be deleted or rearranged; the only | 132 // Elements in this enum should not be deleted or rearranged; the only |
| 125 // permitted operation is to add new elements before NUM_VIDEO_CAPTURE_EVENT. | 133 // permitted operation is to add new elements before NUM_VIDEO_CAPTURE_EVENT. |
| 126 enum VideoCaptureEvent { | 134 enum VideoCaptureEvent { |
| 127 VIDEO_CAPTURE_START_CAPTURE = 0, | 135 VIDEO_CAPTURE_START_CAPTURE = 0, |
| 128 VIDEO_CAPTURE_STOP_CAPTURE_OK = 1, | 136 VIDEO_CAPTURE_STOP_CAPTURE_OK = 1, |
| 129 VIDEO_CAPTURE_STOP_CAPTURE_DUE_TO_ERROR = 2, | 137 VIDEO_CAPTURE_STOP_CAPTURE_DUE_TO_ERROR = 2, |
| 130 VIDEO_CAPTURE_STOP_CAPTURE_OK_NO_FRAMES_PRODUCED_BY_DEVICE = 3, | 138 VIDEO_CAPTURE_STOP_CAPTURE_OK_NO_FRAMES_PRODUCED_BY_DEVICE = 3, |
| 131 VIDEO_CAPTURE_STOP_CAPTURE_OK_NO_FRAMES_PRODUCED_BY_DESKTOP_OR_TAB = 4, | 139 VIDEO_CAPTURE_STOP_CAPTURE_OK_NO_FRAMES_PRODUCED_BY_DESKTOP_OR_TAB = 4, |
| 132 NUM_VIDEO_CAPTURE_EVENT | 140 NUM_VIDEO_CAPTURE_EVENT |
| 133 }; | 141 }; |
| 134 | 142 |
| 135 void LogVideoCaptureEvent(VideoCaptureEvent event) { | 143 void LogVideoCaptureEvent(VideoCaptureEvent event) { |
| 136 UMA_HISTOGRAM_ENUMERATION("Media.VideoCaptureManager.Event", | 144 UMA_HISTOGRAM_ENUMERATION("Media.VideoCaptureManager.Event", |
| 137 event, | 145 event, |
| 138 NUM_VIDEO_CAPTURE_EVENT); | 146 NUM_VIDEO_CAPTURE_EVENT); |
| 139 } | 147 } |
| 140 | 148 |
| 141 // Counter used for identifying a DeviceRequest to start a capture device. | 149 // Counter used for identifying a DeviceRequest to start a capture device. |
| 142 static int g_device_start_id = 0; | 150 static int g_device_start_id = 0; |
| 143 | 151 |
| 144 const media::VideoCaptureSessionId kFakeSessionId = -1; | 152 const media::VideoCaptureSessionId kFakeSessionId = -1; |
| 145 | 153 |
| 154 std::unique_ptr<media::VideoCaptureJpegDecoder> CreateGpuJpegDecoder( |
| 155 const media::VideoCaptureJpegDecoder::DecodeDoneCB& decode_done_cb) { |
| 156 return base::MakeUnique<content::VideoCaptureGpuJpegDecoder>(decode_done_cb); |
| 157 } |
| 158 |
| 146 } // namespace | 159 } // namespace |
| 147 | 160 |
| 148 namespace content { | 161 namespace content { |
| 149 | 162 |
| 150 // Instances of this struct go through 3 different phases during their lifetime. | 163 // Instances of this struct go through 3 different phases during their lifetime. |
| 151 // Phase 1: When first created (in GetOrCreateDeviceEntry()), this consists of | 164 // Phase 1: When first created (in GetOrCreateDeviceEntry()), this consists of |
| 152 // only a controller. Clients can already connect to the controller, but there | 165 // only a controller. Clients can already connect to the controller, but there |
| 153 // is no device present. | 166 // is no device present. |
| 154 // Phase 2: When a request to "start" the entry comes in (via | 167 // Phase 2: When a request to "start" the entry comes in (via |
| 155 // HandleQueuedStartRequest()), a VideoCaptureDevice::Client is created | 168 // HandleQueuedStartRequest()), a VideoCaptureDevice::Client is created |
| 156 // via video_capture_controller()->NewDeviceClient() and is used to schedule the | 169 // via CreateDeviceClient() and is used to schedule the |
| 157 // creation and start of a VideoCaptureDevice on the Device Thread. | 170 // creation and start of a VideoCaptureDevice on the Device Thread. |
| 158 // Phase 3: As soon as the creation of the VideoCaptureDevice is complete, this | 171 // Phase 3: As soon as the creation of the VideoCaptureDevice is complete, this |
| 159 // newly created VideoCaptureDevice instance is connected to the | 172 // newly created VideoCaptureDevice instance is connected to the |
| 160 // VideoCaptureController via SetConsumerFeedbackObserver(). | 173 // VideoCaptureController via SetConsumerFeedbackObserver(). |
| 161 class VideoCaptureManager::DeviceEntry { | 174 struct VideoCaptureManager::DeviceEntry { |
| 162 public: | 175 public: |
| 163 DeviceEntry(MediaStreamType stream_type, | 176 DeviceEntry(MediaStreamType stream_type, |
| 164 const std::string& id, | 177 const std::string& id, |
| 165 std::unique_ptr<VideoCaptureController> controller, | |
| 166 const media::VideoCaptureParams& params); | 178 const media::VideoCaptureParams& params); |
| 167 ~DeviceEntry(); | 179 ~DeviceEntry(); |
| 180 std::unique_ptr<media::VideoCaptureDevice::Client> CreateDeviceClient(); |
| 181 std::unique_ptr<media::FrameBufferPool> CreateFrameBufferPool(); |
| 168 | 182 |
| 169 const int serial_id; | 183 const int serial_id; |
| 170 const MediaStreamType stream_type; | 184 const MediaStreamType stream_type; |
| 171 const std::string id; | 185 const std::string id; |
| 172 const media::VideoCaptureParams parameters; | 186 const media::VideoCaptureParams parameters; |
| 173 | 187 VideoCaptureController video_capture_controller; |
| 174 VideoCaptureController* video_capture_controller() const; | 188 scoped_refptr<media::VideoCaptureBufferPool> buffer_pool; |
| 175 media::VideoCaptureDevice* video_capture_device() const; | 189 std::unique_ptr<media::VideoCaptureDevice> video_capture_device; |
| 176 | |
| 177 void SetVideoCaptureDevice(std::unique_ptr<VideoCaptureDevice> device); | |
| 178 std::unique_ptr<VideoCaptureDevice> ReleaseVideoCaptureDevice(); | |
| 179 | |
| 180 private: | |
| 181 const std::unique_ptr<VideoCaptureController> video_capture_controller_; | |
| 182 | |
| 183 std::unique_ptr<VideoCaptureDevice> video_capture_device_; | |
| 184 | |
| 185 base::ThreadChecker thread_checker_; | |
| 186 }; | 190 }; |
| 187 | 191 |
| 188 // Bundles a media::VideoCaptureDeviceDescriptor with corresponding supported | 192 // Bundles a media::VideoCaptureDeviceDescriptor with corresponding supported |
| 189 // video formats. | 193 // video formats. |
| 190 struct VideoCaptureManager::DeviceInfo { | 194 struct VideoCaptureManager::DeviceInfo { |
| 191 DeviceInfo(); | 195 DeviceInfo(); |
| 192 DeviceInfo(media::VideoCaptureDeviceDescriptor descriptor); | 196 DeviceInfo(media::VideoCaptureDeviceDescriptor descriptor); |
| 193 DeviceInfo(const DeviceInfo& other); | 197 DeviceInfo(const DeviceInfo& other); |
| 194 ~DeviceInfo(); | 198 ~DeviceInfo(); |
| 195 DeviceInfo& operator=(const DeviceInfo& other); | 199 DeviceInfo& operator=(const DeviceInfo& other); |
| 196 | 200 |
| 197 media::VideoCaptureDeviceDescriptor descriptor; | 201 media::VideoCaptureDeviceDescriptor descriptor; |
| 198 media::VideoCaptureFormats supported_formats; | 202 media::VideoCaptureFormats supported_formats; |
| 199 }; | 203 }; |
| 200 | 204 |
| 205 class BufferPoolFrameBufferPool : public media::FrameBufferPool { |
| 206 public: |
| 207 explicit BufferPoolFrameBufferPool( |
| 208 scoped_refptr<media::VideoCaptureBufferPool> buffer_pool) |
| 209 : buffer_pool_(std::move(buffer_pool)) {} |
| 210 |
| 211 void SetBufferHold(int buffer_id) override { |
| 212 buffer_pool_->HoldForConsumers(buffer_id, 1); |
| 213 } |
| 214 |
| 215 void ReleaseBufferHold(int buffer_id) override { |
| 216 buffer_pool_->RelinquishConsumerHold(buffer_id, 1); |
| 217 } |
| 218 |
| 219 mojo::ScopedSharedBufferHandle GetHandleForTransit(int buffer_id) override { |
| 220 return buffer_pool_->GetHandleForTransit(buffer_id); |
| 221 } |
| 222 |
| 223 private: |
| 224 scoped_refptr<media::VideoCaptureBufferPool> buffer_pool_; |
| 225 }; |
| 226 |
| 201 // Class used for queuing request for starting a device. | 227 // Class used for queuing request for starting a device. |
| 202 class VideoCaptureManager::CaptureDeviceStartRequest { | 228 class VideoCaptureManager::CaptureDeviceStartRequest { |
| 203 public: | 229 public: |
| 204 CaptureDeviceStartRequest(int serial_id, | 230 CaptureDeviceStartRequest(int serial_id, |
| 205 media::VideoCaptureSessionId session_id, | 231 media::VideoCaptureSessionId session_id, |
| 206 const media::VideoCaptureParams& params); | 232 const media::VideoCaptureParams& params); |
| 207 int serial_id() const { return serial_id_; } | 233 int serial_id() const { return serial_id_; } |
| 208 media::VideoCaptureSessionId session_id() const { return session_id_; } | 234 media::VideoCaptureSessionId session_id() const { return session_id_; } |
| 209 media::VideoCaptureParams params() const { return params_; } | 235 media::VideoCaptureParams params() const { return params_; } |
| 210 | 236 |
| 211 // Set to true if the device should be stopped before it has successfully | 237 // Set to true if the device should be stopped before it has successfully |
| 212 // been started. | 238 // been started. |
| 213 bool abort_start() const { return abort_start_; } | 239 bool abort_start() const { return abort_start_; } |
| 214 void set_abort_start() { abort_start_ = true; } | 240 void set_abort_start() { abort_start_ = true; } |
| 215 | 241 |
| 216 private: | 242 private: |
| 217 const int serial_id_; | 243 const int serial_id_; |
| 218 const media::VideoCaptureSessionId session_id_; | 244 const media::VideoCaptureSessionId session_id_; |
| 219 const media::VideoCaptureParams params_; | 245 const media::VideoCaptureParams params_; |
| 220 // Set to true if the device should be stopped before it has successfully | 246 // Set to true if the device should be stopped before it has successfully |
| 221 // been started. | 247 // been started. |
| 222 bool abort_start_; | 248 bool abort_start_; |
| 223 }; | 249 }; |
| 224 | 250 |
| 225 VideoCaptureManager::DeviceEntry::DeviceEntry( | 251 VideoCaptureManager::DeviceEntry::DeviceEntry( |
| 226 MediaStreamType stream_type, | 252 MediaStreamType stream_type, |
| 227 const std::string& id, | 253 const std::string& id, |
| 228 std::unique_ptr<VideoCaptureController> controller, | |
| 229 const media::VideoCaptureParams& params) | 254 const media::VideoCaptureParams& params) |
| 230 : serial_id(g_device_start_id++), | 255 : serial_id(g_device_start_id++), |
| 231 stream_type(stream_type), | 256 stream_type(stream_type), |
| 232 id(id), | 257 id(id), |
| 233 parameters(params), | 258 parameters(params) {} |
| 234 video_capture_controller_(std::move(controller)) {} | |
| 235 | 259 |
| 236 VideoCaptureManager::DeviceEntry::~DeviceEntry() { | 260 VideoCaptureManager::DeviceEntry::~DeviceEntry() { |
| 237 DCHECK(thread_checker_.CalledOnValidThread()); | 261 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 238 // DCHECK that this DeviceEntry does not still own a | 262 // DCHECK that this DeviceEntry does not still own a |
| 239 // media::VideoCaptureDevice. media::VideoCaptureDevice must be deleted on | 263 // media::VideoCaptureDevice. media::VideoCaptureDevice must be deleted on |
| 240 // the device thread. | 264 // the device thread. |
| 241 DCHECK(video_capture_device_ == nullptr); | 265 DCHECK(video_capture_device == nullptr); |
| 242 } | 266 } |
| 243 | 267 |
| 244 void VideoCaptureManager::DeviceEntry::SetVideoCaptureDevice( | 268 std::unique_ptr<media::VideoCaptureDevice::Client> |
| 245 std::unique_ptr<VideoCaptureDevice> device) { | 269 VideoCaptureManager::DeviceEntry::CreateDeviceClient() { |
| 246 DCHECK(thread_checker_.CalledOnValidThread()); | 270 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 247 video_capture_device_.swap(device); | 271 |
| 272 const int max_buffers = stream_type == MEDIA_TAB_VIDEO_CAPTURE |
| 273 ? kMaxNumberOfBuffersForTabCapture |
| 274 : kMaxNumberOfBuffers; |
| 275 buffer_pool = new media::VideoCaptureBufferPoolImpl( |
| 276 base::MakeUnique<media::VideoCaptureBufferTrackerFactoryImpl>(), |
| 277 max_buffers); |
| 278 |
| 279 return base::MakeUnique<media::VideoCaptureDeviceClient>( |
| 280 base::MakeUnique<VideoFrameReceiverOnIOThread>( |
| 281 video_capture_controller.GetWeakPtrForIOThread()), |
| 282 buffer_pool, |
| 283 base::Bind( |
| 284 &CreateGpuJpegDecoder, |
| 285 base::Bind(&media::VideoFrameReceiver::OnIncomingCapturedVideoFrame, |
| 286 video_capture_controller.GetWeakPtrForIOThread()))); |
| 248 } | 287 } |
| 249 | 288 |
| 250 std::unique_ptr<media::VideoCaptureDevice> | 289 std::unique_ptr<media::FrameBufferPool> |
| 251 VideoCaptureManager::DeviceEntry::ReleaseVideoCaptureDevice() { | 290 VideoCaptureManager::DeviceEntry::CreateFrameBufferPool() { |
| 252 DCHECK(thread_checker_.CalledOnValidThread()); | 291 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 253 return std::move(video_capture_device_); | 292 DCHECK(buffer_pool); |
| 254 } | 293 return base::MakeUnique<BufferPoolFrameBufferPool>(buffer_pool); |
| 255 | |
| 256 VideoCaptureController* | |
| 257 VideoCaptureManager::DeviceEntry::video_capture_controller() const { | |
| 258 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 259 return video_capture_controller_.get(); | |
| 260 } | |
| 261 | |
| 262 media::VideoCaptureDevice* | |
| 263 VideoCaptureManager::DeviceEntry::video_capture_device() const { | |
| 264 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 265 return video_capture_device_.get(); | |
| 266 } | 294 } |
| 267 | 295 |
| 268 VideoCaptureManager::DeviceInfo::DeviceInfo() = default; | 296 VideoCaptureManager::DeviceInfo::DeviceInfo() = default; |
| 269 | 297 |
| 270 VideoCaptureManager::DeviceInfo::DeviceInfo( | 298 VideoCaptureManager::DeviceInfo::DeviceInfo( |
| 271 media::VideoCaptureDeviceDescriptor descriptor) | 299 media::VideoCaptureDeviceDescriptor descriptor) |
| 272 : descriptor(descriptor) {} | 300 : descriptor(descriptor) {} |
| 273 | 301 |
| 274 VideoCaptureManager::DeviceInfo::DeviceInfo( | 302 VideoCaptureManager::DeviceInfo::DeviceInfo( |
| 275 const VideoCaptureManager::DeviceInfo& other) = default; | 303 const VideoCaptureManager::DeviceInfo& other) = default; |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 377 if (session_it == sessions_.end()) { | 405 if (session_it == sessions_.end()) { |
| 378 NOTREACHED(); | 406 NOTREACHED(); |
| 379 return; | 407 return; |
| 380 } | 408 } |
| 381 | 409 |
| 382 DeviceEntry* const existing_device = | 410 DeviceEntry* const existing_device = |
| 383 GetDeviceEntryByTypeAndId(session_it->second.type, session_it->second.id); | 411 GetDeviceEntryByTypeAndId(session_it->second.type, session_it->second.id); |
| 384 if (existing_device) { | 412 if (existing_device) { |
| 385 // Remove any client that is still using the session. This is safe to call | 413 // Remove any client that is still using the session. This is safe to call |
| 386 // even if there are no clients using the session. | 414 // even if there are no clients using the session. |
| 387 existing_device->video_capture_controller() | 415 existing_device->video_capture_controller.StopSession(capture_session_id); |
| 388 ->StopSession(capture_session_id); | |
| 389 | 416 |
| 390 // StopSession() may have removed the last client, so we might need to | 417 // StopSession() may have removed the last client, so we might need to |
| 391 // close the device. | 418 // close the device. |
| 392 DestroyDeviceEntryIfNoClients(existing_device); | 419 DestroyDeviceEntryIfNoClients(existing_device); |
| 393 } | 420 } |
| 394 | 421 |
| 395 // Notify listeners asynchronously, and forget the session. | 422 // Notify listeners asynchronously, and forget the session. |
| 396 base::ThreadTaskRunnerHandle::Get()->PostTask( | 423 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 397 FROM_HERE, base::Bind(&VideoCaptureManager::OnClosed, this, | 424 FROM_HERE, base::Bind(&VideoCaptureManager::OnClosed, this, |
| 398 session_it->second.type, capture_session_id)); | 425 session_it->second.type, capture_session_id)); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 426 if (request->serial_id() == entry->serial_id) { | 453 if (request->serial_id() == entry->serial_id) { |
| 427 request->set_abort_start(); | 454 request->set_abort_start(); |
| 428 DVLOG(3) << "DoStopDevice, aborting start request for device " | 455 DVLOG(3) << "DoStopDevice, aborting start request for device " |
| 429 << entry->id << " serial_id = " << entry->serial_id; | 456 << entry->id << " serial_id = " << entry->serial_id; |
| 430 return; | 457 return; |
| 431 } | 458 } |
| 432 } | 459 } |
| 433 | 460 |
| 434 DVLOG(3) << "DoStopDevice. Send stop request for device = " << entry->id | 461 DVLOG(3) << "DoStopDevice. Send stop request for device = " << entry->id |
| 435 << " serial_id = " << entry->serial_id << "."; | 462 << " serial_id = " << entry->serial_id << "."; |
| 436 entry->video_capture_controller()->OnLog( | 463 entry->video_capture_controller.OnLog( |
| 437 base::StringPrintf("Stopping device: id: %s", entry->id.c_str())); | 464 base::StringPrintf("Stopping device: id: %s", entry->id.c_str())); |
| 438 entry->video_capture_controller()->SetConsumerFeedbackObserver(nullptr); | 465 entry->video_capture_controller.SetConsumerFeedbackObserver(nullptr); |
| 466 entry->video_capture_controller.SetFrameBufferPool(nullptr); |
| 439 | 467 |
| 440 // |entry->video_capture_device| can be null if creating the device has | 468 // |entry->video_capture_device| can be null if creating the device has |
| 441 // failed. | 469 // failed. |
| 442 if (entry->video_capture_device()) { | 470 if (entry->video_capture_device) { |
| 443 device_task_runner_->PostTask( | 471 device_task_runner_->PostTask( |
| 444 FROM_HERE, | 472 FROM_HERE, |
| 445 base::Bind(&VideoCaptureManager::DoStopDeviceOnDeviceThread, this, | 473 base::Bind(&VideoCaptureManager::DoStopDeviceOnDeviceThread, this, |
| 446 base::Passed(entry->ReleaseVideoCaptureDevice()))); | 474 base::Passed(&entry->video_capture_device))); |
| 447 } | 475 } |
| 448 } | 476 } |
| 449 | 477 |
| 450 void VideoCaptureManager::HandleQueuedStartRequest() { | 478 void VideoCaptureManager::HandleQueuedStartRequest() { |
| 451 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 479 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 452 // Remove all start requests that have been aborted. | 480 // Remove all start requests that have been aborted. |
| 453 while (device_start_queue_.begin() != device_start_queue_.end() && | 481 while (device_start_queue_.begin() != device_start_queue_.end() && |
| 454 device_start_queue_.begin()->abort_start()) { | 482 device_start_queue_.begin()->abort_start()) { |
| 455 device_start_queue_.pop_front(); | 483 device_start_queue_.pop_front(); |
| 456 } | 484 } |
| 457 DeviceStartQueue::iterator request = device_start_queue_.begin(); | 485 DeviceStartQueue::iterator request = device_start_queue_.begin(); |
| 458 if (request == device_start_queue_.end()) | 486 if (request == device_start_queue_.end()) |
| 459 return; | 487 return; |
| 460 | 488 |
| 461 const int serial_id = request->serial_id(); | 489 const int serial_id = request->serial_id(); |
| 462 DeviceEntry* const entry = GetDeviceEntryBySerialId(serial_id); | 490 DeviceEntry* const entry = GetDeviceEntryBySerialId(serial_id); |
| 463 DCHECK(entry); | 491 DCHECK(entry); |
| 464 | 492 |
| 465 DVLOG(3) << "HandleQueuedStartRequest, Post start to device thread, device = " | 493 DVLOG(3) << "HandleQueuedStartRequest, Post start to device thread, device = " |
| 466 << entry->id << " start id = " << entry->serial_id; | 494 << entry->id << " start id = " << entry->serial_id; |
| 467 | 495 |
| 496 std::unique_ptr<media::VideoCaptureDevice::Client> device_client = |
| 497 entry->CreateDeviceClient(); |
| 498 std::unique_ptr<media::FrameBufferPool> frame_buffer_pool = |
| 499 entry->CreateFrameBufferPool(); |
| 500 |
| 468 base::Callback<std::unique_ptr<VideoCaptureDevice>(void)> | 501 base::Callback<std::unique_ptr<VideoCaptureDevice>(void)> |
| 469 start_capture_function; | 502 start_capture_function; |
| 470 | 503 |
| 471 switch (entry->stream_type) { | 504 switch (entry->stream_type) { |
| 472 case MEDIA_DEVICE_VIDEO_CAPTURE: { | 505 case MEDIA_DEVICE_VIDEO_CAPTURE: { |
| 473 // We look up the device id from the renderer in our local enumeration | 506 // We look up the device id from the renderer in our local enumeration |
| 474 // since the renderer does not have all the information that might be | 507 // since the renderer does not have all the information that might be |
| 475 // held in the browser-side VideoCaptureDevice::Name structure. | 508 // held in the browser-side VideoCaptureDevice::Name structure. |
| 476 const DeviceInfo* found = GetDeviceInfoById(entry->id); | 509 const DeviceInfo* found = GetDeviceInfoById(entry->id); |
| 477 if (found) { | 510 if (found) { |
| 478 entry->video_capture_controller()->OnLog( | 511 entry->video_capture_controller.OnLog( |
| 479 base::StringPrintf("Starting device: id: %s, name: %s, api: %s", | 512 base::StringPrintf("Starting device: id: %s, name: %s, api: %s", |
| 480 found->descriptor.device_id.c_str(), | 513 found->descriptor.device_id.c_str(), |
| 481 found->descriptor.GetNameAndModel().c_str(), | 514 found->descriptor.GetNameAndModel().c_str(), |
| 482 found->descriptor.GetCaptureApiTypeString())); | 515 found->descriptor.GetCaptureApiTypeString())); |
| 483 | 516 |
| 484 start_capture_function = base::Bind( | 517 start_capture_function = |
| 485 &VideoCaptureManager::DoStartDeviceCaptureOnDeviceThread, this, | 518 base::Bind(&VideoCaptureManager::DoStartDeviceCaptureOnDeviceThread, |
| 486 found->descriptor, request->params(), | 519 this, found->descriptor, request->params(), |
| 487 base::Passed(entry->video_capture_controller()->NewDeviceClient())); | 520 base::Passed(std::move(device_client))); |
| 488 } else { | 521 } else { |
| 489 // Errors from DoStartDeviceCaptureOnDeviceThread go via | 522 // Errors from DoStartDeviceCaptureOnDeviceThread go via |
| 490 // VideoCaptureDeviceClient::OnError, which needs some thread | 523 // VideoCaptureDeviceClient::OnError, which needs some thread |
| 491 // dancing to get errors processed on the IO thread. But since | 524 // dancing to get errors processed on the IO thread. But since |
| 492 // we're on that thread, we call VideoCaptureController | 525 // we're on that thread, we call VideoCaptureController |
| 493 // methods directly. | 526 // methods directly. |
| 494 const std::string log_message = base::StringPrintf( | 527 const std::string log_message = base::StringPrintf( |
| 495 "Error on %s:%d: device %s unknown. Maybe recently disconnected?", | 528 "Error on %s:%d: device %s unknown. Maybe recently disconnected?", |
| 496 __FILE__, __LINE__, entry->id.c_str()); | 529 __FILE__, __LINE__, entry->id.c_str()); |
| 497 DLOG(ERROR) << log_message; | 530 DLOG(ERROR) << log_message; |
| 498 entry->video_capture_controller()->OnLog(log_message); | 531 entry->video_capture_controller.OnLog(log_message); |
| 499 entry->video_capture_controller()->OnError(); | 532 entry->video_capture_controller.OnError(); |
| 500 // Drop the failed start request. | 533 // Drop the failed start request. |
| 501 device_start_queue_.pop_front(); | 534 device_start_queue_.pop_front(); |
| 502 | 535 |
| 503 return; | 536 return; |
| 504 } | 537 } |
| 505 break; | 538 break; |
| 506 } | 539 } |
| 507 case MEDIA_TAB_VIDEO_CAPTURE: | 540 case MEDIA_TAB_VIDEO_CAPTURE: |
| 508 start_capture_function = base::Bind( | 541 start_capture_function = base::Bind( |
| 509 &VideoCaptureManager::DoStartTabCaptureOnDeviceThread, this, | 542 &VideoCaptureManager::DoStartTabCaptureOnDeviceThread, this, |
| 510 entry->id, request->params(), | 543 entry->id, request->params(), base::Passed(std::move(device_client))); |
| 511 base::Passed(entry->video_capture_controller()->NewDeviceClient())); | |
| 512 break; | 544 break; |
| 513 | 545 |
| 514 case MEDIA_DESKTOP_VIDEO_CAPTURE: | 546 case MEDIA_DESKTOP_VIDEO_CAPTURE: |
| 515 start_capture_function = base::Bind( | 547 start_capture_function = base::Bind( |
| 516 &VideoCaptureManager::DoStartDesktopCaptureOnDeviceThread, this, | 548 &VideoCaptureManager::DoStartDesktopCaptureOnDeviceThread, this, |
| 517 entry->id, request->params(), | 549 entry->id, request->params(), base::Passed(std::move(device_client))); |
| 518 base::Passed(entry->video_capture_controller()->NewDeviceClient())); | |
| 519 break; | 550 break; |
| 520 | 551 |
| 521 default: { | 552 default: { |
| 522 NOTIMPLEMENTED(); | 553 NOTIMPLEMENTED(); |
| 523 return; | 554 return; |
| 524 } | 555 } |
| 525 } | 556 } |
| 526 base::PostTaskAndReplyWithResult( | 557 base::PostTaskAndReplyWithResult( |
| 527 device_task_runner_.get(), FROM_HERE, start_capture_function, | 558 device_task_runner_.get(), FROM_HERE, start_capture_function, |
| 528 base::Bind(&VideoCaptureManager::OnDeviceStarted, this, | 559 base::Bind(&VideoCaptureManager::OnDeviceStarted, this, |
| 529 request->serial_id())); | 560 request->serial_id(), base::Passed(&frame_buffer_pool))); |
| 530 } | 561 } |
| 531 | 562 |
| 532 void VideoCaptureManager::OnDeviceStarted( | 563 void VideoCaptureManager::OnDeviceStarted( |
| 533 int serial_id, | 564 int serial_id, |
| 565 std::unique_ptr<media::FrameBufferPool> frame_buffer_pool, |
| 534 std::unique_ptr<VideoCaptureDevice> device) { | 566 std::unique_ptr<VideoCaptureDevice> device) { |
| 535 DVLOG(3) << __func__; | 567 DVLOG(3) << __func__; |
| 536 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 568 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 537 DCHECK_EQ(serial_id, device_start_queue_.begin()->serial_id()); | 569 DCHECK_EQ(serial_id, device_start_queue_.begin()->serial_id()); |
| 538 // |device| can be null if creation failed in | 570 // |device| can be null if creation failed in |
| 539 // DoStartDeviceCaptureOnDeviceThread. | 571 // DoStartDeviceCaptureOnDeviceThread. |
| 540 if (device_start_queue_.front().abort_start()) { | 572 if (device_start_queue_.front().abort_start()) { |
| 541 // The device is no longer wanted. Stop the device again. | 573 // The device is no longer wanted. Stop the device again. |
| 542 DVLOG(3) << "OnDeviceStarted but start request have been aborted."; | 574 DVLOG(3) << "OnDeviceStarted but start request have been aborted."; |
| 543 media::VideoCaptureDevice* device_ptr = device.get(); | 575 media::VideoCaptureDevice* device_ptr = device.get(); |
| 544 base::Closure closure = | 576 base::Closure closure = |
| 545 base::Bind(&VideoCaptureManager::DoStopDeviceOnDeviceThread, this, | 577 base::Bind(&VideoCaptureManager::DoStopDeviceOnDeviceThread, this, |
| 546 base::Passed(&device)); | 578 base::Passed(&device)); |
| 547 if (device_ptr && !device_task_runner_->PostTask(FROM_HERE, closure)) { | 579 if (device_ptr && !device_task_runner_->PostTask(FROM_HERE, closure)) { |
| 548 // PostTask failed. The device must be stopped anyway. | 580 // PostTask failed. The device must be stopped anyway. |
| 549 device_ptr->StopAndDeAllocate(); | 581 device_ptr->StopAndDeAllocate(); |
| 550 } | 582 } |
| 551 } else { | 583 } else { |
| 552 DeviceEntry* const entry = GetDeviceEntryBySerialId(serial_id); | 584 DeviceEntry* const entry = GetDeviceEntryBySerialId(serial_id); |
| 553 DCHECK(entry); | 585 DCHECK(entry); |
| 554 DCHECK(!entry->video_capture_device()); | 586 DCHECK(!entry->video_capture_device); |
| 555 // Passing raw pointer |device.get()| to the controller is safe, | 587 if (device) { |
| 556 // because we transfer ownership of it to |entry|. We are calling | 588 entry->video_capture_controller.SetFrameBufferPool( |
| 557 // SetConsumerFeedbackObserver(nullptr) before releasing | 589 std::move(frame_buffer_pool)); |
| 558 // |entry->video_capture_device_| on the |device_task_runner_|. | 590 // Passing raw pointer |device.get()| to the controller is safe, |
| 559 entry->video_capture_controller()->SetConsumerFeedbackObserver( | 591 // because we transfer ownership of it to |entry|. We are calling |
| 560 base::MakeUnique<VideoFrameConsumerFeedbackObserverOnTaskRunner>( | 592 // SetConsumerFeedbackObserver(nullptr) before releasing |
| 561 device.get(), device_task_runner_)); | 593 // |entry->video_capture_device_| on the |device_task_runner_|. |
| 562 entry->SetVideoCaptureDevice(std::move(device)); | 594 entry->video_capture_controller.SetConsumerFeedbackObserver( |
| 595 base::MakeUnique<VideoFrameConsumerFeedbackObserverOnTaskRunner>( |
| 596 device.get(), device_task_runner_)); |
| 597 } |
| 598 entry->video_capture_device = std::move(device); |
| 563 | 599 |
| 564 if (entry->stream_type == MEDIA_DESKTOP_VIDEO_CAPTURE) { | 600 if (entry->stream_type == MEDIA_DESKTOP_VIDEO_CAPTURE) { |
| 565 const media::VideoCaptureSessionId session_id = | 601 const media::VideoCaptureSessionId session_id = |
| 566 device_start_queue_.front().session_id(); | 602 device_start_queue_.front().session_id(); |
| 567 DCHECK(session_id != kFakeSessionId); | 603 DCHECK(session_id != kFakeSessionId); |
| 568 MaybePostDesktopCaptureWindowId(session_id); | 604 MaybePostDesktopCaptureWindowId(session_id); |
| 569 } | 605 } |
| 570 | 606 |
| 571 auto it = photo_request_queue_.begin(); | 607 auto it = photo_request_queue_.begin(); |
| 572 while (it != photo_request_queue_.end()) { | 608 while (it != photo_request_queue_.end()) { |
| 573 auto request = it++; | 609 auto request = it++; |
| 574 DeviceEntry* maybe_entry = GetDeviceEntryBySessionId(request->first); | 610 DeviceEntry* maybe_entry = GetDeviceEntryBySessionId(request->first); |
| 575 if (maybe_entry && maybe_entry->video_capture_device()) { | 611 if (maybe_entry && maybe_entry->video_capture_device) { |
| 576 request->second.Run(maybe_entry->video_capture_device()); | 612 request->second.Run(maybe_entry->video_capture_device.get()); |
| 577 photo_request_queue_.erase(request); | 613 photo_request_queue_.erase(request); |
| 578 } | 614 } |
| 579 } | 615 } |
| 580 } | 616 } |
| 581 | 617 |
| 582 device_start_queue_.pop_front(); | 618 device_start_queue_.pop_front(); |
| 583 HandleQueuedStartRequest(); | 619 HandleQueuedStartRequest(); |
| 584 } | 620 } |
| 585 | 621 |
| 586 std::unique_ptr<media::VideoCaptureDevice> | 622 std::unique_ptr<media::VideoCaptureDevice> |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 679 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 715 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 680 DVLOG(1) << __func__ << ", session_id = " << session_id << ", request: " | 716 DVLOG(1) << __func__ << ", session_id = " << session_id << ", request: " |
| 681 << media::VideoCaptureFormat::ToString(params.requested_format); | 717 << media::VideoCaptureFormat::ToString(params.requested_format); |
| 682 | 718 |
| 683 DeviceEntry* entry = GetOrCreateDeviceEntry(session_id, params); | 719 DeviceEntry* entry = GetOrCreateDeviceEntry(session_id, params); |
| 684 if (!entry) { | 720 if (!entry) { |
| 685 done_cb.Run(base::WeakPtr<VideoCaptureController>()); | 721 done_cb.Run(base::WeakPtr<VideoCaptureController>()); |
| 686 return; | 722 return; |
| 687 } | 723 } |
| 688 | 724 |
| 689 DCHECK(entry->video_capture_controller()); | |
| 690 | |
| 691 LogVideoCaptureEvent(VIDEO_CAPTURE_START_CAPTURE); | 725 LogVideoCaptureEvent(VIDEO_CAPTURE_START_CAPTURE); |
| 692 | 726 |
| 693 // First client starts the device. | 727 // First client starts the device. |
| 694 if (!entry->video_capture_controller()->HasActiveClient() && | 728 if (!entry->video_capture_controller.HasActiveClient() && |
| 695 !entry->video_capture_controller()->HasPausedClient()) { | 729 !entry->video_capture_controller.HasPausedClient()) { |
| 696 DVLOG(1) << "VideoCaptureManager starting device (type = " | 730 DVLOG(1) << "VideoCaptureManager starting device (type = " |
| 697 << entry->stream_type << ", id = " << entry->id << ")"; | 731 << entry->stream_type << ", id = " << entry->id << ")"; |
| 698 QueueStartDevice(session_id, entry, params); | 732 QueueStartDevice(session_id, entry, params); |
| 699 } | 733 } |
| 700 // Run the callback first, as AddClient() may trigger OnFrameInfo(). | 734 // Run the callback first, as AddClient() may trigger OnFrameInfo(). |
| 701 done_cb.Run(entry->video_capture_controller()->GetWeakPtrForIOThread()); | 735 done_cb.Run(entry->video_capture_controller.GetWeakPtrForIOThread()); |
| 702 entry->video_capture_controller()->AddClient( | 736 entry->video_capture_controller.AddClient(client_id, client_handler, |
| 703 client_id, client_handler, session_id, params); | 737 session_id, params); |
| 704 } | 738 } |
| 705 | 739 |
| 706 void VideoCaptureManager::StopCaptureForClient( | 740 void VideoCaptureManager::StopCaptureForClient( |
| 707 VideoCaptureController* controller, | 741 VideoCaptureController* controller, |
| 708 VideoCaptureControllerID client_id, | 742 VideoCaptureControllerID client_id, |
| 709 VideoCaptureControllerEventHandler* client_handler, | 743 VideoCaptureControllerEventHandler* client_handler, |
| 710 bool aborted_due_to_error) { | 744 bool aborted_due_to_error) { |
| 711 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 745 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 712 DCHECK(controller); | 746 DCHECK(controller); |
| 713 DCHECK(client_handler); | 747 DCHECK(client_handler); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 758 DCHECK(controller); | 792 DCHECK(controller); |
| 759 DCHECK(client_handler); | 793 DCHECK(client_handler); |
| 760 DeviceEntry* entry = GetDeviceEntryByController(controller); | 794 DeviceEntry* entry = GetDeviceEntryByController(controller); |
| 761 if (!entry) | 795 if (!entry) |
| 762 NOTREACHED() << "Got Null entry while pausing capture"; | 796 NOTREACHED() << "Got Null entry while pausing capture"; |
| 763 | 797 |
| 764 const bool had_active_client = controller->HasActiveClient(); | 798 const bool had_active_client = controller->HasActiveClient(); |
| 765 controller->PauseClient(client_id, client_handler); | 799 controller->PauseClient(client_id, client_handler); |
| 766 if (!had_active_client || controller->HasActiveClient()) | 800 if (!had_active_client || controller->HasActiveClient()) |
| 767 return; | 801 return; |
| 768 if (media::VideoCaptureDevice* device = entry->video_capture_device()) { | 802 if (media::VideoCaptureDevice* device = entry->video_capture_device.get()) { |
| 769 device_task_runner_->PostTask( | 803 device_task_runner_->PostTask( |
| 770 FROM_HERE, | 804 FROM_HERE, |
| 771 base::Bind(&VideoCaptureDevice::MaybeSuspend, | 805 base::Bind(&VideoCaptureDevice::MaybeSuspend, |
| 772 // Unretained is safe to use here because |device| would be | 806 // Unretained is safe to use here because |device| would be |
| 773 // null if it was scheduled for shutdown and destruction, and | 807 // null if it was scheduled for shutdown and destruction, and |
| 774 // because this task is guaranteed to run before the task | 808 // because this task is guaranteed to run before the task |
| 775 // that destroys the |device|. | 809 // that destroys the |device|. |
| 776 base::Unretained(device))); | 810 base::Unretained(device))); |
| 777 } | 811 } |
| 778 } | 812 } |
| 779 | 813 |
| 780 void VideoCaptureManager::ResumeCaptureForClient( | 814 void VideoCaptureManager::ResumeCaptureForClient( |
| 781 media::VideoCaptureSessionId session_id, | 815 media::VideoCaptureSessionId session_id, |
| 782 const media::VideoCaptureParams& params, | 816 const media::VideoCaptureParams& params, |
| 783 VideoCaptureController* controller, | 817 VideoCaptureController* controller, |
| 784 VideoCaptureControllerID client_id, | 818 VideoCaptureControllerID client_id, |
| 785 VideoCaptureControllerEventHandler* client_handler) { | 819 VideoCaptureControllerEventHandler* client_handler) { |
| 786 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 820 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 787 DCHECK(controller); | 821 DCHECK(controller); |
| 788 DCHECK(client_handler); | 822 DCHECK(client_handler); |
| 789 | 823 |
| 790 DeviceEntry* entry = GetDeviceEntryByController(controller); | 824 DeviceEntry* entry = GetDeviceEntryByController(controller); |
| 791 if (!entry) | 825 if (!entry) |
| 792 NOTREACHED() << "Got Null entry while resuming capture"; | 826 NOTREACHED() << "Got Null entry while resuming capture"; |
| 793 | 827 |
| 794 const bool had_active_client = controller->HasActiveClient(); | 828 const bool had_active_client = controller->HasActiveClient(); |
| 795 controller->ResumeClient(client_id, client_handler); | 829 controller->ResumeClient(client_id, client_handler); |
| 796 if (had_active_client || !controller->HasActiveClient()) | 830 if (had_active_client || !controller->HasActiveClient()) |
| 797 return; | 831 return; |
| 798 if (media::VideoCaptureDevice* device = entry->video_capture_device()) { | 832 if (media::VideoCaptureDevice* device = entry->video_capture_device.get()) { |
| 799 device_task_runner_->PostTask( | 833 device_task_runner_->PostTask( |
| 800 FROM_HERE, | 834 FROM_HERE, |
| 801 base::Bind(&VideoCaptureDevice::Resume, | 835 base::Bind(&VideoCaptureDevice::Resume, |
| 802 // Unretained is safe to use here because |device| would be | 836 // Unretained is safe to use here because |device| would be |
| 803 // null if it was scheduled for shutdown and destruction, and | 837 // null if it was scheduled for shutdown and destruction, and |
| 804 // because this task is guaranteed to run before the task | 838 // because this task is guaranteed to run before the task |
| 805 // that destroys the |device|. | 839 // that destroys the |device|. |
| 806 base::Unretained(device))); | 840 base::Unretained(device))); |
| 807 } | 841 } |
| 808 } | 842 } |
| 809 | 843 |
| 810 void VideoCaptureManager::RequestRefreshFrameForClient( | 844 void VideoCaptureManager::RequestRefreshFrameForClient( |
| 811 VideoCaptureController* controller) { | 845 VideoCaptureController* controller) { |
| 812 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 846 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 813 | 847 |
| 814 if (DeviceEntry* entry = GetDeviceEntryByController(controller)) { | 848 if (DeviceEntry* entry = GetDeviceEntryByController(controller)) { |
| 815 if (media::VideoCaptureDevice* device = entry->video_capture_device()) { | 849 if (media::VideoCaptureDevice* device = entry->video_capture_device.get()) { |
| 816 device_task_runner_->PostTask( | 850 device_task_runner_->PostTask( |
| 817 FROM_HERE, | 851 FROM_HERE, |
| 818 base::Bind(&VideoCaptureDevice::RequestRefreshFrame, | 852 base::Bind(&VideoCaptureDevice::RequestRefreshFrame, |
| 819 // Unretained is safe to use here because |device| would be | 853 // Unretained is safe to use here because |device| would be |
| 820 // null if it was scheduled for shutdown and destruction, | 854 // null if it was scheduled for shutdown and destruction, |
| 821 // and because this task is guaranteed to run before the | 855 // and because this task is guaranteed to run before the |
| 822 // task that destroys the |device|. | 856 // task that destroys the |device|. |
| 823 base::Unretained(device))); | 857 base::Unretained(device))); |
| 824 } | 858 } |
| 825 } | 859 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 853 if (it == sessions_.end()) | 887 if (it == sessions_.end()) |
| 854 return false; | 888 return false; |
| 855 DVLOG(1) << "GetDeviceFormatsInUse for device: " << it->second.name; | 889 DVLOG(1) << "GetDeviceFormatsInUse for device: " << it->second.name; |
| 856 | 890 |
| 857 // Return the currently in-use format(s) of the device, if it's started. | 891 // Return the currently in-use format(s) of the device, if it's started. |
| 858 DeviceEntry* device_in_use = | 892 DeviceEntry* device_in_use = |
| 859 GetDeviceEntryByTypeAndId(it->second.type, it->second.id); | 893 GetDeviceEntryByTypeAndId(it->second.type, it->second.id); |
| 860 if (device_in_use) { | 894 if (device_in_use) { |
| 861 // Currently only one format-in-use is supported at the VCC level. | 895 // Currently only one format-in-use is supported at the VCC level. |
| 862 formats_in_use->push_back( | 896 formats_in_use->push_back( |
| 863 device_in_use->video_capture_controller()->GetVideoCaptureFormat()); | 897 device_in_use->video_capture_controller.GetVideoCaptureFormat()); |
| 864 } | 898 } |
| 865 return true; | 899 return true; |
| 866 } | 900 } |
| 867 | 901 |
| 868 void VideoCaptureManager::SetDesktopCaptureWindowId( | 902 void VideoCaptureManager::SetDesktopCaptureWindowId( |
| 869 media::VideoCaptureSessionId session_id, | 903 media::VideoCaptureSessionId session_id, |
| 870 gfx::NativeViewId window_id) { | 904 gfx::NativeViewId window_id) { |
| 871 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 905 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 872 VLOG(2) << "SetDesktopCaptureWindowId called for session " << session_id; | 906 VLOG(2) << "SetDesktopCaptureWindowId called for session " << session_id; |
| 873 | 907 |
| 874 notification_window_ids_[session_id] = window_id; | 908 notification_window_ids_[session_id] = window_id; |
| 875 MaybePostDesktopCaptureWindowId(session_id); | 909 MaybePostDesktopCaptureWindowId(session_id); |
| 876 } | 910 } |
| 877 | 911 |
| 878 void VideoCaptureManager::MaybePostDesktopCaptureWindowId( | 912 void VideoCaptureManager::MaybePostDesktopCaptureWindowId( |
| 879 media::VideoCaptureSessionId session_id) { | 913 media::VideoCaptureSessionId session_id) { |
| 880 SessionMap::iterator session_it = sessions_.find(session_id); | 914 SessionMap::iterator session_it = sessions_.find(session_id); |
| 881 if (session_it == sessions_.end()) | 915 if (session_it == sessions_.end()) |
| 882 return; | 916 return; |
| 883 | 917 |
| 884 DeviceEntry* const existing_device = | 918 DeviceEntry* const existing_device = |
| 885 GetDeviceEntryByTypeAndId(session_it->second.type, session_it->second.id); | 919 GetDeviceEntryByTypeAndId(session_it->second.type, session_it->second.id); |
| 886 if (!existing_device) { | 920 if (!existing_device) { |
| 887 DVLOG(2) << "Failed to find an existing screen capture device."; | 921 DVLOG(2) << "Failed to find an existing screen capture device."; |
| 888 return; | 922 return; |
| 889 } | 923 } |
| 890 | 924 |
| 891 if (!existing_device->video_capture_device()) { | 925 if (!existing_device->video_capture_device) { |
| 892 DVLOG(2) << "Screen capture device not yet started."; | 926 DVLOG(2) << "Screen capture device not yet started."; |
| 893 return; | 927 return; |
| 894 } | 928 } |
| 895 | 929 |
| 896 DCHECK_EQ(MEDIA_DESKTOP_VIDEO_CAPTURE, existing_device->stream_type); | 930 DCHECK_EQ(MEDIA_DESKTOP_VIDEO_CAPTURE, existing_device->stream_type); |
| 897 DesktopMediaID id = DesktopMediaID::Parse(existing_device->id); | 931 DesktopMediaID id = DesktopMediaID::Parse(existing_device->id); |
| 898 if (id.is_null()) | 932 if (id.is_null()) |
| 899 return; | 933 return; |
| 900 | 934 |
| 901 auto window_id_it = notification_window_ids_.find(session_id); | 935 auto window_id_it = notification_window_ids_.find(session_id); |
| 902 if (window_id_it == notification_window_ids_.end()) { | 936 if (window_id_it == notification_window_ids_.end()) { |
| 903 DVLOG(2) << "Notification window id not set for screen capture."; | 937 DVLOG(2) << "Notification window id not set for screen capture."; |
| 904 return; | 938 return; |
| 905 } | 939 } |
| 906 | 940 |
| 907 // Post |existing_device->video_capture_device| to the VideoCaptureDevice to | 941 // Post |existing_device->video_capture_device| to the VideoCaptureDevice to |
| 908 // the device_task_runner_. This is safe since the device is destroyed on the | 942 // the device_task_runner_. This is safe since the device is destroyed on the |
| 909 // device_task_runner_. | 943 // device_task_runner_. |
| 910 device_task_runner_->PostTask( | 944 device_task_runner_->PostTask( |
| 911 FROM_HERE, | 945 FROM_HERE, |
| 912 base::Bind(&VideoCaptureManager::SetDesktopCaptureWindowIdOnDeviceThread, | 946 base::Bind(&VideoCaptureManager::SetDesktopCaptureWindowIdOnDeviceThread, |
| 913 this, | 947 this, existing_device->video_capture_device.get(), |
| 914 existing_device->video_capture_device(), | |
| 915 window_id_it->second)); | 948 window_id_it->second)); |
| 916 | 949 |
| 917 notification_window_ids_.erase(window_id_it); | 950 notification_window_ids_.erase(window_id_it); |
| 918 } | 951 } |
| 919 | 952 |
| 920 void VideoCaptureManager::GetPhotoCapabilities( | 953 void VideoCaptureManager::GetPhotoCapabilities( |
| 921 int session_id, | 954 int session_id, |
| 922 VideoCaptureDevice::GetPhotoCapabilitiesCallback callback) { | 955 VideoCaptureDevice::GetPhotoCapabilitiesCallback callback) { |
| 923 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 956 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 924 | 957 |
| 925 const DeviceEntry* entry = GetDeviceEntryBySessionId(session_id); | 958 const DeviceEntry* entry = GetDeviceEntryBySessionId(session_id); |
| 926 if (!entry) | 959 if (!entry) |
| 927 return; | 960 return; |
| 928 VideoCaptureDevice* device = entry->video_capture_device(); | 961 VideoCaptureDevice* device = entry->video_capture_device.get(); |
| 929 if (device) { | 962 if (device) { |
| 930 VideoCaptureManager::DoGetPhotoCapabilities(std::move(callback), device); | 963 VideoCaptureManager::DoGetPhotoCapabilities(std::move(callback), device); |
| 931 return; | 964 return; |
| 932 } | 965 } |
| 933 // |entry| is known but |device| is nullptr, queue up a request for later. | 966 // |entry| is known but |device| is nullptr, queue up a request for later. |
| 934 photo_request_queue_.emplace_back( | 967 photo_request_queue_.emplace_back( |
| 935 session_id, base::Bind(&VideoCaptureManager::DoGetPhotoCapabilities, this, | 968 session_id, base::Bind(&VideoCaptureManager::DoGetPhotoCapabilities, this, |
| 936 base::Passed(&callback))); | 969 base::Passed(&callback))); |
| 937 } | 970 } |
| 938 | 971 |
| 939 void VideoCaptureManager::SetPhotoOptions( | 972 void VideoCaptureManager::SetPhotoOptions( |
| 940 int session_id, | 973 int session_id, |
| 941 media::mojom::PhotoSettingsPtr settings, | 974 media::mojom::PhotoSettingsPtr settings, |
| 942 VideoCaptureDevice::SetPhotoOptionsCallback callback) { | 975 VideoCaptureDevice::SetPhotoOptionsCallback callback) { |
| 943 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 976 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 944 | 977 |
| 945 const DeviceEntry* entry = GetDeviceEntryBySessionId(session_id); | 978 const DeviceEntry* entry = GetDeviceEntryBySessionId(session_id); |
| 946 if (!entry) | 979 if (!entry) |
| 947 return; | 980 return; |
| 948 VideoCaptureDevice* device = entry->video_capture_device(); | 981 VideoCaptureDevice* device = entry->video_capture_device.get(); |
| 949 if (device) { | 982 if (device) { |
| 950 VideoCaptureManager::DoSetPhotoOptions(std::move(callback), | 983 VideoCaptureManager::DoSetPhotoOptions(std::move(callback), |
| 951 std::move(settings), device); | 984 std::move(settings), device); |
| 952 return; | 985 return; |
| 953 } | 986 } |
| 954 // |entry| is known but |device| is nullptr, queue up a request for later. | 987 // |entry| is known but |device| is nullptr, queue up a request for later. |
| 955 photo_request_queue_.emplace_back( | 988 photo_request_queue_.emplace_back( |
| 956 session_id, base::Bind(&VideoCaptureManager::DoSetPhotoOptions, this, | 989 session_id, base::Bind(&VideoCaptureManager::DoSetPhotoOptions, this, |
| 957 base::Passed(&callback), base::Passed(&settings))); | 990 base::Passed(&callback), base::Passed(&settings))); |
| 958 } | 991 } |
| 959 | 992 |
| 960 void VideoCaptureManager::TakePhoto( | 993 void VideoCaptureManager::TakePhoto( |
| 961 int session_id, | 994 int session_id, |
| 962 VideoCaptureDevice::TakePhotoCallback callback) { | 995 VideoCaptureDevice::TakePhotoCallback callback) { |
| 963 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 996 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 964 | 997 |
| 965 const DeviceEntry* entry = GetDeviceEntryBySessionId(session_id); | 998 const DeviceEntry* entry = GetDeviceEntryBySessionId(session_id); |
| 966 if (!entry) | 999 if (!entry) |
| 967 return; | 1000 return; |
| 968 VideoCaptureDevice* device = entry->video_capture_device(); | 1001 VideoCaptureDevice* device = entry->video_capture_device.get(); |
| 969 if (device) { | 1002 if (device) { |
| 970 VideoCaptureManager::DoTakePhoto(std::move(callback), device); | 1003 VideoCaptureManager::DoTakePhoto(std::move(callback), device); |
| 971 return; | 1004 return; |
| 972 } | 1005 } |
| 973 // |entry| is known but |device| is nullptr, queue up a request for later. | 1006 // |entry| is known but |device| is nullptr, queue up a request for later. |
| 974 photo_request_queue_.emplace_back( | 1007 photo_request_queue_.emplace_back( |
| 975 session_id, base::Bind(&VideoCaptureManager::DoTakePhoto, this, | 1008 session_id, base::Bind(&VideoCaptureManager::DoTakePhoto, this, |
| 976 base::Passed(&callback))); | 1009 base::Passed(&callback))); |
| 977 } | 1010 } |
| 978 | 1011 |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1066 ConsolidateCaptureFormats(&device_info.supported_formats); | 1099 ConsolidateCaptureFormats(&device_info.supported_formats); |
| 1067 new_devices_info_cache.push_back(device_info); | 1100 new_devices_info_cache.push_back(device_info); |
| 1068 } | 1101 } |
| 1069 | 1102 |
| 1070 on_devices_enumerated_callback.Run(new_devices_info_cache); | 1103 on_devices_enumerated_callback.Run(new_devices_info_cache); |
| 1071 } | 1104 } |
| 1072 | 1105 |
| 1073 void VideoCaptureManager::DestroyDeviceEntryIfNoClients(DeviceEntry* entry) { | 1106 void VideoCaptureManager::DestroyDeviceEntryIfNoClients(DeviceEntry* entry) { |
| 1074 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1107 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1075 // Removal of the last client stops the device. | 1108 // Removal of the last client stops the device. |
| 1076 if (!entry->video_capture_controller()->HasActiveClient() && | 1109 if (!entry->video_capture_controller.HasActiveClient() && |
| 1077 !entry->video_capture_controller()->HasPausedClient()) { | 1110 !entry->video_capture_controller.HasPausedClient()) { |
| 1078 DVLOG(1) << "VideoCaptureManager stopping device (type = " | 1111 DVLOG(1) << "VideoCaptureManager stopping device (type = " |
| 1079 << entry->stream_type << ", id = " << entry->id << ")"; | 1112 << entry->stream_type << ", id = " << entry->id << ")"; |
| 1080 | 1113 |
| 1081 // The DeviceEntry is removed from |devices_| immediately. The controller is | 1114 // The DeviceEntry is removed from |devices_| immediately. The controller is |
| 1082 // deleted immediately, and the device is freed asynchronously. After this | 1115 // deleted immediately, and the device is freed asynchronously. After this |
| 1083 // point, subsequent requests to open this same device ID will create a new | 1116 // point, subsequent requests to open this same device ID will create a new |
| 1084 // DeviceEntry, VideoCaptureController, and VideoCaptureDevice. | 1117 // DeviceEntry, VideoCaptureController, and VideoCaptureDevice. |
| 1085 DoStopDevice(entry); | 1118 DoStopDevice(entry); |
| 1086 // TODO(mcasas): use a helper function https://crbug.com/624854. | 1119 // TODO(mcasas): use a helper function https://crbug.com/624854. |
| 1087 DeviceEntries::iterator device_it = | 1120 DeviceEntries::iterator device_it = |
| (...skipping 29 matching lines...) Expand all Loading... |
| 1117 return nullptr; | 1150 return nullptr; |
| 1118 } | 1151 } |
| 1119 | 1152 |
| 1120 VideoCaptureManager::DeviceEntry* | 1153 VideoCaptureManager::DeviceEntry* |
| 1121 VideoCaptureManager::GetDeviceEntryByController( | 1154 VideoCaptureManager::GetDeviceEntryByController( |
| 1122 const VideoCaptureController* controller) const { | 1155 const VideoCaptureController* controller) const { |
| 1123 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1156 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1124 | 1157 |
| 1125 // Look up |controller| in |devices_|. | 1158 // Look up |controller| in |devices_|. |
| 1126 for (const std::unique_ptr<DeviceEntry>& device : devices_) { | 1159 for (const std::unique_ptr<DeviceEntry>& device : devices_) { |
| 1127 if (device->video_capture_controller() == controller) | 1160 if (&device->video_capture_controller == controller) |
| 1128 return device.get(); | 1161 return device.get(); |
| 1129 } | 1162 } |
| 1130 return nullptr; | 1163 return nullptr; |
| 1131 } | 1164 } |
| 1132 | 1165 |
| 1133 VideoCaptureManager::DeviceEntry* VideoCaptureManager::GetDeviceEntryBySerialId( | 1166 VideoCaptureManager::DeviceEntry* VideoCaptureManager::GetDeviceEntryBySerialId( |
| 1134 int serial_id) const { | 1167 int serial_id) const { |
| 1135 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1168 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1136 | 1169 |
| 1137 for (const std::unique_ptr<DeviceEntry>& device : devices_) { | 1170 for (const std::unique_ptr<DeviceEntry>& device : devices_) { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 1162 | 1195 |
| 1163 // Check if another session has already opened this device. If so, just | 1196 // Check if another session has already opened this device. If so, just |
| 1164 // use that opened device. | 1197 // use that opened device. |
| 1165 DeviceEntry* const existing_device = | 1198 DeviceEntry* const existing_device = |
| 1166 GetDeviceEntryByTypeAndId(device_info.type, device_info.id); | 1199 GetDeviceEntryByTypeAndId(device_info.type, device_info.id); |
| 1167 if (existing_device) { | 1200 if (existing_device) { |
| 1168 DCHECK_EQ(device_info.type, existing_device->stream_type); | 1201 DCHECK_EQ(device_info.type, existing_device->stream_type); |
| 1169 return existing_device; | 1202 return existing_device; |
| 1170 } | 1203 } |
| 1171 | 1204 |
| 1172 const int max_buffers = device_info.type == MEDIA_TAB_VIDEO_CAPTURE ? | 1205 devices_.emplace_back( |
| 1173 kMaxNumberOfBuffersForTabCapture : kMaxNumberOfBuffers; | 1206 new DeviceEntry(device_info.type, device_info.id, params)); |
| 1174 std::unique_ptr<VideoCaptureController> video_capture_controller( | 1207 return devices_.back().get(); |
| 1175 new VideoCaptureController(max_buffers)); | |
| 1176 DeviceEntry* new_device = | |
| 1177 new DeviceEntry(device_info.type, device_info.id, | |
| 1178 std::move(video_capture_controller), params); | |
| 1179 devices_.emplace_back(new_device); | |
| 1180 return new_device; | |
| 1181 } | 1208 } |
| 1182 | 1209 |
| 1183 void VideoCaptureManager::SetDesktopCaptureWindowIdOnDeviceThread( | 1210 void VideoCaptureManager::SetDesktopCaptureWindowIdOnDeviceThread( |
| 1184 media::VideoCaptureDevice* device, | 1211 media::VideoCaptureDevice* device, |
| 1185 gfx::NativeViewId window_id) { | 1212 gfx::NativeViewId window_id) { |
| 1186 DCHECK(IsOnDeviceThread()); | 1213 DCHECK(IsOnDeviceThread()); |
| 1187 #if defined(ENABLE_SCREEN_CAPTURE) && !defined(OS_ANDROID) | 1214 #if defined(ENABLE_SCREEN_CAPTURE) && !defined(OS_ANDROID) |
| 1188 DesktopCaptureDevice* desktop_device = | 1215 DesktopCaptureDevice* desktop_device = |
| 1189 static_cast<DesktopCaptureDevice*>(device); | 1216 static_cast<DesktopCaptureDevice*>(device); |
| 1190 desktop_device->SetNotificationWindowId(window_id); | 1217 desktop_device->SetNotificationWindowId(window_id); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1256 } | 1283 } |
| 1257 } | 1284 } |
| 1258 | 1285 |
| 1259 void VideoCaptureManager::ResumeDevices() { | 1286 void VideoCaptureManager::ResumeDevices() { |
| 1260 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1287 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1261 | 1288 |
| 1262 for (auto& entry : devices_) { | 1289 for (auto& entry : devices_) { |
| 1263 // Do not resume Content Video Capture devices, e.g. Tab or Screen capture. | 1290 // Do not resume Content Video Capture devices, e.g. Tab or Screen capture. |
| 1264 // Do not try to restart already running devices. | 1291 // Do not try to restart already running devices. |
| 1265 if (entry->stream_type != MEDIA_DEVICE_VIDEO_CAPTURE || | 1292 if (entry->stream_type != MEDIA_DEVICE_VIDEO_CAPTURE || |
| 1266 entry->video_capture_device()) | 1293 entry->video_capture_device) |
| 1267 continue; | 1294 continue; |
| 1268 | 1295 |
| 1269 // Check if the device is already in the start queue. | 1296 // Check if the device is already in the start queue. |
| 1270 bool device_in_queue = false; | 1297 bool device_in_queue = false; |
| 1271 for (auto& request : device_start_queue_) { | 1298 for (auto& request : device_start_queue_) { |
| 1272 if (request.serial_id() == entry->serial_id) { | 1299 if (request.serial_id() == entry->serial_id) { |
| 1273 device_in_queue = true; | 1300 device_in_queue = true; |
| 1274 break; | 1301 break; |
| 1275 } | 1302 } |
| 1276 } | 1303 } |
| 1277 | 1304 |
| 1278 if (!device_in_queue) { | 1305 if (!device_in_queue) { |
| 1279 // Session ID is only valid for Screen capture. So we can fake it to | 1306 // Session ID is only valid for Screen capture. So we can fake it to |
| 1280 // resume video capture devices here. | 1307 // resume video capture devices here. |
| 1281 QueueStartDevice(kFakeSessionId, entry.get(), entry->parameters); | 1308 QueueStartDevice(kFakeSessionId, entry.get(), entry->parameters); |
| 1282 } | 1309 } |
| 1283 } | 1310 } |
| 1284 } | 1311 } |
| 1285 #endif // defined(OS_ANDROID) | 1312 #endif // defined(OS_ANDROID) |
| 1286 | 1313 |
| 1287 } // namespace content | 1314 } // namespace content |
| OLD | NEW |