f625a0732c06f9f9c59cad2282d83933d3d6b524
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / media / video_capture_controller.cc
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
3 // found in the LICENSE file.
4
5 #include "content/browser/renderer_host/media/video_capture_controller.h"
6
7 #include <map>
8 #include <set>
9
10 #include "base/bind.h"
11 #include "base/debug/trace_event.h"
12 #include "base/metrics/histogram.h"
13 #include "base/metrics/sparse_histogram.h"
14 #include "base/stl_util.h"
15 #include "content/browser/renderer_host/media/media_stream_manager.h"
16 #include "content/browser/renderer_host/media/video_capture_manager.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "gpu/command_buffer/common/mailbox_holder.h"
19 #include "media/base/video_frame.h"
20 #include "media/base/video_util.h"
21 #include "media/base/yuv_convert.h"
22
23 #if !defined(AVOID_LIBYUV_FOR_ANDROID_WEBVIEW)
24 #include "third_party/libyuv/include/libyuv.h"
25 #endif
26
27 using media::VideoCaptureFormat;
28
29 namespace content {
30
31 namespace {
32
33 static const int kInfiniteRatio = 99999;
34
35 #define UMA_HISTOGRAM_ASPECT_RATIO(name, width, height) \
36     UMA_HISTOGRAM_SPARSE_SLOWLY( \
37         name, \
38         (height) ? ((width) * 100) / (height) : kInfiniteRatio);
39
40 // The number of buffers that VideoCaptureBufferPool should allocate.
41 const int kNoOfBuffers = 3;
42
43 class PoolBuffer : public media::VideoCaptureDevice::Client::Buffer {
44  public:
45   PoolBuffer(const scoped_refptr<VideoCaptureBufferPool>& pool,
46              int buffer_id,
47              void* data,
48              size_t size)
49       : Buffer(buffer_id, data, size), pool_(pool) {
50     DCHECK(pool_);
51   }
52
53  private:
54   virtual ~PoolBuffer() { pool_->RelinquishProducerReservation(id()); }
55
56   const scoped_refptr<VideoCaptureBufferPool> pool_;
57 };
58
59 }  // anonymous namespace
60
61 struct VideoCaptureController::ControllerClient {
62   ControllerClient(const VideoCaptureControllerID& id,
63                    VideoCaptureControllerEventHandler* handler,
64                    base::ProcessHandle render_process,
65                    media::VideoCaptureSessionId session_id,
66                    const media::VideoCaptureParams& params)
67       : controller_id(id),
68         event_handler(handler),
69         render_process_handle(render_process),
70         session_id(session_id),
71         parameters(params),
72         session_closed(false) {}
73
74   ~ControllerClient() {}
75
76   // ID used for identifying this object.
77   const VideoCaptureControllerID controller_id;
78   VideoCaptureControllerEventHandler* const event_handler;
79
80   // Handle to the render process that will receive the capture buffers.
81   const base::ProcessHandle render_process_handle;
82   const media::VideoCaptureSessionId session_id;
83   const media::VideoCaptureParams parameters;
84
85   // Buffers that are currently known to this client.
86   std::set<int> known_buffers;
87
88   // Buffers currently held by this client, and syncpoint callback to call when
89   // they are returned from the client.
90   typedef std::map<int, scoped_refptr<media::VideoFrame> > ActiveBufferMap;
91   ActiveBufferMap active_buffers;
92
93   // State of capture session, controlled by VideoCaptureManager directly. This
94   // transitions to true as soon as StopSession() occurs, at which point the
95   // client is sent an OnEnded() event. However, because the client retains a
96   // VideoCaptureController* pointer, its ControllerClient entry lives on until
97   // it unregisters itself via RemoveClient(), which may happen asynchronously.
98   //
99   // TODO(nick): If we changed the semantics of VideoCaptureHost so that
100   // OnEnded() events were processed synchronously (with the RemoveClient() done
101   // implicitly), we could avoid tracking this state here in the Controller, and
102   // simplify the code in both places.
103   bool session_closed;
104 };
105
106 // Receives events from the VideoCaptureDevice and posts them to a
107 // VideoCaptureController on the IO thread. An instance of this class may safely
108 // outlive its target VideoCaptureController.
109 //
110 // Methods of this class may be called from any thread, and in practice will
111 // often be called on some auxiliary thread depending on the platform and the
112 // device type; including, for example, the DirectShow thread on Windows, the
113 // v4l2_thread on Linux, and the UI thread for tab capture.
114 class VideoCaptureController::VideoCaptureDeviceClient
115     : public media::VideoCaptureDevice::Client {
116  public:
117   explicit VideoCaptureDeviceClient(
118       const base::WeakPtr<VideoCaptureController>& controller,
119       const scoped_refptr<VideoCaptureBufferPool>& buffer_pool);
120   virtual ~VideoCaptureDeviceClient();
121
122   // VideoCaptureDevice::Client implementation.
123   virtual scoped_refptr<Buffer> ReserveOutputBuffer(
124       media::VideoFrame::Format format,
125       const gfx::Size& size) OVERRIDE;
126   virtual void OnIncomingCapturedData(const uint8* data,
127                                       int length,
128                                       const VideoCaptureFormat& frame_format,
129                                       int rotation,
130                                       base::TimeTicks timestamp) OVERRIDE;
131   virtual void OnIncomingCapturedVideoFrame(
132       const scoped_refptr<Buffer>& buffer,
133       const VideoCaptureFormat& buffer_format,
134       const scoped_refptr<media::VideoFrame>& frame,
135       base::TimeTicks timestamp) OVERRIDE;
136   virtual void OnError(const std::string& reason) OVERRIDE;
137
138  private:
139   scoped_refptr<Buffer> DoReserveOutputBuffer(media::VideoFrame::Format format,
140                                               const gfx::Size& dimensions);
141
142   // The controller to which we post events.
143   const base::WeakPtr<VideoCaptureController> controller_;
144
145   // The pool of shared-memory buffers used for capturing.
146   const scoped_refptr<VideoCaptureBufferPool> buffer_pool_;
147
148   bool first_frame_;
149 };
150
151 VideoCaptureController::VideoCaptureController()
152     : buffer_pool_(new VideoCaptureBufferPool(kNoOfBuffers)),
153       state_(VIDEO_CAPTURE_STATE_STARTED),
154       weak_ptr_factory_(this) {
155 }
156
157 VideoCaptureController::VideoCaptureDeviceClient::VideoCaptureDeviceClient(
158     const base::WeakPtr<VideoCaptureController>& controller,
159     const scoped_refptr<VideoCaptureBufferPool>& buffer_pool)
160     : controller_(controller), buffer_pool_(buffer_pool), first_frame_(true) {}
161
162 VideoCaptureController::VideoCaptureDeviceClient::~VideoCaptureDeviceClient() {}
163
164 base::WeakPtr<VideoCaptureController> VideoCaptureController::GetWeakPtr() {
165   return weak_ptr_factory_.GetWeakPtr();
166 }
167
168 scoped_ptr<media::VideoCaptureDevice::Client>
169 VideoCaptureController::NewDeviceClient() {
170   scoped_ptr<media::VideoCaptureDevice::Client> result(
171       new VideoCaptureDeviceClient(this->GetWeakPtr(), buffer_pool_));
172   return result.Pass();
173 }
174
175 void VideoCaptureController::AddClient(
176     const VideoCaptureControllerID& id,
177     VideoCaptureControllerEventHandler* event_handler,
178     base::ProcessHandle render_process,
179     media::VideoCaptureSessionId session_id,
180     const media::VideoCaptureParams& params) {
181   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
182   DVLOG(1) << "VideoCaptureController::AddClient, id " << id.device_id
183            << ", " << params.requested_format.frame_size.ToString()
184            << ", " << params.requested_format.frame_rate
185            << ", " << session_id
186            << ")";
187
188   // If this is the first client added to the controller, cache the parameters.
189   if (!controller_clients_.size())
190     video_capture_format_ = params.requested_format;
191
192   // Signal error in case device is already in error state.
193   if (state_ == VIDEO_CAPTURE_STATE_ERROR) {
194     event_handler->OnError(id);
195     return;
196   }
197
198   // Do nothing if this client has called AddClient before.
199   if (FindClient(id, event_handler, controller_clients_))
200     return;
201
202   ControllerClient* client = new ControllerClient(
203       id, event_handler, render_process, session_id, params);
204   // If we already have gotten frame_info from the device, repeat it to the new
205   // client.
206   if (state_ == VIDEO_CAPTURE_STATE_STARTED) {
207     controller_clients_.push_back(client);
208     return;
209   }
210 }
211
212 int VideoCaptureController::RemoveClient(
213     const VideoCaptureControllerID& id,
214     VideoCaptureControllerEventHandler* event_handler) {
215   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
216   DVLOG(1) << "VideoCaptureController::RemoveClient, id " << id.device_id;
217
218   ControllerClient* client = FindClient(id, event_handler, controller_clients_);
219   if (!client)
220     return kInvalidMediaCaptureSessionId;
221
222   // Take back all buffers held by the |client|.
223   for (ControllerClient::ActiveBufferMap::iterator buffer_it =
224            client->active_buffers.begin();
225        buffer_it != client->active_buffers.end();
226        ++buffer_it) {
227     buffer_pool_->RelinquishConsumerHold(buffer_it->first, 1);
228   }
229   client->active_buffers.clear();
230
231   int session_id = client->session_id;
232   controller_clients_.remove(client);
233   delete client;
234
235   return session_id;
236 }
237
238 void VideoCaptureController::StopSession(int session_id) {
239   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
240   DVLOG(1) << "VideoCaptureController::StopSession, id " << session_id;
241
242   ControllerClient* client = FindClient(session_id, controller_clients_);
243
244   if (client) {
245     client->session_closed = true;
246     client->event_handler->OnEnded(client->controller_id);
247   }
248 }
249
250 void VideoCaptureController::ReturnBuffer(
251     const VideoCaptureControllerID& id,
252     VideoCaptureControllerEventHandler* event_handler,
253     int buffer_id,
254     uint32 sync_point) {
255   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
256
257   ControllerClient* client = FindClient(id, event_handler, controller_clients_);
258
259   // If this buffer is not held by this client, or this client doesn't exist
260   // in controller, do nothing.
261   ControllerClient::ActiveBufferMap::iterator iter;
262   if (!client || (iter = client->active_buffers.find(buffer_id)) ==
263                      client->active_buffers.end()) {
264     NOTREACHED();
265     return;
266   }
267   scoped_refptr<media::VideoFrame> frame = iter->second;
268   client->active_buffers.erase(iter);
269
270   if (frame->format() == media::VideoFrame::NATIVE_TEXTURE)
271     frame->mailbox_holder()->sync_point = sync_point;
272
273   buffer_pool_->RelinquishConsumerHold(buffer_id, 1);
274 }
275
276 const media::VideoCaptureFormat&
277 VideoCaptureController::GetVideoCaptureFormat() const {
278   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
279   return video_capture_format_;
280 }
281
282 scoped_refptr<media::VideoCaptureDevice::Client::Buffer>
283 VideoCaptureController::VideoCaptureDeviceClient::ReserveOutputBuffer(
284     media::VideoFrame::Format format,
285     const gfx::Size& size) {
286   return DoReserveOutputBuffer(format, size);
287 }
288
289 void VideoCaptureController::VideoCaptureDeviceClient::OnIncomingCapturedData(
290     const uint8* data,
291     int length,
292     const VideoCaptureFormat& frame_format,
293     int rotation,
294     base::TimeTicks timestamp) {
295   TRACE_EVENT0("video", "VideoCaptureController::OnIncomingCapturedData");
296
297   if (!frame_format.IsValid())
298     return;
299
300   // Chopped pixels in width/height in case video capture device has odd
301   // numbers for width/height.
302   int chopped_width = 0;
303   int chopped_height = 0;
304   int new_unrotated_width = frame_format.frame_size.width();
305   int new_unrotated_height = frame_format.frame_size.height();
306
307   if (new_unrotated_width & 1) {
308     --new_unrotated_width;
309     chopped_width = 1;
310   }
311   if (new_unrotated_height & 1) {
312     --new_unrotated_height;
313     chopped_height = 1;
314   }
315
316   int destination_width = new_unrotated_width;
317   int destination_height = new_unrotated_height;
318   if (rotation == 90 || rotation == 270) {
319     destination_width = new_unrotated_height;
320     destination_height = new_unrotated_width;
321   }
322   const gfx::Size dimensions(destination_width, destination_height);
323   scoped_refptr<Buffer> buffer =
324       DoReserveOutputBuffer(media::VideoFrame::I420, dimensions);
325
326   if (!buffer)
327     return;
328   uint8* yplane = NULL;
329 #if !defined(AVOID_LIBYUV_FOR_ANDROID_WEBVIEW)
330   bool flip = false;
331   yplane = reinterpret_cast<uint8*>(buffer->data());
332   uint8* uplane =
333       yplane +
334       media::VideoFrame::PlaneAllocationSize(
335           media::VideoFrame::I420, media::VideoFrame::kYPlane, dimensions);
336   uint8* vplane =
337       uplane +
338       media::VideoFrame::PlaneAllocationSize(
339           media::VideoFrame::I420, media::VideoFrame::kUPlane, dimensions);
340   int yplane_stride = dimensions.width();
341   int uv_plane_stride = yplane_stride / 2;
342   int crop_x = 0;
343   int crop_y = 0;
344   libyuv::FourCC origin_colorspace = libyuv::FOURCC_ANY;
345
346   libyuv::RotationMode rotation_mode = libyuv::kRotate0;
347   if (rotation == 90)
348     rotation_mode = libyuv::kRotate90;
349   else if (rotation == 180)
350     rotation_mode = libyuv::kRotate180;
351   else if (rotation == 270)
352     rotation_mode = libyuv::kRotate270;
353
354   switch (frame_format.pixel_format) {
355     case media::PIXEL_FORMAT_UNKNOWN:  // Color format not set.
356       break;
357     case media::PIXEL_FORMAT_I420:
358       DCHECK(!chopped_width && !chopped_height);
359       origin_colorspace = libyuv::FOURCC_I420;
360       break;
361     case media::PIXEL_FORMAT_YV12:
362       DCHECK(!chopped_width && !chopped_height);
363       origin_colorspace = libyuv::FOURCC_YV12;
364       break;
365     case media::PIXEL_FORMAT_NV21:
366       DCHECK(!chopped_width && !chopped_height);
367       origin_colorspace = libyuv::FOURCC_NV21;
368       break;
369     case media::PIXEL_FORMAT_YUY2:
370       DCHECK(!chopped_width && !chopped_height);
371       origin_colorspace = libyuv::FOURCC_YUY2;
372       break;
373     case media::PIXEL_FORMAT_UYVY:
374       DCHECK(!chopped_width && !chopped_height);
375       origin_colorspace = libyuv::FOURCC_UYVY;
376       break;
377     case media::PIXEL_FORMAT_RGB24:
378       origin_colorspace = libyuv::FOURCC_24BG;
379 #if defined(OS_WIN)
380       // TODO(wjia): Currently, for RGB24 on WIN, capture device always
381       // passes in positive src_width and src_height. Remove this hardcoded
382       // value when nagative src_height is supported. The negative src_height
383       // indicates that vertical flipping is needed.
384       flip = true;
385 #endif
386       break;
387     case media::PIXEL_FORMAT_ARGB:
388       origin_colorspace = libyuv::FOURCC_ARGB;
389       break;
390     case media::PIXEL_FORMAT_MJPEG:
391       origin_colorspace = libyuv::FOURCC_MJPG;
392       break;
393     default:
394       NOTREACHED();
395   }
396
397   libyuv::ConvertToI420(data,
398                         length,
399                         yplane,
400                         yplane_stride,
401                         uplane,
402                         uv_plane_stride,
403                         vplane,
404                         uv_plane_stride,
405                         crop_x,
406                         crop_y,
407                         frame_format.frame_size.width(),
408                         (flip ? -frame_format.frame_size.height() :
409                                 frame_format.frame_size.height()),
410                         new_unrotated_width,
411                         new_unrotated_height,
412                         rotation_mode,
413                         origin_colorspace);
414 #else
415   // Libyuv is not linked in for Android WebView builds, but video capture is
416   // not used in those builds either. Whenever libyuv is added in that build,
417   // address all these #ifdef parts, see http://crbug.com/299611 .
418   NOTREACHED();
419 #endif  // if !defined(AVOID_LIBYUV_FOR_ANDROID_WEBVIEW)
420   VideoCaptureFormat format(
421       dimensions, frame_format.frame_rate, media::PIXEL_FORMAT_I420);
422   scoped_refptr<media::VideoFrame> frame =
423       media::VideoFrame::WrapExternalPackedMemory(
424           media::VideoFrame::I420,
425           dimensions,
426           gfx::Rect(dimensions),
427           dimensions,
428           yplane,
429           media::VideoFrame::AllocationSize(media::VideoFrame::I420,
430                                             dimensions),
431           base::SharedMemory::NULLHandle(),
432           base::TimeDelta(),
433           base::Closure());
434   DCHECK(frame);
435   BrowserThread::PostTask(
436       BrowserThread::IO,
437       FROM_HERE,
438       base::Bind(
439           &VideoCaptureController::DoIncomingCapturedVideoFrameOnIOThread,
440           controller_,
441           buffer,
442           format,
443           frame,
444           timestamp));
445
446   if (first_frame_) {
447     UMA_HISTOGRAM_COUNTS("Media.VideoCapture.Width",
448                          frame_format.frame_size.width());
449     UMA_HISTOGRAM_COUNTS("Media.VideoCapture.Height",
450                          frame_format.frame_size.height());
451     UMA_HISTOGRAM_ASPECT_RATIO("Media.VideoCapture.AspectRatio",
452                                frame_format.frame_size.width(),
453                                frame_format.frame_size.height());
454     UMA_HISTOGRAM_COUNTS("Media.VideoCapture.FrameRate",
455                          frame_format.frame_rate);
456     UMA_HISTOGRAM_ENUMERATION("Media.VideoCapture.PixelFormat",
457                               frame_format.pixel_format,
458                               media::PIXEL_FORMAT_MAX);
459     first_frame_ = false;
460   }
461 }
462
463 void
464 VideoCaptureController::VideoCaptureDeviceClient::OnIncomingCapturedVideoFrame(
465     const scoped_refptr<Buffer>& buffer,
466     const VideoCaptureFormat& buffer_format,
467     const scoped_refptr<media::VideoFrame>& frame,
468     base::TimeTicks timestamp) {
469   BrowserThread::PostTask(
470       BrowserThread::IO,
471       FROM_HERE,
472       base::Bind(
473           &VideoCaptureController::DoIncomingCapturedVideoFrameOnIOThread,
474           controller_,
475           buffer,
476           buffer_format,
477           frame,
478           timestamp));
479 }
480
481 void VideoCaptureController::VideoCaptureDeviceClient::OnError(
482     const std::string& reason) {
483   MediaStreamManager::SendMessageToNativeLog(
484       "Error on video capture: " + reason);
485   BrowserThread::PostTask(BrowserThread::IO,
486       FROM_HERE,
487       base::Bind(&VideoCaptureController::DoErrorOnIOThread, controller_));
488 }
489
490 scoped_refptr<media::VideoCaptureDevice::Client::Buffer>
491 VideoCaptureController::VideoCaptureDeviceClient::DoReserveOutputBuffer(
492     media::VideoFrame::Format format,
493     const gfx::Size& dimensions) {
494   size_t frame_bytes = 0;
495   if (format == media::VideoFrame::NATIVE_TEXTURE) {
496     DCHECK_EQ(dimensions.width(), 0);
497     DCHECK_EQ(dimensions.height(), 0);
498   } else {
499     // The capture pipeline expects I420 for now.
500     DCHECK_EQ(format, media::VideoFrame::I420)
501         << "Non-I420 output buffer format " << format << " requested";
502     frame_bytes = media::VideoFrame::AllocationSize(format, dimensions);
503   }
504
505   int buffer_id_to_drop = VideoCaptureBufferPool::kInvalidId;
506   int buffer_id =
507       buffer_pool_->ReserveForProducer(frame_bytes, &buffer_id_to_drop);
508   if (buffer_id == VideoCaptureBufferPool::kInvalidId)
509     return NULL;
510   void* data;
511   size_t size;
512   buffer_pool_->GetBufferInfo(buffer_id, &data, &size);
513
514   scoped_refptr<media::VideoCaptureDevice::Client::Buffer> output_buffer(
515       new PoolBuffer(buffer_pool_, buffer_id, data, size));
516
517   if (buffer_id_to_drop != VideoCaptureBufferPool::kInvalidId) {
518     BrowserThread::PostTask(BrowserThread::IO,
519         FROM_HERE,
520         base::Bind(&VideoCaptureController::DoBufferDestroyedOnIOThread,
521                    controller_, buffer_id_to_drop));
522   }
523
524   return output_buffer;
525 }
526
527 VideoCaptureController::~VideoCaptureController() {
528   STLDeleteContainerPointers(controller_clients_.begin(),
529                              controller_clients_.end());
530 }
531
532 void VideoCaptureController::DoIncomingCapturedVideoFrameOnIOThread(
533     const scoped_refptr<media::VideoCaptureDevice::Client::Buffer>& buffer,
534     const media::VideoCaptureFormat& buffer_format,
535     const scoped_refptr<media::VideoFrame>& frame,
536     base::TimeTicks timestamp) {
537   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
538   DCHECK_NE(buffer->id(), VideoCaptureBufferPool::kInvalidId);
539
540   int count = 0;
541   if (state_ == VIDEO_CAPTURE_STATE_STARTED) {
542     for (ControllerClients::iterator client_it = controller_clients_.begin();
543          client_it != controller_clients_.end(); ++client_it) {
544       ControllerClient* client = *client_it;
545       if (client->session_closed)
546         continue;
547
548       if (frame->format() == media::VideoFrame::NATIVE_TEXTURE) {
549         client->event_handler->OnMailboxBufferReady(client->controller_id,
550                                                     buffer->id(),
551                                                     *frame->mailbox_holder(),
552                                                     buffer_format,
553                                                     timestamp);
554       } else {
555         bool is_new_buffer = client->known_buffers.insert(buffer->id()).second;
556         if (is_new_buffer) {
557           // On the first use of a buffer on a client, share the memory handle.
558           size_t memory_size = 0;
559           base::SharedMemoryHandle remote_handle = buffer_pool_->ShareToProcess(
560               buffer->id(), client->render_process_handle, &memory_size);
561           client->event_handler->OnBufferCreated(
562               client->controller_id, remote_handle, memory_size, buffer->id());
563         }
564
565         client->event_handler->OnBufferReady(
566             client->controller_id, buffer->id(), buffer_format, timestamp);
567       }
568
569       bool inserted =
570           client->active_buffers.insert(std::make_pair(buffer->id(), frame))
571               .second;
572       DCHECK(inserted) << "Unexpected duplicate buffer: " << buffer->id();
573       count++;
574     }
575   }
576
577   buffer_pool_->HoldForConsumers(buffer->id(), count);
578 }
579
580 void VideoCaptureController::DoErrorOnIOThread() {
581   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
582   state_ = VIDEO_CAPTURE_STATE_ERROR;
583
584   for (ControllerClients::iterator client_it = controller_clients_.begin();
585        client_it != controller_clients_.end(); ++client_it) {
586     ControllerClient* client = *client_it;
587     if (client->session_closed)
588        continue;
589
590     client->event_handler->OnError(client->controller_id);
591   }
592 }
593
594 void VideoCaptureController::DoBufferDestroyedOnIOThread(
595     int buffer_id_to_drop) {
596   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
597
598   for (ControllerClients::iterator client_it = controller_clients_.begin();
599        client_it != controller_clients_.end(); ++client_it) {
600     ControllerClient* client = *client_it;
601     if (client->session_closed)
602       continue;
603
604     if (client->known_buffers.erase(buffer_id_to_drop)) {
605       client->event_handler->OnBufferDestroyed(client->controller_id,
606                                                buffer_id_to_drop);
607     }
608   }
609 }
610
611 VideoCaptureController::ControllerClient*
612 VideoCaptureController::FindClient(
613     const VideoCaptureControllerID& id,
614     VideoCaptureControllerEventHandler* handler,
615     const ControllerClients& clients) {
616   for (ControllerClients::const_iterator client_it = clients.begin();
617        client_it != clients.end(); ++client_it) {
618     if ((*client_it)->controller_id == id &&
619         (*client_it)->event_handler == handler) {
620       return *client_it;
621     }
622   }
623   return NULL;
624 }
625
626 VideoCaptureController::ControllerClient*
627 VideoCaptureController::FindClient(
628     int session_id,
629     const ControllerClients& clients) {
630   for (ControllerClients::const_iterator client_it = clients.begin();
631        client_it != clients.end(); ++client_it) {
632     if ((*client_it)->session_id == session_id) {
633       return *client_it;
634     }
635   }
636   return NULL;
637 }
638
639 int VideoCaptureController::GetClientCount() {
640   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
641   return controller_clients_.size();
642 }
643
644 }  // namespace content