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