Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / content / renderer / media / video_capture_impl.cc
blobbae0813bbae742623348f9b36aa056b730fbd334
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
3 // found in the LICENSE file.
4 //
5 // Notes about usage of this object by VideoCaptureImplManager.
6 //
7 // VideoCaptureImplManager access this object by using a Unretained()
8 // binding and tasks on the IO thread. It is then important that
9 // VideoCaptureImpl never post task to itself. All operations must be
10 // synchronous.
12 #include "content/renderer/media/video_capture_impl.h"
14 #include "base/bind.h"
15 #include "base/stl_util.h"
16 #include "content/child/child_process.h"
17 #include "content/common/media/video_capture_messages.h"
18 #include "media/base/bind_to_current_loop.h"
19 #include "media/base/limits.h"
20 #include "media/base/video_frame.h"
22 namespace content {
24 class VideoCaptureImpl::ClientBuffer
25 : public base::RefCountedThreadSafe<ClientBuffer> {
26 public:
27 ClientBuffer(scoped_ptr<base::SharedMemory> buffer,
28 size_t buffer_size)
29 : buffer(buffer.Pass()),
30 buffer_size(buffer_size) {}
31 const scoped_ptr<base::SharedMemory> buffer;
32 const size_t buffer_size;
34 private:
35 friend class base::RefCountedThreadSafe<ClientBuffer>;
37 virtual ~ClientBuffer() {}
39 DISALLOW_COPY_AND_ASSIGN(ClientBuffer);
42 VideoCaptureImpl::ClientInfo::ClientInfo() {}
43 VideoCaptureImpl::ClientInfo::~ClientInfo() {}
45 VideoCaptureImpl::VideoCaptureImpl(
46 const media::VideoCaptureSessionId session_id,
47 VideoCaptureMessageFilter* filter)
48 : message_filter_(filter),
49 device_id_(0),
50 session_id_(session_id),
51 suspended_(false),
52 state_(VIDEO_CAPTURE_STATE_STOPPED),
53 weak_factory_(this) {
54 DCHECK(filter);
55 thread_checker_.DetachFromThread();
58 VideoCaptureImpl::~VideoCaptureImpl() {
59 DCHECK(thread_checker_.CalledOnValidThread());
62 void VideoCaptureImpl::Init() {
63 DCHECK(thread_checker_.CalledOnValidThread());
64 message_filter_->AddDelegate(this);
67 void VideoCaptureImpl::DeInit() {
68 DCHECK(thread_checker_.CalledOnValidThread());
69 if (state_ == VIDEO_CAPTURE_STATE_STARTED)
70 Send(new VideoCaptureHostMsg_Stop(device_id_));
71 message_filter_->RemoveDelegate(this);
74 void VideoCaptureImpl::SuspendCapture(bool suspend) {
75 DCHECK(thread_checker_.CalledOnValidThread());
76 suspended_ = suspend;
79 void VideoCaptureImpl::StartCapture(
80 int client_id,
81 const media::VideoCaptureParams& params,
82 const VideoCaptureStateUpdateCB& state_update_cb,
83 const VideoCaptureDeliverFrameCB& deliver_frame_cb) {
84 DCHECK(thread_checker_.CalledOnValidThread());
85 ClientInfo client_info;
86 client_info.params = params;
87 client_info.state_update_cb = state_update_cb;
88 client_info.deliver_frame_cb = deliver_frame_cb;
90 if (state_ == VIDEO_CAPTURE_STATE_ERROR) {
91 state_update_cb.Run(VIDEO_CAPTURE_STATE_ERROR);
92 } else if (clients_pending_on_filter_.count(client_id) ||
93 clients_pending_on_restart_.count(client_id) ||
94 clients_.count(client_id)) {
95 LOG(FATAL) << "This client has already started.";
96 } else if (!device_id_) {
97 clients_pending_on_filter_[client_id] = client_info;
98 } else {
99 // Note: |state_| might not be started at this point. But we tell
100 // client that we have started.
101 state_update_cb.Run(VIDEO_CAPTURE_STATE_STARTED);
102 if (state_ == VIDEO_CAPTURE_STATE_STARTED) {
103 clients_[client_id] = client_info;
104 // TODO(sheu): Allowing resolution change will require that all
105 // outstanding clients of a capture session support resolution change.
106 DCHECK_EQ(params_.allow_resolution_change,
107 params.allow_resolution_change);
108 } else if (state_ == VIDEO_CAPTURE_STATE_STOPPING) {
109 clients_pending_on_restart_[client_id] = client_info;
110 DVLOG(1) << "StartCapture: Got new resolution "
111 << params.requested_format.frame_size.ToString()
112 << " during stopping.";
113 } else {
114 clients_[client_id] = client_info;
115 if (state_ == VIDEO_CAPTURE_STATE_STARTED)
116 return;
117 params_ = params;
118 if (params_.requested_format.frame_rate >
119 media::limits::kMaxFramesPerSecond) {
120 params_.requested_format.frame_rate =
121 media::limits::kMaxFramesPerSecond;
123 DVLOG(1) << "StartCapture: starting with first resolution "
124 << params_.requested_format.frame_size.ToString();
125 first_frame_timestamp_ = base::TimeTicks();
126 StartCaptureInternal();
131 void VideoCaptureImpl::StopCapture(int client_id) {
132 DCHECK(thread_checker_.CalledOnValidThread());
134 // A client ID can be in only one client list.
135 // If this ID is in any client list, we can just remove it from
136 // that client list and don't have to run the other following RemoveClient().
137 if (!RemoveClient(client_id, &clients_pending_on_filter_)) {
138 if (!RemoveClient(client_id, &clients_pending_on_restart_)) {
139 RemoveClient(client_id, &clients_);
143 if (clients_.empty()) {
144 DVLOG(1) << "StopCapture: No more client, stopping ...";
145 StopDevice();
146 client_buffers_.clear();
147 weak_factory_.InvalidateWeakPtrs();
151 void VideoCaptureImpl::GetDeviceSupportedFormats(
152 const VideoCaptureDeviceFormatsCB& callback) {
153 DCHECK(thread_checker_.CalledOnValidThread());
154 device_formats_cb_queue_.push_back(callback);
155 if (device_formats_cb_queue_.size() == 1)
156 Send(new VideoCaptureHostMsg_GetDeviceSupportedFormats(device_id_,
157 session_id_));
160 void VideoCaptureImpl::GetDeviceFormatsInUse(
161 const VideoCaptureDeviceFormatsCB& callback) {
162 DCHECK(thread_checker_.CalledOnValidThread());
163 device_formats_in_use_cb_queue_.push_back(callback);
164 if (device_formats_in_use_cb_queue_.size() == 1)
165 Send(
166 new VideoCaptureHostMsg_GetDeviceFormatsInUse(device_id_, session_id_));
169 void VideoCaptureImpl::OnBufferCreated(
170 base::SharedMemoryHandle handle,
171 int length, int buffer_id) {
172 DCHECK(thread_checker_.CalledOnValidThread());
174 // In case client calls StopCapture before the arrival of created buffer,
175 // just close this buffer and return.
176 if (state_ != VIDEO_CAPTURE_STATE_STARTED) {
177 base::SharedMemory::CloseHandle(handle);
178 return;
181 scoped_ptr<base::SharedMemory> shm(new base::SharedMemory(handle, false));
182 if (!shm->Map(length)) {
183 DLOG(ERROR) << "OnBufferCreated: Map failed.";
184 return;
187 bool inserted =
188 client_buffers_.insert(std::make_pair(
189 buffer_id,
190 new ClientBuffer(shm.Pass(),
191 length))).second;
192 DCHECK(inserted);
195 void VideoCaptureImpl::OnBufferDestroyed(int buffer_id) {
196 DCHECK(thread_checker_.CalledOnValidThread());
198 ClientBufferMap::iterator iter = client_buffers_.find(buffer_id);
199 if (iter == client_buffers_.end())
200 return;
202 DCHECK(!iter->second.get() || iter->second->HasOneRef())
203 << "Instructed to delete buffer we are still using.";
204 client_buffers_.erase(iter);
207 void VideoCaptureImpl::OnBufferReceived(int buffer_id,
208 const media::VideoCaptureFormat& format,
209 base::TimeTicks timestamp) {
210 DCHECK(thread_checker_.CalledOnValidThread());
212 // The capture pipeline supports only I420 for now.
213 DCHECK_EQ(format.pixel_format, media::PIXEL_FORMAT_I420);
215 if (state_ != VIDEO_CAPTURE_STATE_STARTED || suspended_) {
216 Send(new VideoCaptureHostMsg_BufferReady(device_id_, buffer_id, 0));
217 return;
220 last_frame_format_ = format;
221 if (first_frame_timestamp_.is_null())
222 first_frame_timestamp_ = timestamp;
224 // Used by chrome/browser/extension/api/cast_streaming/performance_test.cc
225 TRACE_EVENT_INSTANT2(
226 "cast_perf_test", "OnBufferReceived",
227 TRACE_EVENT_SCOPE_THREAD,
228 "timestamp", timestamp.ToInternalValue(),
229 "time_delta", (timestamp - first_frame_timestamp_).ToInternalValue());
231 ClientBufferMap::iterator iter = client_buffers_.find(buffer_id);
232 DCHECK(iter != client_buffers_.end());
233 scoped_refptr<ClientBuffer> buffer = iter->second;
234 scoped_refptr<media::VideoFrame> frame =
235 media::VideoFrame::WrapExternalPackedMemory(
236 media::VideoFrame::I420,
237 last_frame_format_.frame_size,
238 gfx::Rect(last_frame_format_.frame_size),
239 last_frame_format_.frame_size,
240 reinterpret_cast<uint8*>(buffer->buffer->memory()),
241 buffer->buffer_size,
242 buffer->buffer->handle(),
243 timestamp - first_frame_timestamp_,
244 media::BindToCurrentLoop(
245 base::Bind(&VideoCaptureImpl::OnClientBufferFinished,
246 weak_factory_.GetWeakPtr(),
247 buffer_id,
248 buffer,
249 0)));
251 for (ClientInfoMap::iterator it = clients_.begin(); it != clients_.end();
252 ++it) {
253 it->second.deliver_frame_cb.Run(frame, format, timestamp);
257 static void NullReadPixelsCB(const SkBitmap& bitmap) { NOTIMPLEMENTED(); }
259 void VideoCaptureImpl::OnMailboxBufferReceived(
260 int buffer_id,
261 const gpu::MailboxHolder& mailbox_holder,
262 const media::VideoCaptureFormat& format,
263 base::TimeTicks timestamp) {
264 DCHECK(thread_checker_.CalledOnValidThread());
266 if (state_ != VIDEO_CAPTURE_STATE_STARTED || suspended_) {
267 Send(new VideoCaptureHostMsg_BufferReady(device_id_, buffer_id, 0));
268 return;
271 last_frame_format_ = format;
272 if (first_frame_timestamp_.is_null())
273 first_frame_timestamp_ = timestamp;
275 scoped_refptr<media::VideoFrame> frame = media::VideoFrame::WrapNativeTexture(
276 make_scoped_ptr(new gpu::MailboxHolder(mailbox_holder)),
277 media::BindToCurrentLoop(
278 base::Bind(&VideoCaptureImpl::OnClientBufferFinished,
279 weak_factory_.GetWeakPtr(),
280 buffer_id,
281 scoped_refptr<ClientBuffer>())),
282 last_frame_format_.frame_size,
283 gfx::Rect(last_frame_format_.frame_size),
284 last_frame_format_.frame_size,
285 timestamp - first_frame_timestamp_,
286 base::Bind(&NullReadPixelsCB));
288 for (ClientInfoMap::iterator it = clients_.begin(); it != clients_.end();
289 ++it) {
290 it->second.deliver_frame_cb.Run(frame, format, timestamp);
294 void VideoCaptureImpl::OnClientBufferFinished(
295 int buffer_id,
296 const scoped_refptr<ClientBuffer>& /* ignored_buffer */,
297 uint32 release_sync_point) {
298 DCHECK(thread_checker_.CalledOnValidThread());
299 Send(new VideoCaptureHostMsg_BufferReady(
300 device_id_, buffer_id, release_sync_point));
303 void VideoCaptureImpl::OnStateChanged(VideoCaptureState state) {
304 DCHECK(thread_checker_.CalledOnValidThread());
306 switch (state) {
307 case VIDEO_CAPTURE_STATE_STARTED:
308 // Camera has started in the browser process. Since we have already
309 // told all clients that we have started there's nothing to do.
310 break;
311 case VIDEO_CAPTURE_STATE_STOPPED:
312 state_ = VIDEO_CAPTURE_STATE_STOPPED;
313 DVLOG(1) << "OnStateChanged: stopped!, device_id = " << device_id_;
314 client_buffers_.clear();
315 weak_factory_.InvalidateWeakPtrs();
316 if (!clients_.empty() || !clients_pending_on_restart_.empty())
317 RestartCapture();
318 break;
319 case VIDEO_CAPTURE_STATE_PAUSED:
320 for (ClientInfoMap::iterator it = clients_.begin();
321 it != clients_.end(); ++it) {
322 it->second.state_update_cb.Run(VIDEO_CAPTURE_STATE_PAUSED);
324 break;
325 case VIDEO_CAPTURE_STATE_ERROR:
326 DVLOG(1) << "OnStateChanged: error!, device_id = " << device_id_;
327 for (ClientInfoMap::iterator it = clients_.begin();
328 it != clients_.end(); ++it) {
329 it->second.state_update_cb.Run(VIDEO_CAPTURE_STATE_ERROR);
331 clients_.clear();
332 state_ = VIDEO_CAPTURE_STATE_ERROR;
333 break;
334 case VIDEO_CAPTURE_STATE_ENDED:
335 DVLOG(1) << "OnStateChanged: ended!, device_id = " << device_id_;
336 for (ClientInfoMap::iterator it = clients_.begin();
337 it != clients_.end(); ++it) {
338 // We'll only notify the client that the stream has stopped.
339 it->second.state_update_cb.Run(VIDEO_CAPTURE_STATE_STOPPED);
341 clients_.clear();
342 state_ = VIDEO_CAPTURE_STATE_ENDED;
343 break;
344 default:
345 break;
349 void VideoCaptureImpl::OnDeviceSupportedFormatsEnumerated(
350 const media::VideoCaptureFormats& supported_formats) {
351 DCHECK(thread_checker_.CalledOnValidThread());
352 for (size_t i = 0; i < device_formats_cb_queue_.size(); ++i)
353 device_formats_cb_queue_[i].Run(supported_formats);
354 device_formats_cb_queue_.clear();
357 void VideoCaptureImpl::OnDeviceFormatsInUseReceived(
358 const media::VideoCaptureFormats& formats_in_use) {
359 DCHECK(thread_checker_.CalledOnValidThread());
360 for (size_t i = 0; i < device_formats_in_use_cb_queue_.size(); ++i)
361 device_formats_in_use_cb_queue_[i].Run(formats_in_use);
362 device_formats_in_use_cb_queue_.clear();
365 void VideoCaptureImpl::OnDelegateAdded(int32 device_id) {
366 DCHECK(thread_checker_.CalledOnValidThread());
367 DVLOG(1) << "OnDelegateAdded: device_id " << device_id;
369 device_id_ = device_id;
370 for (ClientInfoMap::iterator it = clients_pending_on_filter_.begin();
371 it != clients_pending_on_filter_.end(); ) {
372 int client_id = it->first;
373 VideoCaptureStateUpdateCB state_update_cb =
374 it->second.state_update_cb;
375 VideoCaptureDeliverFrameCB deliver_frame_cb =
376 it->second.deliver_frame_cb;
377 const media::VideoCaptureParams params = it->second.params;
378 clients_pending_on_filter_.erase(it++);
379 StartCapture(client_id, params, state_update_cb,
380 deliver_frame_cb);
384 void VideoCaptureImpl::StopDevice() {
385 DCHECK(thread_checker_.CalledOnValidThread());
387 if (state_ == VIDEO_CAPTURE_STATE_STARTED) {
388 state_ = VIDEO_CAPTURE_STATE_STOPPING;
389 Send(new VideoCaptureHostMsg_Stop(device_id_));
390 params_.requested_format.frame_size.SetSize(0, 0);
394 void VideoCaptureImpl::RestartCapture() {
395 DCHECK(thread_checker_.CalledOnValidThread());
396 DCHECK_EQ(state_, VIDEO_CAPTURE_STATE_STOPPED);
398 int width = 0;
399 int height = 0;
400 clients_.insert(clients_pending_on_restart_.begin(),
401 clients_pending_on_restart_.end());
402 clients_pending_on_restart_.clear();
403 for (ClientInfoMap::iterator it = clients_.begin();
404 it != clients_.end(); ++it) {
405 width = std::max(width,
406 it->second.params.requested_format.frame_size.width());
407 height = std::max(height,
408 it->second.params.requested_format.frame_size.height());
410 params_.requested_format.frame_size.SetSize(width, height);
411 DVLOG(1) << "RestartCapture, "
412 << params_.requested_format.frame_size.ToString();
413 StartCaptureInternal();
416 void VideoCaptureImpl::StartCaptureInternal() {
417 DCHECK(thread_checker_.CalledOnValidThread());
418 DCHECK(device_id_);
420 Send(new VideoCaptureHostMsg_Start(device_id_, session_id_, params_));
421 state_ = VIDEO_CAPTURE_STATE_STARTED;
424 void VideoCaptureImpl::Send(IPC::Message* message) {
425 DCHECK(thread_checker_.CalledOnValidThread());
426 message_filter_->Send(message);
429 bool VideoCaptureImpl::RemoveClient(int client_id, ClientInfoMap* clients) {
430 DCHECK(thread_checker_.CalledOnValidThread());
431 bool found = false;
433 ClientInfoMap::iterator it = clients->find(client_id);
434 if (it != clients->end()) {
435 it->second.state_update_cb.Run(VIDEO_CAPTURE_STATE_STOPPED);
436 clients->erase(it);
437 found = true;
439 return found;
442 } // namespace content