OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * |
| 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are |
| 6 * met: |
| 7 * |
| 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above |
| 11 * copyright notice, this list of conditions and the following disclaimer |
| 12 * in the documentation and/or other materials provided with the |
| 13 * distribution. |
| 14 * * Neither the name of Google Inc. nor the names of its |
| 15 * contributors may be used to endorse or promote products derived from |
| 16 * this software without specific prior written permission. |
| 17 * |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ |
| 30 |
| 31 #include "config.h" |
| 32 #include "modules/mediasource/SourceBuffer.h" |
| 33 |
| 34 #include "core/dom/Event.h" |
| 35 #include "core/dom/GenericEventQueue.h" |
| 36 #include "core/html/TimeRanges.h" |
| 37 #include "core/platform/Logging.h" |
| 38 #include "core/platform/graphics/SourceBufferPrivate.h" |
| 39 #include "modules/mediasource/MediaSource.h" |
| 40 #include "wtf/ArrayBuffer.h" |
| 41 #include "wtf/ArrayBufferView.h" |
| 42 |
| 43 namespace WebCore { |
| 44 |
| 45 PassRefPtr<SourceBuffer> SourceBuffer::create(PassOwnPtr<SourceBufferPrivate> so
urceBufferPrivate, MediaSource* source, GenericEventQueue* asyncEventQueue) |
| 46 { |
| 47 RefPtr<SourceBuffer> sourceBuffer(adoptRef(new SourceBuffer(sourceBufferPriv
ate, source, asyncEventQueue))); |
| 48 sourceBuffer->suspendIfNeeded(); |
| 49 return sourceBuffer.release(); |
| 50 } |
| 51 |
| 52 SourceBuffer::SourceBuffer(PassOwnPtr<SourceBufferPrivate> sourceBufferPrivate,
MediaSource* source, GenericEventQueue* asyncEventQueue) |
| 53 : ActiveDOMObject(source->scriptExecutionContext()) |
| 54 , m_private(sourceBufferPrivate) |
| 55 , m_source(source) |
| 56 , m_asyncEventQueue(asyncEventQueue) |
| 57 , m_updating(false) |
| 58 , m_timestampOffset(0) |
| 59 , m_appendBufferTimer(this, &SourceBuffer::appendBufferTimerFired) |
| 60 { |
| 61 ASSERT(m_private); |
| 62 ASSERT(m_source); |
| 63 ScriptWrappable::init(this); |
| 64 } |
| 65 |
| 66 SourceBuffer::~SourceBuffer() |
| 67 { |
| 68 ASSERT(isRemoved()); |
| 69 } |
| 70 |
| 71 PassRefPtr<TimeRanges> SourceBuffer::buffered(ExceptionCode& ec) const |
| 72 { |
| 73 // Section 3.1 buffered attribute steps. |
| 74 // 1. If this object has been removed from the sourceBuffers attribute of th
e parent media source then throw an |
| 75 // INVALID_STATE_ERR exception and abort these steps. |
| 76 if (isRemoved()) { |
| 77 ec = INVALID_STATE_ERR; |
| 78 return 0; |
| 79 } |
| 80 |
| 81 // 2. Return a new static normalized TimeRanges object for the media segment
s buffered. |
| 82 return m_private->buffered(); |
| 83 } |
| 84 |
| 85 double SourceBuffer::timestampOffset() const |
| 86 { |
| 87 return m_timestampOffset; |
| 88 } |
| 89 |
| 90 void SourceBuffer::setTimestampOffset(double offset, ExceptionCode& ec) |
| 91 { |
| 92 // Section 3.1 timestampOffset attribute setter steps. |
| 93 // 1. If this object has been removed from the sourceBuffers attribute of th
e parent media source then throw an |
| 94 // INVALID_STATE_ERR exception and abort these steps. |
| 95 if (isRemoved()) { |
| 96 ec = INVALID_STATE_ERR; |
| 97 return; |
| 98 } |
| 99 |
| 100 // 4. If the readyState attribute of the parent media source is in the "ende
d" state then run the following steps: |
| 101 // 4.1 Set the readyState attribute of the parent media source to "open" |
| 102 // 4.2 Queue a task to fire a simple event named sourceopen at the parent me
dia source. |
| 103 m_source->openIfInEndedState(); |
| 104 |
| 105 // 5. If this object is waiting for the end of a media segment to be appende
d, then throw an INVALID_STATE_ERR |
| 106 // and abort these steps. |
| 107 if (!m_private->setTimestampOffset(offset)) { |
| 108 ec = INVALID_STATE_ERR; |
| 109 return; |
| 110 } |
| 111 |
| 112 // 6. Update the attribute to the new value. |
| 113 m_timestampOffset = offset; |
| 114 } |
| 115 |
| 116 void SourceBuffer::appendBuffer(PassRefPtr<ArrayBuffer> data, ExceptionCode& ec) |
| 117 { |
| 118 // Section 3.2 appendBuffer() |
| 119 // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-sou
rce.html#widl-SourceBuffer-appendBuffer-void-ArrayBufferView-data |
| 120 // 1. If data is null then throw an INVALID_ACCESS_ERR exception and abort t
hese steps. |
| 121 if (!data) { |
| 122 ec = INVALID_ACCESS_ERR; |
| 123 return; |
| 124 } |
| 125 |
| 126 appendBufferInternal(static_cast<unsigned char*>(data->data()), data->byteLe
ngth(), ec); |
| 127 } |
| 128 |
| 129 void SourceBuffer::appendBuffer(PassRefPtr<ArrayBufferView> data, ExceptionCode&
ec) |
| 130 { |
| 131 // Section 3.2 appendBuffer() |
| 132 // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-sou
rce.html#widl-SourceBuffer-appendBuffer-void-ArrayBufferView-data |
| 133 // 1. If data is null then throw an INVALID_ACCESS_ERR exception and abort t
hese steps. |
| 134 if (!data) { |
| 135 ec = INVALID_ACCESS_ERR; |
| 136 return; |
| 137 } |
| 138 |
| 139 appendBufferInternal(static_cast<unsigned char*>(data->baseAddress()), data-
>byteLength(), ec); |
| 140 } |
| 141 |
| 142 void SourceBuffer::abort(ExceptionCode& ec) |
| 143 { |
| 144 // Section 3.2 abort() method steps. |
| 145 // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-sou
rce.html#widl-SourceBuffer-abort-void |
| 146 // 1. If this object has been removed from the sourceBuffers attribute of th
e parent media source |
| 147 // then throw an INVALID_STATE_ERR exception and abort these steps. |
| 148 // 2. If the readyState attribute of the parent media source is not in the "
open" state |
| 149 // then throw an INVALID_STATE_ERR exception and abort these steps. |
| 150 if (isRemoved() || !m_source->isOpen()) { |
| 151 ec = INVALID_STATE_ERR; |
| 152 return; |
| 153 } |
| 154 |
| 155 // 3. If the sourceBuffer.updating attribute equals true, then run the follo
wing steps: ... |
| 156 abortIfUpdating(); |
| 157 |
| 158 // 4. Run the reset parser state algorithm. |
| 159 m_private->abort(); |
| 160 |
| 161 // FIXME(229408) Add steps 5-6 update appendWindowStart & appendWindowEnd. |
| 162 } |
| 163 |
| 164 |
| 165 void SourceBuffer::abortIfUpdating() |
| 166 { |
| 167 // Section 3.2 abort() method step 3 substeps. |
| 168 // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-sou
rce.html#widl-SourceBuffer-abort-void |
| 169 |
| 170 if (!m_updating) |
| 171 return; |
| 172 |
| 173 // 3.1. Abort the buffer append and stream append loop algorithms if they ar
e running. |
| 174 m_appendBufferTimer.stop(); |
| 175 m_pendingAppendData.clear(); |
| 176 |
| 177 // 3.2. Set the updating attribute to false. |
| 178 m_updating = false; |
| 179 |
| 180 // 3.3. Queue a task to fire a simple event named abort at this SourceBuffer
object. |
| 181 scheduleEvent(eventNames().abortEvent); |
| 182 |
| 183 // 3.4. Queue a task to fire a simple event named updateend at this SourceBu
ffer object. |
| 184 scheduleEvent(eventNames().updateendEvent); |
| 185 } |
| 186 |
| 187 void SourceBuffer::removedFromMediaSource() |
| 188 { |
| 189 if (isRemoved()) |
| 190 return; |
| 191 |
| 192 m_private->removedFromMediaSource(); |
| 193 m_source = 0; |
| 194 m_asyncEventQueue = 0; |
| 195 } |
| 196 |
| 197 bool SourceBuffer::hasPendingActivity() const |
| 198 { |
| 199 return m_source; |
| 200 } |
| 201 |
| 202 void SourceBuffer::stop() |
| 203 { |
| 204 m_appendBufferTimer.stop(); |
| 205 } |
| 206 |
| 207 ScriptExecutionContext* SourceBuffer::scriptExecutionContext() const |
| 208 { |
| 209 return ActiveDOMObject::scriptExecutionContext(); |
| 210 } |
| 211 |
| 212 const AtomicString& SourceBuffer::interfaceName() const |
| 213 { |
| 214 return eventNames().interfaceForSourceBuffer; |
| 215 } |
| 216 |
| 217 EventTargetData* SourceBuffer::eventTargetData() |
| 218 { |
| 219 return &m_eventTargetData; |
| 220 } |
| 221 |
| 222 EventTargetData* SourceBuffer::ensureEventTargetData() |
| 223 { |
| 224 return &m_eventTargetData; |
| 225 } |
| 226 |
| 227 bool SourceBuffer::isRemoved() const |
| 228 { |
| 229 return !m_source; |
| 230 } |
| 231 |
| 232 void SourceBuffer::scheduleEvent(const AtomicString& eventName) |
| 233 { |
| 234 ASSERT(m_asyncEventQueue); |
| 235 |
| 236 RefPtr<Event> event = Event::create(eventName, false, false); |
| 237 event->setTarget(this); |
| 238 |
| 239 m_asyncEventQueue->enqueueEvent(event.release()); |
| 240 } |
| 241 |
| 242 void SourceBuffer::appendBufferInternal(unsigned char* data, unsigned size, Exce
ptionCode& ec) |
| 243 { |
| 244 // Section 3.2 appendBuffer() |
| 245 // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-sou
rce.html#widl-SourceBuffer-appendBuffer-void-ArrayBufferView-data |
| 246 |
| 247 // Step 1 is enforced by the caller. |
| 248 // 2. If this object has been removed from the sourceBuffers attribute of th
e parent media source then throw an INVALID_STATE_ERR exception and abort these
steps. |
| 249 // 3. If the updating attribute equals true, then throw an INVALID_STATE_ERR
exception and abort these steps. |
| 250 if (isRemoved() || m_updating) { |
| 251 ec = INVALID_STATE_ERR; |
| 252 return; |
| 253 } |
| 254 |
| 255 // 4. If the readyState attribute of the parent media source is in the "ende
d" state then run the following steps: ... |
| 256 m_source->openIfInEndedState(); |
| 257 |
| 258 // Steps 5-6 |
| 259 |
| 260 // 7. Add data to the end of the input buffer. |
| 261 m_pendingAppendData.append(data, size); |
| 262 |
| 263 // 8. Set the updating attribute to true. |
| 264 m_updating = true; |
| 265 |
| 266 // 9. Queue a task to fire a simple event named updatestart at this SourceBu
ffer object. |
| 267 scheduleEvent(eventNames().updatestartEvent); |
| 268 |
| 269 // 10. Asynchronously run the buffer append algorithm. |
| 270 m_appendBufferTimer.startOneShot(0); |
| 271 } |
| 272 |
| 273 void SourceBuffer::appendBufferTimerFired(Timer<SourceBuffer>*) |
| 274 { |
| 275 ASSERT(m_updating); |
| 276 |
| 277 // Section 3.5.4 Buffer Append Algorithm |
| 278 // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-sou
rce.html#sourcebuffer-buffer-append |
| 279 |
| 280 // 1. Run the segment parser loop algorithm. |
| 281 // Step 2 doesn't apply since we run Step 1 synchronously here. |
| 282 m_private->append(&m_pendingAppendData[0], m_pendingAppendData.size()); |
| 283 |
| 284 |
| 285 // 3. Set the updating attribute to false. |
| 286 m_updating = false; |
| 287 m_pendingAppendData.clear(); |
| 288 |
| 289 // 4. Queue a task to fire a simple event named update at this SourceBuffer
object. |
| 290 scheduleEvent(eventNames().updateEvent); |
| 291 |
| 292 // 5. Queue a task to fire a simple event named updateend at this SourceBuff
er object. |
| 293 scheduleEvent(eventNames().updateendEvent); |
| 294 } |
| 295 |
| 296 } // namespace WebCore |
OLD | NEW |