Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / mediasource / WebKitSourceBuffer.cpp
1 /*
2  * Copyright (C) 2012 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/WebKitSourceBuffer.h"
33
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"
41
42 using blink::WebSourceBuffer;
43
44 namespace WebCore {
45
46 DEFINE_GC_INFO(WebKitSourceBuffer);
47
48 PassRefPtrWillBeRawPtr<WebKitSourceBuffer> WebKitSourceBuffer::create(PassOwnPtr<WebSourceBuffer> webSourceBuffer, PassRefPtrWillBeRawPtr<WebKitMediaSource> source)
49 {
50     return adoptRefWillBeNoop(new WebKitSourceBuffer(webSourceBuffer, source));
51 }
52
53 WebKitSourceBuffer::WebKitSourceBuffer(PassOwnPtr<WebSourceBuffer> webSourceBuffer, PassRefPtrWillBeRawPtr<WebKitMediaSource> source)
54     : m_webSourceBuffer(webSourceBuffer)
55     , m_source(source)
56     , m_timestampOffset(0)
57 {
58     ASSERT(m_webSourceBuffer);
59     ASSERT(m_source);
60     ScriptWrappable::init(this);
61 }
62
63 WebKitSourceBuffer::~WebKitSourceBuffer()
64 {
65 }
66
67 PassRefPtr<TimeRanges> WebKitSourceBuffer::buffered(ExceptionState& exceptionState) const
68 {
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.
72     if (isRemoved()) {
73         exceptionState.throwUninformativeAndGenericDOMException(InvalidStateError);
74         return 0;
75     }
76
77     // 2. Return a new static normalized TimeRanges object for the media segments buffered.
78     return TimeRanges::create(m_webSourceBuffer->buffered());
79 }
80
81 double WebKitSourceBuffer::timestampOffset() const
82 {
83     return m_timestampOffset;
84 }
85
86 void WebKitSourceBuffer::setTimestampOffset(double offset, ExceptionState& exceptionState)
87 {
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.
91     if (isRemoved()) {
92         exceptionState.throwUninformativeAndGenericDOMException(InvalidStateError);
93         return;
94     }
95
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();
100
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);
105         return;
106     }
107
108     // 6. Update the attribute to the new value.
109     m_timestampOffset = offset;
110 }
111
112 void WebKitSourceBuffer::append(PassRefPtr<Uint8Array> data, ExceptionState& exceptionState)
113 {
114     TRACE_EVENT0("media", "SourceBuffer::append");
115
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
118
119     // 2. If data is null then throw an InvalidAccessError exception and abort these steps.
120     if (!data) {
121         exceptionState.throwUninformativeAndGenericDOMException(InvalidAccessError);
122         return;
123     }
124
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.
127     if (isRemoved()) {
128         exceptionState.throwUninformativeAndGenericDOMException(InvalidStateError);
129         return;
130     }
131
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();
136
137     // Steps 6 & beyond are handled by m_webSourceBuffer.
138     m_webSourceBuffer->append(data->data(), data->length());
139 }
140
141 void WebKitSourceBuffer::abort(ExceptionState& exceptionState)
142 {
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);
150         return;
151     }
152
153     // 4. Run the reset parser state algorithm.
154     m_webSourceBuffer->abort();
155 }
156
157 void WebKitSourceBuffer::removedFromMediaSource()
158 {
159     if (isRemoved())
160         return;
161
162     m_webSourceBuffer->removedFromMediaSource();
163     m_source.clear();
164 }
165
166 bool WebKitSourceBuffer::isRemoved() const
167 {
168     return !m_source;
169 }
170
171 void WebKitSourceBuffer::trace(Visitor* visitor)
172 {
173     visitor->trace(m_source);
174 }
175
176 } // namespace WebCore