File: MediaStreamTrackPrivate.cpp

package info (click to toggle)
webkit2gtk 2.6.2%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 115,572 kB
  • ctags: 216,388
  • sloc: cpp: 1,164,175; ansic: 18,422; perl: 16,884; python: 11,608; ruby: 9,409; xml: 8,376; asm: 4,765; yacc: 2,292; lex: 891; sh: 650; makefile: 79
file content (287 lines) | stat: -rw-r--r-- 7,326 bytes parent folder | download
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
 *  Copyright (C) 2013 Nokia Corporation and/or its subsidiary(-ies).
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. 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.
 *
 * 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"

#if ENABLE(MEDIA_STREAM)

#include "MediaStreamTrackPrivate.h"

#include "MediaSourceStates.h"
#include "MediaStreamCapabilities.h"
#include "UUID.h"
#include <wtf/NeverDestroyed.h>

namespace WebCore {

PassRefPtr<MediaStreamTrackPrivate> MediaStreamTrackPrivate::create(PassRefPtr<MediaStreamSource> source)
{
    return adoptRef(new MediaStreamTrackPrivate(source));
}

MediaStreamTrackPrivate::MediaStreamTrackPrivate(const MediaStreamTrackPrivate& other)
    : RefCounted()
    , m_client(nullptr)
{
    m_ignoreMutations = true;
    m_id = createCanonicalUUIDString();
    setSource(other.source());
    m_readyState = other.readyState();
    m_muted = other.muted();
    m_enabled = other.enabled();
    m_stopped = other.stopped();
    m_ignoreMutations = false;
}

MediaStreamTrackPrivate::MediaStreamTrackPrivate(PassRefPtr<MediaStreamSource> source)
    : m_source(nullptr)
    , m_client(nullptr)
    , m_readyState(MediaStreamSource::New)
    , m_muted(false)
    , m_enabled(true)
    , m_stopped(false)
{
    m_ignoreMutations = true;
    setSource(source);
    m_ignoreMutations = false;
}

MediaStreamTrackPrivate::~MediaStreamTrackPrivate()
{
    if (m_source)
        m_source->removeObserver(this);
}

void MediaStreamTrackPrivate::setSource(PassRefPtr<MediaStreamSource> source)
{
    if (m_source)
        m_source->removeObserver(this);

    m_source = source;

    if (!m_source)
        return;

    setMuted(m_source->muted());
    setReadyState(m_source->readyState());
    if (m_source)
        m_source->addObserver(this);
}

const String& MediaStreamTrackPrivate::id() const
{
    if (!m_id.isEmpty())
        return m_id;

    // The spec says:
    //   Unless a MediaStreamTrack object is created as a part a of special purpose algorithm that
    //   specifies how the track id must be initialized, the user agent must generate a globally
    //   unique identifier string and initialize the object's id attribute to that string.
    if (m_source && m_source->useIDForTrackID())
        return m_source->id();

    m_id = createCanonicalUUIDString();
    return m_id;
}

const String& MediaStreamTrackPrivate::label() const
{
    if (m_source)
        return m_source->name();

    return emptyString();
}

bool MediaStreamTrackPrivate::ended() const
{
    return m_stopped || (m_source && m_source->readyState() == MediaStreamSource::Ended);
}

bool MediaStreamTrackPrivate::muted() const
{
    if (m_stopped || !m_source)
        return false;

    return m_source->muted();
}

void MediaStreamTrackPrivate::setMuted(bool muted)
{
    if (m_muted == muted)
        return;

    m_muted = muted;

    if (!m_client || m_ignoreMutations)
        return;

    m_client->trackMutedChanged();
}

bool MediaStreamTrackPrivate::readonly() const
{
    if (m_stopped || !m_source)
        return true;

    return m_source->readonly();
}

bool MediaStreamTrackPrivate::remote() const
{
    if (!m_source)
        return false;

    return m_source->remote();
}

void MediaStreamTrackPrivate::setEnabled(bool enabled)
{
    if (m_stopped || m_enabled == enabled)
        return;

    // 4.3.3.1
    // ... after a MediaStreamTrack is disassociated from its track, its enabled attribute still
    // changes value when set; it just doesn't do anything with that new value.
    m_enabled = enabled;

    if (m_source)
        m_source->setEnabled(enabled);

    if (!m_client || m_ignoreMutations)
        return;

    m_client->trackEnabledChanged();
}

void MediaStreamTrackPrivate::stop(StopBehavior stopSource)
{
    if (m_stopped)
        return;

    if (stopSource == StopTrackAndStopSource && m_source)
        m_source->stop();

    setReadyState(MediaStreamSource::Ended);
    m_stopped = true;
}

MediaStreamSource::ReadyState MediaStreamTrackPrivate::readyState() const
{
    if (m_stopped)
        return MediaStreamSource::Ended;

    return m_readyState;
}

void MediaStreamTrackPrivate::setReadyState(MediaStreamSource::ReadyState state)
{
    if (m_readyState == MediaStreamSource::Ended || m_readyState == state)
        return;

    MediaStreamSource::ReadyState oldState = m_readyState;
    m_readyState = state;

    if (!m_client || m_ignoreMutations)
        return;

    if ((m_readyState == MediaStreamSource::Live && oldState == MediaStreamSource::New) || m_readyState == MediaStreamSource::Ended)
        m_client->trackReadyStateChanged();
}

RefPtr<MediaStreamTrackPrivate> MediaStreamTrackPrivate::clone()
{
    return adoptRef(new MediaStreamTrackPrivate(*this));
}

    
RefPtr<MediaConstraints> MediaStreamTrackPrivate::constraints() const
{
    return m_constraints;
}

const MediaStreamSourceStates& MediaStreamTrackPrivate::states() const
{
    if (!m_source) {
        DEPRECATED_DEFINE_STATIC_LOCAL(const MediaStreamSourceStates, noState, ());
        return noState;
    }
    
    return m_source->states();
}

MediaStreamSource::Type MediaStreamTrackPrivate::type() const
{
    if (!m_source)
        return MediaStreamSource::None;

    return m_source->type();
}

RefPtr<MediaStreamSourceCapabilities> MediaStreamTrackPrivate::capabilities() const
{
    if (!m_source)
        return 0;

    return m_source->capabilities();
}

void MediaStreamTrackPrivate::applyConstraints(PassRefPtr<MediaConstraints>)
{
    // FIXME: apply the new constraints to the track
    // https://bugs.webkit.org/show_bug.cgi?id=122428
}

void MediaStreamTrackPrivate::sourceReadyStateChanged()
{
    if (stopped())
        return;
    
    setReadyState(m_source->readyState());
}

void MediaStreamTrackPrivate::sourceMutedChanged()
{
    if (stopped())
        return;
    
    setMuted(m_source->muted());
}

void MediaStreamTrackPrivate::sourceEnabledChanged()
{
    if (stopped())
        return;
    
    setEnabled(m_source->enabled());
}

bool MediaStreamTrackPrivate::observerIsEnabled()
{
    return enabled();
}

} // namespace WebCore

#endif // ENABLE(MEDIA_STREAM)