Chromium Code Reviews| 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 |
|
zunger
2012/07/09 20:08:26
Overall, the flow of control in this file is spect
| |
| 5 #include "content/renderer/media/video_capture_impl.h" | 5 #include "content/renderer/media/video_capture_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "content/common/child_process.h" | 9 #include "content/common/child_process.h" |
| 10 #include "content/common/media/video_capture_messages.h" | 10 #include "content/common/media/video_capture_messages.h" |
| 11 | 11 |
| 12 struct VideoCaptureImpl::DIBBuffer { | 12 struct VideoCaptureImpl::DIBBuffer { |
| 13 public: | 13 public: |
| 14 DIBBuffer( | 14 DIBBuffer( |
| 15 base::SharedMemory* d, | 15 base::SharedMemory* d, |
| 16 media::VideoCapture::VideoFrameBuffer* ptr) | 16 media::VideoCapture::VideoFrameBuffer* ptr) |
| 17 : dib(d), | 17 : dib(d), |
| 18 mapped_memory(ptr), | 18 mapped_memory(ptr), |
| 19 references(0) { | 19 references(0) { |
| 20 } | 20 } |
| 21 ~DIBBuffer() {} | 21 ~DIBBuffer() {} |
| 22 | 22 |
| 23 scoped_ptr<base::SharedMemory> dib; | 23 scoped_ptr<base::SharedMemory> dib; |
| 24 scoped_refptr<media::VideoCapture::VideoFrameBuffer> mapped_memory; | 24 scoped_refptr<media::VideoCapture::VideoFrameBuffer> mapped_memory; |
| 25 | 25 |
| 26 // Number of clients which hold this DIB. | 26 // Number of clients which hold this DIB. |
| 27 int references; | 27 int references; |
|
zunger
2012/07/09 20:08:26
Are you sure you want to use an unlocked int for a
wjia(left Chromium)
2012/07/10 17:32:28
Yes, DIBBuffer's are accessed only on capture thre
| |
| 28 }; | 28 }; |
| 29 | 29 |
| 30 bool VideoCaptureImpl::CaptureStarted() { | 30 bool VideoCaptureImpl::CaptureStarted() { |
| 31 return state_ == video_capture::kStarted; | 31 return state_ == video_capture::kStarted; |
| 32 } | 32 } |
| 33 | 33 |
| 34 int VideoCaptureImpl::CaptureWidth() { | 34 int VideoCaptureImpl::CaptureWidth() { |
| 35 return current_params_.width; | 35 return current_params_.width; |
| 36 } | 36 } |
| 37 | 37 |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 54 video_type_(media::VideoCaptureCapability::kI420), | 54 video_type_(media::VideoCaptureCapability::kI420), |
| 55 device_info_available_(false), | 55 device_info_available_(false), |
| 56 state_(video_capture::kStopped) { | 56 state_(video_capture::kStopped) { |
| 57 DCHECK(filter); | 57 DCHECK(filter); |
| 58 memset(¤t_params_, 0, sizeof(current_params_)); | 58 memset(¤t_params_, 0, sizeof(current_params_)); |
| 59 memset(&device_info_, 0, sizeof(device_info_)); | 59 memset(&device_info_, 0, sizeof(device_info_)); |
| 60 current_params_.session_id = id; | 60 current_params_.session_id = id; |
| 61 } | 61 } |
| 62 | 62 |
| 63 VideoCaptureImpl::~VideoCaptureImpl() { | 63 VideoCaptureImpl::~VideoCaptureImpl() { |
| 64 STLDeleteContainerPairSecondPointers(cached_dibs_.begin(), | 64 STLDeleteContainerPairSecondPointers(cached_dibs_.begin(), |
|
zunger
2012/07/09 20:08:26
STLDeleteValues(&cached_dibs_);
wjia(left Chromium)
2012/07/10 17:32:28
Done.
| |
| 65 cached_dibs_.end()); | 65 cached_dibs_.end()); |
| 66 } | 66 } |
| 67 | 67 |
| 68 void VideoCaptureImpl::Init() { | 68 void VideoCaptureImpl::Init() { |
| 69 io_message_loop_proxy_ = ChildProcess::current()->io_message_loop_proxy(); | 69 io_message_loop_proxy_ = ChildProcess::current()->io_message_loop_proxy(); |
| 70 if (!io_message_loop_proxy_->BelongsToCurrentThread()) { | 70 if (!io_message_loop_proxy_->BelongsToCurrentThread()) { |
| 71 io_message_loop_proxy_->PostTask(FROM_HERE, | 71 io_message_loop_proxy_->PostTask(FROM_HERE, |
| 72 base::Bind(&VideoCaptureImpl::AddDelegateOnIOThread, | 72 base::Bind(&VideoCaptureImpl::AddDelegateOnIOThread, |
| 73 base::Unretained(this))); | 73 base::Unretained(this))); |
| 74 return; | 74 return; |
|
zunger
2012/07/09 20:08:26
I would use an 'else' instead of a 'return,' it ma
wjia(left Chromium)
2012/07/10 17:32:28
Done.
| |
| 75 } | 75 } |
| 76 | 76 |
| 77 AddDelegateOnIOThread(); | 77 AddDelegateOnIOThread(); |
| 78 } | 78 } |
| 79 | 79 |
| 80 void VideoCaptureImpl::DeInit(base::Closure task) { | 80 void VideoCaptureImpl::DeInit(base::Closure task) { |
| 81 capture_message_loop_proxy_->PostTask(FROM_HERE, | 81 capture_message_loop_proxy_->PostTask(FROM_HERE, |
| 82 base::Bind(&VideoCaptureImpl::DoDeInit, | 82 base::Bind(&VideoCaptureImpl::DoDeInit, |
| 83 base::Unretained(this), task)); | 83 base::Unretained(this), task)); |
| 84 } | 84 } |
| 85 | 85 |
| 86 void VideoCaptureImpl::DoDeInit(base::Closure task) { | 86 void VideoCaptureImpl::DoDeInit(base::Closure task) { |
| 87 if (state_ == video_capture::kStarted) | 87 if (state_ == video_capture::kStarted) |
| 88 Send(new VideoCaptureHostMsg_Stop(device_id_)); | 88 Send(new VideoCaptureHostMsg_Stop(device_id_)); |
| 89 | 89 |
| 90 io_message_loop_proxy_->PostTask(FROM_HERE, | 90 io_message_loop_proxy_->PostTask(FROM_HERE, |
| 91 base::Bind(&VideoCaptureImpl::RemoveDelegateOnIOThread, | 91 base::Bind(&VideoCaptureImpl::RemoveDelegateOnIOThread, |
| 92 base::Unretained(this), task)); | 92 base::Unretained(this), task)); |
| 93 } | 93 } |
| 94 | 94 |
| 95 void VideoCaptureImpl::StartCapture( | 95 void VideoCaptureImpl::StartCapture( |
| 96 media::VideoCapture::EventHandler* handler, | 96 media::VideoCapture::EventHandler* handler, |
| 97 const media::VideoCaptureCapability& capability) { | 97 const media::VideoCaptureCapability& capability) { |
| 98 DCHECK_EQ(capability.color, media::VideoCaptureCapability::kI420); | 98 DCHECK_EQ(capability.color, media::VideoCaptureCapability::kI420); |
| 99 | 99 |
| 100 capture_message_loop_proxy_->PostTask(FROM_HERE, | 100 capture_message_loop_proxy_->PostTask(FROM_HERE, |
| 101 base::Bind(&VideoCaptureImpl::DoStartCapture, | 101 base::Bind(&VideoCaptureImpl::DoStartCapture, |
| 102 base::Unretained(this), handler, capability)); | 102 base::Unretained(this), handler, capability)); |
|
zunger
2012/07/09 20:08:26
You've got a subtle segfault risk here: capability
wjia(left Chromium)
2012/07/10 17:32:28
In Chromium, base::Bind does make a copy of const
| |
| 103 } | 103 } |
| 104 | 104 |
| 105 void VideoCaptureImpl::StopCapture(media::VideoCapture::EventHandler* handler) { | 105 void VideoCaptureImpl::StopCapture(media::VideoCapture::EventHandler* handler) { |
| 106 capture_message_loop_proxy_->PostTask(FROM_HERE, | 106 capture_message_loop_proxy_->PostTask(FROM_HERE, |
| 107 base::Bind(&VideoCaptureImpl::DoStopCapture, | 107 base::Bind(&VideoCaptureImpl::DoStopCapture, |
| 108 base::Unretained(this), handler)); | 108 base::Unretained(this), handler)); |
| 109 } | 109 } |
| 110 | 110 |
| 111 void VideoCaptureImpl::FeedBuffer(scoped_refptr<VideoFrameBuffer> buffer) { | 111 void VideoCaptureImpl::FeedBuffer(scoped_refptr<VideoFrameBuffer> buffer) { |
| 112 capture_message_loop_proxy_->PostTask(FROM_HERE, | 112 capture_message_loop_proxy_->PostTask(FROM_HERE, |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 151 media::VideoCapture::EventHandler* handler, | 151 media::VideoCapture::EventHandler* handler, |
| 152 const media::VideoCaptureCapability& capability) { | 152 const media::VideoCaptureCapability& capability) { |
| 153 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); | 153 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); |
| 154 | 154 |
| 155 if (state_ == video_capture::kError) { | 155 if (state_ == video_capture::kError) { |
| 156 handler->OnError(this, 1); | 156 handler->OnError(this, 1); |
| 157 handler->OnRemoved(this); | 157 handler->OnRemoved(this); |
| 158 return; | 158 return; |
| 159 } | 159 } |
| 160 | 160 |
| 161 ClientInfo::iterator it1 = clients_pending_on_filter_.find(handler); | 161 ClientInfo::iterator it1 = clients_pending_on_filter_.find(handler); |
|
zunger
2012/07/09 20:08:26
Why are you creating these interim variables? (NB
wjia(left Chromium)
2012/07/10 17:32:28
Removed interim variables. The original intention
| |
| 162 ClientInfo::iterator it2 = clients_pending_on_restart_.find(handler); | 162 ClientInfo::iterator it2 = clients_pending_on_restart_.find(handler); |
| 163 if (it1 != clients_pending_on_filter_.end() || | 163 if (it1 != clients_pending_on_filter_.end() || |
| 164 it2 != clients_pending_on_restart_.end() || | 164 it2 != clients_pending_on_restart_.end() || |
| 165 clients_.find(handler) != clients_.end() ) { | 165 clients_.find(handler) != clients_.end() ) { |
| 166 // This client has started. | 166 // This client has started. |
| 167 return; | 167 return; |
| 168 } | 168 } |
| 169 | 169 |
| 170 if (!device_id_) { | 170 if (!device_id_) { |
| 171 clients_pending_on_filter_[handler] = capability; | 171 clients_pending_on_filter_[handler] = capability; |
| 172 return; | 172 return; |
| 173 } | 173 } |
| 174 | 174 |
| 175 handler->OnStarted(this); | 175 handler->OnStarted(this); |
| 176 if (state_ == video_capture::kStarted) { | 176 if (state_ == video_capture::kStarted) { |
| 177 if (capability.width > current_params_.width || | 177 if (capability.width > current_params_.width || |
| 178 capability.height > current_params_.height) { | 178 capability.height > current_params_.height) { |
| 179 StopDevice(); | 179 StopDevice(); |
| 180 DVLOG(1) << "StartCapture: Got client with higher resolution (" | 180 DVLOG(1) << "StartCapture: Got client with higher resolution (" |
| 181 << capability.width << ", " << capability.height << ") " | 181 << capability.width << ", " << capability.height << ") " |
| 182 << "after started, try to restart."; | 182 << "after started, try to restart."; |
| 183 clients_pending_on_restart_[handler] = capability; | 183 clients_pending_on_restart_[handler] = capability; |
| 184 return; | 184 return; |
|
zunger
2012/07/09 20:08:26
Way too many 'returns' in this function. It sugges
wjia(left Chromium)
2012/07/10 17:32:28
Done.
| |
| 185 } | 185 } |
| 186 | 186 |
| 187 if (device_info_available_) { | 187 if (device_info_available_) { |
| 188 handler->OnDeviceInfoReceived(this, device_info_); | 188 handler->OnDeviceInfoReceived(this, device_info_); |
| 189 } | 189 } |
| 190 | 190 |
| 191 clients_[handler] = capability; | 191 clients_[handler] = capability; |
| 192 return; | 192 return; |
| 193 } | 193 } |
| 194 | 194 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 220 if (it != clients_pending_on_filter_.end()) { | 220 if (it != clients_pending_on_filter_.end()) { |
| 221 handler->OnStopped(this); | 221 handler->OnStopped(this); |
| 222 handler->OnRemoved(this); | 222 handler->OnRemoved(this); |
| 223 clients_pending_on_filter_.erase(it); | 223 clients_pending_on_filter_.erase(it); |
| 224 return; | 224 return; |
| 225 } | 225 } |
| 226 it = clients_pending_on_restart_.find(handler); | 226 it = clients_pending_on_restart_.find(handler); |
| 227 if (it != clients_pending_on_restart_.end()) { | 227 if (it != clients_pending_on_restart_.end()) { |
| 228 handler->OnStopped(this); | 228 handler->OnStopped(this); |
| 229 handler->OnRemoved(this); | 229 handler->OnRemoved(this); |
| 230 clients_pending_on_filter_.erase(it); | 230 clients_pending_on_filter_.erase(it); |
|
zunger
2012/07/09 20:08:26
Don't you mean clients_pending_on_restart_?
Maybe
wjia(left Chromium)
2012/07/10 17:32:28
Good catch! Thanks! It should be clients_pending_o
| |
| 231 return; | 231 return; |
| 232 } | 232 } |
| 233 | 233 |
| 234 if (clients_.find(handler) == clients_.end()) | 234 if (clients_.find(handler) == clients_.end()) |
| 235 return; | 235 return; |
| 236 | 236 |
| 237 clients_.erase(handler); | 237 clients_.erase(handler); |
| 238 | 238 |
| 239 if (clients_.empty()) { | 239 if (clients_.empty()) { |
| 240 DVLOG(1) << "StopCapture: No more client, stopping ..."; | 240 DVLOG(1) << "StopCapture: No more client, stopping ..."; |
| 241 StopDevice(); | 241 StopDevice(); |
| 242 } | 242 } |
| 243 handler->OnStopped(this); | 243 handler->OnStopped(this); |
| 244 handler->OnRemoved(this); | 244 handler->OnRemoved(this); |
| 245 } | 245 } |
| 246 | 246 |
| 247 void VideoCaptureImpl::DoFeedBuffer(scoped_refptr<VideoFrameBuffer> buffer) { | 247 void VideoCaptureImpl::DoFeedBuffer(scoped_refptr<VideoFrameBuffer> buffer) { |
| 248 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); | 248 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); |
| 249 | 249 |
| 250 CachedDIB::iterator it; | 250 CachedDIB::iterator it; |
| 251 for (it = cached_dibs_.begin(); it != cached_dibs_.end(); it++) { | 251 for (it = cached_dibs_.begin(); it != cached_dibs_.end(); it++) { |
|
zunger
2012/07/09 20:08:26
++it !!
wjia(left Chromium)
2012/07/10 17:32:28
Done.
| |
| 252 if (buffer == it->second->mapped_memory) | 252 if (buffer == it->second->mapped_memory) |
| 253 break; | 253 break; |
| 254 } | 254 } |
| 255 | 255 |
| 256 DCHECK(it != cached_dibs_.end()); | 256 DCHECK(it != cached_dibs_.end()); |
|
zunger
2012/07/09 20:08:26
NB that this will crash the address space in debug
wjia(left Chromium)
2012/07/10 17:32:28
The clients are supposed to return only buffers th
| |
| 257 DCHECK_GT(it->second->references, 0); | 257 DCHECK_GT(it->second->references, 0); |
| 258 it->second->references--; | 258 it->second->references--; |
|
zunger
2012/07/09 20:08:26
predecrement, but I'm pretty sure that this is not
wjia(left Chromium)
2012/07/10 17:32:28
This "references" means the number of clients whic
| |
| 259 if (it->second->references == 0) { | 259 if (it->second->references == 0) { |
| 260 Send(new VideoCaptureHostMsg_BufferReady(device_id_, it->first)); | 260 Send(new VideoCaptureHostMsg_BufferReady(device_id_, it->first)); |
| 261 } | 261 } |
| 262 } | 262 } |
| 263 | 263 |
| 264 void VideoCaptureImpl::DoBufferCreated( | 264 void VideoCaptureImpl::DoBufferCreated( |
| 265 base::SharedMemoryHandle handle, | 265 base::SharedMemoryHandle handle, |
| 266 int length, int buffer_id) { | 266 int length, int buffer_id) { |
| 267 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); | 267 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); |
| 268 DCHECK(device_info_available_); | 268 DCHECK(device_info_available_); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 309 case video_capture::kStarted: | 309 case video_capture::kStarted: |
| 310 break; | 310 break; |
| 311 case video_capture::kStopped: | 311 case video_capture::kStopped: |
| 312 state_ = video_capture::kStopped; | 312 state_ = video_capture::kStopped; |
| 313 DVLOG(1) << "OnStateChanged: stopped!, device_id = " << device_id_; | 313 DVLOG(1) << "OnStateChanged: stopped!, device_id = " << device_id_; |
| 314 STLDeleteValues(&cached_dibs_); | 314 STLDeleteValues(&cached_dibs_); |
| 315 if (!clients_.empty() || !clients_pending_on_restart_.empty()) | 315 if (!clients_.empty() || !clients_pending_on_restart_.empty()) |
| 316 RestartCapture(); | 316 RestartCapture(); |
| 317 break; | 317 break; |
| 318 case video_capture::kPaused: | 318 case video_capture::kPaused: |
| 319 for (ClientInfo::iterator it = clients_.begin(); | 319 for (ClientInfo::iterator it = clients_.begin(); |
|
zunger
2012/07/09 20:08:26
You can now use the 'auto' keyword here.
wjia(left Chromium)
2012/07/10 17:32:28
I tried to add "auto" here, but the compiler says:
| |
| 320 it != clients_.end(); it++) { | 320 it != clients_.end(); it++) { |
|
zunger
2012/07/09 20:08:26
preincrement!!
wjia(left Chromium)
2012/07/10 17:32:28
Done.
| |
| 321 it->first->OnPaused(this); | 321 it->first->OnPaused(this); |
| 322 } | 322 } |
| 323 break; | 323 break; |
| 324 case video_capture::kError: | 324 case video_capture::kError: |
| 325 DVLOG(1) << "OnStateChanged: error!, device_id = " << device_id_; | 325 DVLOG(1) << "OnStateChanged: error!, device_id = " << device_id_; |
| 326 for (ClientInfo::iterator it = clients_.begin(); | 326 for (ClientInfo::iterator it = clients_.begin(); |
| 327 it != clients_.end(); it++) { | 327 it != clients_.end(); it++) { |
| 328 // TODO(wjia): browser process would send error code. | 328 // TODO(wjia): browser process would send error code. |
| 329 it->first->OnError(this, 1); | 329 it->first->OnError(this, 1); |
| 330 it->first->OnRemoved(this); | 330 it->first->OnRemoved(this); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 374 Send(new VideoCaptureHostMsg_Stop(device_id_)); | 374 Send(new VideoCaptureHostMsg_Stop(device_id_)); |
| 375 current_params_.width = current_params_.height = 0; | 375 current_params_.width = current_params_.height = 0; |
| 376 } | 376 } |
| 377 } | 377 } |
| 378 | 378 |
| 379 void VideoCaptureImpl::RestartCapture() { | 379 void VideoCaptureImpl::RestartCapture() { |
| 380 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); | 380 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); |
| 381 DCHECK_EQ(state_, video_capture::kStopped); | 381 DCHECK_EQ(state_, video_capture::kStopped); |
| 382 | 382 |
| 383 int width = 0; | 383 int width = 0; |
| 384 int height = 0; | 384 int height = 0; |
|
zunger
2012/07/09 20:08:26
Shouldn't you do this loop second, so that if one
wjia(left Chromium)
2012/07/10 17:32:28
No client is supposed to have same address as anot
| |
| 385 for (ClientInfo::iterator it = clients_.begin(); | 385 for (ClientInfo::iterator it = clients_.begin(); |
| 386 it != clients_.end(); it++) { | 386 it != clients_.end(); it++) { |
| 387 if (it->second.width > width) | 387 if (it->second.width > width) |
|
zunger
2012/07/09 20:08:26
with = max(width, it->second.width);
wjia(left Chromium)
2012/07/10 17:32:28
Done.
| |
| 388 width = it->second.width; | 388 width = it->second.width; |
| 389 if (it->second.height > height) | 389 if (it->second.height > height) |
| 390 height = it->second.height; | 390 height = it->second.height; |
| 391 } | 391 } |
| 392 for (ClientInfo::iterator it = clients_pending_on_restart_.begin(); | 392 for (ClientInfo::iterator it = clients_pending_on_restart_.begin(); |
| 393 it != clients_pending_on_restart_.end(); ) { | 393 it != clients_pending_on_restart_.end(); ) { |
| 394 if (it->second.width > width) | 394 if (it->second.width > width) |
| 395 width = it->second.width; | 395 width = it->second.width; |
| 396 if (it->second.height > height) | 396 if (it->second.height > height) |
| 397 height = it->second.height; | 397 height = it->second.height; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 431 } | 431 } |
| 432 | 432 |
| 433 bool VideoCaptureImpl::ClientHasDIB() { | 433 bool VideoCaptureImpl::ClientHasDIB() { |
| 434 CachedDIB::iterator it; | 434 CachedDIB::iterator it; |
| 435 for (it = cached_dibs_.begin(); it != cached_dibs_.end(); it++) { | 435 for (it = cached_dibs_.begin(); it != cached_dibs_.end(); it++) { |
| 436 if (it->second->references > 0) | 436 if (it->second->references > 0) |
| 437 return true; | 437 return true; |
| 438 } | 438 } |
| 439 return false; | 439 return false; |
| 440 } | 440 } |
| OLD | NEW |