Chromium Code Reviews| 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 GetVideoCaptureManager()->RemoveController(controller, this); |
| 30 base::Bind(&VideoCaptureHost::OnReadyToDelete, this, it->first)); | |
| 31 } | 31 } |
| 32 entries_.clear(); | |
| 33 entry_state_.clear(); | |
| 32 } | 34 } |
| 33 | 35 |
| 34 void VideoCaptureHost::OnDestruct() const { | 36 void VideoCaptureHost::OnDestruct() const { |
| 35 BrowserThread::DeleteOnIOThread::Destruct(this); | 37 BrowserThread::DeleteOnIOThread::Destruct(this); |
| 36 } | 38 } |
| 37 | 39 |
| 38 /////////////////////////////////////////////////////////////////////////////// | 40 /////////////////////////////////////////////////////////////////////////////// |
| 39 | 41 |
| 40 // Implements VideoCaptureControllerEventHandler. | 42 // Implements VideoCaptureControllerEventHandler. |
| 41 void VideoCaptureHost::OnError(const VideoCaptureControllerID& id) { | 43 void VideoCaptureHost::OnError(const VideoCaptureControllerID& id) { |
| 42 BrowserThread::PostTask( | 44 BrowserThread::PostTask( |
| 43 BrowserThread::IO, FROM_HERE, | 45 BrowserThread::IO, FROM_HERE, |
| 44 base::Bind(&VideoCaptureHost::DoHandleError, this, id.device_id)); | 46 base::Bind(&VideoCaptureHost::DoHandleError, this, id.device_id)); |
| 45 } | 47 } |
| 46 | 48 |
| 47 void VideoCaptureHost::OnBufferCreated( | 49 void VideoCaptureHost::OnBufferCreated( |
| 48 const VideoCaptureControllerID& id, | 50 const VideoCaptureControllerID& id, |
| 49 base::SharedMemoryHandle handle, | 51 base::SharedMemoryHandle handle, |
| 50 int length, | 52 int length, |
| 51 int buffer_id) { | 53 int buffer_id) { |
| 52 BrowserThread::PostTask( | 54 BrowserThread::PostTask( |
|
perkj_chrome
2011/10/21 14:54:00
You are already on IO thread now.
wjia(left Chromium)
2011/10/21 23:54:16
This PostTask can keep function call (from control
| |
| 53 BrowserThread::IO, FROM_HERE, | 55 BrowserThread::IO, FROM_HERE, |
| 54 base::Bind(&VideoCaptureHost::DoSendNewBuffer, | 56 base::Bind(&VideoCaptureHost::DoSendNewBuffer, |
| 55 this, id.device_id, handle, length, buffer_id)); | 57 this, id.device_id, handle, length, buffer_id)); |
| 56 } | 58 } |
| 57 | 59 |
| 58 void VideoCaptureHost::OnBufferReady( | 60 void VideoCaptureHost::OnBufferReady( |
| 59 const VideoCaptureControllerID& id, | 61 const VideoCaptureControllerID& id, |
| 60 int buffer_id, | 62 int buffer_id, |
| 61 base::Time timestamp) { | 63 base::Time timestamp) { |
| 62 BrowserThread::PostTask( | 64 BrowserThread::PostTask( |
|
perkj_chrome
2011/10/21 14:54:00
This is already on IO thread now.
wjia(left Chromium)
2011/10/21 23:54:16
same as above.
| |
| 63 BrowserThread::IO, FROM_HERE, | 65 BrowserThread::IO, FROM_HERE, |
| 64 base::Bind(&VideoCaptureHost::DoSendFilledBuffer, | 66 base::Bind(&VideoCaptureHost::DoSendFilledBuffer, |
| 65 this, id.device_id, buffer_id, timestamp)); | 67 this, id.device_id, buffer_id, timestamp)); |
| 66 } | 68 } |
| 67 | 69 |
| 68 void VideoCaptureHost::OnFrameInfo(const VideoCaptureControllerID& id, | 70 void VideoCaptureHost::OnFrameInfo(const VideoCaptureControllerID& id, |
| 69 int width, | 71 int width, |
| 70 int height, | 72 int height, |
| 71 int frame_per_second) { | 73 int frame_per_second) { |
| 72 BrowserThread::PostTask( | 74 BrowserThread::PostTask( |
|
perkj_chrome
2011/10/21 14:54:00
This is also on IO thread now.
wjia(left Chromium)
2011/10/21 23:54:16
same as above.
| |
| 73 BrowserThread::IO, FROM_HERE, | 75 BrowserThread::IO, FROM_HERE, |
| 74 base::Bind(&VideoCaptureHost::DoSendFrameInfo, | 76 base::Bind(&VideoCaptureHost::DoSendFrameInfo, |
| 75 this, id.device_id, width, height, frame_per_second)); | 77 this, id.device_id, width, height, frame_per_second)); |
| 76 } | 78 } |
| 77 | 79 |
| 78 void VideoCaptureHost::OnReadyToDelete(const VideoCaptureControllerID& id) { | 80 void VideoCaptureHost::OnReadyToDelete(const VideoCaptureControllerID& id) { |
| 79 BrowserThread::PostTask( | 81 BrowserThread::PostTask( |
| 80 BrowserThread::IO, FROM_HERE, | 82 BrowserThread::IO, FROM_HERE, |
| 81 base::Bind(&VideoCaptureHost::DoDeleteVideoCaptureController, this, id)); | 83 base::Bind(&VideoCaptureHost::DoDeleteVideoCaptureController, this, id)); |
| 82 } | 84 } |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 101 void VideoCaptureHost::DoHandleError(int device_id) { | 103 void VideoCaptureHost::DoHandleError(int device_id) { |
| 102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 103 | 105 |
| 104 Send(new VideoCaptureMsg_StateChanged(device_id, | 106 Send(new VideoCaptureMsg_StateChanged(device_id, |
| 105 media::VideoCapture::kError)); | 107 media::VideoCapture::kError)); |
| 106 | 108 |
| 107 VideoCaptureControllerID id(device_id); | 109 VideoCaptureControllerID id(device_id); |
| 108 EntryMap::iterator it = entries_.find(id); | 110 EntryMap::iterator it = entries_.find(id); |
| 109 if (it != entries_.end()) { | 111 if (it != entries_.end()) { |
| 110 VideoCaptureController* controller = it->second; | 112 VideoCaptureController* controller = it->second; |
| 111 controller->StopCapture(base::Closure()); | 113 controller->StopCapture(id, this, false); |
| 112 } | 114 } |
| 113 } | 115 } |
| 114 | 116 |
| 115 void VideoCaptureHost::DoSendFrameInfo(int device_id, | 117 void VideoCaptureHost::DoSendFrameInfo(int device_id, |
| 116 int width, | 118 int width, |
| 117 int height, | 119 int height, |
| 118 int frame_per_second) { | 120 int frame_per_second) { |
| 119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 121 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 120 | 122 |
| 121 media::VideoCaptureParams params; | 123 media::VideoCaptureParams params; |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 139 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_BufferReady, OnReceiveEmptyBuffer) | 141 IPC_MESSAGE_HANDLER(VideoCaptureHostMsg_BufferReady, OnReceiveEmptyBuffer) |
| 140 IPC_MESSAGE_UNHANDLED(handled = false) | 142 IPC_MESSAGE_UNHANDLED(handled = false) |
| 141 IPC_END_MESSAGE_MAP_EX() | 143 IPC_END_MESSAGE_MAP_EX() |
| 142 | 144 |
| 143 return handled; | 145 return handled; |
| 144 } | 146 } |
| 145 | 147 |
| 146 void VideoCaptureHost::OnStartCapture(int device_id, | 148 void VideoCaptureHost::OnStartCapture(int device_id, |
| 147 const media::VideoCaptureParams& params) { | 149 const media::VideoCaptureParams& params) { |
| 148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 150 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 151 VideoCaptureControllerID controller_id(device_id); | |
| 152 DCHECK(entries_.find(controller_id) == entries_.end()); | |
| 153 DCHECK(entry_state_.find(controller_id) == entry_state_.end()); | |
| 149 | 154 |
| 155 entry_state_[controller_id] = media::VideoCapture::kStarted; | |
| 156 GetVideoCaptureManager()->AddController( | |
| 157 params, this, base::Bind(&VideoCaptureHost::OnControllerAdded, this, | |
| 158 device_id, params)); | |
| 159 } | |
| 160 | |
| 161 void VideoCaptureHost::OnControllerAdded( | |
| 162 int device_id, const media::VideoCaptureParams& params, | |
| 163 VideoCaptureController* controller) { | |
| 164 BrowserThread::PostTask( | |
| 165 BrowserThread::IO, FROM_HERE, | |
| 166 base::Bind(&VideoCaptureHost::DoControllerAddedOnIOThreead, | |
| 167 this, device_id, params, make_scoped_refptr(controller))); | |
| 168 } | |
| 169 | |
| 170 void VideoCaptureHost::DoControllerAddedOnIOThreead( | |
| 171 int device_id, const media::VideoCaptureParams params, | |
| 172 VideoCaptureController* controller) { | |
| 173 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 150 VideoCaptureControllerID controller_id(device_id); | 174 VideoCaptureControllerID controller_id(device_id); |
| 175 DCHECK(entries_.find(controller_id) == entries_.end()); | |
| 176 DCHECK(entry_state_.find(controller_id) != entry_state_.end()); | |
| 151 | 177 |
| 152 DCHECK(entries_.find(controller_id) == entries_.end()); | 178 if (controller == NULL) { |
| 179 Send(new VideoCaptureMsg_StateChanged(device_id, | |
| 180 media::VideoCapture::kError)); | |
| 181 return; | |
| 182 } | |
| 153 | 183 |
| 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)); | 184 entries_.insert(std::make_pair(controller_id, controller)); |
| 159 controller->StartCapture(params); | 185 if (entry_state_[controller_id] == media::VideoCapture::kStarted) { |
| 186 controller->StartCapture(controller_id, this, peer_handle(), params); | |
| 187 } else if (entry_state_[controller_id] == media::VideoCapture::kStopped) { | |
| 188 controller->StopCapture(controller_id, this, false); | |
| 189 } | |
| 160 } | 190 } |
| 161 | 191 |
| 162 void VideoCaptureHost::OnStopCapture(int device_id) { | 192 void VideoCaptureHost::OnStopCapture(int device_id) { |
| 163 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 193 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 194 VideoCaptureControllerID controller_id(device_id); | |
| 164 | 195 |
| 165 VideoCaptureControllerID controller_id(device_id); | 196 if (entry_state_.find(controller_id) == entry_state_.end()) { |
| 197 // It does not exist. So it must have been stopped already. | |
| 198 Send(new VideoCaptureMsg_StateChanged(device_id, | |
| 199 media::VideoCapture::kStopped)); | |
| 200 } | |
| 201 | |
| 202 entry_state_[controller_id] = media::VideoCapture::kStopped; | |
| 203 | |
| 166 EntryMap::iterator it = entries_.find(controller_id); | 204 EntryMap::iterator it = entries_.find(controller_id); |
| 167 if (it != entries_.end()) { | 205 if (it != entries_.end()) { |
| 168 scoped_refptr<VideoCaptureController> controller = it->second; | 206 scoped_refptr<VideoCaptureController> controller = it->second; |
| 169 controller->StopCapture(base::Closure()); | 207 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 } | 208 } |
| 175 } | 209 } |
| 176 | 210 |
| 177 void VideoCaptureHost::OnPauseCapture(int device_id) { | 211 void VideoCaptureHost::OnPauseCapture(int device_id) { |
| 178 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 212 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 179 // Not used. | 213 // Not used. |
| 180 Send(new VideoCaptureMsg_StateChanged(device_id, | 214 Send(new VideoCaptureMsg_StateChanged(device_id, |
| 181 media::VideoCapture::kError)); | 215 media::VideoCapture::kError)); |
| 182 } | 216 } |
| 183 | 217 |
| 184 void VideoCaptureHost::OnReceiveEmptyBuffer(int device_id, int buffer_id) { | 218 void VideoCaptureHost::OnReceiveEmptyBuffer(int device_id, int buffer_id) { |
| 185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 219 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 186 | 220 |
| 187 VideoCaptureControllerID controller_id(device_id); | 221 VideoCaptureControllerID controller_id(device_id); |
| 188 EntryMap::iterator it = entries_.find(controller_id); | 222 EntryMap::iterator it = entries_.find(controller_id); |
| 189 if (it != entries_.end()) { | 223 if (it != entries_.end()) { |
| 190 scoped_refptr<VideoCaptureController> controller = it->second; | 224 scoped_refptr<VideoCaptureController> controller = it->second; |
| 191 controller->ReturnBuffer(buffer_id); | 225 controller->ReturnBuffer(controller_id, this, buffer_id); |
| 192 } | 226 } |
| 193 } | 227 } |
| 194 | 228 |
| 195 void VideoCaptureHost::DoDeleteVideoCaptureController( | 229 void VideoCaptureHost::DoDeleteVideoCaptureController( |
| 196 const VideoCaptureControllerID& id) { | 230 const VideoCaptureControllerID& id) { |
| 197 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 231 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 198 | 232 |
| 199 // Report that the device have successfully been stopped. | 233 // Report that the device have successfully been stopped. |
| 200 Send(new VideoCaptureMsg_StateChanged(id.device_id, | 234 Send(new VideoCaptureMsg_StateChanged(id.device_id, |
| 201 media::VideoCapture::kStopped)); | 235 media::VideoCapture::kStopped)); |
| 236 EntryMap::iterator it = entries_.find(id); | |
| 237 if (it != entries_.end()) { | |
| 238 GetVideoCaptureManager()->RemoveController(it->second, this); | |
| 239 } | |
| 202 entries_.erase(id); | 240 entries_.erase(id); |
| 241 entry_state_.erase(id); | |
| 203 } | 242 } |
| 243 | |
| 244 media_stream::VideoCaptureManager* VideoCaptureHost::GetVideoCaptureManager() { | |
| 245 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 246 return resource_context_->media_stream_manager()->video_capture_manager(); | |
| 247 } | |
| OLD | NEW |