1 /*
2 * Copyright (C) 2012 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
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * 3. Neither the name of Google Inc. nor the names of its contributors
15 * may be used to endorse or promote products derived from this
16 * 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 #include "platform/mediastream/MediaStreamSource.h"
33
34
35 namespace blink {
36
create(const String & id,Type type,const String & name,ReadyState readyState,bool requiresConsumer)37 PassRefPtr<MediaStreamSource> MediaStreamSource::create(const String& id, Type type, const String& name, ReadyState readyState, bool requiresConsumer)
38 {
39 return adoptRef(new MediaStreamSource(id, type, name, readyState, requiresConsumer));
40 }
41
MediaStreamSource(const String & id,Type type,const String & name,ReadyState readyState,bool requiresConsumer)42 MediaStreamSource::MediaStreamSource(const String& id, Type type, const String& name, ReadyState readyState, bool requiresConsumer)
43 : m_id(id)
44 , m_type(type)
45 , m_name(name)
46 , m_readyState(readyState)
47 , m_requiresConsumer(requiresConsumer)
48 {
49 }
50
setReadyState(ReadyState readyState)51 void MediaStreamSource::setReadyState(ReadyState readyState)
52 {
53 if (m_readyState != ReadyStateEnded && m_readyState != readyState) {
54 m_readyState = readyState;
55 for (Vector<Observer*>::iterator i = m_observers.begin(); i != m_observers.end(); ++i)
56 (*i)->sourceChangedState();
57 }
58 }
59
addObserver(MediaStreamSource::Observer * observer)60 void MediaStreamSource::addObserver(MediaStreamSource::Observer* observer)
61 {
62 m_observers.append(observer);
63 }
64
removeObserver(MediaStreamSource::Observer * observer)65 void MediaStreamSource::removeObserver(MediaStreamSource::Observer* observer)
66 {
67 size_t pos = m_observers.find(observer);
68 if (pos != kNotFound)
69 m_observers.remove(pos);
70 }
71
addAudioConsumer(AudioDestinationConsumer * consumer)72 void MediaStreamSource::addAudioConsumer(AudioDestinationConsumer* consumer)
73 {
74 ASSERT(m_requiresConsumer);
75 MutexLocker locker(m_audioConsumersLock);
76 m_audioConsumers.add(consumer);
77 }
78
removeAudioConsumer(AudioDestinationConsumer * consumer)79 bool MediaStreamSource::removeAudioConsumer(AudioDestinationConsumer* consumer)
80 {
81 ASSERT(m_requiresConsumer);
82 MutexLocker locker(m_audioConsumersLock);
83 HeapHashSet<Member<AudioDestinationConsumer> >::iterator it = m_audioConsumers.find(consumer);
84 if (it == m_audioConsumers.end())
85 return false;
86 m_audioConsumers.remove(it);
87 return true;
88 }
89
setAudioFormat(size_t numberOfChannels,float sampleRate)90 void MediaStreamSource::setAudioFormat(size_t numberOfChannels, float sampleRate)
91 {
92 ASSERT(m_requiresConsumer);
93 MutexLocker locker(m_audioConsumersLock);
94 for (HeapHashSet<Member<AudioDestinationConsumer> >::iterator it = m_audioConsumers.begin(); it != m_audioConsumers.end(); ++it)
95 (*it)->setFormat(numberOfChannels, sampleRate);
96 }
97
consumeAudio(AudioBus * bus,size_t numberOfFrames)98 void MediaStreamSource::consumeAudio(AudioBus* bus, size_t numberOfFrames)
99 {
100 ASSERT(m_requiresConsumer);
101 MutexLocker locker(m_audioConsumersLock);
102 for (HeapHashSet<Member<AudioDestinationConsumer> >::iterator it = m_audioConsumers.begin(); it != m_audioConsumers.end(); ++it)
103 (*it)->consumeAudio(bus, numberOfFrames);
104 }
105
106 } // namespace blink
107