2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "modules/mediasource/WebKitSourceBuffer.h"
34 #include "bindings/v8/ExceptionState.h"
35 #include "core/dom/ExceptionCode.h"
36 #include "core/html/TimeRanges.h"
37 #include "modules/mediasource/WebKitMediaSource.h"
38 #include "platform/TraceEvent.h"
39 #include "public/platform/WebSourceBuffer.h"
40 #include "wtf/Uint8Array.h"
42 using blink::WebSourceBuffer;
46 DEFINE_GC_INFO(WebKitSourceBuffer);
48 PassRefPtrWillBeRawPtr<WebKitSourceBuffer> WebKitSourceBuffer::create(PassOwnPtr<WebSourceBuffer> webSourceBuffer, PassRefPtrWillBeRawPtr<WebKitMediaSource> source)
50 return adoptRefWillBeNoop(new WebKitSourceBuffer(webSourceBuffer, source));
53 WebKitSourceBuffer::WebKitSourceBuffer(PassOwnPtr<WebSourceBuffer> webSourceBuffer, PassRefPtrWillBeRawPtr<WebKitMediaSource> source)
54 : m_webSourceBuffer(webSourceBuffer)
56 , m_timestampOffset(0)
58 ASSERT(m_webSourceBuffer);
60 ScriptWrappable::init(this);
63 WebKitSourceBuffer::~WebKitSourceBuffer()
67 PassRefPtr<TimeRanges> WebKitSourceBuffer::buffered(ExceptionState& exceptionState) const
69 // Section 3.1 buffered attribute steps.
70 // 1. If this object has been removed from the sourceBuffers attribute of the parent media source then throw an
71 // InvalidStateError exception and abort these steps.
73 exceptionState.throwUninformativeAndGenericDOMException(InvalidStateError);
77 // 2. Return a new static normalized TimeRanges object for the media segments buffered.
78 return TimeRanges::create(m_webSourceBuffer->buffered());
81 double WebKitSourceBuffer::timestampOffset() const
83 return m_timestampOffset;
86 void WebKitSourceBuffer::setTimestampOffset(double offset, ExceptionState& exceptionState)
88 // Section 3.1 timestampOffset attribute setter steps.
89 // 1. If this object has been removed from the sourceBuffers attribute of the parent media source then throw an
90 // InvalidStateError exception and abort these steps.
92 exceptionState.throwUninformativeAndGenericDOMException(InvalidStateError);
96 // 4. If the readyState attribute of the parent media source is in the "ended" state then run the following steps:
97 // 4.1 Set the readyState attribute of the parent media source to "open"
98 // 4.2 Queue a task to fire a simple event named sourceopen at the parent media source.
99 m_source->openIfInEndedState();
101 // 5. If this object is waiting for the end of a media segment to be appended, then throw an InvalidStateError
102 // and abort these steps.
103 if (!m_webSourceBuffer->setTimestampOffset(offset)) {
104 exceptionState.throwUninformativeAndGenericDOMException(InvalidStateError);
108 // 6. Update the attribute to the new value.
109 m_timestampOffset = offset;
112 void WebKitSourceBuffer::append(PassRefPtr<Uint8Array> data, ExceptionState& exceptionState)
114 TRACE_EVENT0("media", "SourceBuffer::append");
116 // SourceBuffer.append() steps from October 1st version of the Media Source Extensions spec.
117 // https://dvcs.w3.org/hg/html-media/raw-file/7bab66368f2c/media-source/media-source.html#dom-append
119 // 2. If data is null then throw an InvalidAccessError exception and abort these steps.
121 exceptionState.throwUninformativeAndGenericDOMException(InvalidAccessError);
125 // 3. If this object has been removed from the sourceBuffers attribute of media source then throw
126 // an InvalidStateError exception and abort these steps.
128 exceptionState.throwUninformativeAndGenericDOMException(InvalidStateError);
132 // 5. If the readyState attribute of media source is in the "ended" state then run the following steps:
133 // 5.1. Set the readyState attribute of media source to "open"
134 // 5.2. Queue a task to fire a simple event named sourceopen at media source.
135 m_source->openIfInEndedState();
137 // Steps 6 & beyond are handled by m_webSourceBuffer.
138 m_webSourceBuffer->append(data->data(), data->length());
141 void WebKitSourceBuffer::abort(ExceptionState& exceptionState)
143 // Section 3.2 abort() method steps.
144 // 1. If this object has been removed from the sourceBuffers attribute of the parent media source
145 // then throw an InvalidStateError exception and abort these steps.
146 // 2. If the readyState attribute of the parent media source is not in the "open" state
147 // then throw an InvalidStateError exception and abort these steps.
148 if (isRemoved() || !m_source->isOpen()) {
149 exceptionState.throwUninformativeAndGenericDOMException(InvalidStateError);
153 // 4. Run the reset parser state algorithm.
154 m_webSourceBuffer->abort();
157 void WebKitSourceBuffer::removedFromMediaSource()
162 m_webSourceBuffer->removedFromMediaSource();
166 bool WebKitSourceBuffer::isRemoved() const
171 void WebKitSourceBuffer::trace(Visitor* visitor)
173 visitor->trace(m_source);
176 } // namespace WebCore