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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
51 base::MessageLoopProxy* capture_message_loop_proxy, | 53 base::MessageLoopProxy* capture_message_loop_proxy, |
52 VideoCaptureMessageFilter* filter) | 54 VideoCaptureMessageFilter* filter) |
53 : VideoCapture(), | 55 : VideoCapture(), |
54 message_filter_(filter), | 56 message_filter_(filter), |
55 capture_message_loop_proxy_(capture_message_loop_proxy), | 57 capture_message_loop_proxy_(capture_message_loop_proxy), |
56 io_message_loop_proxy_(ChildProcess::current()->io_message_loop_proxy()), | 58 io_message_loop_proxy_(ChildProcess::current()->io_message_loop_proxy()), |
57 device_id_(0), | 59 device_id_(0), |
58 video_type_(media::VideoCaptureCapability::kI420), | 60 video_type_(media::VideoCaptureCapability::kI420), |
59 device_info_available_(false), | 61 device_info_available_(false), |
60 suspended_(false), | 62 suspended_(false), |
61 state_(VIDEO_CAPTURE_STATE_STOPPED) { | 63 state_(VIDEO_CAPTURE_STATE_STOPPED), |
64 bitstream_buffer_size_(0) { | |
62 DCHECK(filter); | 65 DCHECK(filter); |
63 memset(¤t_params_, 0, sizeof(current_params_)); | 66 memset(¤t_params_, 0, sizeof(current_params_)); |
64 memset(&device_info_, 0, sizeof(device_info_)); | 67 memset(&device_info_, 0, sizeof(device_info_)); |
65 current_params_.session_id = id; | 68 current_params_.session_id = id; |
66 } | 69 } |
67 | 70 |
68 VideoCaptureImpl::~VideoCaptureImpl() { | 71 VideoCaptureImpl::~VideoCaptureImpl() { |
69 STLDeleteValues(&cached_dibs_); | 72 STLDeleteValues(&cached_dibs_); |
70 } | 73 } |
71 | 74 |
(...skipping 22 matching lines...) Expand all Loading... | |
94 base::Bind(&VideoCaptureImpl::DoStartCaptureOnCaptureThread, | 97 base::Bind(&VideoCaptureImpl::DoStartCaptureOnCaptureThread, |
95 base::Unretained(this), handler, capability)); | 98 base::Unretained(this), handler, capability)); |
96 } | 99 } |
97 | 100 |
98 void VideoCaptureImpl::StopCapture(media::VideoCapture::EventHandler* handler) { | 101 void VideoCaptureImpl::StopCapture(media::VideoCapture::EventHandler* handler) { |
99 capture_message_loop_proxy_->PostTask(FROM_HERE, | 102 capture_message_loop_proxy_->PostTask(FROM_HERE, |
100 base::Bind(&VideoCaptureImpl::DoStopCaptureOnCaptureThread, | 103 base::Bind(&VideoCaptureImpl::DoStopCaptureOnCaptureThread, |
101 base::Unretained(this), handler)); | 104 base::Unretained(this), handler)); |
102 } | 105 } |
103 | 106 |
107 void VideoCaptureImpl::RequestCapabilities( | |
108 const media::EncodedVideoSource::RequestCapabilitiesCallback& callback) { | |
109 DCHECK(callback_.is_null()); | |
110 callback_ = callback; | |
111 | |
112 // Invoke callback immediately if capabilities are already available. | |
113 if (!capabilities_.empty()) | |
114 base::ResetAndReturn(&callback_).Run(capabilities_); | |
115 } | |
116 | |
117 void VideoCaptureImpl::StartFetchCapabilities() { | |
118 Send(new EncodedVideoCaptureHostMsg_GetCapabilities(device_id_)); | |
119 } | |
120 | |
121 void VideoCaptureImpl::OpenBitstream( | |
122 media::EncodedVideoSource::Client* client, | |
123 const media::VideoEncodingParameters& params) { | |
124 DCHECK(!client_); | |
125 client_ = client; | |
126 Send(new EncodedVideoCaptureHostMsg_OpenBitstream(device_id_, params)); | |
127 } | |
128 | |
129 void VideoCaptureImpl::CloseBitstream() { | |
130 Send(new EncodedVideoCaptureHostMsg_CloseBitstream(device_id_)); | |
131 client_ = NULL; | |
132 } | |
133 | |
134 void VideoCaptureImpl::ReturnBitstreamBuffer( | |
135 scoped_refptr<const media::EncodedBitstreamBuffer> buffer) { | |
136 Send(new EncodedVideoCaptureHostMsg_BitstreamBufferConsumed( | |
137 device_id_, buffer->buffer_id())); | |
138 } | |
139 | |
140 void VideoCaptureImpl::TrySetBitstreamConfig( | |
141 const media::RuntimeVideoEncodingParameters& params) { | |
142 Send(new EncodedVideoCaptureHostMsg_TryConfigureBitstream( | |
143 device_id_, params)); | |
144 } | |
145 | |
146 void VideoCaptureImpl::OnEncodingCapabilitiesAvailable( | |
147 const media::VideoEncodingCapabilities& capabilities) { | |
148 capabilities_ = capabilities; | |
sheu
2013/06/12 22:35:05
This is why we do that little trampoline thing thr
hshi1
2013/06/13 00:25:31
I've changed this so that we always update capabil
| |
149 if (!callback_.is_null()) | |
150 base::ResetAndReturn(&callback_).Run(capabilities_); | |
151 } | |
152 | |
153 void VideoCaptureImpl::OnEncodedBitstreamOpened( | |
154 const media::VideoEncodingParameters& params, | |
155 const std::vector<base::SharedMemoryHandle>& buffers, | |
156 int buffer_size) { | |
157 capture_message_loop_proxy_->PostTask(FROM_HERE, | |
158 base::Bind(&VideoCaptureImpl::DoNotifyBitstreamOpenedOnCaptureThread, | |
159 base::Unretained(this), params, buffers, buffer_size)); | |
160 } | |
161 | |
162 void VideoCaptureImpl::OnEncodedBitstreamClosed() { | |
163 capture_message_loop_proxy_->PostTask(FROM_HERE, | |
164 base::Bind(&VideoCaptureImpl::DoNotifyBitstreamClosedOnCaptureThread, | |
165 base::Unretained(this))); | |
166 } | |
167 | |
168 void VideoCaptureImpl::OnEncodingConfigChanged( | |
169 const media::RuntimeVideoEncodingParameters& params) { | |
170 capture_message_loop_proxy_->PostTask(FROM_HERE, | |
171 base::Bind( | |
172 &VideoCaptureImpl::DoNotifyBitstreamConfigChangedOnCaptureThread, | |
173 base::Unretained(this), params)); | |
174 } | |
175 | |
176 void VideoCaptureImpl::OnEncodedBufferReady( | |
177 int buffer_id, | |
178 int size, | |
179 const media::BufferEncodingMetadata& metadata) { | |
180 capture_message_loop_proxy_->PostTask(FROM_HERE, | |
181 base::Bind(&VideoCaptureImpl::DoNotifyBitstreamBufferReadyOnCaptureThread, | |
182 base::Unretained(this), buffer_id, size, metadata)); | |
183 } | |
184 | |
104 void VideoCaptureImpl::FeedBuffer(scoped_refptr<VideoFrameBuffer> buffer) { | 185 void VideoCaptureImpl::FeedBuffer(scoped_refptr<VideoFrameBuffer> buffer) { |
105 capture_message_loop_proxy_->PostTask(FROM_HERE, | 186 capture_message_loop_proxy_->PostTask(FROM_HERE, |
106 base::Bind(&VideoCaptureImpl::DoFeedBufferOnCaptureThread, | 187 base::Bind(&VideoCaptureImpl::DoFeedBufferOnCaptureThread, |
107 base::Unretained(this), buffer)); | 188 base::Unretained(this), buffer)); |
108 } | 189 } |
109 | 190 |
110 void VideoCaptureImpl::OnBufferCreated( | 191 void VideoCaptureImpl::OnBufferCreated( |
111 base::SharedMemoryHandle handle, | 192 base::SharedMemoryHandle handle, |
112 int length, int buffer_id) { | 193 int length, int buffer_id) { |
113 capture_message_loop_proxy_->PostTask(FROM_HERE, | 194 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); | 380 it->first->OnBufferReady(this, buffer); |
300 } | 381 } |
301 cached_dibs_[buffer_id]->references = clients_.size(); | 382 cached_dibs_[buffer_id]->references = clients_.size(); |
302 } | 383 } |
303 | 384 |
304 void VideoCaptureImpl::DoStateChangedOnCaptureThread(VideoCaptureState state) { | 385 void VideoCaptureImpl::DoStateChangedOnCaptureThread(VideoCaptureState state) { |
305 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); | 386 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); |
306 | 387 |
307 switch (state) { | 388 switch (state) { |
308 case VIDEO_CAPTURE_STATE_STARTED: | 389 case VIDEO_CAPTURE_STATE_STARTED: |
390 StartFetchCapabilities(); | |
309 break; | 391 break; |
310 case VIDEO_CAPTURE_STATE_STOPPED: | 392 case VIDEO_CAPTURE_STATE_STOPPED: |
311 state_ = VIDEO_CAPTURE_STATE_STOPPED; | 393 state_ = VIDEO_CAPTURE_STATE_STOPPED; |
312 DVLOG(1) << "OnStateChanged: stopped!, device_id = " << device_id_; | 394 DVLOG(1) << "OnStateChanged: stopped!, device_id = " << device_id_; |
313 STLDeleteValues(&cached_dibs_); | 395 STLDeleteValues(&cached_dibs_); |
314 if (!clients_.empty() || !clients_pending_on_restart_.empty()) | 396 if (!clients_.empty() || !clients_pending_on_restart_.empty()) |
315 RestartCapture(); | 397 RestartCapture(); |
316 break; | 398 break; |
317 case VIDEO_CAPTURE_STATE_PAUSED: | 399 case VIDEO_CAPTURE_STATE_PAUSED: |
318 for (ClientInfo::iterator it = clients_.begin(); | 400 for (ClientInfo::iterator it = clients_.begin(); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
373 } | 455 } |
374 } | 456 } |
375 | 457 |
376 void VideoCaptureImpl::DoSuspendCaptureOnCaptureThread(bool suspend) { | 458 void VideoCaptureImpl::DoSuspendCaptureOnCaptureThread(bool suspend) { |
377 DVLOG(1) << "DoSuspendCapture: suspend " << (suspend ? "yes" : "no"); | 459 DVLOG(1) << "DoSuspendCapture: suspend " << (suspend ? "yes" : "no"); |
378 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); | 460 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); |
379 | 461 |
380 suspended_ = suspend; | 462 suspended_ = suspend; |
381 } | 463 } |
382 | 464 |
465 void VideoCaptureImpl::DoNotifyBitstreamOpenedOnCaptureThread( | |
466 const media::VideoEncodingParameters& params, | |
467 const std::vector<base::SharedMemoryHandle>& buffers, | |
468 int buffer_size) { | |
469 DCHECK(bitstream_buffers_.empty()); | |
470 bitstream_buffer_size_ = buffer_size; | |
471 for (size_t i = 0; i < buffers.size(); ++i) { | |
472 base::SharedMemory* shm = new base::SharedMemory(buffers[i], true); | |
473 DCHECK(shm->Map(buffer_size)); | |
474 bitstream_buffers_.push_back(shm); | |
475 } | |
476 client_->OnOpened(params); | |
477 } | |
478 | |
479 void VideoCaptureImpl::DoNotifyBitstreamClosedOnCaptureThread() { | |
480 for (size_t i = 0; i < bitstream_buffers_.size(); ++i) | |
481 bitstream_buffers_[i]->Close(); | |
482 bitstream_buffers_.clear(); | |
sheu
2013/06/12 22:35:05
I think we leak base::SharedMemory objects at this
hshi1
2013/06/13 00:25:31
Done.
| |
483 client_->OnClosed(); | |
484 } | |
485 | |
486 void VideoCaptureImpl::DoNotifyBitstreamConfigChangedOnCaptureThread( | |
487 const media::RuntimeVideoEncodingParameters& params) { | |
488 client_->OnConfigChanged(params); | |
489 } | |
490 | |
491 void VideoCaptureImpl::DoNotifyBitstreamBufferReadyOnCaptureThread( | |
492 int buffer_id, | |
493 int size, | |
494 const media::BufferEncodingMetadata& metadata) { | |
495 if (buffer_id >= 0 && buffer_id < (int)bitstream_buffers_.size()) { | |
496 base::SharedMemory* shm = bitstream_buffers_[buffer_id]; | |
497 scoped_refptr<media::EncodedBitstreamBuffer> buffer = | |
498 new media::EncodedBitstreamBuffer( | |
499 buffer_id, (uint8*)shm->memory(), size, metadata); | |
sheu
2013/06/12 22:35:05
What are the lifetime semantics of the shared memo
hshi1
2013/06/13 00:25:31
The EBB object is passed linearly from one point t
sheu
2013/06/13 05:36:51
I'm confident that you've got the lifetime nailed
| |
500 client_->OnBufferReady(buffer); | |
501 } | |
502 } | |
503 | |
383 void VideoCaptureImpl::StopDevice() { | 504 void VideoCaptureImpl::StopDevice() { |
384 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); | 505 DCHECK(capture_message_loop_proxy_->BelongsToCurrentThread()); |
385 | 506 |
386 device_info_available_ = false; | 507 device_info_available_ = false; |
387 if (state_ == VIDEO_CAPTURE_STATE_STARTED) { | 508 if (state_ == VIDEO_CAPTURE_STATE_STARTED) { |
388 state_ = VIDEO_CAPTURE_STATE_STOPPING; | 509 state_ = VIDEO_CAPTURE_STATE_STOPPING; |
389 Send(new VideoCaptureHostMsg_Stop(device_id_)); | 510 Send(new VideoCaptureHostMsg_Stop(device_id_)); |
390 current_params_.width = current_params_.height = 0; | 511 current_params_.width = current_params_.height = 0; |
391 } | 512 } |
392 } | 513 } |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
461 if (it != clients->end()) { | 582 if (it != clients->end()) { |
462 handler->OnStopped(this); | 583 handler->OnStopped(this); |
463 handler->OnRemoved(this); | 584 handler->OnRemoved(this); |
464 clients->erase(it); | 585 clients->erase(it); |
465 found = true; | 586 found = true; |
466 } | 587 } |
467 return found; | 588 return found; |
468 } | 589 } |
469 | 590 |
470 } // namespace content | 591 } // namespace content |
OLD | NEW |