Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(155)

Side by Side Diff: content/renderer/media/video_capture_impl.h

Issue 120893002: Eliminate video capture thread in renderer (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: compile warning Created 6 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
15 // from any threads without worrying about thread safety. 15 // from any threads without worrying about thread safety.
16 // The |capture_message_loop_proxy_| is the working thread of VideoCaptureImpl. 16 // The |capture_message_loop_proxy_| is the working thread of VideoCaptureImpl.
17 // All non-const members are accessed only on that working thread. 17 // All non-const members are accessed only on that working thread.
18 // 18 //
19 // Implementation note: tasks are posted bound to Unretained(this) to both the 19 // 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 20 // 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 21 // process) because VideoCaptureImplManager only triggers deletion of its
22 // scoped to the VideoCaptureImplManager) because VideoCaptureImplManager only 22 // VideoCaptureImpl's by calling DeInit which detours through the capture & I/O
Jói 2013/12/27 13:34:35 This line references the capture thread, I think y
Alpha Left Google 2013/12/27 21:30:01 Done.
23 // triggers deletion of its VideoCaptureImpl's by calling DeInit which detours 23 // threads, so as long as nobody posts tasks after the DeInit() call is made, it
24 // through the capture & I/O threads, so as long as nobody posts tasks after the 24 // is guaranteed none of these Unretained posted tasks will dangle after the
25 // DeInit() call is made, it is guaranteed none of these Unretained posted tasks 25 // delete 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 26 // VideoCaptureImplManager not using devices after they've released
27 // clients of VideoCaptureImplManager not using devices after they've 27 // VideoCaptureHandle, which is a wrapper of this object.
28 // RemoveDevice'd them.
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 virtual ~VideoCaptureImpl();
52
53 // Stop/resume delivering video frames to clients, based on flag |suspend|.
54 void SuspendCapture(bool suspend);
55
52 // media::VideoCapture interface. 56 // media::VideoCapture interface.
53 virtual void StartCapture( 57 virtual void StartCapture(
54 media::VideoCapture::EventHandler* handler, 58 media::VideoCapture::EventHandler* handler,
55 const media::VideoCaptureParams& params) OVERRIDE; 59 const media::VideoCaptureParams& params) OVERRIDE;
56 virtual void StopCapture(media::VideoCapture::EventHandler* handler) OVERRIDE; 60 virtual void StopCapture(media::VideoCapture::EventHandler* handler) OVERRIDE;
57 virtual bool CaptureStarted() OVERRIDE; 61 virtual bool CaptureStarted() OVERRIDE;
58 virtual int CaptureFrameRate() OVERRIDE; 62 virtual int CaptureFrameRate() OVERRIDE;
59 63
60 // VideoCaptureMessageFilter::Delegate interface. 64 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 65
75 private: 66 private:
76 friend class VideoCaptureImplManager; 67 friend class VideoCaptureImplManager;
77 friend class VideoCaptureImplTest; 68 friend class VideoCaptureImplTest;
78 friend class MockVideoCaptureImpl; 69 friend class MockVideoCaptureImpl;
79 70
80 class ClientBuffer; 71 class ClientBuffer;
81 typedef std::map<media::VideoCapture::EventHandler*, 72 typedef std::map<media::VideoCapture::EventHandler*,
82 media::VideoCaptureParams> ClientInfo; 73 media::VideoCaptureParams> ClientInfo;
83 74
84 VideoCaptureImpl(media::VideoCaptureSessionId session_id, 75 VideoCaptureImpl(media::VideoCaptureSessionId session_id,
85 base::MessageLoopProxy* capture_message_loop_proxy,
86 VideoCaptureMessageFilter* filter); 76 VideoCaptureMessageFilter* filter);
87 virtual ~VideoCaptureImpl();
88 77
89 void DoStartCaptureOnCaptureThread( 78 // VideoCaptureMessageFilter::Delegate interface.
90 media::VideoCapture::EventHandler* handler, 79 virtual void OnBufferCreated(base::SharedMemoryHandle handle,
91 const media::VideoCaptureParams& params); 80 int length,
92 void DoStopCaptureOnCaptureThread(media::VideoCapture::EventHandler* handler); 81 int buffer_id) OVERRIDE;
93 void DoBufferCreatedOnCaptureThread(base::SharedMemoryHandle handle, 82 virtual void OnBufferDestroyed(int buffer_id) OVERRIDE;
94 int length, 83 virtual void OnBufferReceived(
95 int buffer_id);
96 void DoBufferDestroyedOnCaptureThread(int buffer_id);
97 void DoBufferReceivedOnCaptureThread(
98 int buffer_id, 84 int buffer_id,
99 base::Time timestamp, 85 base::Time timestamp,
100 const media::VideoCaptureFormat& format); 86 const media::VideoCaptureFormat& format) OVERRIDE;
101 void DoClientBufferFinishedOnCaptureThread( 87 virtual void OnStateChanged(VideoCaptureState state) OVERRIDE;
88 virtual void OnDelegateAdded(int32 device_id) OVERRIDE;
89
90 // Sends a IPC message to browser all clients are done with the video frame.
91 void OnClientBufferFinished(
102 int buffer_id, 92 int buffer_id,
103 const scoped_refptr<ClientBuffer>& buffer); 93 const scoped_refptr<ClientBuffer>& buffer);
104 void DoStateChangedOnCaptureThread(VideoCaptureState state);
105 void DoDelegateAddedOnCaptureThread(int32 device_id);
106
107 void DoSuspendCaptureOnCaptureThread(bool suspend);
108 94
109 void Init(); 95 void Init();
110 void DeInit(base::Closure task); 96 void DeInit(base::Closure task);
111 void DoDeInitOnCaptureThread(base::Closure task);
112 void StopDevice(); 97 void StopDevice();
113 void RestartCapture(); 98 void RestartCapture();
114 void StartCaptureInternal(); 99 void StartCaptureInternal();
115 void AddDelegateOnIOThread();
116 void RemoveDelegateOnIOThread(base::Closure task);
117 virtual void Send(IPC::Message* message); 100 virtual void Send(IPC::Message* message);
118 101
119 // Helpers. 102 // Helpers.
120 bool RemoveClient(media::VideoCapture::EventHandler* handler, 103 bool RemoveClient(media::VideoCapture::EventHandler* handler,
121 ClientInfo* clients); 104 ClientInfo* clients);
122 105
123 const scoped_refptr<VideoCaptureMessageFilter> message_filter_; 106 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_; 107 const scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_;
126 int device_id_; 108 int device_id_;
127 const int session_id_; 109 const int session_id_;
128 110
129 // Buffers available for sending to the client. 111 // Buffers available for sending to the client.
130 typedef std::map<int32, scoped_refptr<ClientBuffer> > ClientBufferMap; 112 typedef std::map<int32, scoped_refptr<ClientBuffer> > ClientBufferMap;
131 ClientBufferMap client_buffers_; 113 ClientBufferMap client_buffers_;
132 114
133 ClientInfo clients_; 115 ClientInfo clients_;
134 ClientInfo clients_pending_on_filter_; 116 ClientInfo clients_pending_on_filter_;
135 ClientInfo clients_pending_on_restart_; 117 ClientInfo clients_pending_on_restart_;
136 118
137 // Member params_ represents the video format requested by the 119 // Member params_ represents the video format requested by the
138 // client to this class via DoStartCaptureOnCaptureThread. 120 // client to this class via DoStartCaptureOnIOThread.
139 media::VideoCaptureParams params_; 121 media::VideoCaptureParams params_;
140 122
141 // The device's video capture format sent from browser process side. 123 // The device's video capture format sent from browser process side.
142 media::VideoCaptureFormat last_frame_format_; 124 media::VideoCaptureFormat last_frame_format_;
143 125
144 bool suspended_; 126 bool suspended_;
145 VideoCaptureState state_; 127 VideoCaptureState state_;
146 128
147 // WeakPtrFactory pointing back to |this| object, for use with 129 // WeakPtrFactory pointing back to |this| object, for use with
148 // media::VideoFrames constructed in OnBufferReceived() from buffers cached 130 // media::VideoFrames constructed in OnBufferReceived() from buffers cached
149 // in |client_buffers_|. 131 // in |client_buffers_|.
150 base::WeakPtrFactory<VideoCaptureImpl> weak_this_factory_; 132 base::WeakPtrFactory<VideoCaptureImpl> weak_this_factory_;
151 133
152 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImpl); 134 DISALLOW_COPY_AND_ASSIGN(VideoCaptureImpl);
153 }; 135 };
154 136
155 } // namespace content 137 } // namespace content
156 138
157 #endif // CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_ 139 #endif // CONTENT_RENDERER_MEDIA_VIDEO_CAPTURE_IMPL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698