Home | History | Annotate | Download | only in media
      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 // VideoCaptureHost serves video capture related messages from
      6 // VideoCaptureMessageFilter which lives inside the render process.
      7 //
      8 // This class is owned by BrowserRenderProcessHost, and instantiated on UI
      9 // thread, but all other operations and method calls happen on IO thread.
     10 //
     11 // Here's an example of a typical IPC dialog for video capture:
     12 //
     13 //   Renderer                          VideoCaptureHost
     14 //      |                                     |
     15 //      |  VideoCaptureHostMsg_Start >        |
     16 //      |  < VideoCaptureMsg_DeviceInfo       |
     17 //      |                                     |
     18 //      | < VideoCaptureMsg_StateChanged      |
     19 //      |        (kStarted)                   |
     20 //      | < VideoCaptureMsg_BufferReady       |
     21 //      |             ...                     |
     22 //      | < VideoCaptureMsg_BufferReady       |
     23 //      |             ...                     |
     24 //      | VideoCaptureHostMsg_BufferReady >   |
     25 //      | VideoCaptureHostMsg_BufferReady >   |
     26 //      |                                     |
     27 //      |             ...                     |
     28 //      |                                     |
     29 //      | < VideoCaptureMsg_BufferReady       |
     30 //      |  VideoCaptureHostMsg_Stop >         |
     31 //      | VideoCaptureHostMsg_BufferReady >   |
     32 //      | < VideoCaptureMsg_StateChanged      |
     33 //      |         (kStopped)                  |
     34 //      v                                     v
     35 
     36 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_HOST_H_
     37 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_HOST_H_
     38 
     39 #include <map>
     40 
     41 #include "base/memory/ref_counted.h"
     42 #include "base/sequenced_task_runner_helpers.h"
     43 #include "content/browser/renderer_host/media/video_capture_controller.h"
     44 #include "content/common/content_export.h"
     45 #include "content/public/browser/browser_message_filter.h"
     46 #include "ipc/ipc_message.h"
     47 
     48 namespace media {
     49 struct VideoCaptureCapability;
     50 }
     51 
     52 namespace content {
     53 class MediaStreamManager;
     54 
     55 class CONTENT_EXPORT VideoCaptureHost
     56     : public BrowserMessageFilter,
     57       public VideoCaptureControllerEventHandler {
     58  public:
     59   explicit VideoCaptureHost(MediaStreamManager* media_stream_manager);
     60 
     61   // BrowserMessageFilter implementation.
     62   virtual void OnChannelClosing() OVERRIDE;
     63   virtual void OnDestruct() const OVERRIDE;
     64   virtual bool OnMessageReceived(const IPC::Message& message,
     65                                  bool* message_was_ok) OVERRIDE;
     66 
     67   // VideoCaptureControllerEventHandler implementation.
     68   virtual void OnError(const VideoCaptureControllerID& id) OVERRIDE;
     69   virtual void OnBufferCreated(const VideoCaptureControllerID& id,
     70                                base::SharedMemoryHandle handle,
     71                                int length, int buffer_id) OVERRIDE;
     72   virtual void OnBufferReady(const VideoCaptureControllerID& id,
     73                              int buffer_id,
     74                              base::Time timestamp) OVERRIDE;
     75   virtual void OnFrameInfo(
     76       const VideoCaptureControllerID& id,
     77       const media::VideoCaptureCapability& format) OVERRIDE;
     78   virtual void OnFrameInfoChanged(const VideoCaptureControllerID& id,
     79                                   int width,
     80                                   int height,
     81                                   int frame_per_second) OVERRIDE;
     82   virtual void OnEnded(const VideoCaptureControllerID& id) OVERRIDE;
     83 
     84  private:
     85   friend class BrowserThread;
     86   friend class base::DeleteHelper<VideoCaptureHost>;
     87   friend class MockVideoCaptureHost;
     88   friend class VideoCaptureHostTest;
     89 
     90   virtual ~VideoCaptureHost();
     91 
     92   // IPC message: Start capture on the VideoCaptureDevice referenced by
     93   // VideoCaptureParams::session_id. |device_id| is an id created by
     94   // VideoCaptureMessageFilter to identify a session
     95   // between a VideoCaptureMessageFilter and a VideoCaptureHost.
     96   void OnStartCapture(int device_id,
     97                       const media::VideoCaptureParams& params);
     98   void OnControllerAdded(
     99       int device_id, const media::VideoCaptureParams& params,
    100       VideoCaptureController* controller);
    101   void DoControllerAddedOnIOThread(
    102       int device_id, const media::VideoCaptureParams params,
    103       VideoCaptureController* controller);
    104 
    105   // IPC message: Stop capture on device referenced by |device_id|.
    106   void OnStopCapture(int device_id);
    107 
    108   // IPC message: Pause capture on device referenced by |device_id|.
    109   void OnPauseCapture(int device_id);
    110 
    111   // IPC message: Receive an empty buffer from renderer. Send it to device
    112   // referenced by |device_id|.
    113   void OnReceiveEmptyBuffer(int device_id, int buffer_id);
    114 
    115   // Send a newly created buffer to the VideoCaptureMessageFilter.
    116   void DoSendNewBufferOnIOThread(
    117       const VideoCaptureControllerID& controller_id,
    118       base::SharedMemoryHandle handle,
    119       int length,
    120       int buffer_id);
    121 
    122   // Send a filled buffer to the VideoCaptureMessageFilter.
    123   void DoSendFilledBufferOnIOThread(
    124       const VideoCaptureControllerID& controller_id,
    125       int buffer_id,
    126       base::Time timestamp);
    127 
    128   // Send information about the capture parameters (resolution, frame rate etc)
    129   // to the VideoCaptureMessageFilter.
    130   void DoSendFrameInfoOnIOThread(const VideoCaptureControllerID& controller_id,
    131                                  const media::VideoCaptureCapability& format);
    132 
    133   // Send newly changed information about frame resolution and frame rate
    134   // to the VideoCaptureMessageFilter.
    135   void DoSendFrameInfoChangedOnIOThread(
    136       const VideoCaptureControllerID& controller_id,
    137       int width,
    138       int height,
    139       int frame_per_second);
    140 
    141   // Handle error coming from VideoCaptureDevice.
    142   void DoHandleErrorOnIOThread(const VideoCaptureControllerID& controller_id);
    143 
    144   void DoEndedOnIOThread(const VideoCaptureControllerID& controller_id);
    145 
    146   void DeleteVideoCaptureControllerOnIOThread(
    147       const VideoCaptureControllerID& controller_id);
    148 
    149   MediaStreamManager* media_stream_manager_;
    150 
    151   struct Entry;
    152   typedef std::map<VideoCaptureControllerID, Entry*> EntryMap;
    153   // A map of VideoCaptureControllerID to its state and VideoCaptureController.
    154   EntryMap entries_;
    155 
    156   DISALLOW_COPY_AND_ASSIGN(VideoCaptureHost);
    157 };
    158 
    159 }  // namespace content
    160 
    161 #endif  // CONTENT_BROWSER_RENDERER_HOST_MEDIA_VIDEO_CAPTURE_HOST_H_
    162