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
|
/*
* Copyright (C) 2013 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * 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.
* * 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"
#include "modules/mediasource/MediaSource.h"
#include "core/dom/ExceptionCodePlaceholder.h"
#include "core/dom/GenericEventQueue.h"
#include "core/html/TimeRanges.h"
#include "core/platform/ContentType.h"
#include "core/platform/Logging.h"
#include "core/platform/MIMETypeRegistry.h"
#include "core/platform/graphics/SourceBufferPrivate.h"
#include "modules/mediasource/MediaSourceRegistry.h"
#include "wtf/Uint8Array.h"
namespace WebCore {
PassRefPtr<MediaSource> MediaSource::create(ScriptExecutionContext* context)
{
RefPtr<MediaSource> mediaSource(adoptRef(new MediaSource(context)));
mediaSource->suspendIfNeeded();
return mediaSource.release();
}
MediaSource::MediaSource(ScriptExecutionContext* context)
: MediaSourceBase(context)
{
LOG(Media, "MediaSource::MediaSource %p", this);
ScriptWrappable::init(this);
m_sourceBuffers = SourceBufferList::create(scriptExecutionContext(), asyncEventQueue());
m_activeSourceBuffers = SourceBufferList::create(scriptExecutionContext(), asyncEventQueue());
}
MediaSource::~MediaSource()
{
LOG(Media, "MediaSource::~MediaSource %p", this);
ASSERT(isClosed());
}
SourceBuffer* MediaSource::addSourceBuffer(const String& type, ExceptionCode& ec)
{
LOG(Media, "MediaSource::addSourceBuffer(%s) %p", type.ascii().data(), this);
// 2.2 https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#widl-MediaSource-addSourceBuffer-SourceBuffer-DOMString-type
// 1. If type is null or an empty then throw an InvalidAccessError exception and
// abort these steps.
if (type.isNull() || type.isEmpty()) {
ec = InvalidAccessError;
return 0;
}
// 2. If type contains a MIME type that is not supported ..., then throw a
// NotSupportedError exception and abort these steps.
if (!isTypeSupported(type)) {
ec = NotSupportedError;
return 0;
}
// 4. If the readyState attribute is not in the "open" state then throw an
// InvalidStateError exception and abort these steps.
if (!isOpen()) {
ec = InvalidStateError;
return 0;
}
// 5. Create a new SourceBuffer object and associated resources.
ContentType contentType(type);
Vector<String> codecs = contentType.codecs();
OwnPtr<SourceBufferPrivate> sourceBufferPrivate = createSourceBufferPrivate(contentType.type(), codecs, ec);
if (!sourceBufferPrivate) {
ASSERT(ec == NotSupportedError || ec == QuotaExceededError);
// 2. If type contains a MIME type that is not supported ..., then throw a NotSupportedError exception and abort these steps.
// 3. If the user agent can't handle any more SourceBuffer objects then throw a QuotaExceededError exception and abort these steps
return 0;
}
RefPtr<SourceBuffer> buffer = SourceBuffer::create(sourceBufferPrivate.release(), this, asyncEventQueue());
// 6. Add the new object to sourceBuffers and fire a addsourcebuffer on that object.
m_sourceBuffers->add(buffer);
m_activeSourceBuffers->add(buffer);
// 7. Return the new object to the caller.
return buffer.get();
}
void MediaSource::removeSourceBuffer(SourceBuffer* buffer, ExceptionCode& ec)
{
LOG(Media, "MediaSource::removeSourceBuffer() %p", this);
RefPtr<SourceBuffer> protect(buffer);
// 2.2 https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#widl-MediaSource-removeSourceBuffer-void-SourceBuffer-sourceBuffer
// 1. If sourceBuffer is null then throw an InvalidAccessError exception and
// abort these steps.
if (!buffer) {
ec = InvalidAccessError;
return;
}
// 2. If sourceBuffer specifies an object that is not in sourceBuffers then
// throw a NotFoundError exception and abort these steps.
if (!m_sourceBuffers->length() || !m_sourceBuffers->contains(buffer)) {
ec = NotFoundError;
return;
}
// 3. If the sourceBuffer.updating attribute equals true, then run the following steps: ...
buffer->abortIfUpdating();
// Steps 4-9 are related to updating audioTracks, videoTracks, and textTracks which aren't implmented yet.
// FIXME(91649): support track selection
// 10. If sourceBuffer is in activeSourceBuffers, then remove sourceBuffer from activeSourceBuffers ...
m_activeSourceBuffers->remove(buffer);
// 11. Remove sourceBuffer from sourceBuffers and fire a removesourcebuffer event
// on that object.
m_sourceBuffers->remove(buffer);
// 12. Destroy all resources for sourceBuffer.
buffer->removedFromMediaSource();
}
void MediaSource::onReadyStateChange(const AtomicString& oldState, const AtomicString& newState)
{
if (isOpen()) {
scheduleEvent(eventNames().sourceopenEvent);
return;
}
if (oldState == openKeyword() && newState == endedKeyword()) {
scheduleEvent(eventNames().sourceendedEvent);
return;
}
ASSERT(isClosed());
m_activeSourceBuffers->clear();
// Clear SourceBuffer references to this object.
for (unsigned long i = 0; i < m_sourceBuffers->length(); ++i)
m_sourceBuffers->item(i)->removedFromMediaSource();
m_sourceBuffers->clear();
scheduleEvent(eventNames().sourcecloseEvent);
}
Vector<RefPtr<TimeRanges> > MediaSource::activeRanges() const
{
Vector<RefPtr<TimeRanges> > activeRanges(m_activeSourceBuffers->length());
for (size_t i = 0; i < m_activeSourceBuffers->length(); ++i)
activeRanges[i] = m_activeSourceBuffers->item(i)->buffered(ASSERT_NO_EXCEPTION);
return activeRanges;
}
bool MediaSource::isTypeSupported(const String& type)
{
LOG(Media, "MediaSource::isTypeSupported(%s)", type.ascii().data());
// Section 2.2 isTypeSupported() method steps.
// https://dvcs.w3.org/hg/html-media/raw-file/tip/media-source/media-source.html#widl-MediaSource-isTypeSupported-boolean-DOMString-type
// 1. If type is an empty string, then return false.
if (type.isNull() || type.isEmpty())
return false;
ContentType contentType(type);
String codecs = contentType.parameter("codecs");
// 2. If type does not contain a valid MIME type string, then return false.
if (contentType.type().isEmpty() || codecs.isEmpty())
return false;
// 3. If type contains a media type or media subtype that the MediaSource does not support, then return false.
// 4. If type contains at a codec that the MediaSource does not support, then return false.
// 5. If the MediaSource does not support the specified combination of media type, media subtype, and codecs then return false.
// 6. Return true.
return MIMETypeRegistry::isSupportedMediaSourceMIMEType(contentType.type(), codecs);
}
const AtomicString& MediaSource::interfaceName() const
{
return eventNames().interfaceForMediaSource;
}
} // namespace WebCore
|