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 #include "content/renderer/pepper/pepper_platform_video_capture.h"
8 #include "base/logging.h"
9 #include "content/renderer/media/video_capture_impl_manager.h"
10 #include "content/renderer/pepper/pepper_media_device_manager.h"
11 #include "content/renderer/pepper/pepper_video_capture_host.h"
12 #include "content/renderer/render_frame_impl.h"
13 #include "content/renderer/render_thread_impl.h"
14 #include "media/base/bind_to_current_loop.h"
19 PepperPlatformVideoCapture::PepperPlatformVideoCapture(
21 const std::string
& device_id
,
22 const GURL
& document_url
,
23 PepperVideoCaptureHost
* handler
)
24 : render_frame_id_(render_frame_id
),
25 device_id_(device_id
),
28 pending_open_device_(false),
29 pending_open_device_id_(-1),
31 // We need to open the device and obtain the label and session ID before
33 PepperMediaDeviceManager
* const device_manager
= GetMediaDeviceManager();
35 pending_open_device_id_
= device_manager
->OpenDevice(
36 PP_DEVICETYPE_DEV_VIDEOCAPTURE
,
39 base::Bind(&PepperPlatformVideoCapture::OnDeviceOpened
,
40 weak_factory_
.GetWeakPtr()));
41 pending_open_device_
= true;
45 void PepperPlatformVideoCapture::StartCapture(
46 const media::VideoCaptureParams
& params
) {
47 DCHECK(thread_checker_
.CalledOnValidThread());
48 if (!stop_capture_cb_
.is_null())
50 VideoCaptureImplManager
* manager
=
51 RenderThreadImpl::current()->video_capture_impl_manager();
53 manager
->StartCapture(session_id_
,
55 media::BindToCurrentLoop(base::Bind(
56 &PepperPlatformVideoCapture::OnStateUpdate
,
57 weak_factory_
.GetWeakPtr())),
58 media::BindToCurrentLoop(base::Bind(
59 &PepperPlatformVideoCapture::OnFrameReady
,
60 weak_factory_
.GetWeakPtr())));
63 void PepperPlatformVideoCapture::StopCapture() {
64 DCHECK(thread_checker_
.CalledOnValidThread());
65 if (stop_capture_cb_
.is_null())
67 stop_capture_cb_
.Run();
68 stop_capture_cb_
.Reset();
71 void PepperPlatformVideoCapture::DetachEventHandler() {
74 if (!release_device_cb_
.is_null()) {
75 release_device_cb_
.Run();
76 release_device_cb_
.Reset();
78 if (!label_
.empty()) {
79 PepperMediaDeviceManager
* const device_manager
= GetMediaDeviceManager();
81 device_manager
->CloseDevice(label_
);
84 if (pending_open_device_
) {
85 PepperMediaDeviceManager
* const device_manager
= GetMediaDeviceManager();
87 device_manager
->CancelOpenDevice(pending_open_device_id_
);
88 pending_open_device_
= false;
89 pending_open_device_id_
= -1;
93 PepperPlatformVideoCapture::~PepperPlatformVideoCapture() {
94 DCHECK(stop_capture_cb_
.is_null());
95 DCHECK(release_device_cb_
.is_null());
96 DCHECK(label_
.empty());
97 DCHECK(!pending_open_device_
);
100 void PepperPlatformVideoCapture::OnDeviceOpened(int request_id
,
102 const std::string
& label
) {
103 pending_open_device_
= false;
104 pending_open_device_id_
= -1;
106 PepperMediaDeviceManager
* const device_manager
= GetMediaDeviceManager();
107 succeeded
= succeeded
&& device_manager
;
110 session_id_
= device_manager
->GetSessionID(
111 PP_DEVICETYPE_DEV_VIDEOCAPTURE
, label
);
112 VideoCaptureImplManager
* manager
=
113 RenderThreadImpl::current()->video_capture_impl_manager();
114 release_device_cb_
= manager
->UseDevice(session_id_
);
118 handler_
->OnInitialized(succeeded
);
121 void PepperPlatformVideoCapture::OnStateUpdate(VideoCaptureState state
) {
125 case VIDEO_CAPTURE_STATE_STARTED
:
126 handler_
->OnStarted();
128 case VIDEO_CAPTURE_STATE_STOPPED
:
129 handler_
->OnStopped();
131 case VIDEO_CAPTURE_STATE_PAUSED
:
132 handler_
->OnPaused();
134 case VIDEO_CAPTURE_STATE_ERROR
:
138 NOTREACHED() << "Unexpected state: " << state
<< ".";
142 void PepperPlatformVideoCapture::OnFrameReady(
143 const scoped_refptr
<media::VideoFrame
>& frame
,
144 const base::TimeTicks
& estimated_capture_time
) {
145 if (handler_
&& !stop_capture_cb_
.is_null())
146 handler_
->OnFrameReady(frame
);
149 PepperMediaDeviceManager
* PepperPlatformVideoCapture::GetMediaDeviceManager() {
150 RenderFrameImpl
* const render_frame
=
151 RenderFrameImpl::FromRoutingID(render_frame_id_
);
152 return render_frame
?
153 PepperMediaDeviceManager::GetForRenderFrame(render_frame
).get() : NULL
;
156 } // namespace content