| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/renderer/media/media_stream_video_capture_device_source.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/debug/trace_event.h" |
| 9 #include "content/renderer/media/rtc_video_capturer.h" |
| 10 |
| 11 namespace { |
| 12 |
| 13 // Returns true if the mandatory constraints in |c2| match the mandatory |
| 14 // constraints in |c1|. All mandatory constraints in |c1| do not need to exist |
| 15 // in |c2|. |
| 16 bool MandatoryConstraintsMatch(const blink::WebMediaConstraints& c1, |
| 17 const blink::WebMediaConstraints& c2) { |
| 18 if (c1.isNull() && c2.isNull()) { |
| 19 // WebMediaConstraints are NULL in unit tests unfortunately. |
| 20 return true; |
| 21 } |
| 22 blink::WebVector<blink::WebMediaConstraint> m1; |
| 23 c1.getMandatoryConstraints(m1); |
| 24 blink::WebVector<blink::WebMediaConstraint> m2; |
| 25 c2.getMandatoryConstraints(m2); |
| 26 |
| 27 if (m1.size() < m2.size()) |
| 28 return false; |
| 29 |
| 30 size_t matched_constraints = 0; |
| 31 |
| 32 for (size_t i = 0; i < m1.size(); ++i) { |
| 33 blink::WebString value; |
| 34 if (!c2.getMandatoryConstraintValue(m1[i].m_name, value)) |
| 35 continue; |
| 36 if (value != m1[i].m_value) |
| 37 return false; |
| 38 ++matched_constraints; |
| 39 } |
| 40 return m1.size() >= matched_constraints + m2.size(); |
| 41 } |
| 42 |
| 43 } // anonymous namespace |
| 44 |
| 45 namespace content { |
| 46 |
| 47 MediaStreamVideoCaptureDeviceSource::MediaStreamVideoCaptureDeviceSource( |
| 48 const StreamDeviceInfo& device_info, |
| 49 const SourceStopCallback& stop_callback, |
| 50 MediaStreamDependencyFactory* factory) |
| 51 : MediaStreamVideoSource(factory), |
| 52 initializing_(false) { |
| 53 SetDeviceInfo(device_info); |
| 54 SetStopCallback(stop_callback); |
| 55 } |
| 56 |
| 57 MediaStreamVideoCaptureDeviceSource::~MediaStreamVideoCaptureDeviceSource() { |
| 58 if (initializing_) { |
| 59 GetAdapter()->UnregisterObserver(this); |
| 60 } |
| 61 } |
| 62 |
| 63 void MediaStreamVideoCaptureDeviceSource::ApplyConstraints( |
| 64 const blink::WebMediaStreamTrack& track, |
| 65 const blink::WebMediaConstraints& constraints, |
| 66 const ConstraintsCallback& callback) { |
| 67 DCHECK(CalledOnValidThread()); |
| 68 |
| 69 if (!GetAdapter()) { |
| 70 // Create the webrtc::VideoSource implementation if it does not exist. |
| 71 cricket::VideoCapturer* capturer = |
| 72 factory()->CreateVideoCapturer(device_info()); |
| 73 SetAdapter(factory()->CreateLocalVideoSource(capturer, |
| 74 constraints)); |
| 75 current_constraints_ = constraints; |
| 76 initializing_ = true; |
| 77 GetAdapter()->RegisterObserver(this); |
| 78 } |
| 79 |
| 80 // Currently, reconfiguring the source is not supported. |
| 81 if (!MandatoryConstraintsMatch(current_constraints_, constraints)) { |
| 82 callback.Run(this, false); |
| 83 return; |
| 84 } |
| 85 |
| 86 // There might be multiple tracks attaching to the source while it is being |
| 87 // configured. |
| 88 constraints_callbacks_.push_back(callback); |
| 89 CheckIfWebRtcSourceIsLive(); |
| 90 } |
| 91 |
| 92 void MediaStreamVideoCaptureDeviceSource::OnChanged() { |
| 93 DCHECK(CalledOnValidThread()); |
| 94 CheckIfWebRtcSourceIsLive(); |
| 95 } |
| 96 |
| 97 void MediaStreamVideoCaptureDeviceSource::CheckIfWebRtcSourceIsLive() { |
| 98 webrtc::VideoSourceInterface* webrtc_source = GetAdapter(); |
| 99 |
| 100 if (webrtc_source->state() == webrtc::MediaSourceInterface::kInitializing) |
| 101 return; |
| 102 |
| 103 bool success = webrtc_source->state() == webrtc::MediaSourceInterface::kLive |
| 104 ? true : false; |
| 105 |
| 106 if (initializing_) { |
| 107 webrtc_source->UnregisterObserver(this); |
| 108 initializing_ = false; |
| 109 } |
| 110 |
| 111 std::vector<ConstraintsCallback> callbacks(constraints_callbacks_); |
| 112 constraints_callbacks_.clear(); |
| 113 |
| 114 for (std::vector<ConstraintsCallback>::iterator it = callbacks.begin(); |
| 115 it != callbacks.end(); ++it) { |
| 116 if (!it->is_null()) |
| 117 it->Run(this, success); |
| 118 } |
| 119 } |
| 120 |
| 121 } // namespace content |
| OLD | NEW |