blob: 0543dff3f383f628f102535b1c3e84d90ca930ee [file] [log] [blame]
perkj11e18052016-03-07 22:03:23 +01001/*
perkj11e18052016-03-07 22:03:23 +01002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "webrtc/api/videocapturertracksource.h"
12
perkja3ede6c2016-03-08 01:27:48 +010013#include <cstdlib>
14#include <string>
15#include <vector>
16
17#include "webrtc/api/mediaconstraintsinterface.h"
18#include "webrtc/base/arraysize.h"
19
20using cricket::CaptureState;
21using webrtc::MediaConstraintsInterface;
22using webrtc::MediaSourceInterface;
23
24namespace {
25
26const double kRoundingTruncation = 0.0005;
27
28// Default resolution. If no constraint is specified, this is the resolution we
29// will use.
30static const cricket::VideoFormatPod kDefaultFormat = {
31 640, 480, FPS_TO_INTERVAL(30), cricket::FOURCC_ANY};
32
33// List of formats used if the camera doesn't support capability enumeration.
34static const cricket::VideoFormatPod kVideoFormats[] = {
35 {1920, 1080, FPS_TO_INTERVAL(30), cricket::FOURCC_ANY},
36 {1280, 720, FPS_TO_INTERVAL(30), cricket::FOURCC_ANY},
37 {960, 720, FPS_TO_INTERVAL(30), cricket::FOURCC_ANY},
38 {640, 360, FPS_TO_INTERVAL(30), cricket::FOURCC_ANY},
39 {640, 480, FPS_TO_INTERVAL(30), cricket::FOURCC_ANY},
40 {320, 240, FPS_TO_INTERVAL(30), cricket::FOURCC_ANY},
41 {320, 180, FPS_TO_INTERVAL(30), cricket::FOURCC_ANY}};
42
43MediaSourceInterface::SourceState GetReadyState(cricket::CaptureState state) {
44 switch (state) {
45 case cricket::CS_STARTING:
46 return MediaSourceInterface::kInitializing;
47 case cricket::CS_RUNNING:
48 return MediaSourceInterface::kLive;
49 case cricket::CS_FAILED:
50 case cricket::CS_STOPPED:
51 return MediaSourceInterface::kEnded;
perkja3ede6c2016-03-08 01:27:48 +010052 default:
53 ASSERT(false && "GetReadyState unknown state");
54 }
55 return MediaSourceInterface::kEnded;
56}
57
58void SetUpperLimit(int new_limit, int* original_limit) {
59 if (*original_limit < 0 || new_limit < *original_limit)
60 *original_limit = new_limit;
61}
62
63// Updates |format_upper_limit| from |constraint|.
64// If constraint.maxFoo is smaller than format_upper_limit.foo,
65// set format_upper_limit.foo to constraint.maxFoo.
66void SetUpperLimitFromConstraint(
67 const MediaConstraintsInterface::Constraint& constraint,
68 cricket::VideoFormat* format_upper_limit) {
69 if (constraint.key == MediaConstraintsInterface::kMaxWidth) {
70 int value = rtc::FromString<int>(constraint.value);
71 SetUpperLimit(value, &(format_upper_limit->width));
72 } else if (constraint.key == MediaConstraintsInterface::kMaxHeight) {
73 int value = rtc::FromString<int>(constraint.value);
74 SetUpperLimit(value, &(format_upper_limit->height));
75 }
76}
77
78// Fills |format_out| with the max width and height allowed by |constraints|.
79void FromConstraintsForScreencast(
80 const MediaConstraintsInterface::Constraints& constraints,
81 cricket::VideoFormat* format_out) {
82 typedef MediaConstraintsInterface::Constraints::const_iterator
83 ConstraintsIterator;
84
85 cricket::VideoFormat upper_limit(-1, -1, 0, 0);
86 for (ConstraintsIterator constraints_it = constraints.begin();
87 constraints_it != constraints.end(); ++constraints_it)
88 SetUpperLimitFromConstraint(*constraints_it, &upper_limit);
89
90 if (upper_limit.width >= 0)
91 format_out->width = upper_limit.width;
92 if (upper_limit.height >= 0)
93 format_out->height = upper_limit.height;
94}
95
96// Returns true if |constraint| is fulfilled. |format_out| can differ from
97// |format_in| if the format is changed by the constraint. Ie - the frame rate
98// can be changed by setting maxFrameRate.
99bool NewFormatWithConstraints(
100 const MediaConstraintsInterface::Constraint& constraint,
101 const cricket::VideoFormat& format_in,
102 bool mandatory,
103 cricket::VideoFormat* format_out) {
104 ASSERT(format_out != NULL);
105 *format_out = format_in;
106
107 if (constraint.key == MediaConstraintsInterface::kMinWidth) {
108 int value = rtc::FromString<int>(constraint.value);
109 return (value <= format_in.width);
110 } else if (constraint.key == MediaConstraintsInterface::kMaxWidth) {
111 int value = rtc::FromString<int>(constraint.value);
112 return (value >= format_in.width);
113 } else if (constraint.key == MediaConstraintsInterface::kMinHeight) {
114 int value = rtc::FromString<int>(constraint.value);
115 return (value <= format_in.height);
116 } else if (constraint.key == MediaConstraintsInterface::kMaxHeight) {
117 int value = rtc::FromString<int>(constraint.value);
118 return (value >= format_in.height);
119 } else if (constraint.key == MediaConstraintsInterface::kMinFrameRate) {
120 int value = rtc::FromString<int>(constraint.value);
121 return (value <= cricket::VideoFormat::IntervalToFps(format_in.interval));
122 } else if (constraint.key == MediaConstraintsInterface::kMaxFrameRate) {
123 int value = rtc::FromString<int>(constraint.value);
124 if (value == 0) {
125 if (mandatory) {
126 // TODO(ronghuawu): Convert the constraint value to float when sub-1fps
127 // is supported by the capturer.
128 return false;
129 } else {
130 value = 1;
131 }
132 }
133 if (value <= cricket::VideoFormat::IntervalToFps(format_in.interval))
134 format_out->interval = cricket::VideoFormat::FpsToInterval(value);
135 return true;
136 } else if (constraint.key == MediaConstraintsInterface::kMinAspectRatio) {
137 double value = rtc::FromString<double>(constraint.value);
138 // The aspect ratio in |constraint.value| has been converted to a string and
139 // back to a double, so it may have a rounding error.
140 // E.g if the value 1/3 is converted to a string, the string will not have
141 // infinite length.
142 // We add a margin of 0.0005 which is high enough to detect the same aspect
143 // ratio but small enough to avoid matching wrong aspect ratios.
144 double ratio = static_cast<double>(format_in.width) / format_in.height;
145 return (value <= ratio + kRoundingTruncation);
146 } else if (constraint.key == MediaConstraintsInterface::kMaxAspectRatio) {
147 double value = rtc::FromString<double>(constraint.value);
148 double ratio = static_cast<double>(format_in.width) / format_in.height;
149 // Subtract 0.0005 to avoid rounding problems. Same as above.
150 const double kRoundingTruncation = 0.0005;
151 return (value >= ratio - kRoundingTruncation);
152 } else if (constraint.key == MediaConstraintsInterface::kNoiseReduction) {
153 // These are actually options, not constraints, so they can be satisfied
154 // regardless of the format.
155 return true;
156 }
157 LOG(LS_WARNING) << "Found unknown MediaStream constraint. Name:"
158 << constraint.key << " Value:" << constraint.value;
159 return false;
160}
161
162// Removes cricket::VideoFormats from |formats| that don't meet |constraint|.
163void FilterFormatsByConstraint(
164 const MediaConstraintsInterface::Constraint& constraint,
165 bool mandatory,
166 std::vector<cricket::VideoFormat>* formats) {
167 std::vector<cricket::VideoFormat>::iterator format_it = formats->begin();
168 while (format_it != formats->end()) {
169 // Modify the format_it to fulfill the constraint if possible.
170 // Delete it otherwise.
171 if (!NewFormatWithConstraints(constraint, (*format_it), mandatory,
172 &(*format_it))) {
173 format_it = formats->erase(format_it);
174 } else {
175 ++format_it;
176 }
177 }
178}
179
180// Returns a vector of cricket::VideoFormat that best match |constraints|.
181std::vector<cricket::VideoFormat> FilterFormats(
182 const MediaConstraintsInterface::Constraints& mandatory,
183 const MediaConstraintsInterface::Constraints& optional,
184 const std::vector<cricket::VideoFormat>& supported_formats) {
185 typedef MediaConstraintsInterface::Constraints::const_iterator
186 ConstraintsIterator;
187 std::vector<cricket::VideoFormat> candidates = supported_formats;
188
189 for (ConstraintsIterator constraints_it = mandatory.begin();
190 constraints_it != mandatory.end(); ++constraints_it)
191 FilterFormatsByConstraint(*constraints_it, true, &candidates);
192
193 if (candidates.size() == 0)
194 return candidates;
195
196 // Ok - all mandatory checked and we still have a candidate.
197 // Let's try filtering using the optional constraints.
198 for (ConstraintsIterator constraints_it = optional.begin();
199 constraints_it != optional.end(); ++constraints_it) {
200 std::vector<cricket::VideoFormat> current_candidates = candidates;
201 FilterFormatsByConstraint(*constraints_it, false, &current_candidates);
202 if (current_candidates.size() > 0) {
203 candidates = current_candidates;
204 }
205 }
206
207 // We have done as good as we can to filter the supported resolutions.
208 return candidates;
209}
210
211// Find the format that best matches the default video size.
212// Constraints are optional and since the performance of a video call
213// might be bad due to bitrate limitations, CPU, and camera performance,
214// it is better to select a resolution that is as close as possible to our
215// default and still meets the contraints.
216const cricket::VideoFormat& GetBestCaptureFormat(
217 const std::vector<cricket::VideoFormat>& formats) {
218 ASSERT(formats.size() > 0);
219
220 int default_area = kDefaultFormat.width * kDefaultFormat.height;
221
222 std::vector<cricket::VideoFormat>::const_iterator it = formats.begin();
223 std::vector<cricket::VideoFormat>::const_iterator best_it = formats.begin();
224 int best_diff_area = std::abs(default_area - it->width * it->height);
225 int64_t best_diff_interval = kDefaultFormat.interval;
226 for (; it != formats.end(); ++it) {
227 int diff_area = std::abs(default_area - it->width * it->height);
228 int64_t diff_interval = std::abs(kDefaultFormat.interval - it->interval);
229 if (diff_area < best_diff_area ||
230 (diff_area == best_diff_area && diff_interval < best_diff_interval)) {
231 best_diff_area = diff_area;
232 best_diff_interval = diff_interval;
233 best_it = it;
234 }
235 }
236 return *best_it;
237}
238
239// Set |option| to the highest-priority value of |key| in the constraints.
240// Return false if the key is mandatory, and the value is invalid.
241bool ExtractOption(const MediaConstraintsInterface* all_constraints,
242 const std::string& key,
Perc0d31e92016-03-31 17:23:39 +0200243 rtc::Optional<bool>* option) {
perkja3ede6c2016-03-08 01:27:48 +0100244 size_t mandatory = 0;
Perc0d31e92016-03-31 17:23:39 +0200245 bool value;
246 if (FindConstraint(all_constraints, key, &value, &mandatory)) {
247 *option = rtc::Optional<bool>(value);
perkja3ede6c2016-03-08 01:27:48 +0100248 return true;
249 }
250
251 return mandatory == 0;
252}
253
perkja3ede6c2016-03-08 01:27:48 +0100254} // anonymous namespace
255
256namespace webrtc {
257
258rtc::scoped_refptr<VideoTrackSourceInterface> VideoCapturerTrackSource::Create(
259 rtc::Thread* worker_thread,
260 cricket::VideoCapturer* capturer,
261 const webrtc::MediaConstraintsInterface* constraints,
262 bool remote) {
263 RTC_DCHECK(worker_thread != NULL);
264 RTC_DCHECK(capturer != NULL);
265 rtc::scoped_refptr<VideoCapturerTrackSource> source(
266 new rtc::RefCountedObject<VideoCapturerTrackSource>(worker_thread,
267 capturer, remote));
268 source->Initialize(constraints);
269 return source;
270}
271
272rtc::scoped_refptr<VideoTrackSourceInterface> VideoCapturerTrackSource::Create(
273 rtc::Thread* worker_thread,
274 cricket::VideoCapturer* capturer,
275 bool remote) {
276 RTC_DCHECK(worker_thread != NULL);
277 RTC_DCHECK(capturer != NULL);
278 rtc::scoped_refptr<VideoCapturerTrackSource> source(
279 new rtc::RefCountedObject<VideoCapturerTrackSource>(worker_thread,
280 capturer, remote));
281 source->Initialize(nullptr);
282 return source;
283}
284
285VideoCapturerTrackSource::VideoCapturerTrackSource(
286 rtc::Thread* worker_thread,
287 cricket::VideoCapturer* capturer,
288 bool remote)
perkj0d3eef22016-03-09 02:39:17 +0100289 : VideoTrackSource(capturer, worker_thread, remote),
290 signaling_thread_(rtc::Thread::Current()),
perkja3ede6c2016-03-08 01:27:48 +0100291 video_capturer_(capturer),
Perc0d31e92016-03-31 17:23:39 +0200292 started_(false) {
perkja3ede6c2016-03-08 01:27:48 +0100293 video_capturer_->SignalStateChange.connect(
294 this, &VideoCapturerTrackSource::OnStateChange);
295}
296
297VideoCapturerTrackSource::~VideoCapturerTrackSource() {
298 video_capturer_->SignalStateChange.disconnect(this);
299 Stop();
300}
301
302void VideoCapturerTrackSource::Initialize(
303 const webrtc::MediaConstraintsInterface* constraints) {
304 std::vector<cricket::VideoFormat> formats =
305 *video_capturer_->GetSupportedFormats();
306 if (formats.empty()) {
307 if (video_capturer_->IsScreencast()) {
308 // The screen capturer can accept any resolution and we will derive the
309 // format from the constraints if any.
310 // Note that this only affects tab capturing, not desktop capturing,
311 // since the desktop capturer does not respect the VideoFormat passed in.
312 formats.push_back(cricket::VideoFormat(kDefaultFormat));
313 } else {
314 // The VideoCapturer implementation doesn't support capability
315 // enumeration. We need to guess what the camera supports.
316 for (int i = 0; i < arraysize(kVideoFormats); ++i) {
317 formats.push_back(cricket::VideoFormat(kVideoFormats[i]));
318 }
319 }
320 }
321
322 if (constraints) {
323 MediaConstraintsInterface::Constraints mandatory_constraints =
324 constraints->GetMandatory();
325 MediaConstraintsInterface::Constraints optional_constraints;
326 optional_constraints = constraints->GetOptional();
327
328 if (video_capturer_->IsScreencast()) {
329 // Use the maxWidth and maxHeight allowed by constraints for screencast.
330 FromConstraintsForScreencast(mandatory_constraints, &(formats[0]));
331 }
332
333 formats =
334 FilterFormats(mandatory_constraints, optional_constraints, formats);
335 }
336
337 if (formats.size() == 0) {
338 LOG(LS_WARNING) << "Failed to find a suitable video format.";
339 SetState(kEnded);
340 return;
341 }
342
perkj0d3eef22016-03-09 02:39:17 +0100343 if (!ExtractOption(constraints, MediaConstraintsInterface::kNoiseReduction,
344 &needs_denoising_)) {
345 LOG(LS_WARNING) << "Invalid mandatory value for"
346 << MediaConstraintsInterface::kNoiseReduction;
perkja3ede6c2016-03-08 01:27:48 +0100347 SetState(kEnded);
348 return;
349 }
perkja3ede6c2016-03-08 01:27:48 +0100350
351 format_ = GetBestCaptureFormat(formats);
352 // Start the camera with our best guess.
perkj0d3eef22016-03-09 02:39:17 +0100353 if (!worker_thread()->Invoke<bool>(
perkja3ede6c2016-03-08 01:27:48 +0100354 rtc::Bind(&cricket::VideoCapturer::StartCapturing,
355 video_capturer_.get(), format_))) {
356 SetState(kEnded);
357 return;
358 }
359 started_ = true;
360 // Initialize hasn't succeeded until a successful state change has occurred.
361}
362
363void VideoCapturerTrackSource::Stop() {
364 if (!started_) {
365 return;
366 }
367 started_ = false;
perkj0d3eef22016-03-09 02:39:17 +0100368 worker_thread()->Invoke<void>(
perkja3ede6c2016-03-08 01:27:48 +0100369 rtc::Bind(&cricket::VideoCapturer::Stop, video_capturer_.get()));
370}
371
372void VideoCapturerTrackSource::Restart() {
373 if (started_) {
374 return;
375 }
perkj0d3eef22016-03-09 02:39:17 +0100376 if (!worker_thread()->Invoke<bool>(
perkja3ede6c2016-03-08 01:27:48 +0100377 rtc::Bind(&cricket::VideoCapturer::StartCapturing,
378 video_capturer_.get(), format_))) {
379 SetState(kEnded);
380 return;
381 }
382 started_ = true;
383}
384
perkja3ede6c2016-03-08 01:27:48 +0100385// OnStateChange listens to the cricket::VideoCapturer::SignalStateChange.
386void VideoCapturerTrackSource::OnStateChange(
387 cricket::VideoCapturer* capturer,
388 cricket::CaptureState capture_state) {
389 if (rtc::Thread::Current() != signaling_thread_) {
390 invoker_.AsyncInvoke<void>(
391 signaling_thread_, rtc::Bind(&VideoCapturerTrackSource::OnStateChange,
392 this, capturer, capture_state));
393 return;
394 }
395
396 if (capturer == video_capturer_.get()) {
397 SetState(GetReadyState(capture_state));
398 }
399}
400
perkja3ede6c2016-03-08 01:27:48 +0100401} // namespace webrtc