- add sources.
[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 // The media::VideoCapture and VideoCaptureMessageFilter::Delegate are
14 // asynchronous interfaces, which means callers can call those interfaces
15 // from any threads without worrying about thread safety.
16 // The |capture_message_loop_proxy_| is the working thread of VideoCaptureImpl.
17 // All non-const members are accessed only on that working thread.
18 //
19 // Implementation note: tasks are posted bound to Unretained(this) to both the
20 // I/O and Capture threads and this is safe (even though the I/O thread is
21 // scoped to the renderer process and the capture_message_loop_proxy_ thread is
22 // scoped to the VideoCaptureImplManager) because VideoCaptureImplManager only
23 // triggers deletion of its VideoCaptureImpl's by calling DeInit which detours
24 // through the capture & I/O threads, so as long as nobody posts tasks after the
25 // DeInit() call is made, it is guaranteed none of these Unretained posted tasks
26 // will dangle after the delete goes through.  The "as long as" is guaranteed by
27 // clients of VideoCaptureImplManager not using devices after they've
28 // RemoveDevice'd them.
29
30 #ifndef CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_
31 #define CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_
32
33 #include <list>
34 #include <map>
35
36 #include "base/memory/weak_ptr.h"
37 #include "content/common/content_export.h"
38 #include "content/common/media/video_capture.h"
39 #include "content/renderer/media/video_capture_message_filter.h"
40 #include "media/video/capture/video_capture.h"
41 #include "media/video/capture/video_capture_types.h"
42
43 namespace base {
44 class MessageLoopProxy;
45 }
46
47 namespace content {
48
49 class CONTENT_EXPORT VideoCaptureImpl
50     : public media::VideoCapture, public VideoCaptureMessageFilter::Delegate {
51  public:
52   // media::VideoCapture interface.
53   virtual void StartCapture(
54       media::VideoCapture::EventHandler* handler,
55       const media::VideoCaptureParams& params) OVERRIDE;
56   virtual void StopCapture(media::VideoCapture::EventHandler* handler) OVERRIDE;
57   virtual bool CaptureStarted() OVERRIDE;
58   virtual int CaptureFrameRate() OVERRIDE;
59
60   // VideoCaptureMessageFilter::Delegate interface.
61   virtual void OnBufferCreated(base::SharedMemoryHandle handle,
62                                int length,
63                                int buffer_id) OVERRIDE;
64   virtual void OnBufferDestroyed(int buffer_id) OVERRIDE;
65   virtual void OnBufferReceived(
66       int buffer_id,
67       base::Time timestamp,
68       const media::VideoCaptureFormat& format) OVERRIDE;
69   virtual void OnStateChanged(VideoCaptureState state) OVERRIDE;
70   virtual void OnDelegateAdded(int32 device_id) OVERRIDE;
71
72   // Stop/resume delivering video frames to clients, based on flag |suspend|.
73   virtual void SuspendCapture(bool suspend);
74
75  private:
76   friend class VideoCaptureImplManager;
77   friend class VideoCaptureImplTest;
78   friend class MockVideoCaptureImpl;
79
80   class ClientBuffer;
81   typedef std::map<media::VideoCapture::EventHandler*,
82                    media::VideoCaptureParams> ClientInfo;
83
84   VideoCaptureImpl(media::VideoCaptureSessionId session_id,
85                    base::MessageLoopProxy* capture_message_loop_proxy,
86                    VideoCaptureMessageFilter* filter);
87   virtual ~VideoCaptureImpl();
88
89   void DoStartCaptureOnCaptureThread(
90       media::VideoCapture::EventHandler* handler,
91       const media::VideoCaptureParams& params);
92   void DoStopCaptureOnCaptureThread(media::VideoCapture::EventHandler* handler);
93   void DoBufferCreatedOnCaptureThread(base::SharedMemoryHandle handle,
94                                       int length,
95                                       int buffer_id);
96   void DoBufferDestroyedOnCaptureThread(int buffer_id);
97   void DoBufferReceivedOnCaptureThread(
98       int buffer_id,
99       base::Time timestamp,
100       const media::VideoCaptureFormat& format);
101   void DoClientBufferFinishedOnCaptureThread(
102       int buffer_id,
103       const scoped_refptr<ClientBuffer>& buffer);
104   void DoStateChangedOnCaptureThread(VideoCaptureState state);
105   void DoDelegateAddedOnCaptureThread(int32 device_id);
106
107   void DoSuspendCaptureOnCaptureThread(bool suspend);
108
109   void Init();
110   void DeInit(base::Closure task);
111   void DoDeInitOnCaptureThread(base::Closure task);
112   void StopDevice();
113   void RestartCapture();
114   void StartCaptureInternal();
115   void AddDelegateOnIOThread();
116   void RemoveDelegateOnIOThread(base::Closure task);
117   virtual void Send(IPC::Message* message);
118
119   // Helpers.
120   bool RemoveClient(media::VideoCapture::EventHandler* handler,
121                     ClientInfo* clients);
122
123   const scoped_refptr<VideoCaptureMessageFilter> message_filter_;
124   const scoped_refptr<base::MessageLoopProxy> capture_message_loop_proxy_;
125   const scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_;
126   int device_id_;
127   const int session_id_;
128
129   // Buffers available for sending to the client.
130   typedef std::map<int32, scoped_refptr<ClientBuffer> > ClientBufferMap;
131   ClientBufferMap client_buffers_;
132   // WeakPtrFactory pointing back to |this| object, for use with
133   // media::VideoFrames constructed in OnBufferReceived() from buffers cached
134   // in |client_buffers_|.
135   base::WeakPtrFactory<VideoCaptureImpl> client_buffer_weak_this_factory_;
136
137   ClientInfo clients_;
138
139   ClientInfo clients_pending_on_filter_;
140   ClientInfo clients_pending_on_restart_;
141
142   // Member params_ represents the video format requested by the
143   // client to this class via DoStartCaptureOnCaptureThread.
144   media::VideoCaptureParams params_;
145
146   // The device's video capture format sent from browser process side.
147   media::VideoCaptureFormat last_frame_format_;
148
149   bool suspended_;
150   VideoCaptureState state_;
151
152   DISALLOW_COPY_AND_ASSIGN(VideoCaptureImpl);
153 };
154
155 }  // namespace content
156
157 #endif  // CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_