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) { |
| 32 DCHECK(thread_checker_.CalledOnValidThread()); |
35 // We need to open the device and obtain the label and session ID before | 33 // We need to open the device and obtain the label and session ID before |
36 // initializing. | 34 // initializing. |
37 if (render_view_.get()) { | 35 if (render_view_.get()) { |
38 pending_open_device_id_ = GetMediaDeviceManager()->OpenDevice( | 36 pending_open_device_id_ = GetMediaDeviceManager()->OpenDevice( |
39 PP_DEVICETYPE_DEV_VIDEOCAPTURE, | 37 PP_DEVICETYPE_DEV_VIDEOCAPTURE, |
40 device_id, | 38 device_id, |
41 document_url, | 39 document_url, |
42 base::Bind(&PepperPlatformVideoCapture::OnDeviceOpened, this)); | 40 base::Bind(&PepperPlatformVideoCapture::OnDeviceOpened, |
| 41 weak_factory_.GetWeakPtr())); |
43 pending_open_device_ = true; | 42 pending_open_device_ = true; |
44 } | 43 } |
45 } | 44 } |
46 | 45 |
47 void PepperPlatformVideoCapture::StartCapture( | 46 void PepperPlatformVideoCapture::StartCapture( |
48 media::VideoCapture::EventHandler* handler, | |
49 const media::VideoCaptureParams& params) { | 47 const media::VideoCaptureParams& params) { |
50 DCHECK(handler == handler_); | 48 DCHECK(thread_checker_.CalledOnValidThread()); |
51 | 49 if (!stop_capture_cb_.is_null()) |
52 if (unbalanced_start_) | |
53 return; | 50 return; |
54 | 51 VideoCaptureImplManager* manager = |
55 if (video_capture_) { | 52 RenderThreadImpl::current()->video_capture_impl_manager(); |
56 unbalanced_start_ = true; | 53 stop_capture_cb_ = manager->StartCapture( |
57 AddRef(); // Will be balanced in OnRemoved(). | 54 session_id_, |
58 video_capture_->StartCapture(handler_proxy_.get(), params); | 55 params, |
59 } | 56 media::BindToCurrentLoop( |
| 57 base::Bind(&PepperPlatformVideoCapture::OnStateUpdate, |
| 58 weak_factory_.GetWeakPtr())), |
| 59 media::BindToCurrentLoop( |
| 60 base::Bind(&PepperPlatformVideoCapture::OnFrameReady, |
| 61 weak_factory_.GetWeakPtr()))); |
60 } | 62 } |
61 | 63 |
62 void PepperPlatformVideoCapture::StopCapture( | 64 void PepperPlatformVideoCapture::StopCapture() { |
63 media::VideoCapture::EventHandler* handler) { | 65 DCHECK(thread_checker_.CalledOnValidThread()); |
64 DCHECK(handler == handler_); | 66 if (stop_capture_cb_.is_null()) |
65 if (!unbalanced_start_) | |
66 return; | 67 return; |
67 | 68 stop_capture_cb_.Run(); |
68 if (video_capture_) { | 69 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 } | 70 } |
91 | 71 |
92 void PepperPlatformVideoCapture::DetachEventHandler() { | 72 void PepperPlatformVideoCapture::DetachEventHandler() { |
93 handler_ = NULL; | 73 handler_ = NULL; |
94 StopCapture(NULL); | 74 StopCapture(); |
95 | 75 if (!release_device_cb_.is_null()) { |
96 video_capture_.reset(); | 76 release_device_cb_.Run(); |
97 | 77 release_device_cb_.Reset(); |
| 78 } |
98 if (render_view_.get()) { | 79 if (render_view_.get()) { |
99 if (!label_.empty()) { | 80 if (!label_.empty()) { |
100 GetMediaDeviceManager()->CloseDevice(label_); | 81 GetMediaDeviceManager()->CloseDevice(label_); |
101 label_.clear(); | 82 label_.clear(); |
102 } | 83 } |
103 if (pending_open_device_) { | 84 if (pending_open_device_) { |
104 GetMediaDeviceManager()->CancelOpenDevice(pending_open_device_id_); | 85 GetMediaDeviceManager()->CancelOpenDevice(pending_open_device_id_); |
105 pending_open_device_ = false; | 86 pending_open_device_ = false; |
106 pending_open_device_id_ = -1; | 87 pending_open_device_id_ = -1; |
107 } | 88 } |
108 } | 89 } |
109 } | 90 } |
110 | 91 |
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() { | 92 PepperPlatformVideoCapture::~PepperPlatformVideoCapture() { |
147 DCHECK(!video_capture_); | 93 DCHECK(stop_capture_cb_.is_null()); |
| 94 DCHECK(release_device_cb_.is_null()); |
148 DCHECK(label_.empty()); | 95 DCHECK(label_.empty()); |
149 DCHECK(!pending_open_device_); | 96 DCHECK(!pending_open_device_); |
150 } | 97 } |
151 | 98 |
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, | 99 void PepperPlatformVideoCapture::OnDeviceOpened(int request_id, |
159 bool succeeded, | 100 bool succeeded, |
160 const std::string& label) { | 101 const std::string& label) { |
161 pending_open_device_ = false; | 102 pending_open_device_ = false; |
162 pending_open_device_id_ = -1; | 103 pending_open_device_id_ = -1; |
163 | 104 |
164 succeeded = succeeded && render_view_.get(); | 105 succeeded = succeeded && render_view_.get(); |
165 if (succeeded) { | 106 if (succeeded) { |
166 label_ = label; | 107 label_ = label; |
167 session_id_ = GetMediaDeviceManager()->GetSessionID( | 108 session_id_ = GetMediaDeviceManager()->GetSessionID( |
168 PP_DEVICETYPE_DEV_VIDEOCAPTURE, label); | 109 PP_DEVICETYPE_DEV_VIDEOCAPTURE, label); |
169 Initialize(); | 110 VideoCaptureImplManager* manager = |
| 111 RenderThreadImpl::current()->video_capture_impl_manager(); |
| 112 release_device_cb_ = manager->UseDevice(session_id_); |
170 } | 113 } |
171 | 114 |
172 if (handler_) | 115 if (handler_) |
173 handler_->OnInitialized(this, succeeded); | 116 handler_->OnInitialized(succeeded); |
| 117 } |
| 118 |
| 119 void PepperPlatformVideoCapture::OnStateUpdate(VideoCaptureState state) { |
| 120 if (!handler_) |
| 121 return; |
| 122 switch (state) { |
| 123 case VIDEO_CAPTURE_STATE_STARTED: |
| 124 handler_->OnStarted(); |
| 125 break; |
| 126 case VIDEO_CAPTURE_STATE_STOPPED: |
| 127 handler_->OnStopped(); |
| 128 break; |
| 129 case VIDEO_CAPTURE_STATE_PAUSED: |
| 130 handler_->OnPaused(); |
| 131 break; |
| 132 case VIDEO_CAPTURE_STATE_ERROR: |
| 133 handler_->OnError(); |
| 134 break; |
| 135 default: |
| 136 NOTREACHED() << "Unexpected state: " << state << "."; |
| 137 } |
| 138 } |
| 139 |
| 140 void PepperPlatformVideoCapture::OnFrameReady( |
| 141 const scoped_refptr<media::VideoFrame>& frame, |
| 142 const media::VideoCaptureFormat& format, |
| 143 const base::TimeTicks& timestamp) { |
| 144 if (handler_ && !stop_capture_cb_.is_null()) |
| 145 handler_->OnFrameReady(frame, format); |
174 } | 146 } |
175 | 147 |
176 PepperMediaDeviceManager* PepperPlatformVideoCapture::GetMediaDeviceManager() { | 148 PepperMediaDeviceManager* PepperPlatformVideoCapture::GetMediaDeviceManager() { |
177 return PepperMediaDeviceManager::GetForRenderView(render_view_.get()); | 149 return PepperMediaDeviceManager::GetForRenderView(render_view_.get()); |
178 } | 150 } |
179 | 151 |
180 } // namespace content | 152 } // namespace content |
OLD | NEW |