OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // VideoCaptureImpl represents a capture device in renderer process. It provides | 5 // VideoCaptureImpl represents a capture device in renderer process. It provides |
6 // interfaces for clients to Start/Stop capture. It also communicates to clients | 6 // interfaces for clients to Start/Stop capture. It also communicates to clients |
7 // when buffer is ready, state of capture device is changed. | 7 // when buffer is ready, state of capture device is changed. |
8 | 8 |
9 // VideoCaptureImpl is also a delegate of VideoCaptureMessageFilter which relays | 9 // VideoCaptureImpl is also a delegate of VideoCaptureMessageFilter which relays |
10 // operation of a capture device to the browser process and receives responses | 10 // operation of a capture device to the browser process and receives responses |
11 // from browser process. | 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 // | 12 // |
19 // Implementation note: tasks are posted bound to Unretained(this) to both the | 13 // All public methods of VideoCaptureImpl can be called on any thread. |
20 // I/O and Capture threads and this is safe (even though the I/O thread is | 14 // Internally it runs on the IO thread. Clients of this class implement |
21 // scoped to the renderer process and the capture_message_loop_proxy_ thread is | 15 // interface media::VideoCapture::EventHandler which is called only on the IO |
22 // scoped to the VideoCaptureImplManager) because VideoCaptureImplManager only | 16 // thread. |
23 // triggers deletion of its VideoCaptureImpl's by calling DeInit which detours | 17 // |
24 // through the capture & I/O threads, so as long as nobody posts tasks after the | 18 // Implementation note: tasks are posted bound to Unretained(this) to the I/O |
25 // DeInit() call is made, it is guaranteed none of these Unretained posted tasks | 19 // thread and this is safe (even though the I/O thread is scoped to the renderer |
26 // will dangle after the delete goes through. The "as long as" is guaranteed by | 20 // process) because VideoCaptureImplManager only triggers deletion of its |
27 // clients of VideoCaptureImplManager not using devices after they've | 21 // VideoCaptureImpl's by calling DeInit which detours through the I/O thread, so |
28 // RemoveDevice'd them. | 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. |
29 | 27 |
30 #ifndef CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_ | 28 #ifndef CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_ |
31 #define CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_ | 29 #define CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_ |
32 | 30 |
33 #include <list> | 31 #include <list> |
34 #include <map> | 32 #include <map> |
35 | 33 |
36 #include "base/memory/weak_ptr.h" | 34 #include "base/memory/weak_ptr.h" |
37 #include "content/common/content_export.h" | 35 #include "content/common/content_export.h" |
38 #include "content/common/media/video_capture.h" | 36 #include "content/common/media/video_capture.h" |
39 #include "content/renderer/media/video_capture_message_filter.h" | 37 #include "content/renderer/media/video_capture_message_filter.h" |
40 #include "media/video/capture/video_capture.h" | 38 #include "media/video/capture/video_capture.h" |
41 #include "media/video/capture/video_capture_types.h" | 39 #include "media/video/capture/video_capture_types.h" |
42 | 40 |
43 namespace base { | 41 namespace base { |
44 class MessageLoopProxy; | 42 class MessageLoopProxy; |
45 } | 43 } |
46 | 44 |
47 namespace content { | 45 namespace content { |
48 | 46 |
49 class CONTENT_EXPORT VideoCaptureImpl | 47 class CONTENT_EXPORT VideoCaptureImpl |
50 : public media::VideoCapture, public VideoCaptureMessageFilter::Delegate { | 48 : public media::VideoCapture, public VideoCaptureMessageFilter::Delegate { |
51 public: | 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 |
52 // media::VideoCapture interface. | 63 // media::VideoCapture interface. |
53 virtual void StartCapture( | 64 virtual void StartCapture( |
54 media::VideoCapture::EventHandler* handler, | 65 media::VideoCapture::EventHandler* handler, |
55 const media::VideoCaptureParams& params) OVERRIDE; | 66 const media::VideoCaptureParams& params) OVERRIDE; |
56 virtual void StopCapture(media::VideoCapture::EventHandler* handler) OVERRIDE; | 67 virtual void StopCapture(media::VideoCapture::EventHandler* handler) OVERRIDE; |
57 virtual bool CaptureStarted() OVERRIDE; | 68 virtual bool CaptureStarted() OVERRIDE; |
58 virtual int CaptureFrameRate() OVERRIDE; | 69 virtual int CaptureFrameRate() OVERRIDE; |
59 | 70 |
60 // VideoCaptureMessageFilter::Delegate interface. | 71 media::VideoCaptureSessionId session_id() const { return session_id_; } |
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(int buffer_id, | |
66 base::TimeTicks timestamp, | |
67 const media::VideoCaptureFormat& format) | |
68 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 | 72 |
75 private: | 73 private: |
76 friend class VideoCaptureImplManager; | |
77 friend class VideoCaptureImplTest; | 74 friend class VideoCaptureImplTest; |
78 friend class MockVideoCaptureImpl; | 75 friend class MockVideoCaptureImpl; |
79 | 76 |
80 class ClientBuffer; | 77 class ClientBuffer; |
81 typedef std::map<media::VideoCapture::EventHandler*, | 78 typedef std::map<media::VideoCapture::EventHandler*, |
82 media::VideoCaptureParams> ClientInfo; | 79 media::VideoCaptureParams> ClientInfo; |
83 | 80 |
84 VideoCaptureImpl(media::VideoCaptureSessionId session_id, | 81 void InitOnIOThread(); |
85 base::MessageLoopProxy* capture_message_loop_proxy, | 82 void DeInitOnIOThread(base::Closure done_cb); |
86 VideoCaptureMessageFilter* filter); | 83 void SuspendCaptureOnIOThread(bool suspend); |
87 virtual ~VideoCaptureImpl(); | 84 void StartCaptureOnIOThread( |
88 | |
89 void DoStartCaptureOnCaptureThread( | |
90 media::VideoCapture::EventHandler* handler, | 85 media::VideoCapture::EventHandler* handler, |
91 const media::VideoCaptureParams& params); | 86 const media::VideoCaptureParams& params); |
92 void DoStopCaptureOnCaptureThread(media::VideoCapture::EventHandler* handler); | 87 void StopCaptureOnIOThread(media::VideoCapture::EventHandler* handler); |
93 void DoBufferCreatedOnCaptureThread(base::SharedMemoryHandle handle, | 88 |
94 int length, | 89 // VideoCaptureMessageFilter::Delegate interface. |
95 int buffer_id); | 90 virtual void OnBufferCreated(base::SharedMemoryHandle handle, |
96 void DoBufferDestroyedOnCaptureThread(int buffer_id); | 91 int length, |
97 void DoBufferReceivedOnCaptureThread(int buffer_id, | 92 int buffer_id) OVERRIDE; |
98 base::TimeTicks timestamp, | 93 virtual void OnBufferDestroyed(int buffer_id) OVERRIDE; |
99 const media::VideoCaptureFormat& format); | 94 virtual void OnBufferReceived( |
100 void DoClientBufferFinishedOnCaptureThread( | 95 int buffer_id, |
| 96 base::TimeTicks timestamp, |
| 97 const media::VideoCaptureFormat& format) OVERRIDE; |
| 98 virtual void OnStateChanged(VideoCaptureState state) OVERRIDE; |
| 99 virtual void OnDelegateAdded(int32 device_id) OVERRIDE; |
| 100 |
| 101 // Sends an IPC message to browser process when all clients are done with the |
| 102 // buffer. |
| 103 void OnClientBufferFinished( |
101 int buffer_id, | 104 int buffer_id, |
102 const scoped_refptr<ClientBuffer>& buffer); | 105 const scoped_refptr<ClientBuffer>& buffer); |
103 void DoStateChangedOnCaptureThread(VideoCaptureState state); | |
104 void DoDelegateAddedOnCaptureThread(int32 device_id); | |
105 | 106 |
106 void DoSuspendCaptureOnCaptureThread(bool suspend); | |
107 | |
108 void Init(); | |
109 void DeInit(base::Closure task); | |
110 void DoDeInitOnCaptureThread(base::Closure task); | |
111 void StopDevice(); | 107 void StopDevice(); |
112 void RestartCapture(); | 108 void RestartCapture(); |
113 void StartCaptureInternal(); | 109 void StartCaptureInternal(); |
114 void AddDelegateOnIOThread(); | |
115 void RemoveDelegateOnIOThread(base::Closure task); | |
116 virtual void Send(IPC::Message* message); | 110 virtual void Send(IPC::Message* message); |
117 | 111 |
118 // Helpers. | 112 // Helpers. |
119 bool RemoveClient(media::VideoCapture::EventHandler* handler, | 113 bool RemoveClient(media::VideoCapture::EventHandler* handler, |
120 ClientInfo* clients); | 114 ClientInfo* clients); |
121 | 115 |
122 const scoped_refptr<VideoCaptureMessageFilter> message_filter_; | 116 const scoped_refptr<VideoCaptureMessageFilter> message_filter_; |
123 const scoped_refptr<base::MessageLoopProxy> capture_message_loop_proxy_; | |
124 const scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | 117 const scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; |
125 int device_id_; | 118 int device_id_; |
126 const int session_id_; | 119 const int session_id_; |
127 | 120 |
128 // Buffers available for sending to the client. | 121 // Buffers available for sending to the client. |
129 typedef std::map<int32, scoped_refptr<ClientBuffer> > ClientBufferMap; | 122 typedef std::map<int32, scoped_refptr<ClientBuffer> > ClientBufferMap; |
130 ClientBufferMap client_buffers_; | 123 ClientBufferMap client_buffers_; |
131 | 124 |
132 ClientInfo clients_; | 125 ClientInfo clients_; |
133 ClientInfo clients_pending_on_filter_; | 126 ClientInfo clients_pending_on_filter_; |
134 ClientInfo clients_pending_on_restart_; | 127 ClientInfo clients_pending_on_restart_; |
135 | 128 |
136 // Member params_ represents the video format requested by the | 129 // Member params_ represents the video format requested by the |
137 // client to this class via DoStartCaptureOnCaptureThread. | 130 // client to this class via StartCapture(). |
138 media::VideoCaptureParams params_; | 131 media::VideoCaptureParams params_; |
139 | 132 |
140 // The device's video capture format sent from browser process side. | 133 // The device's video capture format sent from browser process side. |
141 media::VideoCaptureFormat last_frame_format_; | 134 media::VideoCaptureFormat last_frame_format_; |
142 | 135 |
143 // The device's first captured frame timestamp sent from browser process side. | 136 // The device's first captured frame timestamp sent from browser process side. |
144 base::TimeTicks first_frame_timestamp_; | 137 base::TimeTicks first_frame_timestamp_; |
145 | 138 |
146 bool suspended_; | 139 bool suspended_; |
147 VideoCaptureState state_; | 140 VideoCaptureState state_; |
148 | 141 |
149 // WeakPtrFactory pointing back to |this| object, for use with | 142 // WeakPtrFactory pointing back to |this| object, for use with |
150 // media::VideoFrames constructed in OnBufferReceived() from buffers cached | 143 // media::VideoFrames constructed in OnBufferReceived() from buffers cached |
151 // in |client_buffers_|. | 144 // in |client_buffers_|. |
152 base::WeakPtrFactory<VideoCaptureImpl> weak_this_factory_; | 145 base::WeakPtrFactory<VideoCaptureImpl> weak_this_factory_; |
153 | 146 |
154 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImpl); | 147 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImpl); |
155 }; | 148 }; |
156 | 149 |
157 } // namespace content | 150 } // namespace content |
158 | 151 |
159 #endif // CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_ | 152 #endif // CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_ |
OLD | NEW |