| OLD | NEW |
| 1 // Copyright 2014 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/media/media_stream_video_capturer_source.h" | 5 #include "content/renderer/media/media_stream_video_capturer_source.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback_helpers.h" | 8 #include "base/callback_helpers.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "content/renderer/media/video_capture_impl_manager.h" | 10 #include "content/renderer/media/video_capture_impl_manager.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 const double kMaxScreenCastFrameRate = 120.0; | 34 const double kMaxScreenCastFrameRate = 120.0; |
| 35 | 35 |
| 36 } // namespace | 36 } // namespace |
| 37 | 37 |
| 38 namespace content { | 38 namespace content { |
| 39 | 39 |
| 40 VideoCapturerDelegate::VideoCapturerDelegate( | 40 VideoCapturerDelegate::VideoCapturerDelegate( |
| 41 const StreamDeviceInfo& device_info) | 41 const StreamDeviceInfo& device_info) |
| 42 : session_id_(device_info.session_id), | 42 : session_id_(device_info.session_id), |
| 43 is_screen_cast_(device_info.device.type == MEDIA_TAB_VIDEO_CAPTURE || | 43 is_screen_cast_(device_info.device.type == MEDIA_TAB_VIDEO_CAPTURE || |
| 44 device_info.device.type == MEDIA_DESKTOP_VIDEO_CAPTURE) { | 44 device_info.device.type == MEDIA_DESKTOP_VIDEO_CAPTURE), |
| 45 weak_factory_(this) { |
| 45 DVLOG(3) << "VideoCapturerDelegate::ctor"; | 46 DVLOG(3) << "VideoCapturerDelegate::ctor"; |
| 46 | 47 |
| 47 // NULL in unit test. | 48 // NULL in unit test. |
| 48 if (RenderThreadImpl::current()) { | 49 if (RenderThreadImpl::current()) { |
| 49 VideoCaptureImplManager* manager = | 50 VideoCaptureImplManager* manager = |
| 50 RenderThreadImpl::current()->video_capture_impl_manager(); | 51 RenderThreadImpl::current()->video_capture_impl_manager(); |
| 51 if (manager) | 52 if (manager) |
| 52 release_device_cb_ = manager->UseDevice(session_id_); | 53 release_device_cb_ = manager->UseDevice(session_id_); |
| 53 } | 54 } |
| 54 } | 55 } |
| 55 | 56 |
| 56 VideoCapturerDelegate::~VideoCapturerDelegate() { | 57 VideoCapturerDelegate::~VideoCapturerDelegate() { |
| 58 DCHECK(thread_checker_.CalledOnValidThread()); |
| 57 DVLOG(3) << "VideoCapturerDelegate::dtor"; | 59 DVLOG(3) << "VideoCapturerDelegate::dtor"; |
| 58 if (!release_device_cb_.is_null()) | 60 if (!release_device_cb_.is_null()) |
| 59 release_device_cb_.Run(); | 61 release_device_cb_.Run(); |
| 60 } | 62 } |
| 61 | 63 |
| 62 void VideoCapturerDelegate::GetCurrentSupportedFormats( | 64 void VideoCapturerDelegate::GetCurrentSupportedFormats( |
| 63 int max_requested_width, | 65 int max_requested_width, |
| 64 int max_requested_height, | 66 int max_requested_height, |
| 65 double max_requested_frame_rate, | 67 double max_requested_frame_rate, |
| 66 const VideoCaptureDeviceFormatsCB& callback) { | 68 const VideoCaptureDeviceFormatsCB& callback) { |
| (...skipping 22 matching lines...) Expand all Loading... |
| 89 VideoCaptureImplManager* manager = | 91 VideoCaptureImplManager* manager = |
| 90 RenderThreadImpl::current()->video_capture_impl_manager(); | 92 RenderThreadImpl::current()->video_capture_impl_manager(); |
| 91 if (!manager) | 93 if (!manager) |
| 92 return; | 94 return; |
| 93 DCHECK(source_formats_callback_.is_null()); | 95 DCHECK(source_formats_callback_.is_null()); |
| 94 source_formats_callback_ = callback; | 96 source_formats_callback_ = callback; |
| 95 manager->GetDeviceFormatsInUse( | 97 manager->GetDeviceFormatsInUse( |
| 96 session_id_, | 98 session_id_, |
| 97 media::BindToCurrentLoop( | 99 media::BindToCurrentLoop( |
| 98 base::Bind( | 100 base::Bind( |
| 99 &VideoCapturerDelegate::OnDeviceFormatsInUseReceived, this))); | 101 &VideoCapturerDelegate::OnDeviceFormatsInUseReceived, |
| 102 weak_factory_.GetWeakPtr()))); |
| 100 } | 103 } |
| 101 | 104 |
| 102 void VideoCapturerDelegate::StartCapture( | 105 void VideoCapturerDelegate::StartCapture( |
| 103 const media::VideoCaptureParams& params, | 106 const media::VideoCaptureParams& params, |
| 104 const VideoCaptureDeliverFrameCB& new_frame_callback, | 107 const VideoCaptureDeliverFrameCB& new_frame_callback, |
| 105 const RunningCallback& running_callback) { | 108 const RunningCallback& running_callback) { |
| 106 DCHECK(params.requested_format.IsValid()); | 109 DCHECK(params.requested_format.IsValid()); |
| 107 DCHECK(thread_checker_.CalledOnValidThread()); | 110 DCHECK(thread_checker_.CalledOnValidThread()); |
| 108 running_callback_ = running_callback; | 111 running_callback_ = running_callback; |
| 109 | 112 |
| 110 // NULL in unit test. | 113 // NULL in unit test. |
| 111 if (!RenderThreadImpl::current()) | 114 if (!RenderThreadImpl::current()) |
| 112 return; | 115 return; |
| 113 VideoCaptureImplManager* manager = | 116 VideoCaptureImplManager* manager = |
| 114 RenderThreadImpl::current()->video_capture_impl_manager(); | 117 RenderThreadImpl::current()->video_capture_impl_manager(); |
| 115 if (!manager) | 118 if (!manager) |
| 116 return; | 119 return; |
| 117 stop_capture_cb_ = | 120 stop_capture_cb_ = |
| 118 manager->StartCapture( | 121 manager->StartCapture( |
| 119 session_id_, | 122 session_id_, |
| 120 params, | 123 params, |
| 121 media::BindToCurrentLoop(base::Bind( | 124 media::BindToCurrentLoop(base::Bind( |
| 122 &VideoCapturerDelegate::OnStateUpdateOnRenderThread, this)), | 125 &VideoCapturerDelegate::OnStateUpdateOnRenderThread, |
| 126 weak_factory_.GetWeakPtr())), |
| 123 new_frame_callback); | 127 new_frame_callback); |
| 124 } | 128 } |
| 125 | 129 |
| 126 void VideoCapturerDelegate::StopCapture() { | 130 void VideoCapturerDelegate::StopCapture() { |
| 127 // Immediately make sure we don't provide more frames. | 131 // Immediately make sure we don't provide more frames. |
| 128 DVLOG(3) << "VideoCapturerDelegate::StopCapture()"; | 132 DVLOG(3) << "VideoCapturerDelegate::StopCapture()"; |
| 129 DCHECK(thread_checker_.CalledOnValidThread()); | 133 DCHECK(thread_checker_.CalledOnValidThread()); |
| 130 if (!stop_capture_cb_.is_null()) { | 134 if (!stop_capture_cb_.is_null()) { |
| 131 base::ResetAndReturn(&stop_capture_cb_).Run(); | 135 base::ResetAndReturn(&stop_capture_cb_).Run(); |
| 132 } | 136 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 return; | 168 return; |
| 165 } | 169 } |
| 166 | 170 |
| 167 // NULL in unit test. | 171 // NULL in unit test. |
| 168 if (!RenderThreadImpl::current()) | 172 if (!RenderThreadImpl::current()) |
| 169 return; | 173 return; |
| 170 VideoCaptureImplManager* manager = | 174 VideoCaptureImplManager* manager = |
| 171 RenderThreadImpl::current()->video_capture_impl_manager(); | 175 RenderThreadImpl::current()->video_capture_impl_manager(); |
| 172 if (!manager) | 176 if (!manager) |
| 173 return; | 177 return; |
| 178 |
| 174 manager->GetDeviceSupportedFormats( | 179 manager->GetDeviceSupportedFormats( |
| 175 session_id_, | 180 session_id_, |
| 176 media::BindToCurrentLoop( | 181 media::BindToCurrentLoop( |
| 177 base::Bind( | 182 base::Bind( |
| 178 &VideoCapturerDelegate::OnDeviceSupportedFormatsEnumerated, | 183 &VideoCapturerDelegate::OnDeviceSupportedFormatsEnumerated, |
| 179 this))); | 184 weak_factory_.GetWeakPtr()))); |
| 180 } | 185 } |
| 181 | 186 |
| 182 void VideoCapturerDelegate::OnDeviceSupportedFormatsEnumerated( | 187 void VideoCapturerDelegate::OnDeviceSupportedFormatsEnumerated( |
| 183 const media::VideoCaptureFormats& formats) { | 188 const media::VideoCaptureFormats& formats) { |
| 184 DVLOG(3) << "OnDeviceSupportedFormatsEnumerated: " << formats.size() | 189 DVLOG(3) << "OnDeviceSupportedFormatsEnumerated: " << formats.size() |
| 185 << " received"; | 190 << " received"; |
| 186 DCHECK(thread_checker_.CalledOnValidThread()); | 191 DCHECK(thread_checker_.CalledOnValidThread()); |
| 187 // StopCapture() might have destroyed |source_formats_callback_| before | 192 // StopCapture() might have destroyed |source_formats_callback_| before |
| 188 // arriving here. | 193 // arriving here. |
| 189 if (source_formats_callback_.is_null()) | 194 if (source_formats_callback_.is_null()) |
| 190 return; | 195 return; |
| 191 if (formats.size()) { | 196 if (formats.size()) { |
| 192 source_formats_callback_.Run(formats); | 197 base::ResetAndReturn(&source_formats_callback_).Run(formats); |
| 193 } else { | 198 } else { |
| 194 // The capture device doesn't seem to support capability enumeration, | 199 // The capture device doesn't seem to support capability enumeration, |
| 195 // compose a fallback list of capabilities. | 200 // compose a fallback list of capabilities. |
| 196 media::VideoCaptureFormats default_formats; | 201 media::VideoCaptureFormats default_formats; |
| 197 for (size_t i = 0; i < arraysize(kVideoResolutions); ++i) { | 202 for (size_t i = 0; i < arraysize(kVideoResolutions); ++i) { |
| 198 for (size_t j = 0; j < arraysize(kVideoFrameRates); ++j) { | 203 for (size_t j = 0; j < arraysize(kVideoFrameRates); ++j) { |
| 199 default_formats.push_back(media::VideoCaptureFormat( | 204 default_formats.push_back(media::VideoCaptureFormat( |
| 200 gfx::Size(kVideoResolutions[i].width, kVideoResolutions[i].height), | 205 gfx::Size(kVideoResolutions[i].width, kVideoResolutions[i].height), |
| 201 kVideoFrameRates[j], media::PIXEL_FORMAT_I420)); | 206 kVideoFrameRates[j], media::PIXEL_FORMAT_I420)); |
| 202 } | 207 } |
| 203 } | 208 } |
| 204 source_formats_callback_.Run(default_formats); | 209 base::ResetAndReturn(&source_formats_callback_).Run(default_formats); |
| 205 } | 210 } |
| 206 source_formats_callback_.Reset(); | |
| 207 } | 211 } |
| 208 | 212 |
| 209 MediaStreamVideoCapturerSource::MediaStreamVideoCapturerSource( | 213 MediaStreamVideoCapturerSource::MediaStreamVideoCapturerSource( |
| 210 const StreamDeviceInfo& device_info, | 214 const StreamDeviceInfo& device_info, |
| 211 const SourceStoppedCallback& stop_callback, | 215 const SourceStoppedCallback& stop_callback, |
| 212 const scoped_refptr<VideoCapturerDelegate>& delegate) | 216 scoped_ptr<VideoCapturerDelegate> delegate) |
| 213 : delegate_(delegate) { | 217 : delegate_(delegate.Pass()) { |
| 214 SetDeviceInfo(device_info); | 218 SetDeviceInfo(device_info); |
| 215 SetStopCallback(stop_callback); | 219 SetStopCallback(stop_callback); |
| 216 } | 220 } |
| 217 | 221 |
| 218 MediaStreamVideoCapturerSource::~MediaStreamVideoCapturerSource() { | 222 MediaStreamVideoCapturerSource::~MediaStreamVideoCapturerSource() { |
| 219 } | 223 } |
| 220 | 224 |
| 221 void MediaStreamVideoCapturerSource::GetCurrentSupportedFormats( | 225 void MediaStreamVideoCapturerSource::GetCurrentSupportedFormats( |
| 222 int max_requested_width, | 226 int max_requested_width, |
| 223 int max_requested_height, | 227 int max_requested_height, |
| (...skipping 21 matching lines...) Expand all Loading... |
| 245 frame_callback, | 249 frame_callback, |
| 246 base::Bind(&MediaStreamVideoCapturerSource::OnStartDone, | 250 base::Bind(&MediaStreamVideoCapturerSource::OnStartDone, |
| 247 base::Unretained(this))); | 251 base::Unretained(this))); |
| 248 } | 252 } |
| 249 | 253 |
| 250 void MediaStreamVideoCapturerSource::StopSourceImpl() { | 254 void MediaStreamVideoCapturerSource::StopSourceImpl() { |
| 251 delegate_->StopCapture(); | 255 delegate_->StopCapture(); |
| 252 } | 256 } |
| 253 | 257 |
| 254 } // namespace content | 258 } // namespace content |
| OLD | NEW |