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
|
/*
* Copyright (C) 2011 Google Inc. All rights reserved.
* Copyright (C) 2011, 2015 Ericsson AB. All rights reserved.
* Copyright (C) 2013-2016 Apple Inc. All rights reserved.
* Copyright (C) 2013 Nokia Corporation and/or its subsidiary(-ies).
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS 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 APPLE INC. OR ITS 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"
#include "MediaStreamTrack.h"
#if ENABLE(MEDIA_STREAM)
#include "Event.h"
#include "EventNames.h"
#include "JSOverconstrainedError.h"
#include "MediaConstraintsImpl.h"
#include "MediaStream.h"
#include "MediaStreamPrivate.h"
#include "NotImplemented.h"
#include "OverconstrainedError.h"
#include "ScriptExecutionContext.h"
#include <wtf/NeverDestroyed.h>
namespace WebCore {
Ref<MediaStreamTrack> MediaStreamTrack::create(ScriptExecutionContext& context, Ref<MediaStreamTrackPrivate>&& privateTrack)
{
return adoptRef(*new MediaStreamTrack(context, WTFMove(privateTrack)));
}
MediaStreamTrack::MediaStreamTrack(ScriptExecutionContext& context, Ref<MediaStreamTrackPrivate>&& privateTrack)
: ActiveDOMObject(&context)
, m_private(WTFMove(privateTrack))
, m_weakPtrFactory(this)
{
suspendIfNeeded();
m_private->addObserver(*this);
}
MediaStreamTrack::~MediaStreamTrack()
{
m_private->removeObserver(*this);
}
const AtomicString& MediaStreamTrack::kind() const
{
static NeverDestroyed<AtomicString> audioKind("audio", AtomicString::ConstructFromLiteral);
static NeverDestroyed<AtomicString> videoKind("video", AtomicString::ConstructFromLiteral);
if (m_private->type() == RealtimeMediaSource::Audio)
return audioKind;
return videoKind;
}
const String& MediaStreamTrack::id() const
{
return m_private->id();
}
const String& MediaStreamTrack::label() const
{
return m_private->label();
}
bool MediaStreamTrack::enabled() const
{
return m_private->enabled();
}
void MediaStreamTrack::setEnabled(bool enabled)
{
m_private->setEnabled(enabled);
}
bool MediaStreamTrack::muted() const
{
return m_private->muted();
}
bool MediaStreamTrack::readonly() const
{
return m_private->readonly();
}
bool MediaStreamTrack::remote() const
{
return m_private->remote();
}
auto MediaStreamTrack::readyState() const -> State
{
return ended() ? State::Ended : State::Live;
}
bool MediaStreamTrack::ended() const
{
return m_ended || m_private->ended();
}
Ref<MediaStreamTrack> MediaStreamTrack::clone()
{
return MediaStreamTrack::create(*scriptExecutionContext(), m_private->clone());
}
void MediaStreamTrack::stopProducingData()
{
// NOTE: this method is called when the "stop" method is called from JS, using
// the "ImplementedAs" IDL attribute. This is done because ActiveDOMObject requires
// a "stop" method.
// http://w3c.github.io/mediacapture-main/#widl-MediaStreamTrack-stop-void
// 4.3.3.2 Methods
// When a MediaStreamTrack object's stop() method is invoked, the User Agent must run following steps:
// 1. Let track be the current MediaStreamTrack object.
// 2. If track is sourced by a non-local source, then abort these steps.
if (remote() || ended())
return;
// 3. Notify track's source that track is ended so that the source may be stopped, unless other
// MediaStreamTrack objects depend on it.
// 4. Set track's readyState attribute to ended.
// Set m_ended to true before telling the private to stop so we do not fire an 'ended' event.
m_ended = true;
m_private->endTrack();
}
MediaStreamTrack::TrackSettings MediaStreamTrack::getSettings() const
{
auto& settings = m_private->settings();
TrackSettings result;
if (settings.supportsWidth())
result.width = settings.width();
if (settings.supportsHeight())
result.height = settings.height();
if (settings.supportsAspectRatio() && settings.aspectRatio()) // FIXME: Why the check for zero here?
result.aspectRatio = settings.aspectRatio();
if (settings.supportsFrameRate())
result.frameRate = settings.frameRate();
if (settings.supportsFacingMode())
result.facingMode = RealtimeMediaSourceSettings::facingMode(settings.facingMode());
if (settings.supportsVolume())
result.volume = settings.volume();
if (settings.supportsSampleRate())
result.sampleRate = settings.sampleRate();
if (settings.supportsSampleSize())
result.sampleSize = settings.sampleSize();
if (settings.supportsEchoCancellation())
result.echoCancellation = settings.echoCancellation();
if (settings.supportsDeviceId())
result.deviceId = settings.deviceId();
if (settings.supportsGroupId())
result.groupId = settings.groupId();
return result;
}
static DoubleRange capabilityDoubleRange(const CapabilityValueOrRange& value)
{
DoubleRange range;
switch (value.type()) {
case CapabilityValueOrRange::Double:
range.min = value.value().asDouble;
range.max = range.min;
break;
case CapabilityValueOrRange::DoubleRange:
range.min = value.rangeMin().asDouble;
range.max = value.rangeMax().asDouble;
break;
case CapabilityValueOrRange::Undefined:
case CapabilityValueOrRange::ULong:
case CapabilityValueOrRange::ULongRange:
ASSERT_NOT_REACHED();
}
return range;
}
static LongRange capabilityIntRange(const CapabilityValueOrRange& value)
{
LongRange range;
switch (value.type()) {
case CapabilityValueOrRange::ULong:
range.min = value.value().asInt;
range.max = range.min;
break;
case CapabilityValueOrRange::ULongRange:
range.min = value.rangeMin().asInt;
range.max = value.rangeMax().asInt;
break;
case CapabilityValueOrRange::Undefined:
case CapabilityValueOrRange::Double:
case CapabilityValueOrRange::DoubleRange:
ASSERT_NOT_REACHED();
}
return range;
}
static Vector<String> capabilityStringVector(const Vector<RealtimeMediaSourceSettings::VideoFacingMode>& modes)
{
Vector<String> result;
result.reserveCapacity(modes.size());
for (auto& mode : modes)
result.uncheckedAppend(RealtimeMediaSourceSettings::facingMode(mode));
return result;
}
static Vector<bool> capabilityBooleanVector(RealtimeMediaSourceCapabilities::EchoCancellation cancellation)
{
Vector<bool> result;
result.reserveCapacity(2);
result.uncheckedAppend(true);
result.uncheckedAppend(cancellation == RealtimeMediaSourceCapabilities::EchoCancellation::ReadWrite);
return result;
}
MediaStreamTrack::TrackCapabilities MediaStreamTrack::getCapabilities() const
{
auto capabilities = m_private->capabilities();
TrackCapabilities result;
if (capabilities->supportsWidth())
result.width = capabilityIntRange(capabilities->width());
if (capabilities->supportsHeight())
result.height = capabilityIntRange(capabilities->height());
if (capabilities->supportsAspectRatio())
result.aspectRatio = capabilityDoubleRange(capabilities->aspectRatio());
if (capabilities->supportsFrameRate())
result.frameRate = capabilityDoubleRange(capabilities->frameRate());
if (capabilities->supportsFacingMode())
result.facingMode = capabilityStringVector(capabilities->facingMode());
if (capabilities->supportsVolume())
result.volume = capabilityDoubleRange(capabilities->volume());
if (capabilities->supportsSampleRate())
result.sampleRate = capabilityIntRange(capabilities->sampleRate());
if (capabilities->supportsSampleSize())
result.sampleSize = capabilityIntRange(capabilities->sampleSize());
if (capabilities->supportsEchoCancellation())
result.echoCancellation = capabilityBooleanVector(capabilities->echoCancellation());
if (capabilities->supportsDeviceId())
result.deviceId = capabilities->deviceId();
if (capabilities->supportsGroupId())
result.groupId = capabilities->groupId();
return result;
}
static Ref<MediaConstraintsImpl> createMediaConstraintsImpl(const std::optional<MediaTrackConstraints>& constraints)
{
if (!constraints)
return MediaConstraintsImpl::create({ }, { }, true);
return createMediaConstraintsImpl(constraints.value());
}
void MediaStreamTrack::applyConstraints(const std::optional<MediaTrackConstraints>& constraints, DOMPromise<void>&& promise)
{
m_promise = WTFMove(promise);
auto weakThis = createWeakPtr();
auto failureHandler = [weakThis] (const String& failedConstraint, const String& message) {
if (!weakThis || !weakThis->m_promise)
return;
weakThis->m_promise->rejectType<IDLInterface<OverconstrainedError>>(OverconstrainedError::create(failedConstraint, message).get());
};
auto successHandler = [weakThis, constraints] () {
if (!weakThis || !weakThis->m_promise)
return;
weakThis->m_promise->resolve();
weakThis->m_constraints = constraints.value_or(MediaTrackConstraints { });
};
m_private->applyConstraints(createMediaConstraintsImpl(constraints), successHandler, failureHandler);
}
void MediaStreamTrack::addObserver(Observer& observer)
{
m_observers.append(&observer);
}
void MediaStreamTrack::removeObserver(Observer& observer)
{
m_observers.removeFirst(&observer);
}
void MediaStreamTrack::trackEnded(MediaStreamTrackPrivate&)
{
// http://w3c.github.io/mediacapture-main/#life-cycle
// When a MediaStreamTrack track ends for any reason other than the stop() method being invoked, the User Agent must queue a task that runs the following steps:
// 1. If the track's readyState attribute has the value ended already, then abort these steps.
if (m_ended)
return;
// 2. Set track's readyState attribute to ended.
m_ended = true;
if (scriptExecutionContext()->activeDOMObjectsAreSuspended() || scriptExecutionContext()->activeDOMObjectsAreStopped())
return;
// 3. Notify track's source that track is ended so that the source may be stopped, unless other MediaStreamTrack objects depend on it.
// 4. Fire a simple event named ended at the object.
dispatchEvent(Event::create(eventNames().endedEvent, false, false));
for (auto& observer : m_observers)
observer->trackDidEnd();
configureTrackRendering();
}
void MediaStreamTrack::trackMutedChanged(MediaStreamTrackPrivate&)
{
if (scriptExecutionContext()->activeDOMObjectsAreSuspended() || scriptExecutionContext()->activeDOMObjectsAreStopped())
return;
AtomicString eventType = muted() ? eventNames().muteEvent : eventNames().unmuteEvent;
dispatchEvent(Event::create(eventType, false, false));
configureTrackRendering();
}
void MediaStreamTrack::trackSettingsChanged(MediaStreamTrackPrivate&)
{
configureTrackRendering();
}
void MediaStreamTrack::trackEnabledChanged(MediaStreamTrackPrivate&)
{
configureTrackRendering();
}
void MediaStreamTrack::configureTrackRendering()
{
// 4.3.1
// ... media from the source only flows when a MediaStreamTrack object is both unmuted and enabled
}
void MediaStreamTrack::stop()
{
stopProducingData();
}
const char* MediaStreamTrack::activeDOMObjectName() const
{
return "MediaStreamTrack";
}
bool MediaStreamTrack::canSuspendForDocumentSuspension() const
{
// FIXME: We should try and do better here.
return false;
}
AudioSourceProvider* MediaStreamTrack::audioSourceProvider()
{
return m_private->audioSourceProvider();
}
} // namespace WebCore
#endif // ENABLE(MEDIA_STREAM)
|