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