OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_image_capture.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback_helpers.h" | |
8 #include "base/logging.h" | 9 #include "base/logging.h" |
9 #include "base/message_loop/message_loop_proxy.h" | 10 #include "base/message_loop/message_loop_proxy.h" |
10 #include "content/renderer/media/video_capture_impl_manager.h" | 11 #include "content/renderer/media/video_capture_impl_manager.h" |
12 #include "content/renderer/pepper/pepper_image_capture_host.h" | |
11 #include "content/renderer/pepper/pepper_media_device_manager.h" | 13 #include "content/renderer/pepper/pepper_media_device_manager.h" |
12 #include "content/renderer/pepper/pepper_video_capture_host.h" | |
13 #include "content/renderer/render_frame_impl.h" | 14 #include "content/renderer/render_frame_impl.h" |
14 #include "content/renderer/render_thread_impl.h" | 15 #include "content/renderer/render_thread_impl.h" |
15 #include "media/base/bind_to_current_loop.h" | 16 #include "media/base/bind_to_current_loop.h" |
17 #include "content/renderer/pepper/gfx_conversion.h" | |
16 #include "url/gurl.h" | 18 #include "url/gurl.h" |
17 | 19 |
18 namespace content { | 20 namespace content { |
19 | 21 |
20 PepperPlatformVideoCapture::PepperPlatformVideoCapture( | 22 PepperPlatformImageCapture::PepperPlatformImageCapture( |
21 int render_frame_id, | 23 int render_frame_id, |
22 const std::string& device_id, | 24 const std::string& device_id, |
23 const GURL& document_url, | 25 const GURL& document_url, |
24 PepperVideoCaptureHost* handler) | 26 PepperImageCaptureHost* handler) |
25 : render_frame_id_(render_frame_id), | 27 : render_frame_id_(render_frame_id), |
26 device_id_(device_id), | 28 device_id_(device_id), |
27 session_id_(0), | 29 session_id_(0), |
28 handler_(handler), | 30 handler_(handler), |
29 pending_open_device_(false), | 31 pending_open_device_(false), |
30 pending_open_device_id_(-1), | 32 pending_open_device_id_(-1), |
31 weak_factory_(this) { | 33 weak_factory_(this) { |
32 // We need to open the device and obtain the label and session ID before | 34 // We need to open the device and obtain the label and session ID before |
33 // initializing. | 35 // initializing. |
34 PepperMediaDeviceManager* const device_manager = GetMediaDeviceManager(); | 36 PepperMediaDeviceManager* const device_manager = GetMediaDeviceManager(); |
35 if (device_manager) { | 37 if (device_manager) { |
36 pending_open_device_id_ = device_manager->OpenDevice( | 38 pending_open_device_id_ = device_manager->OpenDevice( |
37 PP_DEVICETYPE_DEV_VIDEOCAPTURE, | 39 PP_DEVICETYPE_DEV_VIDEOCAPTURE, // TODO(jchuang): add new type? |
wuchengli
2015/01/19 14:05:03
I don't think we need to add a new type. Remove TO
Justin Chuang
2015/01/26 15:00:50
Done.
| |
38 device_id, | 40 device_id, |
39 document_url, | 41 document_url, |
40 base::Bind(&PepperPlatformVideoCapture::OnDeviceOpened, | 42 base::Bind(&PepperPlatformImageCapture::OnDeviceOpened, |
41 weak_factory_.GetWeakPtr())); | 43 weak_factory_.GetWeakPtr())); |
42 pending_open_device_ = true; | 44 pending_open_device_ = true; |
43 } | 45 } |
44 } | 46 } |
45 | 47 |
46 void PepperPlatformVideoCapture::StartCapture( | 48 void PepperPlatformImageCapture::GetPreviewSizes() { |
47 const media::VideoCaptureParams& params) { | |
48 DCHECK(thread_checker_.CalledOnValidThread()); | 49 DCHECK(thread_checker_.CalledOnValidThread()); |
49 if (!stop_capture_cb_.is_null()) | 50 |
50 return; | |
51 VideoCaptureImplManager* manager = | 51 VideoCaptureImplManager* manager = |
52 RenderThreadImpl::current()->video_capture_impl_manager(); | 52 RenderThreadImpl::current()->video_capture_impl_manager(); |
53 stop_capture_cb_ = | 53 manager->GetDeviceSupportedFormats( |
54 manager->StartCapture(session_id_, | 54 session_id_, |
55 params, | 55 media::BindToCurrentLoop( |
56 media::BindToCurrentLoop(base::Bind( | 56 base::Bind( |
57 &PepperPlatformVideoCapture::OnStateUpdate, | 57 &PepperPlatformImageCapture::OnDeviceSupportedFormatsEnumerated, |
58 weak_factory_.GetWeakPtr())), | 58 weak_factory_.GetWeakPtr()))); |
59 media::BindToCurrentLoop(base::Bind( | |
60 &PepperPlatformVideoCapture::OnFrameReady, | |
61 weak_factory_.GetWeakPtr()))); | |
62 } | 59 } |
63 | 60 |
64 void PepperPlatformVideoCapture::StopCapture() { | 61 void PepperPlatformImageCapture::DetachEventHandler() { |
65 DCHECK(thread_checker_.CalledOnValidThread()); | 62 handler_ = NULL; |
66 if (stop_capture_cb_.is_null()) | |
67 return; | |
68 stop_capture_cb_.Run(); | |
69 stop_capture_cb_.Reset(); | |
70 } | |
71 | 63 |
72 void PepperPlatformVideoCapture::DetachEventHandler() { | |
73 handler_ = NULL; | |
74 StopCapture(); | |
75 if (!release_device_cb_.is_null()) { | 64 if (!release_device_cb_.is_null()) { |
76 release_device_cb_.Run(); | 65 base::ResetAndReturn(&release_device_cb_).Run(); |
77 release_device_cb_.Reset(); | |
78 } | 66 } |
79 if (!label_.empty()) { | 67 if (!label_.empty()) { |
80 PepperMediaDeviceManager* const device_manager = GetMediaDeviceManager(); | 68 PepperMediaDeviceManager* const device_manager = GetMediaDeviceManager(); |
81 if (device_manager) | 69 if (device_manager) |
82 device_manager->CloseDevice(label_); | 70 device_manager->CloseDevice(label_); |
83 label_.clear(); | 71 label_.clear(); |
84 } | 72 } |
85 if (pending_open_device_) { | 73 if (pending_open_device_) { |
86 PepperMediaDeviceManager* const device_manager = GetMediaDeviceManager(); | 74 PepperMediaDeviceManager* const device_manager = GetMediaDeviceManager(); |
87 if (device_manager) | 75 if (device_manager) |
88 device_manager->CancelOpenDevice(pending_open_device_id_); | 76 device_manager->CancelOpenDevice(pending_open_device_id_); |
89 pending_open_device_ = false; | 77 pending_open_device_ = false; |
90 pending_open_device_id_ = -1; | 78 pending_open_device_id_ = -1; |
91 } | 79 } |
92 } | 80 } |
93 | 81 |
94 PepperPlatformVideoCapture::~PepperPlatformVideoCapture() { | 82 PepperPlatformImageCapture::~PepperPlatformImageCapture() { |
95 DCHECK(stop_capture_cb_.is_null()); | |
96 DCHECK(release_device_cb_.is_null()); | 83 DCHECK(release_device_cb_.is_null()); |
97 DCHECK(label_.empty()); | 84 DCHECK(label_.empty()); |
98 DCHECK(!pending_open_device_); | 85 DCHECK(!pending_open_device_); |
99 } | 86 } |
100 | 87 |
101 void PepperPlatformVideoCapture::OnDeviceOpened(int request_id, | 88 void PepperPlatformImageCapture::OnDeviceOpened(int request_id, |
102 bool succeeded, | 89 bool succeeded, |
103 const std::string& label) { | 90 const std::string& label) { |
91 if (!handler_) { | |
92 NOTREACHED(); | |
wuchengli
2015/01/19 14:05:03
I'd prefer following PepperPlatformVideoCapture::O
Justin Chuang
2015/01/26 15:00:50
I think the code is correct. handler_ is deregiste
wuchengli
2015/01/29 13:52:14
You are right. PepperPlatformImageCapture::DetachE
Justin Chuang
2015/02/02 08:43:44
Done.
| |
93 return; | |
94 } | |
95 | |
104 pending_open_device_ = false; | 96 pending_open_device_ = false; |
105 pending_open_device_id_ = -1; | 97 pending_open_device_id_ = -1; |
106 | 98 |
107 PepperMediaDeviceManager* const device_manager = GetMediaDeviceManager(); | 99 PepperMediaDeviceManager* const device_manager = GetMediaDeviceManager(); |
108 succeeded = succeeded && device_manager; | 100 succeeded = succeeded && device_manager; |
109 if (succeeded) { | 101 if (succeeded) { |
110 label_ = label; | 102 label_ = label; |
111 session_id_ = device_manager->GetSessionID( | 103 session_id_ = device_manager->GetSessionID( |
112 PP_DEVICETYPE_DEV_VIDEOCAPTURE, label); | 104 PP_DEVICETYPE_DEV_VIDEOCAPTURE, label); // TODO: new device type? |
wuchengli
2015/01/19 14:05:03
Remove TODO.
Justin Chuang
2015/01/26 15:00:50
Done.
| |
113 VideoCaptureImplManager* manager = | 105 VideoCaptureImplManager* manager = |
114 RenderThreadImpl::current()->video_capture_impl_manager(); | 106 RenderThreadImpl::current()->video_capture_impl_manager(); |
115 release_device_cb_ = manager->UseDevice(session_id_); | 107 release_device_cb_ = manager->UseDevice(session_id_); |
116 } | 108 } |
117 | 109 |
118 if (handler_) | 110 handler_->OnPlatformInitialized(succeeded); |
119 handler_->OnInitialized(succeeded); | |
120 } | 111 } |
121 | 112 |
122 void PepperPlatformVideoCapture::OnStateUpdate(VideoCaptureState state) { | 113 void PepperPlatformImageCapture::OnDeviceSupportedFormatsEnumerated( |
123 if (!handler_) | 114 const media::VideoCaptureFormats& formats) { |
115 if (!handler_) { | |
116 NOTREACHED(); | |
wuchengli
2015/01/19 14:05:03
PepperPlatformVideoCapture::OnStateUpdate doesn't
Justin Chuang
2015/01/26 15:00:50
dittos. The pointer to this object is passed as we
wuchengli
2015/01/29 13:52:14
That's correct. Then this can be changed to DCHECK
Justin Chuang
2015/02/02 08:43:44
Done.
| |
124 return; | 117 return; |
125 switch (state) { | |
126 case VIDEO_CAPTURE_STATE_STARTED: | |
127 handler_->OnStarted(); | |
128 break; | |
129 case VIDEO_CAPTURE_STATE_STOPPED: | |
130 handler_->OnStopped(); | |
131 break; | |
132 case VIDEO_CAPTURE_STATE_PAUSED: | |
133 handler_->OnPaused(); | |
134 break; | |
135 case VIDEO_CAPTURE_STATE_ERROR: | |
136 handler_->OnError(); | |
137 break; | |
138 default: | |
139 NOTREACHED() << "Unexpected state: " << state << "."; | |
140 } | 118 } |
119 | |
120 // TODO(jchuang): Do we need fallback if the device doesn't support format | |
wuchengli
2015/01/19 14:05:03
Remove TODO. VideoCaptureImpl::GetDeviceSupportedF
Justin Chuang
2015/01/26 15:00:50
Why it won't happen on CrOS? It depends on how the
| |
121 // enumeration (when formats.size() == 0)? | |
122 std::vector<PP_Size> sizes; | |
123 for (media::VideoCaptureFormats::const_iterator it = formats.begin(); | |
124 it != formats.end(); it++) { | |
125 sizes.push_back(PP_FromGfxSize(it->frame_size)); | |
wuchengli
2015/01/19 14:05:03
We should du-dup here. Right? Some frame sizes wil
Justin Chuang
2015/01/26 15:00:50
Thanks. Can you paste result with dup? I don't see
wuchengli
2015/01/29 13:52:14
Sorry. By dup I meant same frame size but differen
| |
126 } | |
127 handler_->OnPlatformPreviewSizesEnumerated(sizes); | |
141 } | 128 } |
142 | 129 |
143 void PepperPlatformVideoCapture::OnFrameReady( | 130 PepperMediaDeviceManager* PepperPlatformImageCapture::GetMediaDeviceManager() { |
144 const scoped_refptr<media::VideoFrame>& frame, | |
145 const media::VideoCaptureFormat& format, | |
146 const base::TimeTicks& estimated_capture_time) { | |
147 if (handler_ && !stop_capture_cb_.is_null()) | |
148 handler_->OnFrameReady(frame, format); | |
149 } | |
150 | |
151 PepperMediaDeviceManager* PepperPlatformVideoCapture::GetMediaDeviceManager() { | |
152 RenderFrameImpl* const render_frame = | 131 RenderFrameImpl* const render_frame = |
153 RenderFrameImpl::FromRoutingID(render_frame_id_); | 132 RenderFrameImpl::FromRoutingID(render_frame_id_); |
154 return render_frame ? | 133 return render_frame |
155 PepperMediaDeviceManager::GetForRenderFrame(render_frame).get() : NULL; | 134 ? PepperMediaDeviceManager::GetForRenderFrame(render_frame).get() : NULL; |
156 } | 135 } |
157 | 136 |
158 } // namespace content | 137 } // namespace content |
OLD | NEW |