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 #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/callback_helpers.h" | |
8 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
9 #include "content/child/child_process.h" | 10 #include "content/child/child_process.h" |
11 #include "content/common/media/encoded_video_capture_messages.h" | |
10 #include "content/common/media/video_capture_messages.h" | 12 #include "content/common/media/video_capture_messages.h" |
11 #include "media/base/limits.h" | 13 #include "media/base/limits.h" |
12 | 14 |
13 namespace content { | 15 namespace content { |
14 | 16 |
15 struct VideoCaptureImpl::DIBBuffer { | 17 struct VideoCaptureImpl::DIBBuffer { |
16 public: | 18 public: |
17 DIBBuffer( | 19 DIBBuffer( |
18 base::SharedMemory* d, | 20 base::SharedMemory* d, |
19 media::VideoCapture::VideoFrameBuffer* ptr) | 21 media::VideoCapture::VideoFrameBuffer* ptr) |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
94 base::Bind(&VideoCaptureImpl::DoStartCaptureOnCaptureThread, | 96 base::Bind(&VideoCaptureImpl::DoStartCaptureOnCaptureThread, |
95 base::Unretained(this), handler, capability)); | 97 base::Unretained(this), handler, capability)); |
96 } | 98 } |
97 | 99 |
98 void VideoCaptureImpl::StopCapture(media::VideoCapture::EventHandler* handler) { | 100 void VideoCaptureImpl::StopCapture(media::VideoCapture::EventHandler* handler) { |
99 capture_message_loop_proxy_->PostTask(FROM_HERE, | 101 capture_message_loop_proxy_->PostTask(FROM_HERE, |
100 base::Bind(&VideoCaptureImpl::DoStopCaptureOnCaptureThread, | 102 base::Bind(&VideoCaptureImpl::DoStopCaptureOnCaptureThread, |
101 base::Unretained(this), handler)); | 103 base::Unretained(this), handler)); |
102 } | 104 } |
103 | 105 |
106 void VideoCaptureImpl::RequestCapabilities( | |
107 const media::EncodedVideoSource::RequestCapabilitiesCallback& callback) { | |
sheu
2013/06/13 05:36:51
We're assuming that this is called always on captu
hshi1
2013/06/13 19:18:46
I'm afraid that the DCHECK failed. During my testi
| |
108 DCHECK(callback_.is_null()); | |
109 callback_ = callback; | |
110 | |
111 // Invoke callback immediately if capabilities are already available. | |
112 if (!capabilities_.empty()) | |
113 base::ResetAndReturn(&callback_).Run(capabilities_); | |
114 } | |
115 | |
116 void VideoCaptureImpl::StartFetchCapabilities() { | |
117 Send(new EncodedVideoCaptureHostMsg_GetCapabilities( | |
118 device_id_, current_params_.session_id)); | |
119 } | |
120 | |
121 void VideoCaptureImpl::OpenBitstream( | |
sheu
2013/06/13 05:36:51
We're assuming that this is called always on captu
hshi1
2013/06/13 19:18:46
I'm afraid that the DCHECK failed. I see this call
| |
122 media::EncodedVideoSource::Client* client, | |
123 const media::VideoEncodingParameters& params) { | |
124 DCHECK(!client_); | |
125 client_ = client; | |
126 Send(new EncodedVideoCaptureHostMsg_OpenBitstream( | |
127 device_id_, current_params_.session_id, params)); | |
128 } | |
129 | |
130 void VideoCaptureImpl::CloseBitstream() { | |
sheu
2013/06/13 05:36:51
This and other entry points that don't require tra
hshi1
2013/06/13 19:18:46
I'm posting this to capture thread as well, becaus
| |
131 Send(new EncodedVideoCaptureHostMsg_CloseBitstream(device_id_)); | |
132 client_ = NULL; | |
133 } | |
134 | |
135 void VideoCaptureImpl::ReturnBitstreamBuffer( | |
136 scoped_refptr<const media::EncodedBitstreamBuffer> buffer) { | |
137 Send(new EncodedVideoCaptureHostMsg_BitstreamBufferConsumed( | |
138 device_id_, buffer->buffer_id())); | |
139 } | |
140 | |
141 void VideoCaptureImpl::TrySetBitstreamConfig( | |
142 const media::RuntimeVideoEncodingParameters& params) { | |
143 Send(new EncodedVideoCaptureHostMsg_TryConfigureBitstream( | |
144 device_id_, params)); | |
145 } | |
146 | |
147 void VideoCaptureImpl::OnEncodingCapabilitiesAvailable( | |
148 const media::VideoEncodingCapabilities& capabilities) { | |
149 capture_message_loop_proxy_->PostTask(FROM_HERE, base::Bind( | |
150 &VideoCaptureImpl::DoNotifyCapabilitiesAvailableOnCaptureThread, | |
151 base::Unretained(this), capabilities)); | |
152 } | |
153 | |
154 void VideoCaptureImpl::OnEncodedBitstreamOpened( | |
155 const media::VideoEncodingParameters& params, | |
156 const std::vector<base::SharedMemoryHandle>& buffers, | |
157 int buffer_size) { | |
158 capture_message_loop_proxy_->PostTask(FROM_HERE, | |
159 base::Bind(&VideoCaptureImpl::DoNotifyBitstreamOpenedOnCaptureThread, | |
160 base::Unretained(this), params, buffers, buffer_size)); | |
161 } | |
162 | |
163 void VideoCaptureImpl::OnEncodedBitstreamClosed() { | |
164 capture_message_loop_proxy_->PostTask(FROM_HERE, | |
165 base::Bind(&VideoCaptureImpl::DoNotifyBitstreamClosedOnCaptureThread, | |
166 base::Unretained(this))); | |
167 } | |
168 | |
169 void VideoCaptureImpl::OnEncodingConfigChanged( | |
170 const media::RuntimeVideoEncodingParameters& params) { | |
171 capture_message_loop_proxy_->PostTask(FROM_HERE, | |
172 base::Bind( | |
173 &VideoCaptureImpl::DoNotifyBitstreamConfigChangedOnCaptureThread, | |
174 base::Unretained(this), params)); | |
175 } | |
176 | |
177 void VideoCaptureImpl::OnEncodedBufferReady( | |
178 int buffer_id, | |
179 int size, | |
180 const media::BufferEncodingMetadata& metadata) { | |
181 capture_message_loop_proxy_->PostTask(FROM_HERE, | |
182 base::Bind(&VideoCaptureImpl::DoNotifyBitstreamBufferReadyOnCaptureThread, | |
183 base::Unretained(this), buffer_id, size, metadata)); | |
184 } | |
185 | |
104 void VideoCaptureImpl::FeedBuffer(scoped_refptr<VideoFrameBuffer> buffer) { | 186 void VideoCaptureImpl::FeedBuffer(scoped_refptr<VideoFrameBuffer> buffer) { |
105 capture_message_loop_proxy_->PostTask(FROM_HERE, | 187 capture_message_loop_proxy_->PostTask(FROM_HERE, |
106 base::Bind(&VideoCaptureImpl::DoFeedBufferOnCaptureThread, | 188 base::Bind(&VideoCaptureImpl::DoFeedBufferOnCaptureThread, |
107 base::Unretained(this), buffer)); | 189 base::Unretained(this), buffer)); |
108 } | 190 } |
109 | 191 |
110 void VideoCaptureImpl::OnBufferCreated( | 192 void VideoCaptureImpl::OnBufferCreated( |
111 base::SharedMemoryHandle handle, | 193 base::SharedMemoryHandle handle, |
112 int length, int buffer_id) { | 194 int length, int buffer_id) { |
113 capture_message_loop_proxy_->PostTask(FROM_HERE, | 195 capture_message_loop_proxy_->PostTask(FROM_HERE, |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
299 it->first->OnBufferReady(this, buffer); | 381 it->first->OnBufferReady(this, buffer); |
300 } | 382 } |
301 cached_dibs_[buffer_id]->references = clients_.size(); | 383 cached_dibs_[buffer_id]->references = clients_.size(); |
302 } | 384 } |
303 | 385 |
304 void VideoCaptureImpl::DoStateChangedOnCaptureThread(VideoCaptureState state) { | 386 void VideoCaptureImpl::DoStateChangedOnCaptureThread(VideoCaptureState state) { |
305 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); | 387 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); |
306 | 388 |
307 switch (state) { | 389 switch (state) { |
308 case VIDEO_CAPTURE_STATE_STARTED: | 390 case VIDEO_CAPTURE_STATE_STARTED: |
391 StartFetchCapabilities(); | |
309 break; | 392 break; |
310 case VIDEO_CAPTURE_STATE_STOPPED: | 393 case VIDEO_CAPTURE_STATE_STOPPED: |
311 state_ = VIDEO_CAPTURE_STATE_STOPPED; | 394 state_ = VIDEO_CAPTURE_STATE_STOPPED; |
312 DVLOG(1) << "OnStateChanged: stopped!, device_id = " << device_id_; | 395 DVLOG(1) << "OnStateChanged: stopped!, device_id = " << device_id_; |
313 STLDeleteValues(&cached_dibs_); | 396 STLDeleteValues(&cached_dibs_); |
314 if (!clients_.empty() || !clients_pending_on_restart_.empty()) | 397 if (!clients_.empty() || !clients_pending_on_restart_.empty()) |
315 RestartCapture(); | 398 RestartCapture(); |
316 break; | 399 break; |
317 case VIDEO_CAPTURE_STATE_PAUSED: | 400 case VIDEO_CAPTURE_STATE_PAUSED: |
318 for (ClientInfo::iterator it = clients_.begin(); | 401 for (ClientInfo::iterator it = clients_.begin(); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
373 } | 456 } |
374 } | 457 } |
375 | 458 |
376 void VideoCaptureImpl::DoSuspendCaptureOnCaptureThread(bool suspend) { | 459 void VideoCaptureImpl::DoSuspendCaptureOnCaptureThread(bool suspend) { |
377 DVLOG(1) << "DoSuspendCapture: suspend " << (suspend ? "yes" : "no"); | 460 DVLOG(1) << "DoSuspendCapture: suspend " << (suspend ? "yes" : "no"); |
378 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); | 461 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); |
379 | 462 |
380 suspended_ = suspend; | 463 suspended_ = suspend; |
381 } | 464 } |
382 | 465 |
466 void VideoCaptureImpl::DoNotifyBitstreamOpenedOnCaptureThread( | |
467 const media::VideoEncodingParameters& params, | |
468 const std::vector<base::SharedMemoryHandle>& buffers, | |
469 int buffer_size) { | |
470 DCHECK(bitstream_buffers_.empty()); | |
471 for (size_t i = 0; i < buffers.size(); ++i) { | |
472 base::SharedMemory* shm = new base::SharedMemory(buffers[i], true); | |
473 bitstream_buffers_.push_back(shm); | |
474 } | |
475 client_->OnOpened(params); | |
476 } | |
477 | |
478 void VideoCaptureImpl::DoNotifyBitstreamClosedOnCaptureThread() { | |
479 for (size_t i = 0; i < bitstream_buffers_.size(); ++i) { | |
480 bitstream_buffers_[i]->Close(); | |
481 delete bitstream_buffers_[i]; | |
482 } | |
483 bitstream_buffers_.clear(); | |
484 client_->OnClosed(); | |
485 } | |
486 | |
487 void VideoCaptureImpl::DoNotifyBitstreamConfigChangedOnCaptureThread( | |
488 const media::RuntimeVideoEncodingParameters& params) { | |
489 client_->OnConfigChanged(params); | |
490 } | |
491 | |
492 void VideoCaptureImpl::DoNotifyBitstreamBufferReadyOnCaptureThread( | |
493 int buffer_id, | |
494 int size, | |
495 const media::BufferEncodingMetadata& metadata) { | |
496 if (buffer_id >= 0 && buffer_id < (int)bitstream_buffers_.size()) { | |
497 base::SharedMemory* shm = bitstream_buffers_[buffer_id]; | |
498 if (shm && shm->Map(size)) { | |
499 scoped_refptr<media::EncodedBitstreamBuffer> buffer = | |
500 new media::EncodedBitstreamBuffer( | |
501 buffer_id, (uint8*)shm->memory(), size, metadata); | |
502 client_->OnBufferReady(buffer); | |
503 shm->Unmap(); | |
504 } | |
505 } | |
506 } | |
507 | |
508 void VideoCaptureImpl::DoNotifyCapabilitiesAvailableOnCaptureThread( | |
509 const media::VideoEncodingCapabilities& capabilities) { | |
510 capabilities_ = capabilities; | |
511 if (!callback_.is_null()) | |
512 base::ResetAndReturn(&callback_).Run(capabilities_); | |
513 } | |
514 | |
383 void VideoCaptureImpl::StopDevice() { | 515 void VideoCaptureImpl::StopDevice() { |
384 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); | 516 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); |
385 | 517 |
386 device_info_available_ = false; | 518 device_info_available_ = false; |
387 if (state_ == VIDEO_CAPTURE_STATE_STARTED) { | 519 if (state_ == VIDEO_CAPTURE_STATE_STARTED) { |
388 state_ = VIDEO_CAPTURE_STATE_STOPPING; | 520 state_ = VIDEO_CAPTURE_STATE_STOPPING; |
389 Send(new VideoCaptureHostMsg_Stop(device_id_)); | 521 Send(new VideoCaptureHostMsg_Stop(device_id_)); |
390 current_params_.width = current_params_.height = 0; | 522 current_params_.width = current_params_.height = 0; |
391 } | 523 } |
392 } | 524 } |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
461 if (it != clients->end()) { | 593 if (it != clients->end()) { |
462 handler->OnStopped(this); | 594 handler->OnStopped(this); |
463 handler->OnRemoved(this); | 595 handler->OnRemoved(this); |
464 clients->erase(it); | 596 clients->erase(it); |
465 found = true; | 597 found = true; |
466 } | 598 } |
467 return found; | 599 return found; |
468 } | 600 } |
469 | 601 |
470 } // namespace content | 602 } // namespace content |
OLD | NEW |