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