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.
5 // Notes about usage of this object by VideoCaptureImplManager.
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
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"
24 class VideoCaptureImpl::ClientBuffer
25 : public base::RefCountedThreadSafe
<ClientBuffer
> {
27 ClientBuffer(scoped_ptr
<base::SharedMemory
> buffer
,
29 : buffer(buffer
.Pass()),
30 buffer_size(buffer_size
) {}
31 const scoped_ptr
<base::SharedMemory
> buffer
;
32 const size_t buffer_size
;
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
),
50 session_id_(session_id
),
52 state_(VIDEO_CAPTURE_STATE_STOPPED
),
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());
79 void VideoCaptureImpl::StartCapture(
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
;
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.";
114 clients_
[client_id
] = client_info
;
115 if (state_
== VIDEO_CAPTURE_STATE_STARTED
)
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 ...";
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_
,
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)
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
);
181 scoped_ptr
<base::SharedMemory
> shm(new base::SharedMemory(handle
, false));
182 if (!shm
->Map(length
)) {
183 DLOG(ERROR
) << "OnBufferCreated: Map failed.";
188 client_buffers_
.insert(std::make_pair(
190 new ClientBuffer(shm
.Pass(),
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())
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));
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()),
242 buffer
->buffer
->handle(),
243 timestamp
- first_frame_timestamp_
,
244 media::BindToCurrentLoop(
245 base::Bind(&VideoCaptureImpl::OnClientBufferFinished
,
246 weak_factory_
.GetWeakPtr(),
251 for (ClientInfoMap::iterator it
= clients_
.begin(); it
!= clients_
.end();
253 it
->second
.deliver_frame_cb
.Run(frame
, format
, timestamp
);
257 static void NullReadPixelsCB(const SkBitmap
& bitmap
) { NOTIMPLEMENTED(); }
259 void VideoCaptureImpl::OnMailboxBufferReceived(
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));
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(),
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();
290 it
->second
.deliver_frame_cb
.Run(frame
, format
, timestamp
);
294 void VideoCaptureImpl::OnClientBufferFinished(
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());
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.
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())
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
);
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
);
332 state_
= VIDEO_CAPTURE_STATE_ERROR
;
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
);
342 state_
= VIDEO_CAPTURE_STATE_ENDED
;
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
,
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
);
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());
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());
433 ClientInfoMap::iterator it
= clients
->find(client_id
);
434 if (it
!= clients
->end()) {
435 it
->second
.state_update_cb
.Run(VIDEO_CAPTURE_STATE_STOPPED
);
442 } // namespace content