2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
33 #include "public/platform/WebMediaStreamSource.h"
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"
48 class ExtraDataContainer
: public MediaStreamSource::ExtraData
{
50 ExtraDataContainer(PassOwnPtr
<WebMediaStreamSource::ExtraData
> extraData
) : m_extraData(extraData
) { }
52 WebMediaStreamSource::ExtraData
* extraData() { return m_extraData
.get(); }
55 OwnPtr
<WebMediaStreamSource::ExtraData
> m_extraData
;
60 WebMediaStreamSource
WebMediaStreamSource::ExtraData::owner()
63 return WebMediaStreamSource(m_owner
);
66 void WebMediaStreamSource::ExtraData::setOwner(MediaStreamSource
* owner
)
72 WebMediaStreamSource::WebMediaStreamSource(const PassRefPtr
<MediaStreamSource
>& mediaStreamSource
)
73 : m_private(mediaStreamSource
)
77 WebMediaStreamSource
& WebMediaStreamSource::operator=(MediaStreamSource
* mediaStreamSource
)
79 m_private
= mediaStreamSource
;
83 void WebMediaStreamSource::assign(const WebMediaStreamSource
& other
)
85 m_private
= other
.m_private
;
88 void WebMediaStreamSource::reset()
93 WebMediaStreamSource::operator PassRefPtr
<MediaStreamSource
>() const
95 return m_private
.get();
98 WebMediaStreamSource::operator MediaStreamSource
*() const
100 return m_private
.get();
103 void WebMediaStreamSource::initialize(const WebString
& id
, Type type
, const WebString
& name
)
105 m_private
= MediaStreamSource::create(id
, static_cast<MediaStreamSource::Type
>(type
), name
, false, true);
108 void WebMediaStreamSource::initialize(const WebString
& id
, Type type
, const WebString
& name
, bool remote
, bool readonly
)
110 m_private
= MediaStreamSource::create(id
, static_cast<MediaStreamSource::Type
>(type
), name
, remote
, readonly
);
113 WebString
WebMediaStreamSource::id() const
115 ASSERT(!m_private
.isNull());
116 return m_private
.get()->id();
119 WebMediaStreamSource::Type
WebMediaStreamSource::type() const
121 ASSERT(!m_private
.isNull());
122 return static_cast<Type
>(m_private
.get()->type());
125 WebString
WebMediaStreamSource::name() const
127 ASSERT(!m_private
.isNull());
128 return m_private
.get()->name();
131 bool WebMediaStreamSource::remote() const
133 ASSERT(!m_private
.isNull());
134 return m_private
.get()->remote();
137 void WebMediaStreamSource::setReadyState(ReadyState state
)
139 ASSERT(!m_private
.isNull());
140 m_private
->setReadyState(static_cast<MediaStreamSource::ReadyState
>(state
));
143 WebMediaStreamSource::ReadyState
WebMediaStreamSource::readyState() const
145 ASSERT(!m_private
.isNull());
146 return static_cast<ReadyState
>(m_private
->readyState());
149 WebMediaStreamSource::ExtraData
* WebMediaStreamSource::extraData() const
151 ASSERT(!m_private
.isNull());
152 MediaStreamSource::ExtraData
* data
= m_private
->extraData();
155 return static_cast<ExtraDataContainer
*>(data
)->extraData();
158 void WebMediaStreamSource::setExtraData(ExtraData
* extraData
)
160 ASSERT(!m_private
.isNull());
163 extraData
->setOwner(m_private
.get());
165 m_private
->setExtraData(adoptPtr(new ExtraDataContainer(adoptPtr(extraData
))));
168 WebMediaConstraints
WebMediaStreamSource::constraints()
170 ASSERT(!m_private
.isNull());
171 return m_private
->constraints();
174 bool WebMediaStreamSource::requiresAudioConsumer() const
176 ASSERT(!m_private
.isNull());
177 return m_private
->requiresAudioConsumer();
180 class ConsumerWrapper final
: public AudioDestinationConsumer
{
182 static ConsumerWrapper
* create(WebAudioDestinationConsumer
* consumer
)
184 return new ConsumerWrapper(consumer
);
187 void setFormat(size_t numberOfChannels
, float sampleRate
) override
;
188 void consumeAudio(AudioBus
*, size_t numberOfFrames
) override
;
190 WebAudioDestinationConsumer
* consumer() { return m_consumer
; }
193 explicit ConsumerWrapper(WebAudioDestinationConsumer
* consumer
) : m_consumer(consumer
) { }
195 // m_consumer is not owned by this class.
196 WebAudioDestinationConsumer
* m_consumer
;
199 void ConsumerWrapper::setFormat(size_t numberOfChannels
, float sampleRate
)
201 m_consumer
->setFormat(numberOfChannels
, sampleRate
);
204 void ConsumerWrapper::consumeAudio(AudioBus
* bus
, size_t numberOfFrames
)
210 size_t numberOfChannels
= bus
->numberOfChannels();
211 WebVector
<const float*> busVector(numberOfChannels
);
212 for (size_t i
= 0; i
< numberOfChannels
; ++i
)
213 busVector
[i
] = bus
->channel(i
)->data();
215 m_consumer
->consumeAudio(busVector
, numberOfFrames
);
218 void WebMediaStreamSource::addAudioConsumer(WebAudioDestinationConsumer
* consumer
)
220 ASSERT(isMainThread());
221 ASSERT(!m_private
.isNull() && consumer
);
223 m_private
->addAudioConsumer(ConsumerWrapper::create(consumer
));
226 bool WebMediaStreamSource::removeAudioConsumer(WebAudioDestinationConsumer
* consumer
)
228 ASSERT(isMainThread());
229 ASSERT(!m_private
.isNull() && consumer
);
231 const HeapHashSet
<Member
<AudioDestinationConsumer
>>& consumers
= m_private
->audioConsumers();
232 for (HeapHashSet
<Member
<AudioDestinationConsumer
>>::const_iterator it
= consumers
.begin(); it
!= consumers
.end(); ++it
) {
233 ConsumerWrapper
* wrapper
= static_cast<ConsumerWrapper
*>(it
->get());
234 if (wrapper
->consumer() == consumer
) {
235 m_private
->removeAudioConsumer(wrapper
);