278355a395eeff3c2487827a047d496e3b884703
[platform/framework/web/crosswalk.git] / src / content / renderer / media / video_capture_impl.h
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 // All public methods of VideoCaptureImpl can be called on any thread.
14 // Internally it runs on the IO thread. Clients of this class implement
15 // interface media::VideoCapture::EventHandler which is called only on the IO
16 // thread.
17 //
18 // Implementation note: tasks are posted bound to Unretained(this) to the I/O
19 // thread and this is safe (even though the I/O thread is scoped to the renderer
20 // process) because VideoCaptureImplManager only triggers deletion of its
21 // VideoCaptureImpl's by calling DeInit which detours through the I/O thread, so
22 // as long as nobody posts tasks after the DeInit() call is made, it is
23 // guaranteed none of these Unretained posted tasks will dangle after the delete
24 // goes through. The "as long as" is guaranteed by clients of
25 // VideoCaptureImplManager not using devices after they've released
26 // VideoCaptureHandle, which is a wrapper of this object.
27
28 #ifndef CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_
29 #define CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_
30
31 #include <list>
32 #include <map>
33
34 #include "base/memory/weak_ptr.h"
35 #include "content/common/content_export.h"
36 #include "content/common/media/video_capture.h"
37 #include "content/renderer/media/video_capture_message_filter.h"
38 #include "media/video/capture/video_capture.h"
39 #include "media/video/capture/video_capture_types.h"
40
41 namespace base {
42 class MessageLoopProxy;
43 }
44
45 namespace content {
46
47 class CONTENT_EXPORT VideoCaptureImpl
48     : public media::VideoCapture, public VideoCaptureMessageFilter::Delegate {
49  public:
50   VideoCaptureImpl(media::VideoCaptureSessionId session_id,
51                    VideoCaptureMessageFilter* filter);
52   virtual ~VideoCaptureImpl();
53
54   // Start listening to IPC messages.
55   void Init();
56
57   // Stop listening to IPC messages. Call |done_cb| when done.
58   void DeInit(base::Closure done_cb);
59
60   // Stop/resume delivering video frames to clients, based on flag |suspend|.
61   void SuspendCapture(bool suspend);
62
63   // media::VideoCapture interface.
64   virtual void StartCapture(
65       media::VideoCapture::EventHandler* handler,
66       const media::VideoCaptureParams& params) OVERRIDE;
67   virtual void StopCapture(media::VideoCapture::EventHandler* handler) OVERRIDE;
68   virtual bool CaptureStarted() OVERRIDE;
69   virtual int CaptureFrameRate() OVERRIDE;
70   virtual void GetDeviceSupportedFormats(
71       const DeviceFormatsCallback& callback) OVERRIDE;
72   virtual void GetDeviceFormatsInUse(
73       const DeviceFormatsInUseCallback& callback) OVERRIDE;
74
75   media::VideoCaptureSessionId session_id() const { return session_id_; }
76
77  private:
78   friend class VideoCaptureImplTest;
79   friend class MockVideoCaptureImpl;
80
81   class ClientBuffer;
82   typedef std::map<media::VideoCapture::EventHandler*,
83                    media::VideoCaptureParams> ClientInfo;
84
85   void InitOnIOThread();
86   void DeInitOnIOThread(base::Closure done_cb);
87   void SuspendCaptureOnIOThread(bool suspend);
88   void StartCaptureOnIOThread(
89       media::VideoCapture::EventHandler* handler,
90       const media::VideoCaptureParams& params);
91   void StopCaptureOnIOThread(media::VideoCapture::EventHandler* handler);
92   void GetDeviceSupportedFormatsOnIOThread(
93       const DeviceFormatsCallback& callback);
94   void GetDeviceFormatsInUseOnIOThread(
95       const DeviceFormatsInUseCallback& callback);
96
97   // VideoCaptureMessageFilter::Delegate interface.
98   virtual void OnBufferCreated(base::SharedMemoryHandle handle,
99                                int length,
100                                int buffer_id) OVERRIDE;
101   virtual void OnBufferDestroyed(int buffer_id) OVERRIDE;
102   virtual void OnBufferReceived(
103       int buffer_id,
104       base::TimeTicks timestamp,
105       const media::VideoCaptureFormat& format) OVERRIDE;
106   virtual void OnStateChanged(VideoCaptureState state) OVERRIDE;
107   virtual void OnDeviceSupportedFormatsEnumerated(
108       const media::VideoCaptureFormats& supported_formats) OVERRIDE;
109   virtual void OnDeviceFormatsInUseReceived(
110       const media::VideoCaptureFormats& formats_in_use) OVERRIDE;
111   virtual void OnDelegateAdded(int32 device_id) OVERRIDE;
112
113   // Sends an IPC message to browser process when all clients are done with the
114   // buffer.
115   void OnClientBufferFinished(
116       int buffer_id,
117       const scoped_refptr<ClientBuffer>& buffer);
118
119   void StopDevice();
120   void RestartCapture();
121   void StartCaptureInternal();
122
123   virtual void Send(IPC::Message* message);
124
125   // Helpers.
126   bool RemoveClient(media::VideoCapture::EventHandler* handler,
127                     ClientInfo* clients);
128
129   const scoped_refptr<VideoCaptureMessageFilter> message_filter_;
130   const scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_;
131   int device_id_;
132   const int session_id_;
133
134   // Vector of callbacks to be notified of device format enumerations, used only
135   // on IO Thread.
136   std::vector<DeviceFormatsCallback> device_formats_callback_queue_;
137   // Vector of callbacks to be notified of a device's in use capture format(s),
138   // used only on IO Thread.
139   std::vector<DeviceFormatsInUseCallback> device_formats_in_use_callback_queue_;
140
141   // Buffers available for sending to the client.
142   typedef std::map<int32, scoped_refptr<ClientBuffer> > ClientBufferMap;
143   ClientBufferMap client_buffers_;
144
145   ClientInfo clients_;
146   ClientInfo clients_pending_on_filter_;
147   ClientInfo clients_pending_on_restart_;
148
149   // Member params_ represents the video format requested by the
150   // client to this class via StartCapture().
151   media::VideoCaptureParams params_;
152
153   // The device's video capture format sent from browser process side.
154   media::VideoCaptureFormat last_frame_format_;
155
156   // The device's first captured frame timestamp sent from browser process side.
157   base::TimeTicks first_frame_timestamp_;
158
159   bool suspended_;
160   VideoCaptureState state_;
161
162   // WeakPtrFactory pointing back to |this| object, for use with
163   // media::VideoFrames constructed in OnBufferReceived() from buffers cached
164   // in |client_buffers_|.
165   base::WeakPtrFactory<VideoCaptureImpl> weak_this_factory_;
166
167   DISALLOW_COPY_AND_ASSIGN(VideoCaptureImpl);
168 };
169
170 }  // namespace content
171
172 #endif  // CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_