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

Side by Side Diff: content/browser/renderer_host/media/video_capture_host.cc

Issue 1017503002: VideoCaptureHost/VideoCaptureControllerEventHandler cleanup (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: s/base::TimeTicks/const base::TimeTicks&/, reinstalling OnEnded()/OnError->Do.. allowing for Delete… Created 5 years, 9 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 "content/browser/renderer_host/media/video_capture_host.h" 5 #include "content/browser/renderer_host/media/video_capture_host.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
10 #include "content/browser/browser_main_loop.h" 10 #include "content/browser/browser_main_loop.h"
11 #include "content/browser/renderer_host/media/media_stream_manager.h" 11 #include "content/browser/renderer_host/media/media_stream_manager.h"
12 #include "content/browser/renderer_host/media/video_capture_manager.h" 12 #include "content/browser/renderer_host/media/video_capture_manager.h"
13 #include "content/common/media/video_capture_messages.h" 13 #include "content/common/media/video_capture_messages.h"
14 #include "media/base/bind_to_current_loop.h"
14 15
15 namespace content { 16 namespace content {
16 17
17 VideoCaptureHost::VideoCaptureHost(MediaStreamManager* media_stream_manager) 18 VideoCaptureHost::VideoCaptureHost(MediaStreamManager* media_stream_manager)
18 : BrowserMessageFilter(VideoCaptureMsgStart), 19 : BrowserMessageFilter(VideoCaptureMsgStart),
19 media_stream_manager_(media_stream_manager) { 20 media_stream_manager_(media_stream_manager) {
21 DCHECK_CURRENTLY_ON(BrowserThread::UI);
20 } 22 }
21 23
22 VideoCaptureHost::~VideoCaptureHost() {} 24 VideoCaptureHost::~VideoCaptureHost() {}
23 25
24 void VideoCaptureHost::OnChannelClosing() { 26 void VideoCaptureHost::OnChannelClosing() {
25 // Since the IPC sender is gone, close all requested VideoCaptureDevices. 27 // Since the IPC sender is gone, close all requested VideoCaptureDevices.
26 for (EntryMap::iterator it = entries_.begin(); it != entries_.end(); ) { 28 for (EntryMap::iterator it = entries_.begin(); it != entries_.end(); ) {
27 const base::WeakPtr<VideoCaptureController>& controller = it->second; 29 const base::WeakPtr<VideoCaptureController>& controller = it->second;
28 if (controller) { 30 if (controller) {
29 VideoCaptureControllerID controller_id(it->first); 31 const VideoCaptureControllerID controller_id(it->first);
30 media_stream_manager_->video_capture_manager()->StopCaptureForClient( 32 media_stream_manager_->video_capture_manager()->StopCaptureForClient(
31 controller.get(), controller_id, this, false); 33 controller.get(), controller_id, this, false);
32 ++it; 34 ++it;
33 } else { 35 } else {
34 // Remove the entry for this controller_id so that when the controller 36 // Remove the entry for this controller_id so that when the controller
35 // is added, the controller will be notified to stop for this client 37 // is added, the controller will be notified to stop for this client
36 // in DoControllerAddedOnIOThread. 38 // in DoControllerAdded.
37 entries_.erase(it++); 39 entries_.erase(it++);
38 } 40 }
39 } 41 }
40 } 42 }
41 43
42 void VideoCaptureHost::OnDestruct() const { 44 void VideoCaptureHost::OnDestruct() const {
43 BrowserThread::DeleteOnIOThread::Destruct(this); 45 BrowserThread::DeleteOnIOThread::Destruct(this);
44 } 46 }
45 47
46 /////////////////////////////////////////////////////////////////////////////// 48 ///////////////////////////////////////////////////////////////////////////////
47 49
48 // Implements VideoCaptureControllerEventHandler. 50 // Implements VideoCaptureControllerEventHandler.
49 void VideoCaptureHost::OnError(const VideoCaptureControllerID& controller_id) { 51 void VideoCaptureHost::OnError(VideoCaptureControllerID controller_id) {
50 DVLOG(1) << "VideoCaptureHost::OnError"; 52 DVLOG(1) << "VideoCaptureHost::OnError";
53 DCHECK_CURRENTLY_ON(BrowserThread::IO);
51 BrowserThread::PostTask( 54 BrowserThread::PostTask(
52 BrowserThread::IO, FROM_HERE, 55 BrowserThread::IO, FROM_HERE,
53 base::Bind(&VideoCaptureHost::DoHandleErrorOnIOThread, 56 base::Bind(&VideoCaptureHost::DoError, this, controller_id));
54 this, controller_id));
55 } 57 }
56 58
57 void VideoCaptureHost::OnBufferCreated( 59 void VideoCaptureHost::OnBufferCreated(VideoCaptureControllerID controller_id,
58 const VideoCaptureControllerID& controller_id, 60 base::SharedMemoryHandle handle,
59 base::SharedMemoryHandle handle, 61 int length,
60 int length, 62 int buffer_id) {
61 int buffer_id) { 63 DCHECK_CURRENTLY_ON(BrowserThread::IO);
62 BrowserThread::PostTask( 64 if (entries_.find(controller_id) == entries_.end())
63 BrowserThread::IO, FROM_HERE, 65 return;
64 base::Bind(&VideoCaptureHost::DoSendNewBufferOnIOThread, 66
65 this, controller_id, handle, length, buffer_id)); 67 Send(new VideoCaptureMsg_NewBuffer(controller_id, handle, length, buffer_id));
66 } 68 }
67 69
68 void VideoCaptureHost::OnBufferDestroyed( 70 void VideoCaptureHost::OnBufferDestroyed(VideoCaptureControllerID controller_id,
69 const VideoCaptureControllerID& controller_id, 71 int buffer_id) {
70 int buffer_id) { 72 DCHECK_CURRENTLY_ON(BrowserThread::IO);
71 BrowserThread::PostTask( 73 if (entries_.find(controller_id) == entries_.end())
72 BrowserThread::IO, FROM_HERE, 74 return;
73 base::Bind(&VideoCaptureHost::DoSendFreeBufferOnIOThread, 75
74 this, controller_id, buffer_id)); 76 Send(new VideoCaptureMsg_FreeBuffer(controller_id, buffer_id));
75 } 77 }
76 78
77 void VideoCaptureHost::OnBufferReady( 79 void VideoCaptureHost::OnBufferReady(
78 const VideoCaptureControllerID& controller_id, 80 VideoCaptureControllerID controller_id,
79 int buffer_id, 81 int buffer_id,
80 const gfx::Size& coded_size, 82 const gfx::Size& coded_size,
81 const gfx::Rect& visible_rect, 83 const gfx::Rect& visible_rect,
82 base::TimeTicks timestamp, 84 const base::TimeTicks& timestamp,
83 scoped_ptr<base::DictionaryValue> metadata) {
84 BrowserThread::PostTask(
85 BrowserThread::IO,
86 FROM_HERE,
87 base::Bind(&VideoCaptureHost::DoSendFilledBufferOnIOThread,
88 this,
89 controller_id,
90 buffer_id,
91 coded_size,
92 visible_rect,
93 timestamp,
94 base::Passed(&metadata)));
95 }
96
97 void VideoCaptureHost::OnMailboxBufferReady(
98 const VideoCaptureControllerID& controller_id,
99 int buffer_id,
100 const gpu::MailboxHolder& mailbox_holder,
101 const gfx::Size& packed_frame_size,
102 base::TimeTicks timestamp,
103 scoped_ptr<base::DictionaryValue> metadata) {
104 BrowserThread::PostTask(
105 BrowserThread::IO,
106 FROM_HERE,
107 base::Bind(&VideoCaptureHost::DoSendFilledMailboxBufferOnIOThread,
108 this,
109 controller_id,
110 buffer_id,
111 mailbox_holder,
112 packed_frame_size,
113 timestamp,
114 base::Passed(&metadata)));
115 }
116
117 void VideoCaptureHost::OnEnded(const VideoCaptureControllerID& controller_id) {
118 DVLOG(1) << "VideoCaptureHost::OnEnded";
119 BrowserThread::PostTask(
120 BrowserThread::IO, FROM_HERE,
121 base::Bind(&VideoCaptureHost::DoEndedOnIOThread, this, controller_id));
122 }
123
124 void VideoCaptureHost::DoSendNewBufferOnIOThread(
125 const VideoCaptureControllerID& controller_id,
126 base::SharedMemoryHandle handle,
127 int length,
128 int buffer_id) {
129 DCHECK_CURRENTLY_ON(BrowserThread::IO);
130
131 if (entries_.find(controller_id) == entries_.end())
132 return;
133
134 Send(new VideoCaptureMsg_NewBuffer(controller_id.device_id, handle,
135 length, buffer_id));
136 }
137
138 void VideoCaptureHost::DoSendFreeBufferOnIOThread(
139 const VideoCaptureControllerID& controller_id,
140 int buffer_id) {
141 DCHECK_CURRENTLY_ON(BrowserThread::IO);
142
143 if (entries_.find(controller_id) == entries_.end())
144 return;
145
146 Send(new VideoCaptureMsg_FreeBuffer(controller_id.device_id, buffer_id));
147 }
148
149 void VideoCaptureHost::DoSendFilledBufferOnIOThread(
150 const VideoCaptureControllerID& controller_id,
151 int buffer_id,
152 const gfx::Size& coded_size,
153 const gfx::Rect& visible_rect,
154 base::TimeTicks timestamp,
155 scoped_ptr<base::DictionaryValue> metadata) { 85 scoped_ptr<base::DictionaryValue> metadata) {
156 DCHECK_CURRENTLY_ON(BrowserThread::IO); 86 DCHECK_CURRENTLY_ON(BrowserThread::IO);
157
158 if (entries_.find(controller_id) == entries_.end()) 87 if (entries_.find(controller_id) == entries_.end())
159 return; 88 return;
160 89
161 VideoCaptureMsg_BufferReady_Params params; 90 VideoCaptureMsg_BufferReady_Params params;
162 params.device_id = controller_id.device_id; 91 params.device_id = controller_id;
163 params.buffer_id = buffer_id; 92 params.buffer_id = buffer_id;
164 params.coded_size = coded_size; 93 params.coded_size = coded_size;
165 params.visible_rect = visible_rect; 94 params.visible_rect = visible_rect;
166 params.timestamp = timestamp; 95 params.timestamp = timestamp;
167 if (metadata) 96 if (metadata)
168 params.metadata.Swap(metadata.get()); 97 params.metadata.Swap(metadata.get());
169 Send(new VideoCaptureMsg_BufferReady(params)); 98 Send(new VideoCaptureMsg_BufferReady(params));
170 } 99 }
171 100
172 void VideoCaptureHost::DoSendFilledMailboxBufferOnIOThread( 101 void VideoCaptureHost::OnMailboxBufferReady(
173 const VideoCaptureControllerID& controller_id, 102 VideoCaptureControllerID controller_id,
174 int buffer_id, 103 int buffer_id,
175 const gpu::MailboxHolder& mailbox_holder, 104 const gpu::MailboxHolder& mailbox_holder,
176 const gfx::Size& packed_frame_size, 105 const gfx::Size& packed_frame_size,
177 base::TimeTicks timestamp, 106 const base::TimeTicks& timestamp,
178 scoped_ptr<base::DictionaryValue> metadata) { 107 scoped_ptr<base::DictionaryValue> metadata) {
179 DCHECK_CURRENTLY_ON(BrowserThread::IO); 108 DCHECK_CURRENTLY_ON(BrowserThread::IO);
180 109
181 if (entries_.find(controller_id) == entries_.end()) 110 if (entries_.find(controller_id) == entries_.end())
182 return; 111 return;
183 112
184 VideoCaptureMsg_MailboxBufferReady_Params params; 113 VideoCaptureMsg_MailboxBufferReady_Params params;
185 params.device_id = controller_id.device_id; 114 params.device_id = controller_id;
186 params.buffer_id = buffer_id; 115 params.buffer_id = buffer_id;
187 params.mailbox_holder = mailbox_holder; 116 params.mailbox_holder = mailbox_holder;
188 params.packed_frame_size = packed_frame_size; 117 params.packed_frame_size = packed_frame_size;
189 params.timestamp = timestamp; 118 params.timestamp = timestamp;
190 if (metadata) 119 if (metadata)
191 params.metadata.Swap(metadata.get()); 120 params.metadata.Swap(metadata.get());
192 Send(new VideoCaptureMsg_MailboxBufferReady(params)); 121 Send(new VideoCaptureMsg_MailboxBufferReady(params));
193 } 122 }
194 123
195 void VideoCaptureHost::DoHandleErrorOnIOThread( 124 void VideoCaptureHost::OnEnded(VideoCaptureControllerID controller_id) {
196 const VideoCaptureControllerID& controller_id) { 125 DVLOG(1) << "VideoCaptureHost::OnEnded";
197 DCHECK_CURRENTLY_ON(BrowserThread::IO); 126 DCHECK_CURRENTLY_ON(BrowserThread::IO);
127 BrowserThread::PostTask(
128 BrowserThread::IO, FROM_HERE,
129 base::Bind(&VideoCaptureHost::DoEnded, this, controller_id));
130 }
198 131
132 void VideoCaptureHost::DoError(VideoCaptureControllerID controller_id) {
133 DVLOG(1) << "VideoCaptureHost::DoError";
134 DCHECK_CURRENTLY_ON(BrowserThread::IO);
199 if (entries_.find(controller_id) == entries_.end()) 135 if (entries_.find(controller_id) == entries_.end())
200 return; 136 return;
201 137
202 Send(new VideoCaptureMsg_StateChanged(controller_id.device_id, 138 Send(new VideoCaptureMsg_StateChanged(controller_id,
203 VIDEO_CAPTURE_STATE_ERROR)); 139 VIDEO_CAPTURE_STATE_ERROR));
204 DeleteVideoCaptureControllerOnIOThread(controller_id, true); 140 DeleteVideoCaptureController(controller_id, true);
205 } 141 }
206 142
207 void VideoCaptureHost::DoEndedOnIOThread( 143 void VideoCaptureHost::DoEnded(VideoCaptureControllerID controller_id) {
208 const VideoCaptureControllerID& controller_id) { 144 DVLOG(1) << "VideoCaptureHost::DoEnded";
209 DCHECK_CURRENTLY_ON(BrowserThread::IO); 145 DCHECK_CURRENTLY_ON(BrowserThread::IO);
210 DVLOG(1) << "VideoCaptureHost::DoEndedOnIOThread";
211 if (entries_.find(controller_id) == entries_.end()) 146 if (entries_.find(controller_id) == entries_.end())
212 return; 147 return;
213 148
214 Send(new VideoCaptureMsg_StateChanged(controller_id.device_id, 149 Send(new VideoCaptureMsg_StateChanged(controller_id,
215 VIDEO_CAPTURE_STATE_ENDED)); 150 VIDEO_CAPTURE_STATE_ENDED));
216 DeleteVideoCaptureControllerOnIOThread(controller_id, false); 151 DeleteVideoCaptureController(controller_id, false);
217 } 152 }
218 153
219 /////////////////////////////////////////////////////////////////////////////// 154 ///////////////////////////////////////////////////////////////////////////////
220 // IPC Messages handler. 155 // IPC Messages handler.
221 bool VideoCaptureHost::OnMessageReceived(const IPC::Message& message) { 156 bool VideoCaptureHost::OnMessageReceived(const IPC::Message& message) {
222 bool handled = true; 157 bool handled = true;
223 IPC_BEGIN_MESSAGE_MAP(VideoCaptureHost, message) 158 IPC_BEGIN_MESSAGE_MAP(VideoCaptureHost, message)
224 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Start, OnStartCapture) 159 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Start, OnStartCapture)
225 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Pause, OnPauseCapture) 160 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Pause, OnPauseCapture)
226 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Resume, OnResumeCapture) 161 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_Resume, OnResumeCapture)
(...skipping 29 matching lines...) Expand all
256 return; 191 return;
257 } 192 }
258 193
259 entries_[controller_id] = base::WeakPtr<VideoCaptureController>(); 194 entries_[controller_id] = base::WeakPtr<VideoCaptureController>();
260 media_stream_manager_->video_capture_manager()->StartCaptureForClient( 195 media_stream_manager_->video_capture_manager()->StartCaptureForClient(
261 session_id, 196 session_id,
262 params, 197 params,
263 PeerHandle(), 198 PeerHandle(),
264 controller_id, 199 controller_id,
265 this, 200 this,
266 base::Bind(&VideoCaptureHost::OnControllerAdded, this, device_id)); 201 media::BindToCurrentLoop(
perkj_chrome 2015/03/17 06:46:59 Why do you do this extra bindtocurrentloop? OnCont
mcasas 2015/03/17 15:57:44 Not needed indeed. I think I started by changing t
202 base::Bind(&VideoCaptureHost::OnControllerAdded, this, device_id)));
267 } 203 }
268 204
269 void VideoCaptureHost::OnControllerAdded( 205 void VideoCaptureHost::OnControllerAdded(
270 int device_id, 206 int device_id,
271 const base::WeakPtr<VideoCaptureController>& controller) { 207 const base::WeakPtr<VideoCaptureController>& controller) {
272 DCHECK_CURRENTLY_ON(BrowserThread::IO); 208 DCHECK_CURRENTLY_ON(BrowserThread::IO);
273 BrowserThread::PostTask(
274 BrowserThread::IO,
275 FROM_HERE,
276 base::Bind(&VideoCaptureHost::DoControllerAddedOnIOThread,
277 this,
278 device_id,
279 controller));
280 }
281
282 void VideoCaptureHost::DoControllerAddedOnIOThread(
283 int device_id,
284 const base::WeakPtr<VideoCaptureController>& controller) {
285 DCHECK_CURRENTLY_ON(BrowserThread::IO);
286 VideoCaptureControllerID controller_id(device_id); 209 VideoCaptureControllerID controller_id(device_id);
287 EntryMap::iterator it = entries_.find(controller_id); 210 EntryMap::iterator it = entries_.find(controller_id);
288 if (it == entries_.end()) { 211 if (it == entries_.end()) {
289 if (controller) { 212 if (controller) {
290 media_stream_manager_->video_capture_manager()->StopCaptureForClient( 213 media_stream_manager_->video_capture_manager()->StopCaptureForClient(
291 controller.get(), controller_id, this, false); 214 controller.get(), controller_id, this, false);
292 } 215 }
293 return; 216 return;
294 } 217 }
295 218
296 if (!controller) { 219 if (!controller) {
297 Send(new VideoCaptureMsg_StateChanged(device_id, 220 Send(new VideoCaptureMsg_StateChanged(device_id,
298 VIDEO_CAPTURE_STATE_ERROR)); 221 VIDEO_CAPTURE_STATE_ERROR));
299 entries_.erase(controller_id); 222 entries_.erase(controller_id);
300 return; 223 return;
301 } 224 }
302 225
303 DCHECK(!it->second); 226 DCHECK(!it->second);
304 it->second = controller; 227 it->second = controller;
305 } 228 }
306 229
307 void VideoCaptureHost::OnStopCapture(int device_id) { 230 void VideoCaptureHost::OnStopCapture(int device_id) {
308 DCHECK_CURRENTLY_ON(BrowserThread::IO); 231 DCHECK_CURRENTLY_ON(BrowserThread::IO);
309 DVLOG(1) << "VideoCaptureHost::OnStopCapture, device_id " << device_id; 232 DVLOG(1) << "VideoCaptureHost::OnStopCapture, device_id " << device_id;
310 233
311 VideoCaptureControllerID controller_id(device_id); 234 VideoCaptureControllerID controller_id(device_id);
312 235
313 Send(new VideoCaptureMsg_StateChanged(device_id, 236 Send(new VideoCaptureMsg_StateChanged(device_id,
314 VIDEO_CAPTURE_STATE_STOPPED)); 237 VIDEO_CAPTURE_STATE_STOPPED));
315 DeleteVideoCaptureControllerOnIOThread(controller_id, false); 238 DeleteVideoCaptureController(controller_id, false);
316 } 239 }
317 240
318 void VideoCaptureHost::OnPauseCapture(int device_id) { 241 void VideoCaptureHost::OnPauseCapture(int device_id) {
319 DCHECK_CURRENTLY_ON(BrowserThread::IO); 242 DCHECK_CURRENTLY_ON(BrowserThread::IO);
320 DVLOG(1) << "VideoCaptureHost::OnPauseCapture, device_id " << device_id; 243 DVLOG(1) << "VideoCaptureHost::OnPauseCapture, device_id " << device_id;
321 244
322 VideoCaptureControllerID controller_id(device_id); 245 VideoCaptureControllerID controller_id(device_id);
323 EntryMap::iterator it = entries_.find(controller_id); 246 EntryMap::iterator it = entries_.find(controller_id);
324 if (it == entries_.end()) 247 if (it == entries_.end())
325 return; 248 return;
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
389 media::VideoCaptureFormats formats_in_use; 312 media::VideoCaptureFormats formats_in_use;
390 if (!media_stream_manager_->video_capture_manager()->GetDeviceFormatsInUse( 313 if (!media_stream_manager_->video_capture_manager()->GetDeviceFormatsInUse(
391 capture_session_id, &formats_in_use)) { 314 capture_session_id, &formats_in_use)) {
392 DVLOG(1) << "Could not retrieve device format(s) in use for device_id=" 315 DVLOG(1) << "Could not retrieve device format(s) in use for device_id="
393 << device_id << " capture_session_id=" << capture_session_id; 316 << device_id << " capture_session_id=" << capture_session_id;
394 } 317 }
395 Send(new VideoCaptureMsg_DeviceFormatsInUseReceived(device_id, 318 Send(new VideoCaptureMsg_DeviceFormatsInUseReceived(device_id,
396 formats_in_use)); 319 formats_in_use));
397 } 320 }
398 321
399 void VideoCaptureHost::DeleteVideoCaptureControllerOnIOThread( 322 void VideoCaptureHost::DeleteVideoCaptureController(
400 const VideoCaptureControllerID& controller_id, bool on_error) { 323 VideoCaptureControllerID controller_id, bool on_error) {
401 DCHECK_CURRENTLY_ON(BrowserThread::IO); 324 DCHECK_CURRENTLY_ON(BrowserThread::IO);
402 325
403 EntryMap::iterator it = entries_.find(controller_id); 326 EntryMap::iterator it = entries_.find(controller_id);
404 if (it == entries_.end()) 327 if (it == entries_.end())
405 return; 328 return;
406 329
407 if (it->second) { 330 if (it->second) {
408 media_stream_manager_->video_capture_manager()->StopCaptureForClient( 331 media_stream_manager_->video_capture_manager()->StopCaptureForClient(
409 it->second.get(), controller_id, this, on_error); 332 it->second.get(), controller_id, this, on_error);
410 } 333 }
411 entries_.erase(it); 334 entries_.erase(it);
412 } 335 }
413 336
414 } // namespace content 337 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698