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

Side by Side Diff: media/capture/video/linux/video_capture_device_linux.cc

Issue 2815643002: Make V4L2CaptureDelegate non-ref-counted (Closed)
Patch Set: Added weak pointer to V4L2CaptureDelegate 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 (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 "media/capture/video/linux/video_capture_device_linux.h" 5 #include "media/capture/video/linux/video_capture_device_linux.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <list> 9 #include <list>
10 10
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 void VideoCaptureDeviceLinux::AllocateAndStart( 50 void VideoCaptureDeviceLinux::AllocateAndStart(
51 const VideoCaptureParams& params, 51 const VideoCaptureParams& params,
52 std::unique_ptr<VideoCaptureDevice::Client> client) { 52 std::unique_ptr<VideoCaptureDevice::Client> client) {
53 DCHECK(!capture_impl_); 53 DCHECK(!capture_impl_);
54 if (v4l2_thread_.IsRunning()) 54 if (v4l2_thread_.IsRunning())
55 return; // Wrong state. 55 return; // Wrong state.
56 v4l2_thread_.Start(); 56 v4l2_thread_.Start();
57 57
58 const int line_frequency = 58 const int line_frequency =
59 TranslatePowerLineFrequencyToV4L2(GetPowerLineFrequency(params)); 59 TranslatePowerLineFrequencyToV4L2(GetPowerLineFrequency(params));
60 capture_impl_ = new V4L2CaptureDelegate( 60 capture_impl_ = base::MakeUnique<V4L2CaptureDelegate>(
61 device_descriptor_, v4l2_thread_.task_runner(), line_frequency); 61 device_descriptor_, v4l2_thread_.task_runner(), line_frequency);
62 if (!capture_impl_) { 62 if (!capture_impl_) {
63 client->OnError(FROM_HERE, "Failed to create VideoCaptureDelegate"); 63 client->OnError(FROM_HERE, "Failed to create VideoCaptureDelegate");
64 return; 64 return;
65 } 65 }
66 v4l2_thread_.task_runner()->PostTask( 66 v4l2_thread_.task_runner()->PostTask(
67 FROM_HERE, 67 FROM_HERE,
68 base::Bind(&V4L2CaptureDelegate::AllocateAndStart, capture_impl_, 68 base::Bind(&V4L2CaptureDelegate::AllocateAndStart,
69 base::Unretained(capture_impl_.get()),
mcasas 2017/04/12 17:32:55 Get a WeakPtr to |capture_impl_| to be used on |v4
Chandan 2017/04/13 09:06:07 Done.
69 params.requested_format.frame_size.width(), 70 params.requested_format.frame_size.width(),
70 params.requested_format.frame_size.height(), 71 params.requested_format.frame_size.height(),
71 params.requested_format.frame_rate, base::Passed(&client))); 72 params.requested_format.frame_rate, base::Passed(&client)));
chfremer 2017/04/12 17:13:46 Please add a comment explaining why the use of bas
Chandan 2017/04/13 09:06:07 We now use WeakPtr to |capture_impl_| instead of b
72 73
73 for (const auto& request : photo_requests_queue_) 74 for (const auto& request : photo_requests_queue_)
74 v4l2_thread_.task_runner()->PostTask(FROM_HERE, request); 75 v4l2_thread_.task_runner()->PostTask(FROM_HERE, request);
75 photo_requests_queue_.clear(); 76 photo_requests_queue_.clear();
76 } 77 }
77 78
78 void VideoCaptureDeviceLinux::StopAndDeAllocate() { 79 void VideoCaptureDeviceLinux::StopAndDeAllocate() {
79 if (!v4l2_thread_.IsRunning()) 80 if (!v4l2_thread_.IsRunning())
80 return; // Wrong state. 81 return; // Wrong state.
81 v4l2_thread_.task_runner()->PostTask( 82 v4l2_thread_.task_runner()->PostTask(
82 FROM_HERE, 83 FROM_HERE, base::Bind(&V4L2CaptureDelegate::StopAndDeAllocate,
83 base::Bind(&V4L2CaptureDelegate::StopAndDeAllocate, capture_impl_)); 84 base::Unretained(capture_impl_.get())));
85 v4l2_thread_.task_runner()->DeleteSoon(FROM_HERE, capture_impl_.release());
84 v4l2_thread_.Stop(); 86 v4l2_thread_.Stop();
85
86 capture_impl_ = nullptr;
87 } 87 }
88 88
89 void VideoCaptureDeviceLinux::TakePhoto(TakePhotoCallback callback) { 89 void VideoCaptureDeviceLinux::TakePhoto(TakePhotoCallback callback) {
90 DCHECK(capture_impl_); 90 DCHECK(capture_impl_);
91 auto functor = base::Bind(&V4L2CaptureDelegate::TakePhoto, capture_impl_, 91 auto functor = base::Bind(&V4L2CaptureDelegate::TakePhoto,
92 base::Unretained(capture_impl_.get()),
92 base::Passed(&callback)); 93 base::Passed(&callback));
93 if (!v4l2_thread_.IsRunning()) { 94 if (!v4l2_thread_.IsRunning()) {
94 // We have to wait until we get the device AllocateAndStart()ed. 95 // We have to wait until we get the device AllocateAndStart()ed.
95 photo_requests_queue_.push_back(std::move(functor)); 96 photo_requests_queue_.push_back(std::move(functor));
96 return; 97 return;
97 } 98 }
98 v4l2_thread_.task_runner()->PostTask(FROM_HERE, std::move(functor)); 99 v4l2_thread_.task_runner()->PostTask(FROM_HERE, std::move(functor));
99 } 100 }
100 101
101 void VideoCaptureDeviceLinux::GetPhotoCapabilities( 102 void VideoCaptureDeviceLinux::GetPhotoCapabilities(
102 GetPhotoCapabilitiesCallback callback) { 103 GetPhotoCapabilitiesCallback callback) {
103 auto functor = base::Bind(&V4L2CaptureDelegate::GetPhotoCapabilities, 104 auto functor = base::Bind(&V4L2CaptureDelegate::GetPhotoCapabilities,
104 capture_impl_, base::Passed(&callback)); 105 base::Unretained(capture_impl_.get()),
106 base::Passed(&callback));
105 if (!v4l2_thread_.IsRunning()) { 107 if (!v4l2_thread_.IsRunning()) {
106 // We have to wait until we get the device AllocateAndStart()ed. 108 // We have to wait until we get the device AllocateAndStart()ed.
107 photo_requests_queue_.push_back(std::move(functor)); 109 photo_requests_queue_.push_back(std::move(functor));
108 return; 110 return;
109 } 111 }
110 v4l2_thread_.task_runner()->PostTask(FROM_HERE, std::move(functor)); 112 v4l2_thread_.task_runner()->PostTask(FROM_HERE, std::move(functor));
111 } 113 }
112 114
113 void VideoCaptureDeviceLinux::SetPhotoOptions( 115 void VideoCaptureDeviceLinux::SetPhotoOptions(
114 mojom::PhotoSettingsPtr settings, 116 mojom::PhotoSettingsPtr settings,
115 SetPhotoOptionsCallback callback) { 117 SetPhotoOptionsCallback callback) {
116 auto functor = 118 auto functor = base::Bind(&V4L2CaptureDelegate::SetPhotoOptions,
117 base::Bind(&V4L2CaptureDelegate::SetPhotoOptions, capture_impl_, 119 base::Unretained(capture_impl_.get()),
118 base::Passed(&settings), base::Passed(&callback)); 120 base::Passed(&settings), base::Passed(&callback));
119 if (!v4l2_thread_.IsRunning()) { 121 if (!v4l2_thread_.IsRunning()) {
120 // We have to wait until we get the device AllocateAndStart()ed. 122 // We have to wait until we get the device AllocateAndStart()ed.
121 photo_requests_queue_.push_back(std::move(functor)); 123 photo_requests_queue_.push_back(std::move(functor));
122 return; 124 return;
123 } 125 }
124 v4l2_thread_.task_runner()->PostTask(FROM_HERE, std::move(functor)); 126 v4l2_thread_.task_runner()->PostTask(FROM_HERE, std::move(functor));
125 } 127 }
126 128
127 void VideoCaptureDeviceLinux::SetRotation(int rotation) { 129 void VideoCaptureDeviceLinux::SetRotation(int rotation) {
128 if (v4l2_thread_.IsRunning()) { 130 if (v4l2_thread_.IsRunning()) {
129 v4l2_thread_.task_runner()->PostTask( 131 v4l2_thread_.task_runner()->PostTask(
130 FROM_HERE, 132 FROM_HERE, base::Bind(&V4L2CaptureDelegate::SetRotation,
131 base::Bind(&V4L2CaptureDelegate::SetRotation, capture_impl_, rotation)); 133 base::Unretained(capture_impl_.get()), rotation));
132 } 134 }
133 } 135 }
134 136
135 // static 137 // static
136 int VideoCaptureDeviceLinux::TranslatePowerLineFrequencyToV4L2( 138 int VideoCaptureDeviceLinux::TranslatePowerLineFrequencyToV4L2(
137 PowerLineFrequency frequency) { 139 PowerLineFrequency frequency) {
138 switch (frequency) { 140 switch (frequency) {
139 case media::PowerLineFrequency::FREQUENCY_50HZ: 141 case media::PowerLineFrequency::FREQUENCY_50HZ:
140 return V4L2_CID_POWER_LINE_FREQUENCY_50HZ; 142 return V4L2_CID_POWER_LINE_FREQUENCY_50HZ;
141 case media::PowerLineFrequency::FREQUENCY_60HZ: 143 case media::PowerLineFrequency::FREQUENCY_60HZ:
142 return V4L2_CID_POWER_LINE_FREQUENCY_60HZ; 144 return V4L2_CID_POWER_LINE_FREQUENCY_60HZ;
143 default: 145 default:
144 // If we have no idea of the frequency, at least try and set it to AUTO. 146 // If we have no idea of the frequency, at least try and set it to AUTO.
145 return V4L2_CID_POWER_LINE_FREQUENCY_AUTO; 147 return V4L2_CID_POWER_LINE_FREQUENCY_AUTO;
146 } 148 }
147 } 149 }
148 150
149 } // namespace media 151 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698