Chromium Code Reviews| 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_source.h" | 5 #include "content/renderer/media/media_stream_video_source.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 207 if (diff < best_diff) { | 207 if (diff < best_diff) { |
| 208 best_diff = diff; | 208 best_diff = diff; |
| 209 best_it = it; | 209 best_it = it; |
| 210 } | 210 } |
| 211 } | 211 } |
| 212 return *best_it; | 212 return *best_it; |
| 213 } | 213 } |
| 214 | 214 |
| 215 } // anonymous namespace | 215 } // anonymous namespace |
| 216 | 216 |
| 217 // static | |
| 218 MediaStreamVideoSource* MediaStreamVideoSource::GetVideoSource( | |
| 219 const blink::WebMediaStreamSource& source) { | |
| 220 return static_cast<MediaStreamVideoSource*>(source.extraData()); | |
| 221 } | |
| 222 | |
| 217 MediaStreamVideoSource::MediaStreamVideoSource( | 223 MediaStreamVideoSource::MediaStreamVideoSource( |
| 218 MediaStreamDependencyFactory* factory) | 224 MediaStreamDependencyFactory* factory) |
| 219 : state_(NEW), | 225 : state_(NEW), |
| 220 factory_(factory), | 226 factory_(factory), |
| 221 capture_adapter_(NULL) { | 227 capture_adapter_(NULL) { |
| 222 DCHECK(factory_); | 228 DCHECK(factory_); |
| 223 } | 229 } |
| 224 | 230 |
| 225 MediaStreamVideoSource::~MediaStreamVideoSource() { | 231 MediaStreamVideoSource::~MediaStreamVideoSource() { |
| 232 DVLOG(3) << "~MediaStreamVideoSource()"; | |
| 226 } | 233 } |
| 227 | 234 |
| 228 void MediaStreamVideoSource::AddTrack( | 235 void MediaStreamVideoSource::AddTrack( |
| 229 const blink::WebMediaStreamTrack& track, | 236 const blink::WebMediaStreamTrack& track, |
| 230 const blink::WebMediaConstraints& constraints, | 237 const blink::WebMediaConstraints& constraints, |
| 231 const ConstraintsCallback& callback) { | 238 const ConstraintsCallback& callback) { |
| 232 DCHECK(CalledOnValidThread()); | 239 DCHECK(CalledOnValidThread()); |
| 233 requested_constraints_.push_back(RequestedConstraints(constraints, | 240 MediaStreamVideoTrack* video_track = |
| 234 callback)); | 241 MediaStreamVideoTrack::GetVideoTrack(track); |
| 242 DCHECK(std::find(tracks_.begin(), tracks_.end(), | |
| 243 video_track) == tracks_.end()); | |
| 244 tracks_.push_back(video_track); | |
| 245 | |
| 246 requested_constraints_.push_back( | |
| 247 RequestedConstraints(constraints, callback)); | |
| 248 | |
| 235 switch (state_) { | 249 switch (state_) { |
| 236 case NEW: { | 250 case NEW: { |
| 237 // Tab capture and Screen capture needs the maximum requested height | 251 // Tab capture and Screen capture needs the maximum requested height |
| 238 // and width to decide on the resolution. | 252 // and width to decide on the resolution. |
| 239 blink::WebString max_width; | 253 blink::WebString max_width; |
| 240 int max_requested_width = 0; | 254 int max_requested_width = 0; |
| 241 if (constraints.getMandatoryConstraintValue(kMaxWidth, max_width)) | 255 if (constraints.getMandatoryConstraintValue(kMaxWidth, max_width)) |
| 242 base::StringToInt(max_width.utf8(), &max_requested_width); | 256 base::StringToInt(max_width.utf8(), &max_requested_width); |
| 243 | 257 |
| 244 int max_requested_height = 0; | 258 int max_requested_height = 0; |
| 245 blink::WebString max_height; | 259 blink::WebString max_height; |
| 246 if (constraints.getMandatoryConstraintValue(kMaxHeight, max_height)) | 260 if (constraints.getMandatoryConstraintValue(kMaxHeight, max_height)) |
| 247 base::StringToInt(max_height.utf8(), &max_requested_height); | 261 base::StringToInt(max_height.utf8(), &max_requested_height); |
| 248 | 262 |
| 249 state_ = RETRIEVING_CAPABILITIES; | 263 state_ = RETRIEVING_CAPABILITIES; |
| 250 GetCurrentSupportedFormats(max_requested_width, | 264 GetCurrentSupportedFormats(max_requested_width, |
| 251 max_requested_height); | 265 max_requested_height); |
| 252 | 266 |
| 253 break; | 267 break; |
| 254 } | 268 } |
| 255 case STARTING: | 269 case STARTING: |
| 256 case RETRIEVING_CAPABILITIES: { | 270 case RETRIEVING_CAPABILITIES: { |
| 257 // The |callback| will be triggered once the delegate has started or | 271 // The |callback| will be triggered once the source has started or |
| 258 // the capabilitites has been retrieved. | 272 // the capabilities has been retrieved. |
| 259 break; | 273 break; |
| 260 } | 274 } |
| 261 case ENDED: | 275 case ENDED: |
| 262 case STARTED: { | 276 case STARTED: { |
| 263 // Currently, reconfiguring the source is not supported. | 277 // Currently, reconfiguring the source is not supported. |
| 264 FinalizeAddTrack(); | 278 FinalizeAddTrack(); |
| 265 } | 279 } |
| 266 } | 280 } |
| 267 } | 281 } |
| 268 | 282 |
| 269 void MediaStreamVideoSource::RemoveTrack( | 283 void MediaStreamVideoSource::RemoveTrack(MediaStreamVideoTrack* video_track) { |
| 270 const blink::WebMediaStreamTrack& track) { | 284 std::vector<MediaStreamVideoTrack*>::iterator it = |
| 271 // TODO(ronghuawu): What should be done here? Do we really need RemoveTrack? | 285 std::find(tracks_.begin(), tracks_.end(), video_track); |
| 286 DCHECK(it != tracks_.end()); | |
| 287 tracks_.erase(it); | |
| 272 } | 288 } |
| 273 | 289 |
| 274 void MediaStreamVideoSource::InitAdapter() { | 290 void MediaStreamVideoSource::InitAdapter() { |
| 275 if (adapter_) | 291 if (adapter_) |
| 276 return; | 292 return; |
| 277 // Create the webrtc::MediaStreamVideoSourceInterface adapter. | 293 // Create the webrtc::MediaStreamVideoSourceInterface adapter. |
| 278 // It needs the constraints so that constraints used by a PeerConnection | 294 // It needs the constraints so that constraints used by a PeerConnection |
| 279 // will be available such as constraints for CPU adaptation and a tab | 295 // will be available such as constraints for CPU adaptation and a tab |
| 280 // capture. | 296 // capture. |
| 281 bool is_screeencast = | 297 bool is_screeencast = |
| 282 device_info().device.type == MEDIA_TAB_VIDEO_CAPTURE || | 298 device_info().device.type == MEDIA_TAB_VIDEO_CAPTURE || |
| 283 device_info().device.type == MEDIA_DESKTOP_VIDEO_CAPTURE; | 299 device_info().device.type == MEDIA_DESKTOP_VIDEO_CAPTURE; |
| 284 capture_adapter_ = factory_->CreateVideoCapturer(is_screeencast); | 300 capture_adapter_ = factory_->CreateVideoCapturer(is_screeencast); |
| 285 capture_adapter_->SetRequestedFormat(current_format_); | 301 capture_adapter_->SetRequestedFormat(current_format_); |
| 286 adapter_ = factory_->CreateVideoSource(capture_adapter_, | 302 adapter_ = factory_->CreateVideoSource(capture_adapter_, |
| 287 current_constraints_); | 303 current_constraints_); |
| 288 } | 304 } |
| 289 | 305 |
| 290 webrtc::VideoSourceInterface* MediaStreamVideoSource::GetAdapter() { | 306 webrtc::VideoSourceInterface* MediaStreamVideoSource::GetAdapter() { |
| 291 if (!adapter_) { | 307 if (!adapter_) { |
| 292 InitAdapter(); | 308 InitAdapter(); |
| 293 } | 309 } |
| 294 return adapter_; | 310 return adapter_; |
| 295 } | 311 } |
| 296 | 312 |
| 297 void MediaStreamVideoSource::DoStopSource() { | 313 void MediaStreamVideoSource::DoStopSource() { |
| 298 DVLOG(3) << "DoStopSource()"; | 314 DVLOG(3) << "DoStopSource()"; |
| 299 StopSourceImpl(); | 315 StopSourceImpl(); |
| 300 state_ = ENDED; | 316 state_ = ENDED; |
| 317 SetReadyState(blink::WebMediaStreamSource::ReadyStateEnded); | |
| 301 } | 318 } |
| 302 | 319 |
| 303 void MediaStreamVideoSource::DeliverVideoFrame( | 320 void MediaStreamVideoSource::DeliverVideoFrame( |
| 304 const scoped_refptr<media::VideoFrame>& frame) { | 321 const scoped_refptr<media::VideoFrame>& frame) { |
| 305 if (capture_adapter_) | 322 if (capture_adapter_) |
| 306 capture_adapter_->OnFrameCaptured(frame); | 323 capture_adapter_->OnFrameCaptured(frame); |
| 324 | |
| 325 for (std::vector<MediaStreamVideoTrack*>::iterator it = tracks_.begin(); | |
|
Ronghua Wu (Left Chromium)
2014/03/01 01:26:04
should we do this if capture_adapter_ is not null?
perkj_chrome
2014/03/02 09:20:44
No- capture_adapter_ provides frames to the webrtc
Ronghua Wu (Left Chromium)
2014/03/04 01:04:11
Assume you meant webrtc::VideoSource. So you are s
perkj_chrome
2014/03/04 10:44:51
Currently they work in parallell. ie video frames
| |
| 326 it != tracks_.end(); ++it) { | |
| 327 (*it)->OnVideoFrame(frame); | |
| 328 } | |
| 307 } | 329 } |
| 308 | 330 |
| 309 void MediaStreamVideoSource::OnSupportedFormats( | 331 void MediaStreamVideoSource::OnSupportedFormats( |
| 310 const media::VideoCaptureFormats& formats) { | 332 const media::VideoCaptureFormats& formats) { |
| 311 DCHECK(CalledOnValidThread()); | 333 DCHECK(CalledOnValidThread()); |
| 312 DCHECK_EQ(RETRIEVING_CAPABILITIES, state_); | 334 DCHECK_EQ(RETRIEVING_CAPABILITIES, state_); |
| 313 | 335 |
| 314 supported_formats_ = formats; | 336 supported_formats_ = formats; |
| 315 if (!FindBestFormatWithConstraints(supported_formats_, ¤t_format_, | 337 if (!FindBestFormatWithConstraints(supported_formats_, ¤t_format_, |
| 316 ¤t_constraints_)) { | 338 ¤t_constraints_)) { |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 370 } | 392 } |
| 371 | 393 |
| 372 void MediaStreamVideoSource::FinalizeAddTrack() { | 394 void MediaStreamVideoSource::FinalizeAddTrack() { |
| 373 media::VideoCaptureFormats formats; | 395 media::VideoCaptureFormats formats; |
| 374 formats.push_back(current_format_); | 396 formats.push_back(current_format_); |
| 375 | 397 |
| 376 std::vector<RequestedConstraints> callbacks; | 398 std::vector<RequestedConstraints> callbacks; |
| 377 callbacks.swap(requested_constraints_); | 399 callbacks.swap(requested_constraints_); |
| 378 for (std::vector<RequestedConstraints>::iterator it = callbacks.begin(); | 400 for (std::vector<RequestedConstraints>::iterator it = callbacks.begin(); |
| 379 it != callbacks.end(); ++it) { | 401 it != callbacks.end(); ++it) { |
| 402 | |
| 380 bool success = state_ == STARTED && | 403 bool success = state_ == STARTED && |
| 381 !FilterFormats(it->constraints, formats).empty(); | 404 !FilterFormats(it->constraints, formats).empty(); |
| 382 DVLOG(3) << "FinalizeAddTrack() success " << success; | 405 DVLOG(3) << "FinalizeAddTrack() success " << success; |
| 383 if (!it->callback.is_null()) | 406 if (!it->callback.is_null()) |
| 384 it->callback.Run(this, success); | 407 it->callback.Run(this, success); |
| 385 } | 408 } |
| 386 } | 409 } |
| 387 | 410 |
| 388 void MediaStreamVideoSource::SetReadyState( | 411 void MediaStreamVideoSource::SetReadyState( |
| 389 blink::WebMediaStreamSource::ReadyState state) { | 412 blink::WebMediaStreamSource::ReadyState state) { |
| 390 if (!owner().isNull()) { | 413 if (!owner().isNull()) { |
| 391 owner().setReadyState(state); | 414 owner().setReadyState(state); |
| 392 } | 415 } |
| 393 // TODO(perkj): Notify all registered tracks. | 416 for (std::vector<MediaStreamVideoTrack*>::iterator it = tracks_.begin(); |
| 417 it != tracks_.end(); ++it) { | |
| 418 (*it)->OnReadyStateChanged(state); | |
| 419 } | |
| 394 } | 420 } |
| 395 | 421 |
| 396 MediaStreamVideoSource::RequestedConstraints::RequestedConstraints( | 422 MediaStreamVideoSource::RequestedConstraints::RequestedConstraints( |
| 397 const blink::WebMediaConstraints& constraints, | 423 const blink::WebMediaConstraints& constraints, |
| 398 const ConstraintsCallback& callback) | 424 const ConstraintsCallback& callback) |
| 399 : constraints(constraints), callback(callback) { | 425 : constraints(constraints), callback(callback) { |
| 400 } | 426 } |
| 401 | 427 |
| 402 MediaStreamVideoSource::RequestedConstraints::~RequestedConstraints() { | 428 MediaStreamVideoSource::RequestedConstraints::~RequestedConstraints() { |
| 403 } | 429 } |
| 404 | 430 |
| 405 } // namespace content | 431 } // namespace content |
| OLD | NEW |