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