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 // |
| 5 // Notes about usage of this object by VideoCaptureImplManager. | 5 // Notes about usage of this object by VideoCaptureImplManager. |
| 6 // | 6 // |
| 7 // VideoCaptureImplManager access this object by using a Unretained() | 7 // VideoCaptureImplManager access this object by using a Unretained() |
| 8 // binding and tasks on the IO thread. It is then important that | 8 // binding and tasks on the IO thread. It is then important that |
| 9 // VideoCaptureImpl never post task to itself. All operations must be | 9 // VideoCaptureImpl never post task to itself. All operations must be |
| 10 // synchronous. | 10 // synchronous. |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 191 client_buffers_.insert(std::make_pair( | 191 client_buffers_.insert(std::make_pair( |
| 192 buffer_id, | 192 buffer_id, |
| 193 new ClientBuffer(shm.Pass(), | 193 new ClientBuffer(shm.Pass(), |
| 194 length))).second; | 194 length))).second; |
| 195 DCHECK(inserted); | 195 DCHECK(inserted); |
| 196 } | 196 } |
| 197 | 197 |
| 198 void VideoCaptureImpl::OnBufferDestroyed(int buffer_id) { | 198 void VideoCaptureImpl::OnBufferDestroyed(int buffer_id) { |
| 199 DCHECK(thread_checker_.CalledOnValidThread()); | 199 DCHECK(thread_checker_.CalledOnValidThread()); |
| 200 | 200 |
| 201 ClientBufferMap::iterator iter = client_buffers_.find(buffer_id); | 201 const ClientBufferMap::iterator iter = client_buffers_.find(buffer_id); |
| 202 if (iter == client_buffers_.end()) | 202 if (iter == client_buffers_.end()) |
| 203 return; | 203 return; |
| 204 | 204 |
| 205 DCHECK(!iter->second.get() || iter->second->HasOneRef()) | 205 DCHECK(!iter->second.get() || iter->second->HasOneRef()) |
| 206 << "Instructed to delete buffer we are still using."; | 206 << "Instructed to delete buffer we are still using."; |
| 207 client_buffers_.erase(iter); | 207 client_buffers_.erase(iter); |
| 208 } | 208 } |
| 209 | 209 |
| 210 void VideoCaptureImpl::OnBufferReceived(int buffer_id, | 210 void VideoCaptureImpl::OnBufferReceived(int buffer_id, |
| 211 const media::VideoCaptureFormat& format, | 211 const media::VideoCaptureFormat& format, |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 225 if (first_frame_timestamp_.is_null()) | 225 if (first_frame_timestamp_.is_null()) |
| 226 first_frame_timestamp_ = timestamp; | 226 first_frame_timestamp_ = timestamp; |
| 227 | 227 |
| 228 // Used by chrome/browser/extension/api/cast_streaming/performance_test.cc | 228 // Used by chrome/browser/extension/api/cast_streaming/performance_test.cc |
| 229 TRACE_EVENT_INSTANT2( | 229 TRACE_EVENT_INSTANT2( |
| 230 "cast_perf_test", "OnBufferReceived", | 230 "cast_perf_test", "OnBufferReceived", |
| 231 TRACE_EVENT_SCOPE_THREAD, | 231 TRACE_EVENT_SCOPE_THREAD, |
| 232 "timestamp", timestamp.ToInternalValue(), | 232 "timestamp", timestamp.ToInternalValue(), |
| 233 "time_delta", (timestamp - first_frame_timestamp_).ToInternalValue()); | 233 "time_delta", (timestamp - first_frame_timestamp_).ToInternalValue()); |
| 234 | 234 |
| 235 ClientBufferMap::iterator iter = client_buffers_.find(buffer_id); | 235 const ClientBufferMap::const_iterator iter = client_buffers_.find(buffer_id); |
| 236 DCHECK(iter != client_buffers_.end()); | 236 DCHECK(iter != client_buffers_.end()); |
| 237 scoped_refptr<ClientBuffer> buffer = iter->second; | 237 scoped_refptr<ClientBuffer> buffer = iter->second; |
| 238 scoped_refptr<media::VideoFrame> frame = | 238 scoped_refptr<media::VideoFrame> frame = |
| 239 media::VideoFrame::WrapExternalPackedMemory( | 239 media::VideoFrame::WrapExternalPackedMemory( |
| 240 media::VideoFrame::I420, | 240 media::VideoFrame::I420, |
| 241 last_frame_format_.frame_size, | 241 last_frame_format_.frame_size, |
| 242 visible_rect, | 242 visible_rect, |
| 243 gfx::Size(visible_rect.width(), visible_rect.height()), | 243 gfx::Size(visible_rect.width(), visible_rect.height()), |
| 244 reinterpret_cast<uint8*>(buffer->buffer->memory()), | 244 reinterpret_cast<uint8*>(buffer->buffer->memory()), |
| 245 buffer->buffer_size, | 245 buffer->buffer_size, |
| 246 buffer->buffer->handle(), | 246 buffer->buffer->handle(), |
| 247 0, | 247 0, |
| 248 timestamp - first_frame_timestamp_, | 248 timestamp - first_frame_timestamp_, |
| 249 media::BindToCurrentLoop( | 249 media::BindToCurrentLoop( |
| 250 base::Bind(&VideoCaptureImpl::OnClientBufferFinished, | 250 base::Bind(&VideoCaptureImpl::OnClientBufferFinished, |
| 251 weak_factory_.GetWeakPtr(), | 251 weak_factory_.GetWeakPtr(), |
| 252 buffer_id, | 252 buffer_id, |
| 253 buffer, | 253 buffer, |
| 254 0))); | 254 0))); |
| 255 | 255 |
| 256 for (ClientInfoMap::iterator it = clients_.begin(); it != clients_.end(); | 256 for (const auto& it : clients_) |
|
wolenetz
2015/03/02 23:51:09
nit: s/it/something-more-type-descriptive/ ?
mcasas
2015/03/03 15:40:52
Done.
| |
| 257 ++it) { | 257 it.second.deliver_frame_cb.Run(frame, format, timestamp); |
| 258 it->second.deliver_frame_cb.Run(frame, format, timestamp); | |
| 259 } | |
| 260 } | 258 } |
| 261 | 259 |
| 262 void VideoCaptureImpl::OnMailboxBufferReceived( | 260 void VideoCaptureImpl::OnMailboxBufferReceived( |
| 263 int buffer_id, | 261 int buffer_id, |
| 264 const gpu::MailboxHolder& mailbox_holder, | 262 const gpu::MailboxHolder& mailbox_holder, |
| 265 const media::VideoCaptureFormat& format, | 263 const media::VideoCaptureFormat& format, |
| 266 base::TimeTicks timestamp) { | 264 base::TimeTicks timestamp) { |
| 267 DCHECK(thread_checker_.CalledOnValidThread()); | 265 DCHECK(thread_checker_.CalledOnValidThread()); |
| 268 | 266 |
| 269 if (state_ != VIDEO_CAPTURE_STATE_STARTED || suspended_) { | 267 if (state_ != VIDEO_CAPTURE_STATE_STARTED || suspended_) { |
| 270 Send(new VideoCaptureHostMsg_BufferReady(device_id_, buffer_id, 0)); | 268 Send(new VideoCaptureHostMsg_BufferReady(device_id_, buffer_id, 0)); |
| 271 return; | 269 return; |
| 272 } | 270 } |
| 273 | 271 |
| 274 last_frame_format_ = format; | 272 last_frame_format_ = format; |
| 275 if (first_frame_timestamp_.is_null()) | 273 if (first_frame_timestamp_.is_null()) |
| 276 first_frame_timestamp_ = timestamp; | 274 first_frame_timestamp_ = timestamp; |
| 277 | 275 |
| 278 scoped_refptr<media::VideoFrame> frame = media::VideoFrame::WrapNativeTexture( | 276 scoped_refptr<media::VideoFrame> frame = media::VideoFrame::WrapNativeTexture( |
| 279 make_scoped_ptr(new gpu::MailboxHolder(mailbox_holder)), | 277 make_scoped_ptr(new gpu::MailboxHolder(mailbox_holder)), |
| 280 media::BindToCurrentLoop(base::Bind( | 278 media::BindToCurrentLoop(base::Bind( |
| 281 &VideoCaptureImpl::OnClientBufferFinished, weak_factory_.GetWeakPtr(), | 279 &VideoCaptureImpl::OnClientBufferFinished, weak_factory_.GetWeakPtr(), |
| 282 buffer_id, scoped_refptr<ClientBuffer>())), | 280 buffer_id, scoped_refptr<ClientBuffer>())), |
| 283 last_frame_format_.frame_size, gfx::Rect(last_frame_format_.frame_size), | 281 last_frame_format_.frame_size, gfx::Rect(last_frame_format_.frame_size), |
| 284 last_frame_format_.frame_size, timestamp - first_frame_timestamp_, false); | 282 last_frame_format_.frame_size, timestamp - first_frame_timestamp_, false); |
| 285 | 283 |
| 286 for (ClientInfoMap::iterator it = clients_.begin(); it != clients_.end(); | 284 for (const auto& it : clients_) |
|
wolenetz
2015/03/02 23:51:09
nit: ditto here and elsewhere in this CL.
mcasas
2015/03/03 15:40:52
Done.
| |
| 287 ++it) { | 285 it.second.deliver_frame_cb.Run(frame, format, timestamp); |
| 288 it->second.deliver_frame_cb.Run(frame, format, timestamp); | |
| 289 } | |
| 290 } | 286 } |
| 291 | 287 |
| 292 void VideoCaptureImpl::OnClientBufferFinished( | 288 void VideoCaptureImpl::OnClientBufferFinished( |
| 293 int buffer_id, | 289 int buffer_id, |
| 294 const scoped_refptr<ClientBuffer>& /* ignored_buffer */, | 290 const scoped_refptr<ClientBuffer>& /* ignored_buffer */, |
| 295 uint32 release_sync_point) { | 291 uint32 release_sync_point) { |
| 296 DCHECK(thread_checker_.CalledOnValidThread()); | 292 DCHECK(thread_checker_.CalledOnValidThread()); |
| 297 Send(new VideoCaptureHostMsg_BufferReady( | 293 Send(new VideoCaptureHostMsg_BufferReady( |
| 298 device_id_, buffer_id, release_sync_point)); | 294 device_id_, buffer_id, release_sync_point)); |
| 299 } | 295 } |
| 300 | 296 |
| 301 void VideoCaptureImpl::OnStateChanged(VideoCaptureState state) { | 297 void VideoCaptureImpl::OnStateChanged(VideoCaptureState state) { |
| 302 DCHECK(thread_checker_.CalledOnValidThread()); | 298 DCHECK(thread_checker_.CalledOnValidThread()); |
| 303 | 299 |
| 304 switch (state) { | 300 switch (state) { |
| 305 case VIDEO_CAPTURE_STATE_STARTED: | 301 case VIDEO_CAPTURE_STATE_STARTED: |
| 306 // Camera has started in the browser process. Since we have already | 302 // Camera has started in the browser process. Since we have already |
| 307 // told all clients that we have started there's nothing to do. | 303 // told all clients that we have started there's nothing to do. |
| 308 break; | 304 break; |
| 309 case VIDEO_CAPTURE_STATE_STOPPED: | 305 case VIDEO_CAPTURE_STATE_STOPPED: |
| 310 state_ = VIDEO_CAPTURE_STATE_STOPPED; | 306 state_ = VIDEO_CAPTURE_STATE_STOPPED; |
| 311 DVLOG(1) << "OnStateChanged: stopped!, device_id = " << device_id_; | 307 DVLOG(1) << "OnStateChanged: stopped!, device_id = " << device_id_; |
| 312 client_buffers_.clear(); | 308 client_buffers_.clear(); |
| 313 weak_factory_.InvalidateWeakPtrs(); | 309 weak_factory_.InvalidateWeakPtrs(); |
| 314 if (!clients_.empty() || !clients_pending_on_restart_.empty()) | 310 if (!clients_.empty() || !clients_pending_on_restart_.empty()) |
| 315 RestartCapture(); | 311 RestartCapture(); |
| 316 break; | 312 break; |
| 317 case VIDEO_CAPTURE_STATE_PAUSED: | 313 case VIDEO_CAPTURE_STATE_PAUSED: |
| 318 for (ClientInfoMap::iterator it = clients_.begin(); | 314 for (const auto& it : clients_) |
| 319 it != clients_.end(); ++it) { | 315 it.second.state_update_cb.Run(VIDEO_CAPTURE_STATE_PAUSED); |
| 320 it->second.state_update_cb.Run(VIDEO_CAPTURE_STATE_PAUSED); | |
| 321 } | |
| 322 break; | 316 break; |
| 323 case VIDEO_CAPTURE_STATE_ERROR: | 317 case VIDEO_CAPTURE_STATE_ERROR: |
| 324 DVLOG(1) << "OnStateChanged: error!, device_id = " << device_id_; | 318 DVLOG(1) << "OnStateChanged: error!, device_id = " << device_id_; |
| 325 for (ClientInfoMap::iterator it = clients_.begin(); | 319 for (const auto& it : clients_) |
| 326 it != clients_.end(); ++it) { | 320 it.second.state_update_cb.Run(VIDEO_CAPTURE_STATE_ERROR); |
| 327 it->second.state_update_cb.Run(VIDEO_CAPTURE_STATE_ERROR); | |
| 328 } | |
| 329 clients_.clear(); | 321 clients_.clear(); |
| 330 state_ = VIDEO_CAPTURE_STATE_ERROR; | 322 state_ = VIDEO_CAPTURE_STATE_ERROR; |
| 331 break; | 323 break; |
| 332 case VIDEO_CAPTURE_STATE_ENDED: | 324 case VIDEO_CAPTURE_STATE_ENDED: |
| 333 DVLOG(1) << "OnStateChanged: ended!, device_id = " << device_id_; | 325 DVLOG(1) << "OnStateChanged: ended!, device_id = " << device_id_; |
| 334 for (ClientInfoMap::iterator it = clients_.begin(); | 326 for (const auto& it : clients_) { |
| 335 it != clients_.end(); ++it) { | |
| 336 // We'll only notify the client that the stream has stopped. | 327 // We'll only notify the client that the stream has stopped. |
| 337 it->second.state_update_cb.Run(VIDEO_CAPTURE_STATE_STOPPED); | 328 it.second.state_update_cb.Run(VIDEO_CAPTURE_STATE_STOPPED); |
| 338 } | 329 } |
| 339 clients_.clear(); | 330 clients_.clear(); |
| 340 state_ = VIDEO_CAPTURE_STATE_ENDED; | 331 state_ = VIDEO_CAPTURE_STATE_ENDED; |
| 341 break; | 332 break; |
| 342 default: | 333 default: |
| 343 break; | 334 break; |
| 344 } | 335 } |
| 345 } | 336 } |
| 346 | 337 |
| 347 void VideoCaptureImpl::OnDeviceSupportedFormatsEnumerated( | 338 void VideoCaptureImpl::OnDeviceSupportedFormatsEnumerated( |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 358 for (size_t i = 0; i < device_formats_in_use_cb_queue_.size(); ++i) | 349 for (size_t i = 0; i < device_formats_in_use_cb_queue_.size(); ++i) |
| 359 device_formats_in_use_cb_queue_[i].Run(formats_in_use); | 350 device_formats_in_use_cb_queue_[i].Run(formats_in_use); |
| 360 device_formats_in_use_cb_queue_.clear(); | 351 device_formats_in_use_cb_queue_.clear(); |
| 361 } | 352 } |
| 362 | 353 |
| 363 void VideoCaptureImpl::OnDelegateAdded(int32 device_id) { | 354 void VideoCaptureImpl::OnDelegateAdded(int32 device_id) { |
| 364 DCHECK(thread_checker_.CalledOnValidThread()); | 355 DCHECK(thread_checker_.CalledOnValidThread()); |
| 365 DVLOG(1) << "OnDelegateAdded: device_id " << device_id; | 356 DVLOG(1) << "OnDelegateAdded: device_id " << device_id; |
| 366 | 357 |
| 367 device_id_ = device_id; | 358 device_id_ = device_id; |
| 368 for (ClientInfoMap::iterator it = clients_pending_on_filter_.begin(); | 359 for (const auto& it = clients_pending_on_filter_) { |
|
wolenetz
2015/03/02 23:51:09
s/=/:/ ? I don't know this code, this for loop now
mcasas
2015/03/03 15:40:52
Done.
| |
| 369 it != clients_pending_on_filter_.end(); ) { | 360 StartCapture(it.first, it.second.params, it.second.state_update_cb, |
| 370 int client_id = it->first; | 361 it.second.deliver_frame_cb); |
| 371 VideoCaptureStateUpdateCB state_update_cb = | |
| 372 it->second.state_update_cb; | |
| 373 VideoCaptureDeliverFrameCB deliver_frame_cb = | |
| 374 it->second.deliver_frame_cb; | |
| 375 const media::VideoCaptureParams params = it->second.params; | |
| 376 clients_pending_on_filter_.erase(it++); | |
| 377 StartCapture(client_id, params, state_update_cb, | |
| 378 deliver_frame_cb); | |
| 379 } | 362 } |
| 363 clients_pending_on_filter_.clear(); | |
| 380 } | 364 } |
| 381 | 365 |
| 382 void VideoCaptureImpl::StopDevice() { | 366 void VideoCaptureImpl::StopDevice() { |
| 383 DCHECK(thread_checker_.CalledOnValidThread()); | 367 DCHECK(thread_checker_.CalledOnValidThread()); |
| 384 | 368 |
| 385 if (state_ == VIDEO_CAPTURE_STATE_STARTED) { | 369 if (state_ == VIDEO_CAPTURE_STATE_STARTED) { |
| 386 state_ = VIDEO_CAPTURE_STATE_STOPPING; | 370 state_ = VIDEO_CAPTURE_STATE_STOPPING; |
| 387 Send(new VideoCaptureHostMsg_Stop(device_id_)); | 371 Send(new VideoCaptureHostMsg_Stop(device_id_)); |
| 388 params_.requested_format.frame_size.SetSize(0, 0); | 372 params_.requested_format.frame_size.SetSize(0, 0); |
| 389 } | 373 } |
| 390 } | 374 } |
| 391 | 375 |
| 392 void VideoCaptureImpl::RestartCapture() { | 376 void VideoCaptureImpl::RestartCapture() { |
| 393 DCHECK(thread_checker_.CalledOnValidThread()); | 377 DCHECK(thread_checker_.CalledOnValidThread()); |
| 394 DCHECK_EQ(state_, VIDEO_CAPTURE_STATE_STOPPED); | 378 DCHECK_EQ(state_, VIDEO_CAPTURE_STATE_STOPPED); |
| 395 | 379 |
| 396 int width = 0; | 380 int width = 0; |
| 397 int height = 0; | 381 int height = 0; |
| 398 clients_.insert(clients_pending_on_restart_.begin(), | 382 clients_.insert(clients_pending_on_restart_.begin(), |
| 399 clients_pending_on_restart_.end()); | 383 clients_pending_on_restart_.end()); |
| 400 clients_pending_on_restart_.clear(); | 384 clients_pending_on_restart_.clear(); |
| 401 for (ClientInfoMap::iterator it = clients_.begin(); | 385 for (const auto& it : clients_) { |
| 402 it != clients_.end(); ++it) { | |
| 403 width = std::max(width, | 386 width = std::max(width, |
| 404 it->second.params.requested_format.frame_size.width()); | 387 it.second.params.requested_format.frame_size.width()); |
| 405 height = std::max(height, | 388 height = std::max(height, |
| 406 it->second.params.requested_format.frame_size.height()); | 389 it.second.params.requested_format.frame_size.height()); |
| 407 } | 390 } |
| 408 params_.requested_format.frame_size.SetSize(width, height); | 391 params_.requested_format.frame_size.SetSize(width, height); |
| 409 DVLOG(1) << "RestartCapture, " | 392 DVLOG(1) << "RestartCapture, " |
| 410 << params_.requested_format.frame_size.ToString(); | 393 << params_.requested_format.frame_size.ToString(); |
| 411 StartCaptureInternal(); | 394 StartCaptureInternal(); |
| 412 } | 395 } |
| 413 | 396 |
| 414 void VideoCaptureImpl::StartCaptureInternal() { | 397 void VideoCaptureImpl::StartCaptureInternal() { |
| 415 DCHECK(thread_checker_.CalledOnValidThread()); | 398 DCHECK(thread_checker_.CalledOnValidThread()); |
| 416 DCHECK(device_id_); | 399 DCHECK(device_id_); |
| 417 | 400 |
| 418 Send(new VideoCaptureHostMsg_Start(device_id_, session_id_, params_)); | 401 Send(new VideoCaptureHostMsg_Start(device_id_, session_id_, params_)); |
| 419 state_ = VIDEO_CAPTURE_STATE_STARTED; | 402 state_ = VIDEO_CAPTURE_STATE_STARTED; |
| 420 } | 403 } |
| 421 | 404 |
| 422 void VideoCaptureImpl::Send(IPC::Message* message) { | 405 void VideoCaptureImpl::Send(IPC::Message* message) { |
| 423 DCHECK(thread_checker_.CalledOnValidThread()); | 406 DCHECK(thread_checker_.CalledOnValidThread()); |
| 424 message_filter_->Send(message); | 407 message_filter_->Send(message); |
| 425 } | 408 } |
| 426 | 409 |
| 427 bool VideoCaptureImpl::RemoveClient(int client_id, ClientInfoMap* clients) { | 410 bool VideoCaptureImpl::RemoveClient(int client_id, ClientInfoMap* clients) { |
| 428 DCHECK(thread_checker_.CalledOnValidThread()); | 411 DCHECK(thread_checker_.CalledOnValidThread()); |
| 429 bool found = false; | 412 bool found = false; |
| 430 | 413 |
| 431 ClientInfoMap::iterator it = clients->find(client_id); | 414 const ClientInfoMap::iterator it = clients->find(client_id); |
| 432 if (it != clients->end()) { | 415 if (it != clients->end()) { |
| 433 it->second.state_update_cb.Run(VIDEO_CAPTURE_STATE_STOPPED); | 416 it->second.state_update_cb.Run(VIDEO_CAPTURE_STATE_STOPPED); |
| 434 clients->erase(it); | 417 clients->erase(it); |
| 435 found = true; | 418 found = true; |
| 436 } | 419 } |
| 437 return found; | 420 return found; |
| 438 } | 421 } |
| 439 | 422 |
| 440 } // namespace content | 423 } // namespace content |
| OLD | NEW |