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 <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 bool mandatory, | 202 bool mandatory, |
203 media::VideoCaptureFormats* formats) { | 203 media::VideoCaptureFormats* formats) { |
204 DVLOG(3) << "FilterFormatsByConstraint(" | 204 DVLOG(3) << "FilterFormatsByConstraint(" |
205 << "{ constraint.m_name = " << constraint.m_name.utf8() | 205 << "{ constraint.m_name = " << constraint.m_name.utf8() |
206 << " constraint.m_value = " << constraint.m_value.utf8() | 206 << " constraint.m_value = " << constraint.m_value.utf8() |
207 << " mandatory = " << mandatory << "})"; | 207 << " mandatory = " << mandatory << "})"; |
208 media::VideoCaptureFormats::iterator format_it = formats->begin(); | 208 media::VideoCaptureFormats::iterator format_it = formats->begin(); |
209 while (format_it != formats->end()) { | 209 while (format_it != formats->end()) { |
210 // Modify the format_it to fulfill the constraint if possible. | 210 // Modify the format_it to fulfill the constraint if possible. |
211 // Delete it otherwise. | 211 // Delete it otherwise. |
212 if (!UpdateFormatForConstraint(constraint, mandatory, &(*format_it))) { | 212 if (!UpdateFormatForConstraint(constraint, mandatory, &(*format_it))) |
213 format_it = formats->erase(format_it); | 213 format_it = formats->erase(format_it); |
214 } else { | 214 else |
215 ++format_it; | 215 ++format_it; |
216 } | |
217 } | 216 } |
218 } | 217 } |
219 | 218 |
220 // Returns the media::VideoCaptureFormats that matches |constraints|. | 219 // Returns the media::VideoCaptureFormats that matches |constraints|. |
221 media::VideoCaptureFormats FilterFormats( | 220 media::VideoCaptureFormats FilterFormats( |
222 const blink::WebMediaConstraints& constraints, | 221 const blink::WebMediaConstraints& constraints, |
223 const media::VideoCaptureFormats& supported_formats, | 222 const media::VideoCaptureFormats& supported_formats, |
224 blink::WebString* unsatisfied_constraint) { | 223 blink::WebString* unsatisfied_constraint) { |
225 if (constraints.isNull()) { | 224 if (constraints.isNull()) |
226 return supported_formats; | 225 return supported_formats; |
227 } | |
228 | 226 |
229 double max_aspect_ratio; | 227 double max_aspect_ratio; |
230 double min_aspect_ratio; | 228 double min_aspect_ratio; |
231 GetDesiredMinAndMaxAspectRatio(constraints, | 229 GetDesiredMinAndMaxAspectRatio(constraints, |
232 &min_aspect_ratio, | 230 &min_aspect_ratio, |
233 &max_aspect_ratio); | 231 &max_aspect_ratio); |
234 | 232 |
235 if (min_aspect_ratio > max_aspect_ratio || max_aspect_ratio < 0.05f) { | 233 if (min_aspect_ratio > max_aspect_ratio || max_aspect_ratio < 0.05f) { |
236 DLOG(WARNING) << "Wrong requested aspect ratio."; | 234 DLOG(WARNING) << "Wrong requested aspect ratio."; |
237 return media::VideoCaptureFormats(); | 235 return media::VideoCaptureFormats(); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 | 282 |
285 // Ok - all mandatory checked and we still have candidates. | 283 // Ok - all mandatory checked and we still have candidates. |
286 // Let's try filtering using the optional constraints. The optional | 284 // Let's try filtering using the optional constraints. The optional |
287 // constraints must be filtered in the order they occur in |optional|. | 285 // constraints must be filtered in the order they occur in |optional|. |
288 // But if a constraint produce zero candidates, the constraint is ignored and | 286 // But if a constraint produce zero candidates, the constraint is ignored and |
289 // the next constraint is tested. | 287 // the next constraint is tested. |
290 // http://dev.w3.org/2011/webrtc/editor/getusermedia.html#idl-def-Constraints | 288 // http://dev.w3.org/2011/webrtc/editor/getusermedia.html#idl-def-Constraints |
291 for (size_t i = 0; i < optional.size(); ++i) { | 289 for (size_t i = 0; i < optional.size(); ++i) { |
292 media::VideoCaptureFormats current_candidates = candidates; | 290 media::VideoCaptureFormats current_candidates = candidates; |
293 FilterFormatsByConstraint(optional[i], false, ¤t_candidates); | 291 FilterFormatsByConstraint(optional[i], false, ¤t_candidates); |
294 if (!current_candidates.empty()) { | 292 if (!current_candidates.empty()) |
295 candidates = current_candidates; | 293 candidates = current_candidates; |
296 } | |
297 } | 294 } |
298 | 295 |
299 // We have done as good as we can to filter the supported resolutions. | 296 // We have done as good as we can to filter the supported resolutions. |
300 return candidates; | 297 return candidates; |
301 } | 298 } |
302 | 299 |
303 const media::VideoCaptureFormat& GetBestFormatBasedOnArea( | 300 const media::VideoCaptureFormat& GetBestFormatBasedOnArea( |
304 const media::VideoCaptureFormats& formats, | 301 const media::VideoCaptureFormats& formats, |
305 int area) { | 302 int area) { |
306 media::VideoCaptureFormats::const_iterator it = formats.begin(); | 303 media::VideoCaptureFormats::const_iterator it = formats.begin(); |
307 media::VideoCaptureFormats::const_iterator best_it = formats.begin(); | 304 media::VideoCaptureFormats::const_iterator best_it = formats.begin(); |
308 int best_diff = std::numeric_limits<int>::max(); | 305 int best_diff = std::numeric_limits<int>::max(); |
309 for (; it != formats.end(); ++it) { | 306 for (; it != formats.end(); ++it) { |
310 int diff = abs(area - it->frame_size.width() * it->frame_size.height()); | 307 const int diff = abs(area - it->frame_size.GetArea()); |
311 if (diff < best_diff) { | 308 if (diff < best_diff) { |
312 best_diff = diff; | 309 best_diff = diff; |
313 best_it = it; | 310 best_it = it; |
314 } | 311 } |
315 } | 312 } |
316 return *best_it; | 313 return *best_it; |
317 } | 314 } |
318 | 315 |
319 // Find the format that best matches the default video size. | 316 // Find the format that best matches the default video size. |
320 // This algorithm is chosen since a resolution must be picked even if no | 317 // This algorithm is chosen since a resolution must be picked even if no |
(...skipping 20 matching lines...) Expand all Loading... |
341 } // anonymous namespace | 338 } // anonymous namespace |
342 | 339 |
343 // static | 340 // static |
344 MediaStreamVideoSource* MediaStreamVideoSource::GetVideoSource( | 341 MediaStreamVideoSource* MediaStreamVideoSource::GetVideoSource( |
345 const blink::WebMediaStreamSource& source) { | 342 const blink::WebMediaStreamSource& source) { |
346 return static_cast<MediaStreamVideoSource*>(source.extraData()); | 343 return static_cast<MediaStreamVideoSource*>(source.extraData()); |
347 } | 344 } |
348 | 345 |
349 // static | 346 // static |
350 bool MediaStreamVideoSource::IsConstraintSupported(const std::string& name) { | 347 bool MediaStreamVideoSource::IsConstraintSupported(const std::string& name) { |
351 for (size_t i = 0; i < arraysize(kSupportedConstraints); ++i) { | 348 for (const char* constraint : kSupportedConstraints) { |
352 if (kSupportedConstraints[i] == name) | 349 if (constraint == name) |
353 return true; | 350 return true; |
354 } | 351 } |
355 return false; | 352 return false; |
356 } | 353 } |
357 | 354 |
358 MediaStreamVideoSource::MediaStreamVideoSource() | 355 MediaStreamVideoSource::MediaStreamVideoSource() |
359 : state_(NEW), | 356 : state_(NEW), |
360 track_adapter_(new VideoTrackAdapter( | 357 track_adapter_(new VideoTrackAdapter( |
361 ChildProcess::current()->io_message_loop_proxy())), | 358 ChildProcess::current()->io_message_loop_proxy())), |
362 weak_factory_(this) { | 359 weak_factory_(this) { |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
485 StartSourceImpl( | 482 StartSourceImpl( |
486 current_format_, | 483 current_format_, |
487 base::Bind(&VideoTrackAdapter::DeliverFrameOnIO, track_adapter_)); | 484 base::Bind(&VideoTrackAdapter::DeliverFrameOnIO, track_adapter_)); |
488 } | 485 } |
489 | 486 |
490 bool MediaStreamVideoSource::FindBestFormatWithConstraints( | 487 bool MediaStreamVideoSource::FindBestFormatWithConstraints( |
491 const media::VideoCaptureFormats& formats, | 488 const media::VideoCaptureFormats& formats, |
492 media::VideoCaptureFormat* best_format) { | 489 media::VideoCaptureFormat* best_format) { |
493 DCHECK(CalledOnValidThread()); | 490 DCHECK(CalledOnValidThread()); |
494 // Find the first constraints that we can fulfill. | 491 // Find the first constraints that we can fulfill. |
495 for (std::vector<RequestedConstraints>::iterator request_it = | 492 for (const auto& request : requested_constraints_) { |
496 requested_constraints_.begin(); | |
497 request_it != requested_constraints_.end(); ++request_it) { | |
498 const blink::WebMediaConstraints& requested_constraints = | 493 const blink::WebMediaConstraints& requested_constraints = |
499 request_it->constraints; | 494 request.constraints; |
500 | 495 |
501 // If the source doesn't support capability enumeration it is still ok if | 496 // If the source doesn't support capability enumeration it is still ok if |
502 // no mandatory constraints have been specified. That just means that | 497 // no mandatory constraints have been specified. That just means that |
503 // we will start with whatever format is native to the source. | 498 // we will start with whatever format is native to the source. |
504 if (formats.empty() && !HasMandatoryConstraints(requested_constraints)) { | 499 if (formats.empty() && !HasMandatoryConstraints(requested_constraints)) { |
505 *best_format = media::VideoCaptureFormat(); | 500 *best_format = media::VideoCaptureFormat(); |
506 return true; | 501 return true; |
507 } | 502 } |
508 blink::WebString unsatisfied_constraint; | 503 blink::WebString unsatisfied_constraint; |
509 media::VideoCaptureFormats filtered_formats = | 504 media::VideoCaptureFormats filtered_formats = |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
541 FinalizeAddTrack(); | 536 FinalizeAddTrack(); |
542 } | 537 } |
543 | 538 |
544 void MediaStreamVideoSource::FinalizeAddTrack() { | 539 void MediaStreamVideoSource::FinalizeAddTrack() { |
545 DCHECK(CalledOnValidThread()); | 540 DCHECK(CalledOnValidThread()); |
546 media::VideoCaptureFormats formats; | 541 media::VideoCaptureFormats formats; |
547 formats.push_back(current_format_); | 542 formats.push_back(current_format_); |
548 | 543 |
549 std::vector<RequestedConstraints> callbacks; | 544 std::vector<RequestedConstraints> callbacks; |
550 callbacks.swap(requested_constraints_); | 545 callbacks.swap(requested_constraints_); |
551 for (std::vector<RequestedConstraints>::iterator it = callbacks.begin(); | 546 for (const auto& request : callbacks) { |
552 it != callbacks.end(); ++it) { | |
553 MediaStreamRequestResult result = MEDIA_DEVICE_OK; | 547 MediaStreamRequestResult result = MEDIA_DEVICE_OK; |
554 blink::WebString unsatisfied_constraint; | 548 blink::WebString unsatisfied_constraint; |
555 | 549 |
556 if (HasMandatoryConstraints(it->constraints) && | 550 if (HasMandatoryConstraints(request.constraints) && |
557 FilterFormats(it->constraints, formats, | 551 FilterFormats(request.constraints, formats, |
558 &unsatisfied_constraint).empty()) | 552 &unsatisfied_constraint).empty()) { |
559 result = MEDIA_DEVICE_CONSTRAINT_NOT_SATISFIED; | 553 result = MEDIA_DEVICE_CONSTRAINT_NOT_SATISFIED; |
| 554 } |
560 | 555 |
561 if (state_ != STARTED && result == MEDIA_DEVICE_OK) | 556 if (state_ != STARTED && result == MEDIA_DEVICE_OK) |
562 result = MEDIA_DEVICE_TRACK_START_FAILURE; | 557 result = MEDIA_DEVICE_TRACK_START_FAILURE; |
563 | 558 |
564 if (result == MEDIA_DEVICE_OK) { | 559 if (result == MEDIA_DEVICE_OK) { |
565 int max_width; | 560 int max_width; |
566 int max_height; | 561 int max_height; |
567 GetDesiredMaxWidthAndHeight(it->constraints, &max_width, &max_height); | 562 GetDesiredMaxWidthAndHeight(request.constraints, &max_width, &max_height); |
568 double max_aspect_ratio; | 563 double max_aspect_ratio; |
569 double min_aspect_ratio; | 564 double min_aspect_ratio; |
570 GetDesiredMinAndMaxAspectRatio(it->constraints, | 565 GetDesiredMinAndMaxAspectRatio(request.constraints, |
571 &min_aspect_ratio, | 566 &min_aspect_ratio, |
572 &max_aspect_ratio); | 567 &max_aspect_ratio); |
573 double max_frame_rate = 0.0f; | 568 double max_frame_rate = 0.0f; |
574 GetConstraintValueAsDouble(it->constraints, | 569 GetConstraintValueAsDouble(request.constraints, |
575 kMaxFrameRate, &max_frame_rate); | 570 kMaxFrameRate, &max_frame_rate); |
576 | 571 |
577 track_adapter_->AddTrack(it->track, it->frame_callback, | 572 track_adapter_->AddTrack(request.track, request.frame_callback, |
578 max_width, max_height, | 573 max_width, max_height, |
579 min_aspect_ratio, max_aspect_ratio, | 574 min_aspect_ratio, max_aspect_ratio, |
580 max_frame_rate); | 575 max_frame_rate); |
581 } | 576 } |
582 | 577 |
583 DVLOG(3) << "FinalizeAddTrack() result " << result; | 578 DVLOG(3) << "FinalizeAddTrack() result " << result; |
584 | 579 |
585 if (!it->callback.is_null()) { | 580 if (!request.callback.is_null()) { |
586 it->callback.Run(this, result, unsatisfied_constraint); | 581 request.callback.Run(this, result, unsatisfied_constraint); |
587 } | 582 } |
588 } | 583 } |
589 } | 584 } |
590 | 585 |
591 void MediaStreamVideoSource::SetReadyState( | 586 void MediaStreamVideoSource::SetReadyState( |
592 blink::WebMediaStreamSource::ReadyState state) { | 587 blink::WebMediaStreamSource::ReadyState state) { |
593 DVLOG(3) << "MediaStreamVideoSource::SetReadyState state " << state; | 588 DVLOG(3) << "MediaStreamVideoSource::SetReadyState state " << state; |
594 DCHECK(CalledOnValidThread()); | 589 DCHECK(CalledOnValidThread()); |
595 if (!owner().isNull()) | 590 if (!owner().isNull()) |
596 owner().setReadyState(state); | 591 owner().setReadyState(state); |
597 for (std::vector<MediaStreamVideoTrack*>::iterator it = tracks_.begin(); | 592 for (const auto& track : tracks_) |
598 it != tracks_.end(); ++it) { | 593 track->OnReadyStateChanged(state); |
599 (*it)->OnReadyStateChanged(state); | |
600 } | |
601 } | 594 } |
602 | 595 |
603 void MediaStreamVideoSource::SetMutedState(bool muted_state) { | 596 void MediaStreamVideoSource::SetMutedState(bool muted_state) { |
604 DVLOG(3) << "MediaStreamVideoSource::SetMutedState state=" << muted_state; | 597 DVLOG(3) << "MediaStreamVideoSource::SetMutedState state=" << muted_state; |
605 DCHECK(CalledOnValidThread()); | 598 DCHECK(CalledOnValidThread()); |
606 if (!owner().isNull()) { | 599 if (!owner().isNull()) { |
607 owner().setReadyState(muted_state | 600 owner().setReadyState(muted_state |
608 ? blink::WebMediaStreamSource::ReadyStateMuted | 601 ? blink::WebMediaStreamSource::ReadyStateMuted |
609 : blink::WebMediaStreamSource::ReadyStateLive); | 602 : blink::WebMediaStreamSource::ReadyStateLive); |
610 } | 603 } |
611 } | 604 } |
612 | 605 |
613 MediaStreamVideoSource::RequestedConstraints::RequestedConstraints( | 606 MediaStreamVideoSource::RequestedConstraints::RequestedConstraints( |
614 MediaStreamVideoTrack* track, | 607 MediaStreamVideoTrack* track, |
615 const VideoCaptureDeliverFrameCB& frame_callback, | 608 const VideoCaptureDeliverFrameCB& frame_callback, |
616 const blink::WebMediaConstraints& constraints, | 609 const blink::WebMediaConstraints& constraints, |
617 const ConstraintsCallback& callback) | 610 const ConstraintsCallback& callback) |
618 : track(track), | 611 : track(track), |
619 frame_callback(frame_callback), | 612 frame_callback(frame_callback), |
620 constraints(constraints), | 613 constraints(constraints), |
621 callback(callback) { | 614 callback(callback) { |
622 } | 615 } |
623 | 616 |
624 MediaStreamVideoSource::RequestedConstraints::~RequestedConstraints() { | 617 MediaStreamVideoSource::RequestedConstraints::~RequestedConstraints() { |
625 } | 618 } |
626 | 619 |
627 } // namespace content | 620 } // namespace content |
OLD | NEW |