• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // VideoCaptureImpl represents a capture device in renderer process. It provides
6 // interfaces for clients to Start/Stop capture. It also communicates to clients
7 // when buffer is ready, state of capture device is changed.
8 
9 // VideoCaptureImpl is also a delegate of VideoCaptureMessageFilter which relays
10 // operation of a capture device to the browser process and receives responses
11 // from browser process.
12 //
13 // VideoCaptureImpl is an IO thread only object. See the comments in
14 // video_capture_impl_manager.cc for the lifetime of this object.
15 // All methods must be called on the IO thread.
16 //
17 // This is an internal class used by VideoCaptureImplManager only. Do not access
18 // this directly.
19 
20 #ifndef CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_
21 #define CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_
22 
23 #include <list>
24 #include <map>
25 
26 #include "base/memory/weak_ptr.h"
27 #include "base/threading/thread_checker.h"
28 #include "content/common/content_export.h"
29 #include "content/common/media/video_capture.h"
30 #include "content/public/renderer/media_stream_video_sink.h"
31 #include "content/renderer/media/video_capture_message_filter.h"
32 #include "media/video/capture/video_capture_types.h"
33 
34 namespace base {
35 class MessageLoopProxy;
36 }  // namespace base
37 
38 namespace gpu {
39 struct MailboxHolder;
40 }  // namespace gpu
41 
42 namespace media {
43 class VideoFrame;
44 }  // namespace media
45 
46 namespace content {
47 
48 class CONTENT_EXPORT VideoCaptureImpl
49     : public VideoCaptureMessageFilter::Delegate {
50  public:
51   virtual ~VideoCaptureImpl();
52 
53   VideoCaptureImpl(media::VideoCaptureSessionId session_id,
54                    VideoCaptureMessageFilter* filter);
55 
56   // Start listening to IPC messages.
57   void Init();
58 
59   // Stop listening to IPC messages.
60   void DeInit();
61 
62   // Stop/resume delivering video frames to clients, based on flag |suspend|.
63   void SuspendCapture(bool suspend);
64 
65   // Start capturing using the provided parameters.
66   // |client_id| must be unique to this object in the render process. It is
67   // used later to stop receiving video frames.
68   // |state_update_cb| will be called when state changes.
69   // |deliver_frame_cb| will be called when a frame is ready.
70   void StartCapture(
71       int client_id,
72       const media::VideoCaptureParams& params,
73       const VideoCaptureStateUpdateCB& state_update_cb,
74       const VideoCaptureDeliverFrameCB& deliver_frame_cb);
75 
76   // Stop capturing. |client_id| is the identifier used to call StartCapture.
77   void StopCapture(int client_id);
78 
79   // Get capturing formats supported by this device.
80   // |callback| will be invoked with the results.
81   void GetDeviceSupportedFormats(
82       const VideoCaptureDeviceFormatsCB& callback);
83 
84   // Get capturing formats currently in use by this device.
85   // |callback| will be invoked with the results.
86   void GetDeviceFormatsInUse(
87       const VideoCaptureDeviceFormatsCB& callback);
88 
session_id()89   media::VideoCaptureSessionId session_id() const { return session_id_; }
90 
91  private:
92   friend class VideoCaptureImplTest;
93   friend class MockVideoCaptureImpl;
94 
95   // Carries a shared memory for transferring video frames from browser to
96   // renderer.
97   class ClientBuffer;
98 
99   // Contains information for a video capture client. Including parameters
100   // for capturing and callbacks to the client.
101   struct ClientInfo {
102     ClientInfo();
103     ~ClientInfo();
104     media::VideoCaptureParams params;
105     VideoCaptureStateUpdateCB state_update_cb;
106     VideoCaptureDeliverFrameCB deliver_frame_cb;
107   };
108   typedef std::map<int, ClientInfo> ClientInfoMap;
109 
110   // VideoCaptureMessageFilter::Delegate interface.
111   virtual void OnBufferCreated(base::SharedMemoryHandle handle,
112                                int length,
113                                int buffer_id) OVERRIDE;
114   virtual void OnBufferDestroyed(int buffer_id) OVERRIDE;
115   virtual void OnBufferReceived(int buffer_id,
116                                 const media::VideoCaptureFormat& format,
117                                 base::TimeTicks) OVERRIDE;
118   virtual void OnMailboxBufferReceived(int buffer_id,
119                                        const gpu::MailboxHolder& mailbox_holder,
120                                        const media::VideoCaptureFormat& format,
121                                        base::TimeTicks timestamp) OVERRIDE;
122   virtual void OnStateChanged(VideoCaptureState state) OVERRIDE;
123   virtual void OnDeviceSupportedFormatsEnumerated(
124       const media::VideoCaptureFormats& supported_formats) OVERRIDE;
125   virtual void OnDeviceFormatsInUseReceived(
126       const media::VideoCaptureFormats& formats_in_use) OVERRIDE;
127   virtual void OnDelegateAdded(int32 device_id) OVERRIDE;
128 
129   // Sends an IPC message to browser process when all clients are done with the
130   // buffer.
131   void OnClientBufferFinished(int buffer_id,
132                               const scoped_refptr<ClientBuffer>& buffer,
133                               const std::vector<uint32>& release_sync_points);
134 
135   void StopDevice();
136   void RestartCapture();
137   void StartCaptureInternal();
138 
139   virtual void Send(IPC::Message* message);
140 
141   // Helpers.
142   bool RemoveClient(int client_id, ClientInfoMap* clients);
143 
144   const scoped_refptr<VideoCaptureMessageFilter> message_filter_;
145   int device_id_;
146   const int session_id_;
147 
148   // Vector of callbacks to be notified of device format enumerations, used only
149   // on IO Thread.
150   std::vector<VideoCaptureDeviceFormatsCB> device_formats_cb_queue_;
151   // Vector of callbacks to be notified of a device's in use capture format(s),
152   // used only on IO Thread.
153   std::vector<VideoCaptureDeviceFormatsCB> device_formats_in_use_cb_queue_;
154 
155   // Buffers available for sending to the client.
156   typedef std::map<int32, scoped_refptr<ClientBuffer> > ClientBufferMap;
157   ClientBufferMap client_buffers_;
158 
159   ClientInfoMap clients_;
160   ClientInfoMap clients_pending_on_filter_;
161   ClientInfoMap clients_pending_on_restart_;
162 
163   // Member params_ represents the video format requested by the
164   // client to this class via StartCapture().
165   media::VideoCaptureParams params_;
166 
167   // The device's video capture format sent from browser process side.
168   media::VideoCaptureFormat last_frame_format_;
169 
170   // The device's first captured frame timestamp sent from browser process side.
171   base::TimeTicks first_frame_timestamp_;
172 
173   bool suspended_;
174   VideoCaptureState state_;
175 
176   // |weak_factory_| and |thread_checker_| are bound to the IO thread.
177   base::ThreadChecker thread_checker_;
178 
179   // WeakPtrFactory pointing back to |this| object, for use with
180   // media::VideoFrames constructed in OnBufferReceived() from buffers cached
181   // in |client_buffers_|.
182   // NOTE: Weak pointers must be invalidated before all other member variables.
183   base::WeakPtrFactory<VideoCaptureImpl> weak_factory_;
184 
185   DISALLOW_COPY_AND_ASSIGN(VideoCaptureImpl);
186 };
187 
188 }  // namespace content
189 
190 #endif  // CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_
191