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

Side by Side Diff: services/video_capture/device_media_to_mojo_adapter.cc

Issue 2824883005: [Mojo Video Capture] Stop service when last client disconnects. (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "services/video_capture/device_media_to_mojo_adapter.h" 5 #include "services/video_capture/device_media_to_mojo_adapter.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/memory/ptr_util.h" 8 #include "base/memory/ptr_util.h"
9 #include "media/capture/video/video_capture_buffer_pool_impl.h" 9 #include "media/capture/video/video_capture_buffer_pool_impl.h"
10 #include "media/capture/video/video_capture_buffer_tracker_factory_impl.h" 10 #include "media/capture/video/video_capture_buffer_tracker_factory_impl.h"
11 #include "media/capture/video/video_capture_jpeg_decoder.h" 11 #include "media/capture/video/video_capture_jpeg_decoder.h"
12 #include "services/video_capture/receiver_mojo_to_media_adapter.h" 12 #include "services/video_capture/receiver_mojo_to_media_adapter.h"
13 13
14 namespace { 14 namespace {
15 15
16 // The maximum number of video frame buffers in-flight at any one time. 16 // The maximum number of video frame buffers in-flight at any one time.
17 // If all buffers are still in use by consumers when new frames are produced 17 // If all buffers are still in use by consumers when new frames are produced
18 // those frames get dropped. 18 // those frames get dropped.
19 static const int kMaxBufferCount = 3; 19 static const int kMaxBufferCount = 3;
20 } 20 }
21 21
22 namespace video_capture { 22 namespace video_capture {
23 23
24 DeviceMediaToMojoAdapter::DeviceMediaToMojoAdapter( 24 DeviceMediaToMojoAdapter::DeviceMediaToMojoAdapter(
25 std::unique_ptr<service_manager::ServiceContextRef> service_ref,
25 std::unique_ptr<media::VideoCaptureDevice> device, 26 std::unique_ptr<media::VideoCaptureDevice> device,
26 const media::VideoCaptureJpegDecoderFactoryCB& 27 const media::VideoCaptureJpegDecoderFactoryCB&
27 jpeg_decoder_factory_callback) 28 jpeg_decoder_factory_callback)
28 : device_(std::move(device)), 29 : service_ref_(std::move(service_ref)),
mcasas 2017/04/26 22:12:31 nit: I'm not sure we need std::move() here, since
chfremer 2017/04/26 23:33:35 Removing the std::move() gives the following error
30 device_(std::move(device)),
29 jpeg_decoder_factory_callback_(jpeg_decoder_factory_callback), 31 jpeg_decoder_factory_callback_(jpeg_decoder_factory_callback),
30 device_running_(false) {} 32 device_started_(false) {}
31 33
32 DeviceMediaToMojoAdapter::~DeviceMediaToMojoAdapter() { 34 DeviceMediaToMojoAdapter::~DeviceMediaToMojoAdapter() {
mcasas 2017/04/26 22:12:31 DCHECK(thread_checker_.CalledOnValidThread());
chfremer 2017/04/26 23:33:35 Done.
33 if (device_running_) 35 if (device_started_)
34 device_->StopAndDeAllocate(); 36 device_->StopAndDeAllocate();
35 } 37 }
36 38
37 void DeviceMediaToMojoAdapter::Start( 39 void DeviceMediaToMojoAdapter::Start(
38 const media::VideoCaptureParams& requested_settings, 40 const media::VideoCaptureParams& requested_settings,
39 mojom::ReceiverPtr receiver) { 41 mojom::ReceiverPtr receiver) {
42 DCHECK(thread_checker_.CalledOnValidThread());
40 receiver.set_connection_error_handler( 43 receiver.set_connection_error_handler(
41 base::Bind(&DeviceMediaToMojoAdapter::OnClientConnectionErrorOrClose, 44 base::Bind(&DeviceMediaToMojoAdapter::OnClientConnectionErrorOrClose,
42 base::Unretained(this))); 45 base::Unretained(this)));
43 46
44 auto media_receiver = 47 auto receiver_adapter =
45 base::MakeUnique<ReceiverMojoToMediaAdapter>(std::move(receiver)); 48 base::MakeUnique<ReceiverMojoToMediaAdapter>(std::move(receiver));
49 // We must hold on something that allows us to unsubscribe from
50 // receiver.set_connection_error_handler() when we stop the device. Otherwise,
51 // we may receive a corresponding callback after having been destroyed.
52 // This happens when the deletion of |receiver| is delayed (scheduled to a
53 // task runner) when we release |device_|, as is the case when using
54 // ReceiverOnTaskRunner.
55 receiver_adapter_ptr_ = receiver_adapter.get();
56 auto media_receiver = base::MakeUnique<ReceiverOnTaskRunner>(
57 std::move(receiver_adapter), base::ThreadTaskRunnerHandle::Get());
46 58
47 // Create a dedicated buffer pool for the device usage session. 59 // Create a dedicated buffer pool for the device usage session.
48 auto buffer_tracker_factory = 60 auto buffer_tracker_factory =
49 base::MakeUnique<media::VideoCaptureBufferTrackerFactoryImpl>(); 61 base::MakeUnique<media::VideoCaptureBufferTrackerFactoryImpl>();
50 scoped_refptr<media::VideoCaptureBufferPool> buffer_pool( 62 scoped_refptr<media::VideoCaptureBufferPool> buffer_pool(
51 new media::VideoCaptureBufferPoolImpl(std::move(buffer_tracker_factory), 63 new media::VideoCaptureBufferPoolImpl(std::move(buffer_tracker_factory),
52 max_buffer_pool_buffer_count())); 64 max_buffer_pool_buffer_count()));
53 65
54 auto device_client = base::MakeUnique<media::VideoCaptureDeviceClient>( 66 auto device_client = base::MakeUnique<media::VideoCaptureDeviceClient>(
55 std::move(media_receiver), buffer_pool, jpeg_decoder_factory_callback_); 67 std::move(media_receiver), buffer_pool, jpeg_decoder_factory_callback_);
56 68
57 device_->AllocateAndStart(requested_settings, std::move(device_client)); 69 device_->AllocateAndStart(requested_settings, std::move(device_client));
58 device_running_ = true; 70 device_started_ = true;
59 } 71 }
60 72
61 void DeviceMediaToMojoAdapter::OnReceiverReportingUtilization( 73 void DeviceMediaToMojoAdapter::OnReceiverReportingUtilization(
62 int32_t frame_feedback_id, 74 int32_t frame_feedback_id,
63 double utilization) { 75 double utilization) {
76 DCHECK(thread_checker_.CalledOnValidThread());
64 device_->OnUtilizationReport(frame_feedback_id, utilization); 77 device_->OnUtilizationReport(frame_feedback_id, utilization);
65 } 78 }
66 79
67 void DeviceMediaToMojoAdapter::Stop() { 80 void DeviceMediaToMojoAdapter::Stop() {
68 if (device_running_ == false) 81 DCHECK(thread_checker_.CalledOnValidThread());
82 if (device_started_ == false)
69 return; 83 return;
70 device_running_ = false; 84 device_started_ = false;
85 // Unsubscribe from connection error callbacks.
86 receiver_adapter_ptr_->ResetConnectionErrorHandler();
87 receiver_adapter_ptr_ = nullptr;
71 device_->StopAndDeAllocate(); 88 device_->StopAndDeAllocate();
72 } 89 }
73 90
74 void DeviceMediaToMojoAdapter::OnClientConnectionErrorOrClose() { 91 void DeviceMediaToMojoAdapter::OnClientConnectionErrorOrClose() {
92 DCHECK(thread_checker_.CalledOnValidThread());
75 Stop(); 93 Stop();
76 } 94 }
77 95
78 // static 96 // static
79 int DeviceMediaToMojoAdapter::max_buffer_pool_buffer_count() { 97 int DeviceMediaToMojoAdapter::max_buffer_pool_buffer_count() {
80 return kMaxBufferCount; 98 return kMaxBufferCount;
81 } 99 }
82 100
83 } // namespace video_capture 101 } // namespace video_capture
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698