content/gpu: Ownership and lifetime cleanup for CommandBufferProxyImpl.
[chromium-blink-merge.git] / content / renderer / pepper / pepper_platform_video_capture.cc
blobeb6e748a5fa93ddd6d9704bd99d2c971b5c1bbe1
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"
7 #include "base/bind.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"
15 #include "url/gurl.h"
17 namespace content {
19 PepperPlatformVideoCapture::PepperPlatformVideoCapture(
20 int render_frame_id,
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),
26 session_id_(0),
27 handler_(handler),
28 pending_open_device_(false),
29 pending_open_device_id_(-1),
30 weak_factory_(this) {
31 // We need to open the device and obtain the label and session ID before
32 // initializing.
33 PepperMediaDeviceManager* const device_manager = GetMediaDeviceManager();
34 if (device_manager) {
35 pending_open_device_id_ = device_manager->OpenDevice(
36 PP_DEVICETYPE_DEV_VIDEOCAPTURE,
37 device_id,
38 document_url,
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())
49 return;
50 VideoCaptureImplManager* manager =
51 RenderThreadImpl::current()->video_capture_impl_manager();
52 stop_capture_cb_ =
53 manager->StartCapture(session_id_,
54 params,
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())
66 return;
67 stop_capture_cb_.Run();
68 stop_capture_cb_.Reset();
71 void PepperPlatformVideoCapture::DetachEventHandler() {
72 handler_ = NULL;
73 StopCapture();
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();
80 if (device_manager)
81 device_manager->CloseDevice(label_);
82 label_.clear();
84 if (pending_open_device_) {
85 PepperMediaDeviceManager* const device_manager = GetMediaDeviceManager();
86 if (device_manager)
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,
101 bool succeeded,
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;
108 if (succeeded) {
109 label_ = label;
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_);
117 if (handler_)
118 handler_->OnInitialized(succeeded);
121 void PepperPlatformVideoCapture::OnStateUpdate(VideoCaptureState state) {
122 if (!handler_)
123 return;
124 switch (state) {
125 case VIDEO_CAPTURE_STATE_STARTED:
126 handler_->OnStarted();
127 break;
128 case VIDEO_CAPTURE_STATE_STOPPED:
129 handler_->OnStopped();
130 break;
131 case VIDEO_CAPTURE_STATE_PAUSED:
132 handler_->OnPaused();
133 break;
134 case VIDEO_CAPTURE_STATE_ERROR:
135 handler_->OnError();
136 break;
137 default:
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