| 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 | 
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 135   ~DeviceInfo(); | 135   ~DeviceInfo(); | 
| 136   DeviceInfo& operator=(const DeviceInfo& other); | 136   DeviceInfo& operator=(const DeviceInfo& other); | 
| 137 | 137 | 
| 138   media::VideoCaptureDeviceDescriptor descriptor; | 138   media::VideoCaptureDeviceDescriptor descriptor; | 
| 139   media::VideoCaptureFormats supported_formats; | 139   media::VideoCaptureFormats supported_formats; | 
| 140 }; | 140 }; | 
| 141 | 141 | 
| 142 // Class used for queuing request for starting a device. | 142 // Class used for queuing request for starting a device. | 
| 143 class VideoCaptureManager::CaptureDeviceStartRequest { | 143 class VideoCaptureManager::CaptureDeviceStartRequest { | 
| 144  public: | 144  public: | 
| 145   CaptureDeviceStartRequest(int serial_id, | 145   CaptureDeviceStartRequest(VideoCaptureController* controller, | 
| 146                             media::VideoCaptureSessionId session_id, | 146                             media::VideoCaptureSessionId session_id, | 
| 147                             const media::VideoCaptureParams& params); | 147                             const media::VideoCaptureParams& params); | 
| 148   int serial_id() const { return serial_id_; } | 148   VideoCaptureController* controller() const { return controller_; } | 
| 149   media::VideoCaptureSessionId session_id() const { return session_id_; } | 149   media::VideoCaptureSessionId session_id() const { return session_id_; } | 
| 150   media::VideoCaptureParams params() const { return params_; } | 150   media::VideoCaptureParams params() const { return params_; } | 
| 151 | 151 | 
| 152   // Set to true if the device should be stopped before it has successfully |  | 
| 153   // been started. |  | 
| 154   bool abort_start() const { return abort_start_; } |  | 
| 155   void set_abort_start() { abort_start_ = true; } |  | 
| 156 |  | 
| 157  private: | 152  private: | 
| 158   const int serial_id_; | 153   VideoCaptureController* const controller_; | 
| 159   const media::VideoCaptureSessionId session_id_; | 154   const media::VideoCaptureSessionId session_id_; | 
| 160   const media::VideoCaptureParams params_; | 155   const media::VideoCaptureParams params_; | 
| 161   // Set to true if the device should be stopped before it has successfully |  | 
| 162   // been started. |  | 
| 163   bool abort_start_; |  | 
| 164 }; | 156 }; | 
| 165 | 157 | 
| 166 VideoCaptureManager::DeviceInfo::DeviceInfo() = default; | 158 VideoCaptureManager::DeviceInfo::DeviceInfo() = default; | 
| 167 | 159 | 
| 168 VideoCaptureManager::DeviceInfo::DeviceInfo( | 160 VideoCaptureManager::DeviceInfo::DeviceInfo( | 
| 169     media::VideoCaptureDeviceDescriptor descriptor) | 161     media::VideoCaptureDeviceDescriptor descriptor) | 
| 170     : descriptor(descriptor) {} | 162     : descriptor(descriptor) {} | 
| 171 | 163 | 
| 172 VideoCaptureManager::DeviceInfo::DeviceInfo( | 164 VideoCaptureManager::DeviceInfo::DeviceInfo( | 
| 173     const VideoCaptureManager::DeviceInfo& other) = default; | 165     const VideoCaptureManager::DeviceInfo& other) = default; | 
| 174 | 166 | 
| 175 VideoCaptureManager::DeviceInfo::~DeviceInfo() = default; | 167 VideoCaptureManager::DeviceInfo::~DeviceInfo() = default; | 
| 176 | 168 | 
| 177 VideoCaptureManager::DeviceInfo& VideoCaptureManager::DeviceInfo::operator=( | 169 VideoCaptureManager::DeviceInfo& VideoCaptureManager::DeviceInfo::operator=( | 
| 178     const VideoCaptureManager::DeviceInfo& other) = default; | 170     const VideoCaptureManager::DeviceInfo& other) = default; | 
| 179 | 171 | 
| 180 VideoCaptureManager::CaptureDeviceStartRequest::CaptureDeviceStartRequest( | 172 VideoCaptureManager::CaptureDeviceStartRequest::CaptureDeviceStartRequest( | 
| 181     int serial_id, | 173     VideoCaptureController* controller, | 
| 182     media::VideoCaptureSessionId session_id, | 174     media::VideoCaptureSessionId session_id, | 
| 183     const media::VideoCaptureParams& params) | 175     const media::VideoCaptureParams& params) | 
| 184     : serial_id_(serial_id), | 176     : controller_(controller), session_id_(session_id), params_(params) {} | 
| 185       session_id_(session_id), |  | 
| 186       params_(params), |  | 
| 187       abort_start_(false) {} |  | 
| 188 | 177 | 
| 189 VideoCaptureManager::VideoCaptureManager( | 178 VideoCaptureManager::VideoCaptureManager( | 
| 190     std::unique_ptr<media::VideoCaptureDeviceFactory> factory, | 179     std::unique_ptr<media::VideoCaptureDeviceFactory> factory, | 
| 191     scoped_refptr<base::SingleThreadTaskRunner> device_task_runner) | 180     scoped_refptr<base::SingleThreadTaskRunner> device_task_runner) | 
| 192     : device_task_runner_(std::move(device_task_runner)), | 181     : device_task_runner_(std::move(device_task_runner)), | 
| 193       new_capture_session_id_(1), | 182       new_capture_session_id_(1), | 
| 194       video_capture_device_factory_(std::move(factory)) {} | 183       video_capture_device_factory_(std::move(factory)) {} | 
| 195 | 184 | 
| 196 VideoCaptureManager::~VideoCaptureManager() { | 185 VideoCaptureManager::~VideoCaptureManager() { | 
| 197   DCHECK(controllers_.empty()); | 186   DCHECK(controllers_.empty()); | 
| 198   DCHECK(device_start_queue_.empty()); | 187   DCHECK(device_start_request_queue_.empty()); | 
| 199 } | 188 } | 
| 200 | 189 | 
| 201 void VideoCaptureManager::AddVideoCaptureObserver( | 190 void VideoCaptureManager::AddVideoCaptureObserver( | 
| 202     media::VideoCaptureObserver* observer) { | 191     media::VideoCaptureObserver* observer) { | 
| 203   DCHECK(observer); | 192   DCHECK(observer); | 
| 204   DCHECK_CURRENTLY_ON(BrowserThread::IO); | 193   DCHECK_CURRENTLY_ON(BrowserThread::IO); | 
| 205   capture_observers_.AddObserver(observer); | 194   capture_observers_.AddObserver(observer); | 
| 206 } | 195 } | 
| 207 | 196 | 
| 208 void VideoCaptureManager::RemoveAllVideoCaptureObservers() { | 197 void VideoCaptureManager::RemoveAllVideoCaptureObservers() { | 
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 285 void VideoCaptureManager::Close(int capture_session_id) { | 274 void VideoCaptureManager::Close(int capture_session_id) { | 
| 286   DCHECK_CURRENTLY_ON(BrowserThread::IO); | 275   DCHECK_CURRENTLY_ON(BrowserThread::IO); | 
| 287   DVLOG(1) << "VideoCaptureManager::Close, id " << capture_session_id; | 276   DVLOG(1) << "VideoCaptureManager::Close, id " << capture_session_id; | 
| 288 | 277 | 
| 289   SessionMap::iterator session_it = sessions_.find(capture_session_id); | 278   SessionMap::iterator session_it = sessions_.find(capture_session_id); | 
| 290   if (session_it == sessions_.end()) { | 279   if (session_it == sessions_.end()) { | 
| 291     NOTREACHED(); | 280     NOTREACHED(); | 
| 292     return; | 281     return; | 
| 293   } | 282   } | 
| 294 | 283 | 
| 295   VideoCaptureController* const existing_device = LookupControllerByTypeAndId( | 284   VideoCaptureController* const existing_device = | 
| 296       session_it->second.type, session_it->second.id); | 285       LookupControllerByMediaTypeAndDeviceId(session_it->second.type, | 
|  | 286                                              session_it->second.id); | 
| 297   if (existing_device) { | 287   if (existing_device) { | 
| 298     // Remove any client that is still using the session. This is safe to call | 288     // Remove any client that is still using the session. This is safe to call | 
| 299     // even if there are no clients using the session. | 289     // even if there are no clients using the session. | 
| 300     existing_device->StopSession(capture_session_id); | 290     existing_device->StopSession(capture_session_id); | 
| 301 | 291 | 
| 302     // StopSession() may have removed the last client, so we might need to | 292     // StopSession() may have removed the last client, so we might need to | 
| 303     // close the device. | 293     // close the device. | 
| 304     DestroyControllerIfNoClients(existing_device); | 294     DestroyControllerIfNoClients(existing_device); | 
| 305   } | 295   } | 
| 306 | 296 | 
| 307   // Notify listeners asynchronously, and forget the session. | 297   // Notify listeners asynchronously, and forget the session. | 
| 308   base::ThreadTaskRunnerHandle::Get()->PostTask( | 298   base::ThreadTaskRunnerHandle::Get()->PostTask( | 
| 309       FROM_HERE, base::Bind(&VideoCaptureManager::OnClosed, this, | 299       FROM_HERE, base::Bind(&VideoCaptureManager::OnClosed, this, | 
| 310                             session_it->second.type, capture_session_id)); | 300                             session_it->second.type, capture_session_id)); | 
| 311   sessions_.erase(session_it); | 301   sessions_.erase(session_it); | 
| 312 } | 302 } | 
| 313 | 303 | 
| 314 void VideoCaptureManager::QueueStartDevice( | 304 void VideoCaptureManager::QueueStartDevice( | 
| 315     media::VideoCaptureSessionId session_id, | 305     media::VideoCaptureSessionId session_id, | 
| 316     VideoCaptureController* controller, | 306     VideoCaptureController* controller, | 
| 317     const media::VideoCaptureParams& params) { | 307     const media::VideoCaptureParams& params) { | 
| 318   DCHECK_CURRENTLY_ON(BrowserThread::IO); | 308   DCHECK_CURRENTLY_ON(BrowserThread::IO); | 
| 319   device_start_queue_.push_back( | 309   device_start_request_queue_.push_back( | 
| 320       CaptureDeviceStartRequest(controller->serial_id(), session_id, params)); | 310       CaptureDeviceStartRequest(controller, session_id, params)); | 
| 321   if (device_start_queue_.size() == 1) | 311   if (device_start_request_queue_.size() == 1) | 
| 322     HandleQueuedStartRequest(); | 312     ProcessDeviceStartRequestQueue(); | 
| 323 } | 313 } | 
| 324 | 314 | 
| 325 void VideoCaptureManager::DoStopDevice(VideoCaptureController* controller) { | 315 void VideoCaptureManager::DoStopDevice(VideoCaptureController* controller) { | 
| 326   DCHECK_CURRENTLY_ON(BrowserThread::IO); | 316   DCHECK_CURRENTLY_ON(BrowserThread::IO); | 
| 327   // TODO(mcasas): use a helper function https://crbug.com/624854. | 317   // TODO(mcasas): use a helper function https://crbug.com/624854. | 
| 328   DCHECK(std::find_if( | 318   DCHECK(std::find_if( | 
| 329              controllers_.begin(), controllers_.end(), | 319              controllers_.begin(), controllers_.end(), | 
| 330              [controller]( | 320              [controller]( | 
| 331                  const scoped_refptr<VideoCaptureController>& device_entry) { | 321                  const scoped_refptr<VideoCaptureController>& device_entry) { | 
| 332                return device_entry.get() == controller; | 322                return device_entry.get() == controller; | 
| 333              }) != controllers_.end()); | 323              }) != controllers_.end()); | 
| 334 | 324 | 
| 335   // Find the matching start request. | 325   // If start request has not yet started processing, i.e. if it is not at the | 
| 336   for (DeviceStartQueue::reverse_iterator request = | 326   // beginning of the queue, remove it from the queue. | 
| 337            device_start_queue_.rbegin(); | 327   auto request_iter = device_start_request_queue_.begin(); | 
| 338        request != device_start_queue_.rend(); ++request) { | 328   if (request_iter != device_start_request_queue_.end()) { | 
| 339     if (request->serial_id() == controller->serial_id()) { | 329     request_iter = | 
| 340       request->set_abort_start(); | 330         std::find_if(++request_iter, device_start_request_queue_.end(), | 
| 341       DVLOG(3) << "DoStopDevice, aborting start request for device " | 331                      [controller](const CaptureDeviceStartRequest& request) { | 
| 342                << controller->device_id() | 332                        return request.controller() == controller; | 
| 343                << " serial_id = " << controller->serial_id(); | 333                      }); | 
|  | 334     if (request_iter != device_start_request_queue_.end()) { | 
|  | 335       device_start_request_queue_.erase(request_iter); | 
| 344       return; | 336       return; | 
| 345     } | 337     } | 
| 346   } | 338   } | 
| 347 | 339 | 
| 348   const DeviceInfo* device_info = GetDeviceInfoById(controller->device_id()); | 340   const DeviceInfo* device_info = GetDeviceInfoById(controller->device_id()); | 
| 349   if (device_info != nullptr) { | 341   if (device_info != nullptr) { | 
| 350     for (auto& observer : capture_observers_) | 342     for (auto& observer : capture_observers_) | 
| 351       observer.OnVideoCaptureStopped(device_info->descriptor.facing); | 343       observer.OnVideoCaptureStopped(device_info->descriptor.facing); | 
| 352   } | 344   } | 
| 353 | 345 | 
| 354   DVLOG(3) << "DoStopDevice. Send stop request for device = " | 346   DVLOG(3) << "DoStopDevice. Send stop request for device = " | 
| 355            << controller->device_id() | 347            << controller->device_id() | 
| 356            << " serial_id = " << controller->serial_id() << "."; | 348            << " serial_id = " << controller->serial_id() << "."; | 
| 357   controller->OnLog(base::StringPrintf("Stopping device: id: %s", | 349   controller->OnLog(base::StringPrintf("Stopping device: id: %s", | 
| 358                                        controller->device_id().c_str())); | 350                                        controller->device_id().c_str())); | 
| 359 | 351 | 
| 360   if (controller->IsDeviceAlive()) { | 352   // Since we may be removing |controller| from |controllers_| while | 
| 361     // Since we may be removing |controller| from |controllers_| while | 353   // ReleaseDeviceAsnyc() is executing, we pass it shared ownership to | 
| 362     // ReleaseDeviceAsnyc() is executing, we pass it shared ownership to | 354   // |controller|. | 
| 363     // |controller|. | 355   controller->ReleaseDeviceAsync( | 
| 364     controller->ReleaseDeviceAsync(base::Bind( | 356       base::Bind([](scoped_refptr<VideoCaptureController>) {}, | 
| 365         [](scoped_refptr<VideoCaptureController>) {}, | 357                  GetControllerSharedRef(controller))); | 
| 366         GetControllerSharedRefFromSerialId(controller->serial_id()))); |  | 
| 367   } |  | 
| 368 } | 358 } | 
| 369 | 359 | 
| 370 void VideoCaptureManager::HandleQueuedStartRequest() { | 360 void VideoCaptureManager::ProcessDeviceStartRequestQueue() { | 
| 371   DCHECK_CURRENTLY_ON(BrowserThread::IO); | 361   DCHECK_CURRENTLY_ON(BrowserThread::IO); | 
| 372   // Remove all start requests that have been aborted. | 362   DeviceStartQueue::iterator request = device_start_request_queue_.begin(); | 
| 373   while (device_start_queue_.begin() != device_start_queue_.end() && | 363   if (request == device_start_request_queue_.end()) | 
| 374          device_start_queue_.begin()->abort_start()) { |  | 
| 375     device_start_queue_.pop_front(); |  | 
| 376   } |  | 
| 377   DeviceStartQueue::iterator request = device_start_queue_.begin(); |  | 
| 378   if (request == device_start_queue_.end()) |  | 
| 379     return; | 364     return; | 
| 380 | 365 | 
| 381   const int serial_id = request->serial_id(); | 366   VideoCaptureController* const controller = request->controller(); | 
| 382   VideoCaptureController* const controller = |  | 
| 383       LookupControllerBySerialId(serial_id); |  | 
| 384   DCHECK(controller); |  | 
| 385 | 367 | 
| 386   DVLOG(3) << "HandleQueuedStartRequest, Post start to device thread, device = " | 368   DVLOG(3) << "HandleQueuedStartRequest, Post start to device thread, device = " | 
| 387            << controller->device_id() | 369            << controller->device_id() | 
| 388            << " start id = " << controller->serial_id(); | 370            << " start id = " << controller->serial_id(); | 
| 389 | 371 | 
| 390   // The method CreateAndStartDeviceAsync() is going to run asynchronously. | 372   // The method CreateAndStartDeviceAsync() is going to run asynchronously. | 
| 391   // Since we may be removing the controller while it is executing, we need to | 373   // Since we may be removing the controller while it is executing, we need to | 
| 392   // pass it shared ownership to itself so that it stays alive while executing. | 374   // pass it shared ownership to itself so that it stays alive while executing. | 
| 393   // And since the execution may make callbacks into |this|, we also need | 375   // And since the execution may make callbacks into |this|, we also need | 
| 394   // to pass it shared ownership to |this|. | 376   // to pass it shared ownership to |this|. | 
| 395   // TODO(chfremer): Check if request->params() can actually be different from | 377   // TODO(chfremer): Check if request->params() can actually be different from | 
| 396   // controller->parameters, and simplify if this is not the case. | 378   // controller->parameters, and simplify if this is not the case. | 
| 397   controller->CreateAndStartDeviceAsync( | 379   controller->CreateAndStartDeviceAsync( | 
| 398       request->params(), | 380       request->params(), | 
| 399       static_cast<BuildableVideoCaptureDevice::Callbacks*>(this), | 381       static_cast<BuildableVideoCaptureDevice::Callbacks*>(this), | 
| 400       base::Bind([](scoped_refptr<VideoCaptureManager>, | 382       base::Bind([](scoped_refptr<VideoCaptureManager>, | 
| 401                     scoped_refptr<VideoCaptureController>) {}, | 383                     scoped_refptr<VideoCaptureController>) {}, | 
| 402                  scoped_refptr<VideoCaptureManager>(this), | 384                  scoped_refptr<VideoCaptureManager>(this), | 
| 403                  GetControllerSharedRefFromSerialId(serial_id))); | 385                  GetControllerSharedRef(controller))); | 
| 404 } | 386 } | 
| 405 | 387 | 
| 406 void VideoCaptureManager::WillStartDevice(media::VideoFacingMode facing_mode) { | 388 void VideoCaptureManager::WillStartDevice(media::VideoFacingMode facing_mode) { | 
| 407   for (auto& observer : capture_observers_) | 389   for (auto& observer : capture_observers_) | 
| 408     observer.OnVideoCaptureStarted(facing_mode); | 390     observer.OnVideoCaptureStarted(facing_mode); | 
| 409 } | 391 } | 
| 410 | 392 | 
| 411 void VideoCaptureManager::DidStartDevice(VideoCaptureController* controller) { | 393 void VideoCaptureManager::DidStartDevice(VideoCaptureController* controller) { | 
| 412   DVLOG(3) << __func__; | 394   DVLOG(3) << __func__; | 
| 413   DCHECK_CURRENTLY_ON(BrowserThread::IO); | 395   DCHECK_CURRENTLY_ON(BrowserThread::IO); | 
|  | 396   DCHECK(!device_start_request_queue_.empty()); | 
|  | 397   DCHECK_EQ(controller, device_start_request_queue_.begin()->controller()); | 
| 414   DCHECK(controller); | 398   DCHECK(controller); | 
| 415   DCHECK_EQ(controller->serial_id(), device_start_queue_.begin()->serial_id()); | 399   if (controller->stream_type() == MEDIA_DESKTOP_VIDEO_CAPTURE) { | 
| 416   if (device_start_queue_.front().abort_start()) { | 400     const media::VideoCaptureSessionId session_id = | 
| 417     // A request to release the device may have arrived during the asynchronous | 401         device_start_request_queue_.front().session_id(); | 
| 418     // device startup. | 402     DCHECK(session_id != kFakeSessionId); | 
| 419     DVLOG(3) << "Device release request has been issued while device was " | 403     MaybePostDesktopCaptureWindowId(session_id); | 
| 420              << "starting up asynchronously."; | 404   } | 
| 421     controller->ReleaseDeviceAsync(base::Bind( |  | 
| 422         [](scoped_refptr<VideoCaptureController>) {}, |  | 
| 423         GetControllerSharedRefFromSerialId(controller->serial_id()))); |  | 
| 424   } else { |  | 
| 425     if (controller->stream_type() == MEDIA_DESKTOP_VIDEO_CAPTURE) { |  | 
| 426       const media::VideoCaptureSessionId session_id = |  | 
| 427           device_start_queue_.front().session_id(); |  | 
| 428       DCHECK(session_id != kFakeSessionId); |  | 
| 429       MaybePostDesktopCaptureWindowId(session_id); |  | 
| 430     } |  | 
| 431 | 405 | 
| 432     auto it = photo_request_queue_.begin(); | 406   auto it = photo_request_queue_.begin(); | 
| 433     while (it != photo_request_queue_.end()) { | 407   while (it != photo_request_queue_.end()) { | 
| 434       auto request = it++; | 408     auto request = it++; | 
| 435       VideoCaptureController* maybe_entry = | 409     VideoCaptureController* maybe_entry = | 
| 436           LookupControllerBySessionId(request->first); | 410         LookupControllerBySessionId(request->first); | 
| 437       if (maybe_entry && maybe_entry->IsDeviceAlive()) { | 411     if (maybe_entry && maybe_entry->IsDeviceAlive()) { | 
| 438         request->second.Run(); | 412       request->second.Run(); | 
| 439         photo_request_queue_.erase(request); | 413       photo_request_queue_.erase(request); | 
| 440       } |  | 
| 441     } | 414     } | 
| 442   } | 415   } | 
| 443 | 416 | 
| 444   device_start_queue_.pop_front(); | 417   device_start_request_queue_.pop_front(); | 
| 445   HandleQueuedStartRequest(); | 418   ProcessDeviceStartRequestQueue(); | 
| 446 } | 419 } | 
| 447 | 420 | 
| 448 void VideoCaptureManager::OnDeviceStartFailed( | 421 void VideoCaptureManager::OnDeviceStartFailed( | 
| 449     VideoCaptureController* controller) { | 422     VideoCaptureController* controller) { | 
| 450   const std::string log_message = base::StringPrintf( | 423   const std::string log_message = base::StringPrintf( | 
| 451       "Starting device %s has failed. Maybe recently disconnected?", | 424       "Starting device %s has failed. Maybe recently disconnected?", | 
| 452       controller->device_id().c_str()); | 425       controller->device_id().c_str()); | 
| 453   DLOG(ERROR) << log_message; | 426   DLOG(ERROR) << log_message; | 
| 454   controller->OnLog(log_message); | 427   controller->OnLog(log_message); | 
| 455   controller->OnError(); | 428   controller->OnError(); | 
| 456 | 429 | 
| 457   device_start_queue_.pop_front(); | 430   device_start_request_queue_.pop_front(); | 
| 458   HandleQueuedStartRequest(); | 431   ProcessDeviceStartRequestQueue(); | 
| 459 } | 432 } | 
| 460 | 433 | 
| 461 void VideoCaptureManager::StartCaptureForClient( | 434 void VideoCaptureManager::OnDeviceStartAborted() { | 
|  | 435   device_start_request_queue_.pop_front(); | 
|  | 436   ProcessDeviceStartRequestQueue(); | 
|  | 437 } | 
|  | 438 | 
|  | 439 void VideoCaptureManager::ConnectClient( | 
| 462     media::VideoCaptureSessionId session_id, | 440     media::VideoCaptureSessionId session_id, | 
| 463     const media::VideoCaptureParams& params, | 441     const media::VideoCaptureParams& params, | 
| 464     VideoCaptureControllerID client_id, | 442     VideoCaptureControllerID client_id, | 
| 465     VideoCaptureControllerEventHandler* client_handler, | 443     VideoCaptureControllerEventHandler* client_handler, | 
| 466     const DoneCB& done_cb) { | 444     const DoneCB& done_cb) { | 
| 467   DCHECK_CURRENTLY_ON(BrowserThread::IO); | 445   DCHECK_CURRENTLY_ON(BrowserThread::IO); | 
| 468   DVLOG(1) << __func__ << ", session_id = " << session_id << ", request: " | 446   DVLOG(1) << __func__ << ", session_id = " << session_id << ", request: " | 
| 469            << media::VideoCaptureFormat::ToString(params.requested_format); | 447            << media::VideoCaptureFormat::ToString(params.requested_format); | 
| 470 | 448 | 
| 471   VideoCaptureController* controller = | 449   VideoCaptureController* controller = | 
| 472       GetOrCreateController(session_id, params); | 450       GetOrCreateController(session_id, params); | 
| 473   if (!controller) { | 451   if (!controller) { | 
| 474     done_cb.Run(base::WeakPtr<VideoCaptureController>()); | 452     done_cb.Run(base::WeakPtr<VideoCaptureController>()); | 
| 475     return; | 453     return; | 
| 476   } | 454   } | 
| 477 | 455 | 
| 478   LogVideoCaptureEvent(VIDEO_CAPTURE_START_CAPTURE); | 456   LogVideoCaptureEvent(VIDEO_CAPTURE_START_CAPTURE); | 
| 479 | 457 | 
| 480   // First client starts the device. | 458   // First client starts the device. | 
| 481   if (!controller->HasActiveClient() && !controller->HasPausedClient()) { | 459   if (!controller->HasActiveClient() && !controller->HasPausedClient()) { | 
| 482     DVLOG(1) << "VideoCaptureManager starting device (id = " | 460     DVLOG(1) << "VideoCaptureManager starting device (id = " | 
| 483              << controller->device_id() << ")"; | 461              << controller->device_id() << ")"; | 
| 484     QueueStartDevice(session_id, controller, params); | 462     QueueStartDevice(session_id, controller, params); | 
| 485   } | 463   } | 
| 486   // Run the callback first, as AddClient() may trigger OnFrameInfo(). | 464   // Run the callback first, as AddClient() may trigger OnFrameInfo(). | 
| 487   done_cb.Run(controller->GetWeakPtrForIOThread()); | 465   done_cb.Run(controller->GetWeakPtrForIOThread()); | 
| 488   controller->AddClient(client_id, client_handler, session_id, params); | 466   controller->AddClient(client_id, client_handler, session_id, params); | 
| 489 } | 467 } | 
| 490 | 468 | 
| 491 void VideoCaptureManager::StopCaptureForClient( | 469 void VideoCaptureManager::DisconnectClient( | 
| 492     VideoCaptureController* controller, | 470     VideoCaptureController* controller, | 
| 493     VideoCaptureControllerID client_id, | 471     VideoCaptureControllerID client_id, | 
| 494     VideoCaptureControllerEventHandler* client_handler, | 472     VideoCaptureControllerEventHandler* client_handler, | 
| 495     bool aborted_due_to_error) { | 473     bool aborted_due_to_error) { | 
| 496   DCHECK_CURRENTLY_ON(BrowserThread::IO); | 474   DCHECK_CURRENTLY_ON(BrowserThread::IO); | 
| 497   DCHECK(controller); | 475   DCHECK(controller); | 
| 498   DCHECK(client_handler); | 476   DCHECK(client_handler); | 
| 499 | 477 | 
| 500   if (!IsControllerPointerValid(controller)) { | 478   if (!IsControllerPointerValid(controller)) { | 
| 501     NOTREACHED(); | 479     NOTREACHED(); | 
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 629 } | 607 } | 
| 630 | 608 | 
| 631 bool VideoCaptureManager::GetDeviceFormatsInUse( | 609 bool VideoCaptureManager::GetDeviceFormatsInUse( | 
| 632     MediaStreamType stream_type, | 610     MediaStreamType stream_type, | 
| 633     const std::string& device_id, | 611     const std::string& device_id, | 
| 634     media::VideoCaptureFormats* formats_in_use) { | 612     media::VideoCaptureFormats* formats_in_use) { | 
| 635   DCHECK_CURRENTLY_ON(BrowserThread::IO); | 613   DCHECK_CURRENTLY_ON(BrowserThread::IO); | 
| 636   DCHECK(formats_in_use->empty()); | 614   DCHECK(formats_in_use->empty()); | 
| 637   // Return the currently in-use format(s) of the device, if it's started. | 615   // Return the currently in-use format(s) of the device, if it's started. | 
| 638   VideoCaptureController* device_in_use = | 616   VideoCaptureController* device_in_use = | 
| 639       LookupControllerByTypeAndId(stream_type, device_id); | 617       LookupControllerByMediaTypeAndDeviceId(stream_type, device_id); | 
| 640   if (device_in_use) { | 618   if (device_in_use) { | 
| 641     // Currently only one format-in-use is supported at the VCC level. | 619     // Currently only one format-in-use is supported at the VCC level. | 
| 642     formats_in_use->push_back(device_in_use->GetVideoCaptureFormat()); | 620     formats_in_use->push_back(device_in_use->GetVideoCaptureFormat()); | 
| 643   } | 621   } | 
| 644   return true; | 622   return true; | 
| 645 } | 623 } | 
| 646 | 624 | 
| 647 void VideoCaptureManager::SetDesktopCaptureWindowId( | 625 void VideoCaptureManager::SetDesktopCaptureWindowId( | 
| 648     media::VideoCaptureSessionId session_id, | 626     media::VideoCaptureSessionId session_id, | 
| 649     gfx::NativeViewId window_id) { | 627     gfx::NativeViewId window_id) { | 
| 650   DCHECK_CURRENTLY_ON(BrowserThread::IO); | 628   DCHECK_CURRENTLY_ON(BrowserThread::IO); | 
| 651   VLOG(2) << "SetDesktopCaptureWindowId called for session " << session_id; | 629   VLOG(2) << "SetDesktopCaptureWindowId called for session " << session_id; | 
| 652 | 630 | 
| 653   notification_window_ids_[session_id] = window_id; | 631   notification_window_ids_[session_id] = window_id; | 
| 654   MaybePostDesktopCaptureWindowId(session_id); | 632   MaybePostDesktopCaptureWindowId(session_id); | 
| 655 } | 633 } | 
| 656 | 634 | 
| 657 void VideoCaptureManager::MaybePostDesktopCaptureWindowId( | 635 void VideoCaptureManager::MaybePostDesktopCaptureWindowId( | 
| 658     media::VideoCaptureSessionId session_id) { | 636     media::VideoCaptureSessionId session_id) { | 
| 659   SessionMap::iterator session_it = sessions_.find(session_id); | 637   SessionMap::iterator session_it = sessions_.find(session_id); | 
| 660   if (session_it == sessions_.end()) | 638   if (session_it == sessions_.end()) | 
| 661     return; | 639     return; | 
| 662 | 640 | 
| 663   VideoCaptureController* const existing_device = LookupControllerByTypeAndId( | 641   VideoCaptureController* const existing_device = | 
| 664       session_it->second.type, session_it->second.id); | 642       LookupControllerByMediaTypeAndDeviceId(session_it->second.type, | 
|  | 643                                              session_it->second.id); | 
| 665   if (!existing_device) { | 644   if (!existing_device) { | 
| 666     DVLOG(2) << "Failed to find an existing screen capture device."; | 645     DVLOG(2) << "Failed to find an existing screen capture device."; | 
| 667     return; | 646     return; | 
| 668   } | 647   } | 
| 669 | 648 | 
| 670   if (!existing_device->IsDeviceAlive()) { | 649   if (!existing_device->IsDeviceAlive()) { | 
| 671     DVLOG(2) << "Screen capture device not yet started."; | 650     DVLOG(2) << "Screen capture device not yet started."; | 
| 672     return; | 651     return; | 
| 673   } | 652   } | 
| 674 | 653 | 
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 851   } | 830   } | 
| 852 } | 831 } | 
| 853 | 832 | 
| 854 VideoCaptureController* VideoCaptureManager::LookupControllerBySessionId( | 833 VideoCaptureController* VideoCaptureManager::LookupControllerBySessionId( | 
| 855     int session_id) { | 834     int session_id) { | 
| 856   DCHECK_CURRENTLY_ON(BrowserThread::IO); | 835   DCHECK_CURRENTLY_ON(BrowserThread::IO); | 
| 857   SessionMap::const_iterator session_it = sessions_.find(session_id); | 836   SessionMap::const_iterator session_it = sessions_.find(session_id); | 
| 858   if (session_it == sessions_.end()) | 837   if (session_it == sessions_.end()) | 
| 859     return nullptr; | 838     return nullptr; | 
| 860 | 839 | 
| 861   return LookupControllerByTypeAndId(session_it->second.type, | 840   return LookupControllerByMediaTypeAndDeviceId(session_it->second.type, | 
| 862                                      session_it->second.id); | 841                                                 session_it->second.id); | 
| 863 } | 842 } | 
| 864 | 843 | 
| 865 VideoCaptureController* VideoCaptureManager::LookupControllerByTypeAndId( | 844 VideoCaptureController* | 
|  | 845 VideoCaptureManager::LookupControllerByMediaTypeAndDeviceId( | 
| 866     MediaStreamType type, | 846     MediaStreamType type, | 
| 867     const std::string& device_id) const { | 847     const std::string& device_id) const { | 
| 868   DCHECK_CURRENTLY_ON(BrowserThread::IO); | 848   DCHECK_CURRENTLY_ON(BrowserThread::IO); | 
| 869 | 849 | 
| 870   for (const auto& entry : controllers_) { | 850   for (const auto& entry : controllers_) { | 
| 871     if (type == entry->stream_type() && device_id == entry->device_id()) | 851     if (type == entry->stream_type() && device_id == entry->device_id()) | 
| 872       return entry.get(); | 852       return entry.get(); | 
| 873   } | 853   } | 
| 874   return nullptr; | 854   return nullptr; | 
| 875 } | 855 } | 
| 876 | 856 | 
| 877 bool VideoCaptureManager::IsControllerPointerValid( | 857 bool VideoCaptureManager::IsControllerPointerValid( | 
| 878     const VideoCaptureController* controller) const { | 858     const VideoCaptureController* controller) const { | 
| 879   DCHECK_CURRENTLY_ON(BrowserThread::IO); | 859   DCHECK_CURRENTLY_ON(BrowserThread::IO); | 
| 880   return base::ContainsValue(controllers_, controller); | 860   return base::ContainsValue(controllers_, controller); | 
| 881 } | 861 } | 
| 882 | 862 | 
| 883 VideoCaptureController* VideoCaptureManager::LookupControllerBySerialId( | 863 scoped_refptr<VideoCaptureController> | 
| 884     int serial_id) const { | 864 VideoCaptureManager::GetControllerSharedRef( | 
|  | 865     VideoCaptureController* controller) const { | 
| 885   DCHECK_CURRENTLY_ON(BrowserThread::IO); | 866   DCHECK_CURRENTLY_ON(BrowserThread::IO); | 
| 886 | 867 | 
| 887   for (const auto& entry : controllers_) { | 868   for (const auto& entry : controllers_) { | 
| 888     if (entry->serial_id() == serial_id) | 869     if (entry.get() == controller) | 
| 889       return entry.get(); |  | 
| 890   } |  | 
| 891   return nullptr; |  | 
| 892 } |  | 
| 893 |  | 
| 894 scoped_refptr<VideoCaptureController> |  | 
| 895 VideoCaptureManager::GetControllerSharedRefFromSerialId(int serial_id) const { |  | 
| 896   DCHECK_CURRENTLY_ON(BrowserThread::IO); |  | 
| 897 |  | 
| 898   for (const auto& entry : controllers_) { |  | 
| 899     if (entry->serial_id() == serial_id) |  | 
| 900       return entry; | 870       return entry; | 
| 901   } | 871   } | 
| 902   return nullptr; | 872   return nullptr; | 
| 903 } | 873 } | 
| 904 | 874 | 
| 905 VideoCaptureManager::DeviceInfo* VideoCaptureManager::GetDeviceInfoById( | 875 VideoCaptureManager::DeviceInfo* VideoCaptureManager::GetDeviceInfoById( | 
| 906     const std::string& id) { | 876     const std::string& id) { | 
| 907   for (auto& it : devices_info_cache_) { | 877   for (auto& it : devices_info_cache_) { | 
| 908     if (it.descriptor.device_id == id) | 878     if (it.descriptor.device_id == id) | 
| 909       return ⁢ | 879       return ⁢ | 
| 910   } | 880   } | 
| 911   return nullptr; | 881   return nullptr; | 
| 912 } | 882 } | 
| 913 | 883 | 
| 914 VideoCaptureController* VideoCaptureManager::GetOrCreateController( | 884 VideoCaptureController* VideoCaptureManager::GetOrCreateController( | 
| 915     media::VideoCaptureSessionId capture_session_id, | 885     media::VideoCaptureSessionId capture_session_id, | 
| 916     const media::VideoCaptureParams& params) { | 886     const media::VideoCaptureParams& params) { | 
| 917   DCHECK_CURRENTLY_ON(BrowserThread::IO); | 887   DCHECK_CURRENTLY_ON(BrowserThread::IO); | 
| 918 | 888 | 
| 919   SessionMap::iterator session_it = sessions_.find(capture_session_id); | 889   SessionMap::iterator session_it = sessions_.find(capture_session_id); | 
| 920   if (session_it == sessions_.end()) | 890   if (session_it == sessions_.end()) | 
| 921     return nullptr; | 891     return nullptr; | 
| 922   const MediaStreamDevice& device_info = session_it->second; | 892   const MediaStreamDevice& device_info = session_it->second; | 
| 923 | 893 | 
| 924   // Check if another session has already opened this device. If so, just | 894   // Check if another session has already opened this device. If so, just | 
| 925   // use that opened device. | 895   // use that opened device. | 
| 926   VideoCaptureController* const existing_device = | 896   VideoCaptureController* const existing_device = | 
| 927       LookupControllerByTypeAndId(device_info.type, device_info.id); | 897       LookupControllerByMediaTypeAndDeviceId(device_info.type, device_info.id); | 
| 928   if (existing_device) { | 898   if (existing_device) { | 
| 929     DCHECK_EQ(device_info.type, existing_device->stream_type()); | 899     DCHECK_EQ(device_info.type, existing_device->stream_type()); | 
| 930     return existing_device; | 900     return existing_device; | 
| 931   } | 901   } | 
| 932 | 902 | 
| 933   VideoCaptureController* new_device_entry = new VideoCaptureController( | 903   VideoCaptureController* new_controller = new VideoCaptureController( | 
| 934       device_info.id, device_info.type, params, | 904       device_info.id, device_info.type, params, | 
| 935       base::MakeUnique<InProcessBuildableVideoCaptureDevice>( | 905       base::MakeUnique<InProcessBuildableVideoCaptureDevice>( | 
| 936           device_task_runner_, video_capture_device_factory_.get())); | 906           device_task_runner_, video_capture_device_factory_.get())); | 
| 937   controllers_.emplace_back(new_device_entry); | 907   controllers_.emplace_back(new_controller); | 
| 938   return new_device_entry; | 908   return new_controller; | 
| 939 } | 909 } | 
| 940 | 910 | 
| 941 base::Optional<CameraCalibration> VideoCaptureManager::GetCameraCalibration( | 911 base::Optional<CameraCalibration> VideoCaptureManager::GetCameraCalibration( | 
| 942     const std::string& device_id) { | 912     const std::string& device_id) { | 
| 943   VideoCaptureManager::DeviceInfo* info = GetDeviceInfoById(device_id); | 913   VideoCaptureManager::DeviceInfo* info = GetDeviceInfoById(device_id); | 
| 944   if (!info) | 914   if (!info) | 
| 945     return base::Optional<CameraCalibration>(); | 915     return base::Optional<CameraCalibration>(); | 
| 946   return info->descriptor.camera_calibration; | 916   return info->descriptor.camera_calibration; | 
| 947 } | 917 } | 
| 948 | 918 | 
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 980 | 950 | 
| 981   for (auto& controller : controllers_) { | 951   for (auto& controller : controllers_) { | 
| 982     // Do not resume Content Video Capture devices, e.g. Tab or Screen capture. | 952     // Do not resume Content Video Capture devices, e.g. Tab or Screen capture. | 
| 983     // Do not try to restart already running devices. | 953     // Do not try to restart already running devices. | 
| 984     if (controller->stream_type() != MEDIA_DEVICE_VIDEO_CAPTURE || | 954     if (controller->stream_type() != MEDIA_DEVICE_VIDEO_CAPTURE || | 
| 985         controller->IsDeviceAlive()) | 955         controller->IsDeviceAlive()) | 
| 986       continue; | 956       continue; | 
| 987 | 957 | 
| 988     // Check if the device is already in the start queue. | 958     // Check if the device is already in the start queue. | 
| 989     bool device_in_queue = false; | 959     bool device_in_queue = false; | 
| 990     for (auto& request : device_start_queue_) { | 960     for (auto& request : device_start_request_queue_) { | 
| 991       if (request.serial_id() == controller->serial_id()) { | 961       if (request.controller() == controller.get()) { | 
| 992         device_in_queue = true; | 962         device_in_queue = true; | 
| 993         break; | 963         break; | 
| 994       } | 964       } | 
| 995     } | 965     } | 
| 996 | 966 | 
| 997     if (!device_in_queue) { | 967     if (!device_in_queue) { | 
| 998       // Session ID is only valid for Screen capture. So we can fake it to | 968       // Session ID is only valid for Screen capture. So we can fake it to | 
| 999       // resume video capture devices here. | 969       // resume video capture devices here. | 
| 1000       QueueStartDevice(kFakeSessionId, controller.get(), | 970       QueueStartDevice(kFakeSessionId, controller.get(), | 
| 1001                        controller->parameters()); | 971                        controller->parameters()); | 
| 1002     } | 972     } | 
| 1003   } | 973   } | 
| 1004 } | 974 } | 
| 1005 #endif  // defined(OS_ANDROID) | 975 #endif  // defined(OS_ANDROID) | 
| 1006 | 976 | 
| 1007 }  // namespace content | 977 }  // namespace content | 
| OLD | NEW | 
|---|