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/pepper/pepper_platform_video_capture.h" | 5 #include "content/renderer/pepper/pepper_platform_video_capture.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop/message_loop_proxy.h" | 9 #include "base/message_loop/message_loop_proxy.h" |
10 #include "content/renderer/media/video_capture_impl_manager.h" | 10 #include "content/renderer/media/video_capture_impl_manager.h" |
11 #include "content/renderer/pepper/pepper_media_device_manager.h" | 11 #include "content/renderer/pepper/pepper_media_device_manager.h" |
12 #include "content/renderer/pepper/pepper_video_capture_host.h" | 12 #include "content/renderer/pepper/pepper_video_capture_host.h" |
13 #include "content/renderer/render_thread_impl.h" | 13 #include "content/renderer/render_thread_impl.h" |
14 #include "content/renderer/render_view_impl.h" | 14 #include "content/renderer/render_view_impl.h" |
15 #include "media/video/capture/video_capture_proxy.h" | 15 #include "media/base/bind_to_current_loop.h" |
16 #include "url/gurl.h" | 16 #include "url/gurl.h" |
17 | 17 |
18 namespace content { | 18 namespace content { |
19 | 19 |
20 PepperPlatformVideoCapture::PepperPlatformVideoCapture( | 20 PepperPlatformVideoCapture::PepperPlatformVideoCapture( |
21 const base::WeakPtr<RenderViewImpl>& render_view, | 21 const base::WeakPtr<RenderViewImpl>& render_view, |
22 const std::string& device_id, | 22 const std::string& device_id, |
23 const GURL& document_url, | 23 const GURL& document_url, |
24 PepperVideoCaptureHost* handler) | 24 PepperVideoCaptureHost* handler) |
25 : render_view_(render_view), | 25 : render_view_(render_view), |
26 device_id_(device_id), | 26 device_id_(device_id), |
27 session_id_(0), | 27 session_id_(0), |
28 handler_proxy_(new media::VideoCaptureHandlerProxy( | |
29 this, | |
30 base::MessageLoopProxy::current())), | |
31 handler_(handler), | 28 handler_(handler), |
32 unbalanced_start_(false), | |
33 pending_open_device_(false), | 29 pending_open_device_(false), |
34 pending_open_device_id_(-1) { | 30 pending_open_device_id_(-1), |
| 31 weak_factory_(this) { |
35 // We need to open the device and obtain the label and session ID before | 32 // We need to open the device and obtain the label and session ID before |
36 // initializing. | 33 // initializing. |
37 if (render_view_.get()) { | 34 if (render_view_.get()) { |
38 pending_open_device_id_ = GetMediaDeviceManager()->OpenDevice( | 35 pending_open_device_id_ = GetMediaDeviceManager()->OpenDevice( |
39 PP_DEVICETYPE_DEV_VIDEOCAPTURE, | 36 PP_DEVICETYPE_DEV_VIDEOCAPTURE, |
40 device_id, | 37 device_id, |
41 document_url, | 38 document_url, |
42 base::Bind(&PepperPlatformVideoCapture::OnDeviceOpened, this)); | 39 base::Bind(&PepperPlatformVideoCapture::OnDeviceOpened, |
| 40 weak_factory_.GetWeakPtr())); |
43 pending_open_device_ = true; | 41 pending_open_device_ = true; |
44 } | 42 } |
45 } | 43 } |
46 | 44 |
47 void PepperPlatformVideoCapture::StartCapture( | 45 void PepperPlatformVideoCapture::StartCapture( |
48 media::VideoCapture::EventHandler* handler, | |
49 const media::VideoCaptureParams& params) { | 46 const media::VideoCaptureParams& params) { |
50 DCHECK(handler == handler_); | 47 DCHECK(thread_checker_.CalledOnValidThread()); |
51 | 48 if (!stop_capture_cb_.is_null()) |
52 if (unbalanced_start_) | |
53 return; | 49 return; |
54 | 50 VideoCaptureImplManager* manager = |
55 if (video_capture_) { | 51 RenderThreadImpl::current()->video_capture_impl_manager(); |
56 unbalanced_start_ = true; | 52 stop_capture_cb_ = |
57 AddRef(); // Will be balanced in OnRemoved(). | 53 manager->StartCapture(session_id_, |
58 video_capture_->StartCapture(handler_proxy_.get(), params); | 54 params, |
59 } | 55 media::BindToCurrentLoop(base::Bind( |
| 56 &PepperPlatformVideoCapture::OnStateUpdate, |
| 57 weak_factory_.GetWeakPtr())), |
| 58 media::BindToCurrentLoop(base::Bind( |
| 59 &PepperPlatformVideoCapture::OnFrameReady, |
| 60 weak_factory_.GetWeakPtr()))); |
60 } | 61 } |
61 | 62 |
62 void PepperPlatformVideoCapture::StopCapture( | 63 void PepperPlatformVideoCapture::StopCapture() { |
63 media::VideoCapture::EventHandler* handler) { | 64 DCHECK(thread_checker_.CalledOnValidThread()); |
64 DCHECK(handler == handler_); | 65 if (stop_capture_cb_.is_null()) |
65 if (!unbalanced_start_) | |
66 return; | 66 return; |
67 | 67 stop_capture_cb_.Run(); |
68 if (video_capture_) { | 68 stop_capture_cb_.Reset(); |
69 unbalanced_start_ = false; | |
70 video_capture_->StopCapture(handler_proxy_.get()); | |
71 } | |
72 } | |
73 | |
74 bool PepperPlatformVideoCapture::CaptureStarted() { | |
75 return handler_proxy_->state().started; | |
76 } | |
77 | |
78 int PepperPlatformVideoCapture::CaptureFrameRate() { | |
79 return handler_proxy_->state().frame_rate; | |
80 } | |
81 | |
82 void PepperPlatformVideoCapture::GetDeviceSupportedFormats( | |
83 const DeviceFormatsCallback& callback) { | |
84 NOTREACHED(); | |
85 } | |
86 | |
87 void PepperPlatformVideoCapture::GetDeviceFormatsInUse( | |
88 const DeviceFormatsInUseCallback& callback) { | |
89 NOTREACHED(); | |
90 } | 69 } |
91 | 70 |
92 void PepperPlatformVideoCapture::DetachEventHandler() { | 71 void PepperPlatformVideoCapture::DetachEventHandler() { |
93 handler_ = NULL; | 72 handler_ = NULL; |
94 StopCapture(NULL); | 73 StopCapture(); |
95 | 74 if (!release_device_cb_.is_null()) { |
96 video_capture_.reset(); | 75 release_device_cb_.Run(); |
97 | 76 release_device_cb_.Reset(); |
| 77 } |
98 if (render_view_.get()) { | 78 if (render_view_.get()) { |
99 if (!label_.empty()) { | 79 if (!label_.empty()) { |
100 GetMediaDeviceManager()->CloseDevice(label_); | 80 GetMediaDeviceManager()->CloseDevice(label_); |
101 label_.clear(); | 81 label_.clear(); |
102 } | 82 } |
103 if (pending_open_device_) { | 83 if (pending_open_device_) { |
104 GetMediaDeviceManager()->CancelOpenDevice(pending_open_device_id_); | 84 GetMediaDeviceManager()->CancelOpenDevice(pending_open_device_id_); |
105 pending_open_device_ = false; | 85 pending_open_device_ = false; |
106 pending_open_device_id_ = -1; | 86 pending_open_device_id_ = -1; |
107 } | 87 } |
108 } | 88 } |
109 } | 89 } |
110 | 90 |
111 void PepperPlatformVideoCapture::OnStarted(VideoCapture* capture) { | |
112 if (handler_) | |
113 handler_->OnStarted(capture); | |
114 } | |
115 | |
116 void PepperPlatformVideoCapture::OnStopped(VideoCapture* capture) { | |
117 if (handler_) | |
118 handler_->OnStopped(capture); | |
119 } | |
120 | |
121 void PepperPlatformVideoCapture::OnPaused(VideoCapture* capture) { | |
122 if (handler_) | |
123 handler_->OnPaused(capture); | |
124 } | |
125 | |
126 void PepperPlatformVideoCapture::OnError(VideoCapture* capture, | |
127 int error_code) { | |
128 if (handler_) | |
129 handler_->OnError(capture, error_code); | |
130 } | |
131 | |
132 void PepperPlatformVideoCapture::OnRemoved(VideoCapture* capture) { | |
133 if (handler_) | |
134 handler_->OnRemoved(capture); | |
135 | |
136 Release(); // Balance the AddRef() in StartCapture(). | |
137 } | |
138 | |
139 void PepperPlatformVideoCapture::OnFrameReady( | |
140 VideoCapture* capture, | |
141 const scoped_refptr<media::VideoFrame>& frame) { | |
142 if (handler_) | |
143 handler_->OnFrameReady(capture, frame); | |
144 } | |
145 | |
146 PepperPlatformVideoCapture::~PepperPlatformVideoCapture() { | 91 PepperPlatformVideoCapture::~PepperPlatformVideoCapture() { |
147 DCHECK(!video_capture_); | 92 DCHECK(stop_capture_cb_.is_null()); |
| 93 DCHECK(release_device_cb_.is_null()); |
148 DCHECK(label_.empty()); | 94 DCHECK(label_.empty()); |
149 DCHECK(!pending_open_device_); | 95 DCHECK(!pending_open_device_); |
150 } | 96 } |
151 | 97 |
152 void PepperPlatformVideoCapture::Initialize() { | |
153 VideoCaptureImplManager* manager = | |
154 RenderThreadImpl::current()->video_capture_impl_manager(); | |
155 video_capture_ = manager->UseDevice(session_id_); | |
156 } | |
157 | |
158 void PepperPlatformVideoCapture::OnDeviceOpened(int request_id, | 98 void PepperPlatformVideoCapture::OnDeviceOpened(int request_id, |
159 bool succeeded, | 99 bool succeeded, |
160 const std::string& label) { | 100 const std::string& label) { |
161 pending_open_device_ = false; | 101 pending_open_device_ = false; |
162 pending_open_device_id_ = -1; | 102 pending_open_device_id_ = -1; |
163 | 103 |
164 succeeded = succeeded && render_view_.get(); | 104 succeeded = succeeded && render_view_.get(); |
165 if (succeeded) { | 105 if (succeeded) { |
166 label_ = label; | 106 label_ = label; |
167 session_id_ = GetMediaDeviceManager()->GetSessionID( | 107 session_id_ = GetMediaDeviceManager()->GetSessionID( |
168 PP_DEVICETYPE_DEV_VIDEOCAPTURE, label); | 108 PP_DEVICETYPE_DEV_VIDEOCAPTURE, label); |
169 Initialize(); | 109 VideoCaptureImplManager* manager = |
| 110 RenderThreadImpl::current()->video_capture_impl_manager(); |
| 111 release_device_cb_ = manager->UseDevice(session_id_); |
170 } | 112 } |
171 | 113 |
172 if (handler_) | 114 if (handler_) |
173 handler_->OnInitialized(this, succeeded); | 115 handler_->OnInitialized(succeeded); |
| 116 } |
| 117 |
| 118 void PepperPlatformVideoCapture::OnStateUpdate(VideoCaptureState state) { |
| 119 if (!handler_) |
| 120 return; |
| 121 switch (state) { |
| 122 case VIDEO_CAPTURE_STATE_STARTED: |
| 123 handler_->OnStarted(); |
| 124 break; |
| 125 case VIDEO_CAPTURE_STATE_STOPPED: |
| 126 handler_->OnStopped(); |
| 127 break; |
| 128 case VIDEO_CAPTURE_STATE_PAUSED: |
| 129 handler_->OnPaused(); |
| 130 break; |
| 131 case VIDEO_CAPTURE_STATE_ERROR: |
| 132 handler_->OnError(); |
| 133 break; |
| 134 default: |
| 135 NOTREACHED() << "Unexpected state: " << state << "."; |
| 136 } |
| 137 } |
| 138 |
| 139 void PepperPlatformVideoCapture::OnFrameReady( |
| 140 const scoped_refptr<media::VideoFrame>& frame, |
| 141 const media::VideoCaptureFormat& format) { |
| 142 if (handler_ && !stop_capture_cb_.is_null()) |
| 143 handler_->OnFrameReady(frame, format); |
174 } | 144 } |
175 | 145 |
176 PepperMediaDeviceManager* PepperPlatformVideoCapture::GetMediaDeviceManager() { | 146 PepperMediaDeviceManager* PepperPlatformVideoCapture::GetMediaDeviceManager() { |
177 return PepperMediaDeviceManager::GetForRenderView(render_view_.get()); | 147 return PepperMediaDeviceManager::GetForRenderView(render_view_.get()); |
178 } | 148 } |
179 | 149 |
180 } // namespace content | 150 } // namespace content |
OLD | NEW |