15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/*
281a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) * Copyright (C) 2011 Ericsson AB. All rights reserved.
3926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * Copyright (C) 2013 Google Inc. All rights reserved.
45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without
65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * modification, are permitted provided that the following conditions are
75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * met:
85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *     * Redistributions of source code must retain the above copyright
105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * notice, this list of conditions and the following disclaimer.
115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *     * Redistributions in binary form must reproduce the above
125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer
135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * in the documentation and/or other materials provided with the
145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * distribution.
155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *     * Neither the name of Google Inc. nor the names of its
165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * contributors may be used to endorse or promote products derived from
175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * this software without specific prior written permission.
185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
305c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) */
315c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
3253e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "config.h"
335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
3451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)#include "platform/mediastream/MediaStreamComponent.h"
3581a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)
361e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)#include "platform/UUID.h"
3751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)#include "platform/audio/AudioBus.h"
3851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)#include "platform/mediastream/MediaStreamSource.h"
391e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)#include "public/platform/WebAudioSourceProvider.h"
405c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
41c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink {
425c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
4381a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)PassRefPtr<MediaStreamComponent> MediaStreamComponent::create(PassRefPtr<MediaStreamSource> source)
4453e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles){
45d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    return adoptRef(new MediaStreamComponent(createCanonicalUUIDString(), source));
4653e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)}
475c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
4881a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)PassRefPtr<MediaStreamComponent> MediaStreamComponent::create(const String& id, PassRefPtr<MediaStreamSource> source)
4953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles){
50d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    return adoptRef(new MediaStreamComponent(id, source));
5153e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)}
525c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
53d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)MediaStreamComponent::MediaStreamComponent(const String& id, PassRefPtr<MediaStreamSource> source)
54d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    : m_source(source)
5581a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)    , m_id(id)
5681a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)    , m_enabled(true)
57197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    , m_muted(false)
5881a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles){
5981a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)    ASSERT(m_id.length());
6053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)}
615c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
629bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)#if ENABLE(WEB_AUDIO)
6351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)void MediaStreamComponent::AudioSourceProviderImpl::wrap(blink::WebAudioSourceProvider* provider)
649bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles){
659bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    MutexLocker locker(m_provideInputLock);
669bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    m_webAudioSourceProvider = provider;
679bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)}
689bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
699bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)void MediaStreamComponent::AudioSourceProviderImpl::provideInput(AudioBus* bus, size_t framesToProcess)
709bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles){
719bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    ASSERT(bus);
729bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (!bus)
739bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        return;
749bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
759bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    MutexTryLocker tryLocker(m_provideInputLock);
769bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    if (!tryLocker.locked() || !m_webAudioSourceProvider) {
779bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        bus->zero();
789bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        return;
799bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
809bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
819bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    // Wrap the AudioBus channel data using WebVector.
829bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    size_t n = bus->numberOfChannels();
8351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    blink::WebVector<float*> webAudioData(n);
849bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    for (size_t i = 0; i < n; ++i)
859bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)        webAudioData[i] = bus->channel(i)->mutableData();
869bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
879bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    m_webAudioSourceProvider->provideInput(webAudioData, framesToProcess);
889bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)}
899bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)#endif // #if ENABLE(WEB_AUDIO)
909bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)
91c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)} // namespace blink
9281a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles)
93