Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / platform / exported / WebMediaStreamSource.cpp
1 /*
2  * Copyright (C) 2011 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
33 #include "public/platform/WebMediaStreamSource.h"
34
35 #include "platform/audio/AudioBus.h"
36 #include "platform/mediastream/MediaStreamSource.h"
37 #include "public/platform/WebAudioDestinationConsumer.h"
38 #include "public/platform/WebMediaConstraints.h"
39 #include "public/platform/WebString.h"
40 #include "wtf/MainThread.h"
41 #include "wtf/PassOwnPtr.h"
42 #include "wtf/Vector.h"
43
44 namespace blink {
45
46 namespace {
47
48 class ExtraDataContainer : public MediaStreamSource::ExtraData {
49 public:
50     ExtraDataContainer(PassOwnPtr<WebMediaStreamSource::ExtraData> extraData) : m_extraData(extraData) { }
51
52     WebMediaStreamSource::ExtraData* extraData() { return m_extraData.get(); }
53
54 private:
55     OwnPtr<WebMediaStreamSource::ExtraData> m_extraData;
56 };
57
58 } // namespace
59
60 WebMediaStreamSource WebMediaStreamSource::ExtraData::owner()
61 {
62     ASSERT(m_owner);
63     return WebMediaStreamSource(m_owner);
64 }
65
66 void WebMediaStreamSource::ExtraData::setOwner(MediaStreamSource* owner)
67 {
68     ASSERT(!m_owner);
69     m_owner = owner;
70 }
71
72 WebMediaStreamSource::WebMediaStreamSource(const PassRefPtr<MediaStreamSource>& mediaStreamSource)
73     : m_private(mediaStreamSource)
74 {
75 }
76
77 WebMediaStreamSource& WebMediaStreamSource::operator=(MediaStreamSource* mediaStreamSource)
78 {
79     m_private = mediaStreamSource;
80     return *this;
81 }
82
83 void WebMediaStreamSource::assign(const WebMediaStreamSource& other)
84 {
85     m_private = other.m_private;
86 }
87
88 void WebMediaStreamSource::reset()
89 {
90     m_private.reset();
91 }
92
93 WebMediaStreamSource::operator PassRefPtr<MediaStreamSource>() const
94 {
95     return m_private.get();
96 }
97
98 WebMediaStreamSource::operator MediaStreamSource*() const
99 {
100     return m_private.get();
101 }
102
103 void WebMediaStreamSource::initialize(const WebString& id, Type type, const WebString& name)
104 {
105     m_private = MediaStreamSource::create(id, static_cast<MediaStreamSource::Type>(type), name, false, true);
106 }
107
108 void WebMediaStreamSource::initialize(const WebString& id, Type type, const WebString& name, bool remote, bool readonly)
109 {
110     m_private = MediaStreamSource::create(id, static_cast<MediaStreamSource::Type>(type), name, remote, readonly);
111 }
112
113 WebString WebMediaStreamSource::id() const
114 {
115     ASSERT(!m_private.isNull());
116     return m_private.get()->id();
117 }
118
119 WebMediaStreamSource::Type WebMediaStreamSource::type() const
120 {
121     ASSERT(!m_private.isNull());
122     return static_cast<Type>(m_private.get()->type());
123 }
124
125 WebString WebMediaStreamSource::name() const
126 {
127     ASSERT(!m_private.isNull());
128     return m_private.get()->name();
129 }
130
131 void WebMediaStreamSource::setReadyState(ReadyState state)
132 {
133     ASSERT(!m_private.isNull());
134     m_private->setReadyState(static_cast<MediaStreamSource::ReadyState>(state));
135 }
136
137 WebMediaStreamSource::ReadyState WebMediaStreamSource::readyState() const
138 {
139     ASSERT(!m_private.isNull());
140     return static_cast<ReadyState>(m_private->readyState());
141 }
142
143 WebMediaStreamSource::ExtraData* WebMediaStreamSource::extraData() const
144 {
145     ASSERT(!m_private.isNull());
146     MediaStreamSource::ExtraData* data = m_private->extraData();
147     if (!data)
148         return 0;
149     return static_cast<ExtraDataContainer*>(data)->extraData();
150 }
151
152 void WebMediaStreamSource::setExtraData(ExtraData* extraData)
153 {
154     ASSERT(!m_private.isNull());
155
156     if (extraData)
157         extraData->setOwner(m_private.get());
158
159     m_private->setExtraData(adoptPtr(new ExtraDataContainer(adoptPtr(extraData))));
160 }
161
162 WebMediaConstraints WebMediaStreamSource::constraints()
163 {
164     ASSERT(!m_private.isNull());
165     return m_private->constraints();
166 }
167
168 bool WebMediaStreamSource::requiresAudioConsumer() const
169 {
170     ASSERT(!m_private.isNull());
171     return m_private->requiresAudioConsumer();
172 }
173
174 class ConsumerWrapper final : public AudioDestinationConsumer {
175 public:
176     static ConsumerWrapper* create(WebAudioDestinationConsumer* consumer)
177     {
178         return new ConsumerWrapper(consumer);
179     }
180
181     virtual void setFormat(size_t numberOfChannels, float sampleRate) override;
182     virtual void consumeAudio(AudioBus*, size_t numberOfFrames) override;
183
184     WebAudioDestinationConsumer* consumer() { return m_consumer; }
185
186 private:
187     explicit ConsumerWrapper(WebAudioDestinationConsumer* consumer) : m_consumer(consumer) { }
188
189     // m_consumer is not owned by this class.
190     WebAudioDestinationConsumer* m_consumer;
191 };
192
193 void ConsumerWrapper::setFormat(size_t numberOfChannels, float sampleRate)
194 {
195     m_consumer->setFormat(numberOfChannels, sampleRate);
196 }
197
198 void ConsumerWrapper::consumeAudio(AudioBus* bus, size_t numberOfFrames)
199 {
200     if (!bus)
201         return;
202
203     // Wrap AudioBus.
204     size_t numberOfChannels = bus->numberOfChannels();
205     WebVector<const float*> busVector(numberOfChannels);
206     for (size_t i = 0; i < numberOfChannels; ++i)
207         busVector[i] = bus->channel(i)->data();
208
209     m_consumer->consumeAudio(busVector, numberOfFrames);
210 }
211
212 void WebMediaStreamSource::addAudioConsumer(WebAudioDestinationConsumer* consumer)
213 {
214     ASSERT(isMainThread());
215     ASSERT(!m_private.isNull() && consumer);
216
217     m_private->addAudioConsumer(ConsumerWrapper::create(consumer));
218 }
219
220 bool WebMediaStreamSource::removeAudioConsumer(WebAudioDestinationConsumer* consumer)
221 {
222     ASSERT(isMainThread());
223     ASSERT(!m_private.isNull() && consumer);
224
225     const HeapHashSet<Member<AudioDestinationConsumer> >& consumers = m_private->audioConsumers();
226     for (HeapHashSet<Member<AudioDestinationConsumer> >::const_iterator it = consumers.begin(); it != consumers.end(); ++it) {
227         ConsumerWrapper* wrapper = static_cast<ConsumerWrapper*>(it->get());
228         if (wrapper->consumer() == consumer) {
229             m_private->removeAudioConsumer(wrapper);
230             return true;
231         }
232     }
233     return false;
234 }
235
236 } // namespace blink