Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(479)

Side by Side Diff: content/renderer/media/media_stream_video_source.cc

Issue 964293002: VideoCaptureImpl & relatives small cleanup. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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, &current_candidates); 291 FilterFormatsByConstraint(optional[i], false, &current_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 return *std::min_element(
307 media::VideoCaptureFormats::const_iterator best_it = formats.begin(); 304 formats.begin(),
308 int best_diff = std::numeric_limits<int>::max(); 305 formats.end(),
309 for (; it != formats.end(); ++it) { 306 [&](const media::VideoCaptureFormat& f1,
emircan 2015/03/02 19:29:07 Default lambda captures are not allowed in the sty
mcasas 2015/03/02 22:57:58 Done.
310 int diff = abs(area - it->frame_size.width() * it->frame_size.height()); 307 const media::VideoCaptureFormat& f2) {
311 if (diff < best_diff) { 308 return abs(area - f1.frame_size.GetArea()) <
312 best_diff = diff; 309 abs(area - f2.frame_size.GetArea());
313 best_it = it; 310 });
emircan 2015/03/02 19:29:07 Overall comparison complexity is O(N) for min_elem
mcasas 2015/03/02 22:57:58 Hmm you're right. I was too centered in making the
314 }
315 }
316 return *best_it;
317 } 311 }
318 312
319 // Find the format that best matches the default video size. 313 // Find the format that best matches the default video size.
320 // This algorithm is chosen since a resolution must be picked even if no 314 // This algorithm is chosen since a resolution must be picked even if no
321 // constraints are provided. We don't just select the maximum supported 315 // constraints are provided. We don't just select the maximum supported
322 // resolution since higher resolutions cost more in terms of complexity and 316 // resolution since higher resolutions cost more in terms of complexity and
323 // many cameras have lower frame rate and have more noise in the image at 317 // many cameras have lower frame rate and have more noise in the image at
324 // their maximum supported resolution. 318 // their maximum supported resolution.
325 void GetBestCaptureFormat( 319 void GetBestCaptureFormat(
326 const media::VideoCaptureFormats& formats, 320 const media::VideoCaptureFormats& formats,
(...skipping 14 matching lines...) Expand all
341 } // anonymous namespace 335 } // anonymous namespace
342 336
343 // static 337 // static
344 MediaStreamVideoSource* MediaStreamVideoSource::GetVideoSource( 338 MediaStreamVideoSource* MediaStreamVideoSource::GetVideoSource(
345 const blink::WebMediaStreamSource& source) { 339 const blink::WebMediaStreamSource& source) {
346 return static_cast<MediaStreamVideoSource*>(source.extraData()); 340 return static_cast<MediaStreamVideoSource*>(source.extraData());
347 } 341 }
348 342
349 // static 343 // static
350 bool MediaStreamVideoSource::IsConstraintSupported(const std::string& name) { 344 bool MediaStreamVideoSource::IsConstraintSupported(const std::string& name) {
351 for (size_t i = 0; i < arraysize(kSupportedConstraints); ++i) { 345 for (const auto& constraint : kSupportedConstraints) {
352 if (kSupportedConstraints[i] == name) 346 if (constraint == name)
353 return true; 347 return true;
354 } 348 }
355 return false; 349 return false;
356 } 350 }
357 351
358 MediaStreamVideoSource::MediaStreamVideoSource() 352 MediaStreamVideoSource::MediaStreamVideoSource()
359 : state_(NEW), 353 : state_(NEW),
360 track_adapter_(new VideoTrackAdapter( 354 track_adapter_(new VideoTrackAdapter(
361 ChildProcess::current()->io_message_loop_proxy())), 355 ChildProcess::current()->io_message_loop_proxy())),
362 weak_factory_(this) { 356 weak_factory_(this) {
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
485 StartSourceImpl( 479 StartSourceImpl(
486 current_format_, 480 current_format_,
487 base::Bind(&VideoTrackAdapter::DeliverFrameOnIO, track_adapter_)); 481 base::Bind(&VideoTrackAdapter::DeliverFrameOnIO, track_adapter_));
488 } 482 }
489 483
490 bool MediaStreamVideoSource::FindBestFormatWithConstraints( 484 bool MediaStreamVideoSource::FindBestFormatWithConstraints(
491 const media::VideoCaptureFormats& formats, 485 const media::VideoCaptureFormats& formats,
492 media::VideoCaptureFormat* best_format) { 486 media::VideoCaptureFormat* best_format) {
493 DCHECK(CalledOnValidThread()); 487 DCHECK(CalledOnValidThread());
494 // Find the first constraints that we can fulfill. 488 // Find the first constraints that we can fulfill.
495 for (std::vector<RequestedConstraints>::iterator request_it = 489 for (const auto& request_it : requested_constraints_) {
496 requested_constraints_.begin();
497 request_it != requested_constraints_.end(); ++request_it) {
498 const blink::WebMediaConstraints& requested_constraints = 490 const blink::WebMediaConstraints& requested_constraints =
499 request_it->constraints; 491 request_it.constraints;
500 492
501 // If the source doesn't support capability enumeration it is still ok if 493 // If the source doesn't support capability enumeration it is still ok if
502 // no mandatory constraints have been specified. That just means that 494 // no mandatory constraints have been specified. That just means that
503 // we will start with whatever format is native to the source. 495 // we will start with whatever format is native to the source.
504 if (formats.empty() && !HasMandatoryConstraints(requested_constraints)) { 496 if (formats.empty() && !HasMandatoryConstraints(requested_constraints)) {
505 *best_format = media::VideoCaptureFormat(); 497 *best_format = media::VideoCaptureFormat();
506 return true; 498 return true;
507 } 499 }
508 blink::WebString unsatisfied_constraint; 500 blink::WebString unsatisfied_constraint;
509 media::VideoCaptureFormats filtered_formats = 501 media::VideoCaptureFormats filtered_formats =
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
541 FinalizeAddTrack(); 533 FinalizeAddTrack();
542 } 534 }
543 535
544 void MediaStreamVideoSource::FinalizeAddTrack() { 536 void MediaStreamVideoSource::FinalizeAddTrack() {
545 DCHECK(CalledOnValidThread()); 537 DCHECK(CalledOnValidThread());
546 media::VideoCaptureFormats formats; 538 media::VideoCaptureFormats formats;
547 formats.push_back(current_format_); 539 formats.push_back(current_format_);
548 540
549 std::vector<RequestedConstraints> callbacks; 541 std::vector<RequestedConstraints> callbacks;
550 callbacks.swap(requested_constraints_); 542 callbacks.swap(requested_constraints_);
551 for (std::vector<RequestedConstraints>::iterator it = callbacks.begin(); 543 for (const auto& it : callbacks) {
552 it != callbacks.end(); ++it) {
553 MediaStreamRequestResult result = MEDIA_DEVICE_OK; 544 MediaStreamRequestResult result = MEDIA_DEVICE_OK;
554 blink::WebString unsatisfied_constraint; 545 blink::WebString unsatisfied_constraint;
555 546
556 if (HasMandatoryConstraints(it->constraints) && 547 if (HasMandatoryConstraints(it.constraints) &&
557 FilterFormats(it->constraints, formats, 548 FilterFormats(it.constraints, formats,
558 &unsatisfied_constraint).empty()) 549 &unsatisfied_constraint).empty()) {
559 result = MEDIA_DEVICE_CONSTRAINT_NOT_SATISFIED; 550 result = MEDIA_DEVICE_CONSTRAINT_NOT_SATISFIED;
551 }
560 552
561 if (state_ != STARTED && result == MEDIA_DEVICE_OK) 553 if (state_ != STARTED && result == MEDIA_DEVICE_OK)
562 result = MEDIA_DEVICE_TRACK_START_FAILURE; 554 result = MEDIA_DEVICE_TRACK_START_FAILURE;
563 555
564 if (result == MEDIA_DEVICE_OK) { 556 if (result == MEDIA_DEVICE_OK) {
565 int max_width; 557 int max_width;
566 int max_height; 558 int max_height;
567 GetDesiredMaxWidthAndHeight(it->constraints, &max_width, &max_height); 559 GetDesiredMaxWidthAndHeight(it.constraints, &max_width, &max_height);
568 double max_aspect_ratio; 560 double max_aspect_ratio;
569 double min_aspect_ratio; 561 double min_aspect_ratio;
570 GetDesiredMinAndMaxAspectRatio(it->constraints, 562 GetDesiredMinAndMaxAspectRatio(it.constraints,
571 &min_aspect_ratio, 563 &min_aspect_ratio,
572 &max_aspect_ratio); 564 &max_aspect_ratio);
573 double max_frame_rate = 0.0f; 565 double max_frame_rate = 0.0f;
574 GetConstraintValueAsDouble(it->constraints, 566 GetConstraintValueAsDouble(it.constraints,
575 kMaxFrameRate, &max_frame_rate); 567 kMaxFrameRate, &max_frame_rate);
576 568
577 track_adapter_->AddTrack(it->track, it->frame_callback, 569 track_adapter_->AddTrack(it.track, it.frame_callback,
578 max_width, max_height, 570 max_width, max_height,
579 min_aspect_ratio, max_aspect_ratio, 571 min_aspect_ratio, max_aspect_ratio,
580 max_frame_rate); 572 max_frame_rate);
581 } 573 }
582 574
583 DVLOG(3) << "FinalizeAddTrack() result " << result; 575 DVLOG(3) << "FinalizeAddTrack() result " << result;
584 576
585 if (!it->callback.is_null()) { 577 if (!it.callback.is_null()) {
586 it->callback.Run(this, result, unsatisfied_constraint); 578 it.callback.Run(this, result, unsatisfied_constraint);
587 } 579 }
588 } 580 }
589 } 581 }
590 582
591 void MediaStreamVideoSource::SetReadyState( 583 void MediaStreamVideoSource::SetReadyState(
592 blink::WebMediaStreamSource::ReadyState state) { 584 blink::WebMediaStreamSource::ReadyState state) {
593 DVLOG(3) << "MediaStreamVideoSource::SetReadyState state " << state; 585 DVLOG(3) << "MediaStreamVideoSource::SetReadyState state " << state;
594 DCHECK(CalledOnValidThread()); 586 DCHECK(CalledOnValidThread());
595 if (!owner().isNull()) 587 if (!owner().isNull())
596 owner().setReadyState(state); 588 owner().setReadyState(state);
597 for (std::vector<MediaStreamVideoTrack*>::iterator it = tracks_.begin(); 589 for (const auto& it : tracks_)
598 it != tracks_.end(); ++it) { 590 it->OnReadyStateChanged(state);
599 (*it)->OnReadyStateChanged(state);
600 }
601 } 591 }
602 592
603 void MediaStreamVideoSource::SetMutedState(bool muted_state) { 593 void MediaStreamVideoSource::SetMutedState(bool muted_state) {
604 DVLOG(3) << "MediaStreamVideoSource::SetMutedState state=" << muted_state; 594 DVLOG(3) << "MediaStreamVideoSource::SetMutedState state=" << muted_state;
605 DCHECK(CalledOnValidThread()); 595 DCHECK(CalledOnValidThread());
606 if (!owner().isNull()) { 596 if (!owner().isNull()) {
607 owner().setReadyState(muted_state 597 owner().setReadyState(muted_state
608 ? blink::WebMediaStreamSource::ReadyStateMuted 598 ? blink::WebMediaStreamSource::ReadyStateMuted
609 : blink::WebMediaStreamSource::ReadyStateLive); 599 : blink::WebMediaStreamSource::ReadyStateLive);
610 } 600 }
611 } 601 }
612 602
613 MediaStreamVideoSource::RequestedConstraints::RequestedConstraints( 603 MediaStreamVideoSource::RequestedConstraints::RequestedConstraints(
614 MediaStreamVideoTrack* track, 604 MediaStreamVideoTrack* track,
615 const VideoCaptureDeliverFrameCB& frame_callback, 605 const VideoCaptureDeliverFrameCB& frame_callback,
616 const blink::WebMediaConstraints& constraints, 606 const blink::WebMediaConstraints& constraints,
617 const ConstraintsCallback& callback) 607 const ConstraintsCallback& callback)
618 : track(track), 608 : track(track),
619 frame_callback(frame_callback), 609 frame_callback(frame_callback),
620 constraints(constraints), 610 constraints(constraints),
621 callback(callback) { 611 callback(callback) {
622 } 612 }
623 613
624 MediaStreamVideoSource::RequestedConstraints::~RequestedConstraints() { 614 MediaStreamVideoSource::RequestedConstraints::~RequestedConstraints() {
625 } 615 }
626 616
627 } // namespace content 617 } // namespace content
OLDNEW
« no previous file with comments | « no previous file | content/renderer/media/video_capture_impl.cc » ('j') | content/renderer/media/video_capture_impl.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698