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