| Index: content/renderer/media/media_stream_video_capture_device_source.cc
|
| diff --git a/content/renderer/media/media_stream_video_capture_device_source.cc b/content/renderer/media/media_stream_video_capture_device_source.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..7b56e5eede6a94178135d8028ed2682783dfd874
|
| --- /dev/null
|
| +++ b/content/renderer/media/media_stream_video_capture_device_source.cc
|
| @@ -0,0 +1,121 @@
|
| +// Copyright 2014 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "content/renderer/media/media_stream_video_capture_device_source.h"
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/debug/trace_event.h"
|
| +#include "content/renderer/media/rtc_video_capturer.h"
|
| +
|
| +namespace {
|
| +
|
| +// Returns true if the mandatory constraints in |c2| match the mandatory
|
| +// constraints in |c1|. All mandatory constraints in |c1| do not need to exist
|
| +// in |c2|.
|
| +bool MandatoryConstraintsMatch(const blink::WebMediaConstraints& c1,
|
| + const blink::WebMediaConstraints& c2) {
|
| + if (c1.isNull() && c2.isNull()) {
|
| + // WebMediaConstraints are NULL in unit tests unfortunately.
|
| + return true;
|
| + }
|
| + blink::WebVector<blink::WebMediaConstraint> m1;
|
| + c1.getMandatoryConstraints(m1);
|
| + blink::WebVector<blink::WebMediaConstraint> m2;
|
| + c2.getMandatoryConstraints(m2);
|
| +
|
| + if (m1.size() < m2.size())
|
| + return false;
|
| +
|
| + size_t matched_constraints = 0;
|
| +
|
| + for (size_t i = 0; i < m1.size(); ++i) {
|
| + blink::WebString value;
|
| + if (!c2.getMandatoryConstraintValue(m1[i].m_name, value))
|
| + continue;
|
| + if (value != m1[i].m_value)
|
| + return false;
|
| + ++matched_constraints;
|
| + }
|
| + return m1.size() >= matched_constraints + m2.size();
|
| +}
|
| +
|
| +} // anonymous namespace
|
| +
|
| +namespace content {
|
| +
|
| +MediaStreamVideoCaptureDeviceSource::MediaStreamVideoCaptureDeviceSource(
|
| + const StreamDeviceInfo& device_info,
|
| + const SourceStopCallback& stop_callback,
|
| + MediaStreamDependencyFactory* factory)
|
| + : MediaStreamVideoSource(factory),
|
| + initializing_(false) {
|
| + SetDeviceInfo(device_info);
|
| + SetStopCallback(stop_callback);
|
| +}
|
| +
|
| +MediaStreamVideoCaptureDeviceSource::~MediaStreamVideoCaptureDeviceSource() {
|
| + if (initializing_) {
|
| + GetAdapter()->UnregisterObserver(this);
|
| + }
|
| +}
|
| +
|
| +void MediaStreamVideoCaptureDeviceSource::ApplyConstraints(
|
| + const blink::WebMediaStreamTrack& track,
|
| + const blink::WebMediaConstraints& constraints,
|
| + const ConstraintsCallback& callback) {
|
| + DCHECK(CalledOnValidThread());
|
| +
|
| + if (!GetAdapter()) {
|
| + // Create the webrtc::VideoSource implementation if it does not exist.
|
| + cricket::VideoCapturer* capturer =
|
| + factory()->CreateVideoCapturer(device_info());
|
| + SetAdapter(factory()->CreateLocalVideoSource(capturer,
|
| + constraints));
|
| + current_constraints_ = constraints;
|
| + initializing_ = true;
|
| + GetAdapter()->RegisterObserver(this);
|
| + }
|
| +
|
| + // Currently, reconfiguring the source is not supported.
|
| + if (!MandatoryConstraintsMatch(current_constraints_, constraints)) {
|
| + callback.Run(this, false);
|
| + return;
|
| + }
|
| +
|
| + // There might be multiple tracks attaching to the source while it is being
|
| + // configured.
|
| + constraints_callbacks_.push_back(callback);
|
| + CheckIfWebRtcSourceIsLive();
|
| +}
|
| +
|
| +void MediaStreamVideoCaptureDeviceSource::OnChanged() {
|
| + DCHECK(CalledOnValidThread());
|
| + CheckIfWebRtcSourceIsLive();
|
| +}
|
| +
|
| +void MediaStreamVideoCaptureDeviceSource::CheckIfWebRtcSourceIsLive() {
|
| + webrtc::VideoSourceInterface* webrtc_source = GetAdapter();
|
| +
|
| + if (webrtc_source->state() == webrtc::MediaSourceInterface::kInitializing)
|
| + return;
|
| +
|
| + bool success = webrtc_source->state() == webrtc::MediaSourceInterface::kLive
|
| + ? true : false;
|
| +
|
| + if (initializing_) {
|
| + webrtc_source->UnregisterObserver(this);
|
| + initializing_ = false;
|
| + }
|
| +
|
| + std::vector<ConstraintsCallback> callbacks(constraints_callbacks_);
|
| + constraints_callbacks_.clear();
|
| +
|
| + for (std::vector<ConstraintsCallback>::iterator it = callbacks.begin();
|
| + it != callbacks.end(); ++it) {
|
| + if (!it->is_null())
|
| + it->Run(this, success);
|
| + }
|
| +}
|
| +
|
| +} // namespace content
|
|
|