1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
|
/*
* Copyright (C) 2012 Google Inc. All rights reserved.
* Copyright (C) 2013-2016 Apple Inc. All rights reserved.
* Copyright (C) 2013 Nokia Corporation and/or its subsidiary(-ies).
* Copyright (C) 2015 Ericsson AB. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name of Google Inc. nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#if ENABLE(MEDIA_STREAM)
#include "RealtimeMediaSource.h"
#include "MediaConstraints.h"
#include "NotImplemented.h"
#include "RealtimeMediaSourceCapabilities.h"
#include "UUID.h"
#include <wtf/MainThread.h>
#include <wtf/text/StringHash.h>
namespace WebCore {
RealtimeMediaSource::RealtimeMediaSource(const String& id, Type type, const String& name)
: m_weakPtrFactory(this)
, m_id(id)
, m_type(type)
, m_name(name)
{
// FIXME(147205): Need to implement fitness score for constraints
if (m_id.isEmpty())
m_id = createCanonicalUUIDString();
m_persistentID = m_id;
m_suppressNotifications = false;
}
void RealtimeMediaSource::reset()
{
m_stopped = false;
m_muted = false;
m_readonly = false;
m_remote = false;
}
void RealtimeMediaSource::addObserver(RealtimeMediaSource::Observer& observer)
{
m_observers.append(&observer);
}
void RealtimeMediaSource::removeObserver(RealtimeMediaSource::Observer& observer)
{
m_observers.removeFirstMatching([&observer](auto* anObserver) {
return anObserver == &observer;
});
if (!m_observers.size())
stop();
}
void RealtimeMediaSource::setMuted(bool muted)
{
if (m_stopped || m_muted == muted)
return;
m_muted = muted;
if (stopped())
return;
for (auto& observer : m_observers)
observer->sourceMutedChanged();
}
void RealtimeMediaSource::setEnabled(bool enabled)
{
if (m_stopped || m_enabled == enabled)
return;
m_enabled = enabled;
if (m_stopped)
return;
for (auto& observer : m_observers)
observer->sourceEnabledChanged();
}
void RealtimeMediaSource::settingsDidChange()
{
ASSERT(isMainThread());
if (m_pendingSettingsDidChangeNotification || m_suppressNotifications)
return;
m_pendingSettingsDidChangeNotification = true;
scheduleDeferredTask([this] {
m_pendingSettingsDidChangeNotification = false;
for (auto& observer : m_observers)
observer->sourceSettingsChanged();
});
}
void RealtimeMediaSource::videoSampleAvailable(MediaSample& mediaSample)
{
ASSERT(isMainThread());
for (const auto& observer : m_observers)
observer->videoSampleAvailable(mediaSample);
}
void RealtimeMediaSource::audioSamplesAvailable(const MediaTime& time, const PlatformAudioData& audioData, const AudioStreamDescription& description, size_t numberOfFrames)
{
for (const auto& observer : m_observers)
observer->audioSamplesAvailable(time, audioData, description, numberOfFrames);
}
bool RealtimeMediaSource::readonly() const
{
return m_readonly;
}
void RealtimeMediaSource::stop(Observer* callingObserver)
{
if (stopped())
return;
m_stopped = true;
for (const auto& observer : m_observers) {
if (observer != callingObserver)
observer->sourceStopped();
}
stopProducingData();
}
void RealtimeMediaSource::requestStop(Observer* callingObserver)
{
if (stopped())
return;
for (const auto& observer : m_observers) {
if (observer->preventSourceFromStopping())
return;
}
stop(callingObserver);
}
bool RealtimeMediaSource::supportsSizeAndFrameRate(std::optional<int>, std::optional<int>, std::optional<double>)
{
// The size and frame rate are within the capability limits, so they are supported.
return true;
}
bool RealtimeMediaSource::supportsSizeAndFrameRate(std::optional<IntConstraint> widthConstraint, std::optional<IntConstraint> heightConstraint, std::optional<DoubleConstraint> frameRateConstraint, String& badConstraint)
{
if (!widthConstraint && !heightConstraint && !frameRateConstraint)
return true;
ASSERT(this->capabilities());
RealtimeMediaSourceCapabilities& capabilities = *this->capabilities();
std::optional<int> width;
if (widthConstraint && capabilities.supportsWidth()) {
if (std::isinf(fitnessDistance(*widthConstraint))) {
badConstraint = widthConstraint->name();
return false;
}
auto range = capabilities.width();
width = widthConstraint->valueForCapabilityRange(size().width(), range.rangeMin().asInt, range.rangeMax().asInt);
}
std::optional<int> height;
if (heightConstraint && capabilities.supportsHeight()) {
if (std::isinf(fitnessDistance(*heightConstraint))) {
badConstraint = heightConstraint->name();
return false;
}
auto range = capabilities.height();
height = heightConstraint->valueForCapabilityRange(size().height(), range.rangeMin().asInt, range.rangeMax().asInt);
}
std::optional<double> frameRate;
if (frameRateConstraint && capabilities.supportsFrameRate()) {
if (std::isinf(fitnessDistance(*frameRateConstraint))) {
badConstraint = frameRateConstraint->name();
return false;
}
auto range = capabilities.frameRate();
frameRate = frameRateConstraint->valueForCapabilityRange(this->frameRate(), range.rangeMin().asDouble, range.rangeMax().asDouble);
}
// Each of the values is supported individually, see if they all can be applied at the same time.
if (!supportsSizeAndFrameRate(WTFMove(width), WTFMove(height), WTFMove(frameRate))) {
if (widthConstraint)
badConstraint = widthConstraint->name();
else if (heightConstraint)
badConstraint = heightConstraint->name();
else
badConstraint = frameRateConstraint->name();
return false;
}
return true;
}
double RealtimeMediaSource::fitnessDistance(const MediaConstraint& constraint)
{
ASSERT(this->capabilities());
RealtimeMediaSourceCapabilities& capabilities = *this->capabilities();
switch (constraint.constraintType()) {
case MediaConstraintType::Width: {
ASSERT(constraint.isInt());
if (!capabilities.supportsWidth())
return 0;
auto range = capabilities.width();
return downcast<IntConstraint>(constraint).fitnessDistance(range.rangeMin().asInt, range.rangeMax().asInt);
break;
}
case MediaConstraintType::Height: {
ASSERT(constraint.isInt());
if (!capabilities.supportsHeight())
return 0;
auto range = capabilities.height();
return downcast<IntConstraint>(constraint).fitnessDistance(range.rangeMin().asInt, range.rangeMax().asInt);
break;
}
case MediaConstraintType::FrameRate: {
ASSERT(constraint.isDouble());
if (!capabilities.supportsFrameRate())
return 0;
auto range = capabilities.frameRate();
return downcast<DoubleConstraint>(constraint).fitnessDistance(range.rangeMin().asDouble, range.rangeMax().asDouble);
break;
}
case MediaConstraintType::AspectRatio: {
ASSERT(constraint.isDouble());
if (!capabilities.supportsAspectRatio())
return 0;
auto range = capabilities.aspectRatio();
return downcast<DoubleConstraint>(constraint).fitnessDistance(range.rangeMin().asDouble, range.rangeMax().asDouble);
break;
}
case MediaConstraintType::Volume: {
ASSERT(constraint.isDouble());
if (!capabilities.supportsVolume())
return 0;
auto range = capabilities.volume();
return downcast<DoubleConstraint>(constraint).fitnessDistance(range.rangeMin().asDouble, range.rangeMax().asDouble);
break;
}
case MediaConstraintType::SampleRate: {
ASSERT(constraint.isInt());
if (!capabilities.supportsSampleRate())
return 0;
auto range = capabilities.sampleRate();
return downcast<IntConstraint>(constraint).fitnessDistance(range.rangeMin().asInt, range.rangeMax().asInt);
break;
}
case MediaConstraintType::SampleSize: {
ASSERT(constraint.isInt());
if (!capabilities.supportsSampleSize())
return 0;
auto range = capabilities.sampleSize();
return downcast<IntConstraint>(constraint).fitnessDistance(range.rangeMin().asInt, range.rangeMax().asInt);
break;
}
case MediaConstraintType::FacingMode: {
ASSERT(constraint.isString());
if (!capabilities.supportsFacingMode())
return 0;
auto& modes = capabilities.facingMode();
Vector<String> supportedModes;
supportedModes.reserveInitialCapacity(modes.size());
for (auto& mode : modes)
supportedModes.uncheckedAppend(RealtimeMediaSourceSettings::facingMode(mode));
return downcast<StringConstraint>(constraint).fitnessDistance(supportedModes);
break;
}
case MediaConstraintType::EchoCancellation: {
ASSERT(constraint.isBoolean());
if (!capabilities.supportsEchoCancellation())
return 0;
bool echoCancellationReadWrite = capabilities.echoCancellation() == RealtimeMediaSourceCapabilities::EchoCancellation::ReadWrite;
return downcast<BooleanConstraint>(constraint).fitnessDistance(echoCancellationReadWrite);
break;
}
case MediaConstraintType::DeviceId: {
ASSERT(constraint.isString());
if (!capabilities.supportsDeviceId())
return 0;
return downcast<StringConstraint>(constraint).fitnessDistance(m_id);
break;
}
case MediaConstraintType::GroupId: {
ASSERT(constraint.isString());
if (!capabilities.supportsDeviceId())
return 0;
return downcast<StringConstraint>(constraint).fitnessDistance(settings().groupId());
break;
}
case MediaConstraintType::Unknown:
// Unknown (or unsupported) constraints should be ignored.
break;
}
return 0;
}
template <typename ValueType>
static void applyNumericConstraint(const NumericConstraint<ValueType>& constraint, ValueType current, ValueType capabilityMin, ValueType capabilityMax, RealtimeMediaSource* source, void (RealtimeMediaSource::*applier)(ValueType))
{
ValueType value = constraint.valueForCapabilityRange(current, capabilityMin, capabilityMax);
if (value != current)
(source->*applier)(value);
}
void RealtimeMediaSource::applySizeAndFrameRate(std::optional<int> width, std::optional<int> height, std::optional<double> frameRate)
{
if (width)
setWidth(width.value());
if (height)
setHeight(height.value());
if (frameRate)
setFrameRate(frameRate.value());
}
void RealtimeMediaSource::applyConstraint(const MediaConstraint& constraint)
{
RealtimeMediaSourceCapabilities& capabilities = *this->capabilities();
switch (constraint.constraintType()) {
case MediaConstraintType::Width: {
ASSERT(constraint.isInt());
if (!capabilities.supportsWidth())
return;
auto range = capabilities.width();
applyNumericConstraint(downcast<IntConstraint>(constraint), size().width(), range.rangeMin().asInt, range.rangeMax().asInt, this, &RealtimeMediaSource::setWidth);
break;
}
case MediaConstraintType::Height: {
ASSERT(constraint.isInt());
if (!capabilities.supportsHeight())
return;
auto range = capabilities.height();
applyNumericConstraint(downcast<IntConstraint>(constraint), size().height(), range.rangeMin().asInt, range.rangeMax().asInt, this, &RealtimeMediaSource::setHeight);
break;
}
case MediaConstraintType::FrameRate: {
ASSERT(constraint.isDouble());
if (!capabilities.supportsFrameRate())
return;
auto range = capabilities.frameRate();
applyNumericConstraint(downcast<DoubleConstraint>(constraint), frameRate(), range.rangeMin().asDouble, range.rangeMax().asDouble, this, &RealtimeMediaSource::setFrameRate);
break;
}
case MediaConstraintType::AspectRatio: {
ASSERT(constraint.isDouble());
if (!capabilities.supportsAspectRatio())
return;
auto range = capabilities.aspectRatio();
applyNumericConstraint(downcast<DoubleConstraint>(constraint), aspectRatio(), range.rangeMin().asDouble, range.rangeMax().asDouble, this, &RealtimeMediaSource::setAspectRatio);
break;
}
case MediaConstraintType::Volume: {
ASSERT(constraint.isDouble());
if (!capabilities.supportsVolume())
return;
auto range = capabilities.volume();
applyNumericConstraint(downcast<DoubleConstraint>(constraint), volume(), range.rangeMin().asDouble, range.rangeMax().asDouble, this, &RealtimeMediaSource::setVolume);
break;
}
case MediaConstraintType::SampleRate: {
ASSERT(constraint.isInt());
if (!capabilities.supportsSampleRate())
return;
auto range = capabilities.sampleRate();
applyNumericConstraint(downcast<IntConstraint>(constraint), sampleRate(), range.rangeMin().asInt, range.rangeMax().asInt, this, &RealtimeMediaSource::setSampleRate);
break;
}
case MediaConstraintType::SampleSize: {
ASSERT(constraint.isInt());
if (!capabilities.supportsSampleSize())
return;
auto range = capabilities.sampleSize();
applyNumericConstraint(downcast<IntConstraint>(constraint), sampleSize(), range.rangeMin().asInt, range.rangeMax().asInt, this, &RealtimeMediaSource::setSampleSize);
break;
}
case MediaConstraintType::EchoCancellation: {
ASSERT(constraint.isBoolean());
if (!capabilities.supportsEchoCancellation())
return;
bool setting;
const BooleanConstraint& boolConstraint = downcast<BooleanConstraint>(constraint);
if (boolConstraint.getExact(setting) || boolConstraint.getIdeal(setting))
setEchoCancellation(setting);
break;
}
case MediaConstraintType::FacingMode: {
ASSERT(constraint.isString());
if (!capabilities.supportsFacingMode())
return;
auto& supportedModes = capabilities.facingMode();
auto filter = [supportedModes](const String& modeString) {
auto mode = RealtimeMediaSourceSettings::videoFacingModeEnum(modeString);
for (auto& supportedMode : supportedModes) {
if (mode == supportedMode)
return true;
}
return false;
};
auto modeString = downcast<StringConstraint>(constraint).find(filter);
if (!modeString.isEmpty())
setFacingMode(RealtimeMediaSourceSettings::videoFacingModeEnum(modeString));
break;
}
case MediaConstraintType::DeviceId:
case MediaConstraintType::GroupId:
ASSERT(constraint.isString());
// There is nothing to do here, neither can be changed.
break;
case MediaConstraintType::Unknown:
break;
}
}
bool RealtimeMediaSource::selectSettings(const MediaConstraints& constraints, FlattenedConstraint& candidates, String& failedConstraint)
{
// https://w3c.github.io/mediacapture-main/#dfn-selectsettings
//
// 1. Each constraint specifies one or more values (or a range of values) for its property.
// A property may appear more than once in the list of 'advanced' ConstraintSets. If an
// empty object or list has been given as the value for a constraint, it must be interpreted
// as if the constraint were not specified (in other words, an empty constraint == no constraint).
//
// Note that unknown properties are discarded by WebIDL, which means that unknown/unsupported required
// constraints will silently disappear. To avoid this being a surprise, application authors are
// expected to first use the getSupportedConstraints() method as shown in the Examples below.
// 2. Let object be the ConstrainablePattern object on which this algorithm is applied. Let copy be an
// unconstrained copy of object (i.e., copy should behave as if it were object with all ConstraintSets
// removed.)
// 3. For every possible settings dictionary of copy compute its fitness distance, treating bare values of
// properties as ideal values. Let candidates be the set of settings dictionaries for which the fitness
// distance is finite.
failedConstraint = emptyString();
// Check width, height, and frame rate separately, because while they may be supported individually the combination may not be supported.
if (!supportsSizeAndFrameRate(constraints.mandatoryConstraints().width(), constraints.mandatoryConstraints().height(), constraints.mandatoryConstraints().frameRate(), failedConstraint))
return false;
constraints.mandatoryConstraints().filter([&](const MediaConstraint& constraint) {
if (constraint.constraintType() == MediaConstraintType::Width || constraint.constraintType() == MediaConstraintType::Height || constraint.constraintType() == MediaConstraintType::FrameRate) {
candidates.set(constraint);
return false;
}
if (std::isinf(fitnessDistance(constraint))) {
failedConstraint = constraint.name();
return true;
}
candidates.set(constraint);
return false;
});
if (!failedConstraint.isEmpty())
return false;
// 4. If candidates is empty, return undefined as the result of the SelectSettings() algorithm.
if (candidates.isEmpty())
return true;
// 5. Iterate over the 'advanced' ConstraintSets in newConstraints in the order in which they were specified.
// For each ConstraintSet:
// 5.1 compute the fitness distance between it and each settings dictionary in candidates, treating bare
// values of properties as exact.
Vector<std::pair<double, MediaTrackConstraintSetMap>> supportedConstraints;
double minimumDistance = std::numeric_limits<double>::infinity();
for (const auto& advancedConstraint : constraints.advancedConstraints()) {
double constraintDistance = 0;
bool supported = false;
advancedConstraint.forEach([&](const MediaConstraint& constraint) {
double distance = fitnessDistance(constraint);
constraintDistance += distance;
if (!std::isinf(distance))
supported = true;
});
if (constraintDistance < minimumDistance)
minimumDistance = constraintDistance;
// 5.2 If the fitness distance is finite for one or more settings dictionaries in candidates, keep those
// settings dictionaries in candidates, discarding others.
// If the fitness distance is infinite for all settings dictionaries in candidates, ignore this ConstraintSet.
if (supported)
supportedConstraints.append({constraintDistance, advancedConstraint});
}
// 6. Select one settings dictionary from candidates, and return it as the result of the SelectSettings() algorithm.
// The UA should use the one with the smallest fitness distance, as calculated in step 3.
if (!std::isinf(minimumDistance)) {
supportedConstraints.removeAllMatching([&](const std::pair<double, MediaTrackConstraintSetMap>& pair) -> bool {
return pair.first > minimumDistance;
});
if (!supportedConstraints.isEmpty()) {
auto& advancedConstraint = supportedConstraints[0].second;
advancedConstraint.forEach([&](const MediaConstraint& constraint) {
candidates.merge(constraint);
});
}
}
return true;
}
bool RealtimeMediaSource::supportsConstraints(const MediaConstraints& constraints, String& invalidConstraint)
{
ASSERT(constraints.isValid());
FlattenedConstraint candidates;
if (!selectSettings(constraints, candidates, invalidConstraint))
return false;
return true;
}
void RealtimeMediaSource::applyConstraints(const FlattenedConstraint& constraints)
{
if (constraints.isEmpty())
return;
beginConfiguration();
RealtimeMediaSourceCapabilities& capabilities = *this->capabilities();
std::optional<int> width;
if (const MediaConstraint* constraint = constraints.find(MediaConstraintType::Width)) {
ASSERT(constraint->isInt());
if (capabilities.supportsWidth()) {
auto range = capabilities.width();
width = downcast<IntConstraint>(*constraint).valueForCapabilityRange(size().width(), range.rangeMin().asInt, range.rangeMax().asInt);
}
}
std::optional<int> height;
if (const MediaConstraint* constraint = constraints.find(MediaConstraintType::Height)) {
ASSERT(constraint->isInt());
if (capabilities.supportsHeight()) {
auto range = capabilities.height();
height = downcast<IntConstraint>(*constraint).valueForCapabilityRange(size().height(), range.rangeMin().asInt, range.rangeMax().asInt);
}
}
std::optional<double> frameRate;
if (const MediaConstraint* constraint = constraints.find(MediaConstraintType::FrameRate)) {
ASSERT(constraint->isDouble());
if (capabilities.supportsFrameRate()) {
auto range = capabilities.frameRate();
frameRate = downcast<DoubleConstraint>(*constraint).valueForCapabilityRange(this->frameRate(), range.rangeMin().asDouble, range.rangeMax().asDouble);
}
}
if (width || height || frameRate)
applySizeAndFrameRate(WTFMove(width), WTFMove(height), WTFMove(frameRate));
for (auto& variant : constraints) {
if (variant.constraintType() == MediaConstraintType::Width || variant.constraintType() == MediaConstraintType::Height || variant.constraintType() == MediaConstraintType::FrameRate)
continue;
applyConstraint(variant);
}
commitConfiguration();
}
std::optional<std::pair<String, String>> RealtimeMediaSource::applyConstraints(const MediaConstraints& constraints)
{
ASSERT(constraints.isValid());
FlattenedConstraint candidates;
String failedConstraint;
if (!selectSettings(constraints, candidates, failedConstraint))
return { { failedConstraint, ASCIILiteral("Constraint not supported") } };
applyConstraints(candidates);
return std::nullopt;
}
void RealtimeMediaSource::applyConstraints(const MediaConstraints& constraints, SuccessHandler successHandler, FailureHandler failureHandler)
{
auto result = applyConstraints(constraints);
if (!result && successHandler)
successHandler();
else if (result && failureHandler)
failureHandler(result.value().first, result.value().second);
}
void RealtimeMediaSource::setWidth(int width)
{
if (width == m_size.width())
return;
int height = m_aspectRatio ? width / m_aspectRatio : m_size.height();
if (!applySize(IntSize(width, height)))
return;
m_size.setWidth(width);
if (m_aspectRatio)
m_size.setHeight(width / m_aspectRatio);
settingsDidChange();
}
void RealtimeMediaSource::setHeight(int height)
{
if (height == m_size.height())
return;
int width = m_aspectRatio ? height * m_aspectRatio : m_size.width();
if (!applySize(IntSize(width, height)))
return;
if (m_aspectRatio)
m_size.setWidth(width);
m_size.setHeight(height);
settingsDidChange();
}
void RealtimeMediaSource::setFrameRate(double rate)
{
if (m_frameRate == rate || !applyFrameRate(rate))
return;
m_frameRate = rate;
settingsDidChange();
}
void RealtimeMediaSource::setAspectRatio(double ratio)
{
if (m_aspectRatio == ratio || !applyAspectRatio(ratio))
return;
m_aspectRatio = ratio;
m_size.setHeight(m_size.width() / ratio);
settingsDidChange();
}
void RealtimeMediaSource::setFacingMode(RealtimeMediaSourceSettings::VideoFacingMode mode)
{
if (m_facingMode == mode || !applyFacingMode(mode))
return;
m_facingMode = mode;
settingsDidChange();
}
void RealtimeMediaSource::setVolume(double volume)
{
if (m_volume == volume || !applyVolume(volume))
return;
m_volume = volume;
settingsDidChange();
}
void RealtimeMediaSource::setSampleRate(int rate)
{
if (m_sampleRate == rate || !applySampleRate(rate))
return;
m_sampleRate = rate;
settingsDidChange();
}
void RealtimeMediaSource::setSampleSize(int size)
{
if (m_sampleSize == size || !applySampleSize(size))
return;
m_sampleSize = size;
settingsDidChange();
}
void RealtimeMediaSource::setEchoCancellation(bool echoCancellation)
{
if (m_echoCancellation == echoCancellation || !applyEchoCancellation(echoCancellation))
return;
m_echoCancellation = echoCancellation;
settingsDidChange();
}
void RealtimeMediaSource::scheduleDeferredTask(std::function<void()>&& function)
{
ASSERT(function);
callOnMainThread([weakThis = createWeakPtr(), function = WTFMove(function)] {
if (!weakThis)
return;
function();
});
}
} // namespace WebCore
#endif // ENABLE(MEDIA_STREAM)
|