[GStreamer][MSE] SourceBufferPrivateGStreamer refactoring
[WebKit-https.git] / Source / WebCore / platform / graphics / gstreamer / mse / SourceBufferPrivateGStreamer.cpp
1 /*
2  * Copyright (C) 2013 Google Inc. All rights reserved.
3  * Copyright (C) 2013 Orange
4  * Copyright (C) 2014 Sebastian Dröge <sebastian@centricular.com>
5  * Copyright (C) 2015, 2016 Metrological Group B.V.
6  * Copyright (C) 2015, 2016 Igalia, S.L
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are
10  * met:
11  *
12  *     * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *     * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following disclaimer
16  * in the documentation and/or other materials provided with the
17  * distribution.
18  *     * Neither the name of Google Inc. nor the names of its
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 #include "config.h"
36 #include "SourceBufferPrivateGStreamer.h"
37
38 #if ENABLE(MEDIA_SOURCE) && USE(GSTREAMER)
39
40 #include "ContentType.h"
41 #include "GStreamerUtilities.h"
42 #include "MediaPlayerPrivateGStreamerMSE.h"
43 #include "MediaSample.h"
44 #include "MediaSourceClientGStreamerMSE.h"
45 #include "MediaSourceGStreamer.h"
46 #include "NotImplemented.h"
47 #include "WebKitMediaSourceGStreamer.h"
48
49 namespace WebCore {
50
51 Ref<SourceBufferPrivateGStreamer> SourceBufferPrivateGStreamer::create(MediaSourceGStreamer* mediaSource, Ref<MediaSourceClientGStreamerMSE> client, const ContentType& contentType)
52 {
53     return adoptRef(*new SourceBufferPrivateGStreamer(mediaSource, client.get(), contentType));
54 }
55
56 SourceBufferPrivateGStreamer::SourceBufferPrivateGStreamer(MediaSourceGStreamer* mediaSource, Ref<MediaSourceClientGStreamerMSE> client, const ContentType& contentType)
57     : SourceBufferPrivate()
58     , m_mediaSource(mediaSource)
59     , m_type(contentType)
60     , m_client(client.get())
61 {
62 }
63
64 void SourceBufferPrivateGStreamer::setClient(SourceBufferPrivateClient* client)
65 {
66     m_sourceBufferPrivateClient = client;
67 }
68
69 void SourceBufferPrivateGStreamer::append(const unsigned char* data, unsigned length)
70 {
71     ASSERT(m_mediaSource);
72
73     if (!m_sourceBufferPrivateClient)
74         return;
75
76     if (m_client->append(this, data, length))
77         return;
78
79     m_sourceBufferPrivateClient->sourceBufferPrivateAppendComplete(this, SourceBufferPrivateClient::ReadStreamFailed);
80 }
81
82 void SourceBufferPrivateGStreamer::abort()
83 {
84     m_client->abort(this);
85 }
86
87 void SourceBufferPrivateGStreamer::resetParserState()
88 {
89     notImplemented();
90 }
91
92 void SourceBufferPrivateGStreamer::removedFromMediaSource()
93 {
94     if (m_mediaSource)
95         m_mediaSource->removeSourceBuffer(this);
96     m_client->removedFromMediaSource(this);
97 }
98
99 MediaPlayer::ReadyState SourceBufferPrivateGStreamer::readyState() const
100 {
101     return m_mediaSource->readyState();
102 }
103
104 void SourceBufferPrivateGStreamer::setReadyState(MediaPlayer::ReadyState state)
105 {
106     m_mediaSource->setReadyState(state);
107 }
108
109 void SourceBufferPrivateGStreamer::flush(AtomicString trackId)
110 {
111     m_client->flush(trackId);
112 }
113
114 void SourceBufferPrivateGStreamer::enqueueSample(PassRefPtr<MediaSample> prpSample, AtomicString)
115 {
116     m_notifyWhenReadyForMoreSamples = false;
117
118     RefPtr<MediaSample> protectedPrpSample = prpSample;
119     m_client->enqueueSample(protectedPrpSample);
120 }
121
122 bool SourceBufferPrivateGStreamer::isReadyForMoreSamples(AtomicString)
123 {
124     return m_isReadyForMoreSamples;
125 }
126
127 void SourceBufferPrivateGStreamer::setReadyForMoreSamples(bool isReady)
128 {
129     ASSERT(WTF::isMainThread());
130     m_isReadyForMoreSamples = isReady;
131 }
132
133 void SourceBufferPrivateGStreamer::notifyReadyForMoreSamples()
134 {
135     ASSERT(WTF::isMainThread());
136     setReadyForMoreSamples(true);
137     if (m_notifyWhenReadyForMoreSamples)
138         m_sourceBufferPrivateClient->sourceBufferPrivateDidBecomeReadyForMoreSamples(this, m_trackId);
139 }
140
141 void SourceBufferPrivateGStreamer::setActive(bool isActive)
142 {
143     if (m_mediaSource)
144         m_mediaSource->sourceBufferPrivateDidChangeActiveState(this, isActive);
145 }
146
147 void SourceBufferPrivateGStreamer::stopAskingForMoreSamples(AtomicString)
148 {
149     notImplemented();
150 }
151
152 void SourceBufferPrivateGStreamer::notifyClientWhenReadyForMoreSamples(AtomicString trackId)
153 {
154     ASSERT(WTF::isMainThread());
155     m_notifyWhenReadyForMoreSamples = true;
156     m_trackId = trackId;
157 }
158
159 void SourceBufferPrivateGStreamer::didReceiveInitializationSegment(const SourceBufferPrivateClient::InitializationSegment& initializationSegment)
160 {
161     if (m_sourceBufferPrivateClient)
162         m_sourceBufferPrivateClient->sourceBufferPrivateDidReceiveInitializationSegment(this, initializationSegment);
163 }
164
165 void SourceBufferPrivateGStreamer::didReceiveSample(PassRefPtr<MediaSample> prpSample)
166 {
167     RefPtr<MediaSample> protectedPrpSample = prpSample;
168     if (m_sourceBufferPrivateClient)
169         m_sourceBufferPrivateClient->sourceBufferPrivateDidReceiveSample(this, *protectedPrpSample);
170 }
171
172 void SourceBufferPrivateGStreamer::didReceiveAllPendingSamples()
173 {
174     if (m_sourceBufferPrivateClient)
175         m_sourceBufferPrivateClient->sourceBufferPrivateAppendComplete(this, SourceBufferPrivateClient::AppendSucceeded);
176 }
177
178 }
179 #endif