1/*
2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 * Copyright (C) 2013 Apple Inc. All rights reserved.
4 * Copyright (C) 2013 Nokia Corporation and/or its subsidiary(-ies).
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer
14 *    in the documentation and/or other materials provided with the
15 *    distribution.
16 * 3. Neither the name of Google Inc. nor the names of its contributors
17 *    may be used to endorse or promote products derived from this
18 *    software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include "config.h"
34
35#if ENABLE(MEDIA_STREAM)
36
37#include "MediaStreamSource.h"
38
39#include "MediaStreamCenter.h"
40#include "MediaStreamSourceCapabilities.h"
41#include "UUID.h"
42
43namespace WebCore {
44
45MediaStreamSource::MediaStreamSource(const String& id, Type type, const String& name)
46    : m_id(id)
47    , m_type(type)
48    , m_name(name)
49    , m_readyState(New)
50    , m_enabled(true)
51    , m_muted(false)
52    , m_readonly(false)
53    , m_remote(false)
54{
55    if (!m_id.isEmpty())
56        return;
57
58    m_id = createCanonicalUUIDString();
59}
60
61void MediaStreamSource::reset()
62{
63    m_readyState = New;
64    m_enabled = true;
65    m_muted = false;
66    m_readonly = false;
67    m_remote = false;
68}
69
70void MediaStreamSource::setReadyState(ReadyState readyState)
71{
72    if (m_readyState == Ended || m_readyState == readyState)
73        return;
74
75    m_readyState = readyState;
76    for (auto observer = m_observers.begin(); observer != m_observers.end(); ++observer)
77        (*observer)->sourceReadyStateChanged();
78
79    if (m_readyState == Live) {
80        startProducingData();
81        return;
82    }
83
84    // There are no more consumers of this source's data, shut it down as appropriate.
85    if (m_readyState == Ended)
86        stopProducingData();
87}
88
89void MediaStreamSource::addObserver(MediaStreamSource::Observer* observer)
90{
91    m_observers.append(observer);
92}
93
94void MediaStreamSource::removeObserver(MediaStreamSource::Observer* observer)
95{
96    size_t pos = m_observers.find(observer);
97    if (pos != notFound)
98        m_observers.remove(pos);
99
100    if (!m_observers.size())
101        stop();
102}
103
104void MediaStreamSource::setMuted(bool muted)
105{
106    if (m_muted == muted)
107        return;
108
109    m_muted = muted;
110
111    if (m_readyState == Ended)
112        return;
113
114    for (auto observer = m_observers.begin(); observer != m_observers.end(); ++observer)
115        (*observer)->sourceMutedChanged();
116}
117
118void MediaStreamSource::setEnabled(bool enabled)
119{
120    if (!enabled) {
121        // Don't disable the source unless all observers are disabled.
122        for (auto observer = m_observers.begin(); observer != m_observers.end(); ++observer) {
123            if ((*observer)->observerIsEnabled())
124                return;
125        }
126    }
127
128    if (m_enabled == enabled)
129        return;
130
131    m_enabled = enabled;
132
133    if (m_readyState == Ended)
134        return;
135
136    if (!enabled)
137        stopProducingData();
138    else
139        startProducingData();
140
141    for (auto observer = m_observers.begin(); observer != m_observers.end(); ++observer)
142        (*observer)->sourceEnabledChanged();
143}
144
145bool MediaStreamSource::readonly() const
146{
147    return m_readonly;
148}
149
150void MediaStreamSource::stop()
151{
152    // This is called from the track.stop() method, which should "Permanently stop the generation of data
153    // for track's source", so go straight to ended. This will notify any other tracks using this source
154    // that it is no longer available.
155    setReadyState(Ended);
156}
157
158} // namespace WebCore
159
160#endif // ENABLE(MEDIA_STREAM)
161