OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include "content/browser/renderer_host/media/video_capture_host.h" | 5 #include "content/browser/renderer_host/media/video_capture_host.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "content/browser/renderer_host/media/media_stream_manager.h" | 10 #include "content/browser/renderer_host/media/media_stream_manager.h" |
11 #include "content/browser/renderer_host/media/video_capture_manager.h" | |
11 #include "content/browser/resource_context.h" | 12 #include "content/browser/resource_context.h" |
12 #include "content/common/media/video_capture_messages.h" | 13 #include "content/common/media/video_capture_messages.h" |
13 | 14 |
14 VideoCaptureHost::VideoCaptureHost( | 15 VideoCaptureHost::VideoCaptureHost( |
15 const content::ResourceContext* resource_context) | 16 const content::ResourceContext* resource_context) |
16 : resource_context_(resource_context) { | 17 : resource_context_(resource_context) { |
17 } | 18 } |
18 | 19 |
19 VideoCaptureHost::~VideoCaptureHost() {} | 20 VideoCaptureHost::~VideoCaptureHost() {} |
20 | 21 |
21 void VideoCaptureHost::OnChannelClosing() { | 22 void VideoCaptureHost::OnChannelClosing() { |
22 BrowserMessageFilter::OnChannelClosing(); | 23 BrowserMessageFilter::OnChannelClosing(); |
23 | 24 |
24 // Since the IPC channel is gone, close all requested VideCaptureDevices. | 25 // Since the IPC channel is gone, close all requested VideCaptureDevices. |
25 for (EntryMap::iterator it = entries_.begin(); it != entries_.end(); it++) { | 26 for (EntryMap::iterator it = entries_.begin(); it != entries_.end(); it++) { |
26 VideoCaptureController* controller = it->second; | 27 VideoCaptureController* controller = it->second; |
27 // Since the channel is closing we need a task to make sure VideoCaptureHost | 28 VideoCaptureControllerID controller_id(it->first); |
28 // is not deleted before VideoCaptureController. | 29 controller->StopCapture(controller_id, this, true); |
29 controller->StopCapture( | |
30 base::Bind(&VideoCaptureHost::OnReadyToDelete, this, it->first)); | |
31 } | 30 } |
32 } | 31 } |
33 | 32 |
34 void VideoCaptureHost::OnDestruct() const { | 33 void VideoCaptureHost::OnDestruct() const { |
35 BrowserThread::DeleteOnIOThread::Destruct(this); | 34 BrowserThread::DeleteOnIOThread::Destruct(this); |
36 } | 35 } |
37 | 36 |
38 /////////////////////////////////////////////////////////////////////////////// | 37 /////////////////////////////////////////////////////////////////////////////// |
39 | 38 |
40 // Implements VideoCaptureControllerEventHandler. | 39 // Implements VideoCaptureControllerEventHandler. |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
101 void VideoCaptureHost::DoHandleError(int device_id) { | 100 void VideoCaptureHost::DoHandleError(int device_id) { |
102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 101 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
103 | 102 |
104 Send(new VideoCaptureMsg_StateChanged(device_id, | 103 Send(new VideoCaptureMsg_StateChanged(device_id, |
105 media::VideoCapture::kError)); | 104 media::VideoCapture::kError)); |
106 | 105 |
107 VideoCaptureControllerID id(device_id); | 106 VideoCaptureControllerID id(device_id); |
108 EntryMap::iterator it = entries_.find(id); | 107 EntryMap::iterator it = entries_.find(id); |
109 if (it != entries_.end()) { | 108 if (it != entries_.end()) { |
110 VideoCaptureController* controller = it->second; | 109 VideoCaptureController* controller = it->second; |
111 controller->StopCapture(base::Closure()); | 110 controller->StopCapture(id, this, false); |
112 } | 111 } |
113 } | 112 } |
114 | 113 |
115 void VideoCaptureHost::DoSendFrameInfo(int device_id, | 114 void VideoCaptureHost::DoSendFrameInfo(int device_id, |
116 int width, | 115 int width, |
117 int height, | 116 int height, |
118 int frame_per_second) { | 117 int frame_per_second) { |
119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 118 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
120 | 119 |
121 media::VideoCaptureParams params; | 120 media::VideoCaptureParams params; |
(...skipping 17 matching lines...) Expand all Loading... | |
139 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_BufferReady, OnReceiveEmptyBuffer) | 138 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_BufferReady, OnReceiveEmptyBuffer) |
140 IPC_MESSAGE_UNHANDLED(handled = false) | 139 IPC_MESSAGE_UNHANDLED(handled = false) |
141 IPC_END_MESSAGE_MAP_EX() | 140 IPC_END_MESSAGE_MAP_EX() |
142 | 141 |
143 return handled; | 142 return handled; |
144 } | 143 } |
145 | 144 |
146 void VideoCaptureHost::OnStartCapture(int device_id, | 145 void VideoCaptureHost::OnStartCapture(int device_id, |
147 const media::VideoCaptureParams& params) { | 146 const media::VideoCaptureParams& params) { |
148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
148 VideoCaptureControllerID controller_id(device_id); | |
149 DCHECK(entries_.find(controller_id) == entries_.end()); | |
150 DCHECK(entry_state_.find(controller_id) == entry_state_.end()); | |
149 | 151 |
152 entry_state_[controller_id] = media::VideoCapture::kStarted; | |
153 GetVideoCaptureManager()->AddController( | |
154 params, this, base::Bind(&VideoCaptureHost::OnControllerAdded, this, | |
155 device_id, params)); | |
156 } | |
157 | |
158 void VideoCaptureHost::OnControllerAdded( | |
159 int device_id, const media::VideoCaptureParams& params, | |
160 VideoCaptureController* controller) { | |
161 BrowserThread::PostTask( | |
162 BrowserThread::IO, FROM_HERE, | |
163 base::Bind(&VideoCaptureHost::DoControllerAddedOnIOThreead, | |
perkj_chrome
2011/10/17 08:39:43
isn't this already on the io thread?
mflodman_chromium_OOO
2011/10/19 18:18:23
This is called from the video manager thread.
Wei
wjia(left Chromium)
2011/10/21 00:56:13
It's called from manager's device thread.
wjia(left Chromium)
2011/10/21 00:56:13
I am fine with either way. VCManager will have lot
| |
164 this, device_id, params, make_scoped_refptr(controller))); | |
165 } | |
166 | |
167 void VideoCaptureHost::DoControllerAddedOnIOThreead( | |
168 int device_id, const media::VideoCaptureParams params, | |
169 VideoCaptureController* controller) { | |
170 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
150 VideoCaptureControllerID controller_id(device_id); | 171 VideoCaptureControllerID controller_id(device_id); |
172 DCHECK(entries_.find(controller_id) == entries_.end()); | |
173 DCHECK(entry_state_.find(controller_id) != entry_state_.end()); | |
151 | 174 |
152 DCHECK(entries_.find(controller_id) == entries_.end()); | 175 if (controller == NULL) { |
176 Send(new VideoCaptureMsg_StateChanged(device_id, | |
177 media::VideoCapture::kError)); | |
178 return; | |
179 } | |
153 | 180 |
154 scoped_refptr<VideoCaptureController> controller = | |
155 new VideoCaptureController( | |
156 controller_id, peer_handle(), this, | |
157 resource_context_->media_stream_manager()->video_capture_manager()); | |
158 entries_.insert(std::make_pair(controller_id, controller)); | 181 entries_.insert(std::make_pair(controller_id, controller)); |
159 controller->StartCapture(params); | 182 if (entry_state_[controller_id] == media::VideoCapture::kStarted) { |
183 controller->StartCapture(controller_id, this, peer_handle(), params); | |
184 } else if (entry_state_[controller_id] == media::VideoCapture::kStopped) { | |
185 controller->StopCapture(controller_id, this, false); | |
186 } | |
160 } | 187 } |
161 | 188 |
162 void VideoCaptureHost::OnStopCapture(int device_id) { | 189 void VideoCaptureHost::OnStopCapture(int device_id) { |
163 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
191 VideoCaptureControllerID controller_id(device_id); | |
164 | 192 |
165 VideoCaptureControllerID controller_id(device_id); | 193 if (entry_state_.find(controller_id) == entry_state_.end()) { |
194 // It does not exist. So it must have been stopped already. | |
195 Send(new VideoCaptureMsg_StateChanged(device_id, | |
196 media::VideoCapture::kStopped)); | |
197 } | |
198 | |
199 entry_state_[controller_id] = media::VideoCapture::kStopped; | |
200 | |
166 EntryMap::iterator it = entries_.find(controller_id); | 201 EntryMap::iterator it = entries_.find(controller_id); |
167 if (it != entries_.end()) { | 202 if (it != entries_.end()) { |
168 scoped_refptr<VideoCaptureController> controller = it->second; | 203 scoped_refptr<VideoCaptureController> controller = it->second; |
169 controller->StopCapture(base::Closure()); | 204 controller->StopCapture(controller_id, this, false); |
170 } else { | |
171 // It does not exist so it must have been stopped already. | |
172 Send(new VideoCaptureMsg_StateChanged(device_id, | |
173 media::VideoCapture::kStopped)); | |
174 } | 205 } |
175 } | 206 } |
176 | 207 |
177 void VideoCaptureHost::OnPauseCapture(int device_id) { | 208 void VideoCaptureHost::OnPauseCapture(int device_id) { |
178 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
179 // Not used. | 210 // Not used. |
180 Send(new VideoCaptureMsg_StateChanged(device_id, | 211 Send(new VideoCaptureMsg_StateChanged(device_id, |
181 media::VideoCapture::kError)); | 212 media::VideoCapture::kError)); |
182 } | 213 } |
183 | 214 |
184 void VideoCaptureHost::OnReceiveEmptyBuffer(int device_id, int buffer_id) { | 215 void VideoCaptureHost::OnReceiveEmptyBuffer(int device_id, int buffer_id) { |
185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 216 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
186 | 217 |
187 VideoCaptureControllerID controller_id(device_id); | 218 VideoCaptureControllerID controller_id(device_id); |
188 EntryMap::iterator it = entries_.find(controller_id); | 219 EntryMap::iterator it = entries_.find(controller_id); |
189 if (it != entries_.end()) { | 220 if (it != entries_.end()) { |
190 scoped_refptr<VideoCaptureController> controller = it->second; | 221 scoped_refptr<VideoCaptureController> controller = it->second; |
191 controller->ReturnBuffer(buffer_id); | 222 controller->ReturnBuffer(controller_id, this, buffer_id); |
192 } | 223 } |
193 } | 224 } |
194 | 225 |
195 void VideoCaptureHost::DoDeleteVideoCaptureController( | 226 void VideoCaptureHost::DoDeleteVideoCaptureController( |
196 const VideoCaptureControllerID& id) { | 227 const VideoCaptureControllerID& id) { |
197 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 228 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
198 | 229 |
199 // Report that the device have successfully been stopped. | 230 // Report that the device have successfully been stopped. |
200 Send(new VideoCaptureMsg_StateChanged(id.device_id, | 231 Send(new VideoCaptureMsg_StateChanged(id.device_id, |
201 media::VideoCapture::kStopped)); | 232 media::VideoCapture::kStopped)); |
233 EntryMap::iterator it = entries_.find(id); | |
234 if (it != entries_.end()) { | |
235 GetVideoCaptureManager()->RemoveController(it->second, this); | |
236 } | |
202 entries_.erase(id); | 237 entries_.erase(id); |
238 entry_state_.erase(id); | |
203 } | 239 } |
240 | |
241 media_stream::VideoCaptureManager* VideoCaptureHost::GetVideoCaptureManager() { | |
242 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
243 return resource_context_->media_stream_manager()->video_capture_manager(); | |
244 } | |
OLD | NEW |