OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2012 Google Inc. All rights reserved. | 2 * Copyright (C) 2012 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
(...skipping 16 matching lines...) Expand all Loading... |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #include "config.h" | 31 #include "config.h" |
32 #include "platform/mediastream/MediaStreamSource.h" | 32 #include "platform/mediastream/MediaStreamSource.h" |
33 | 33 |
34 | 34 |
35 namespace blink { | 35 namespace blink { |
36 | 36 |
37 PassRefPtr<MediaStreamSource> MediaStreamSource::create(const String& id, Type t
ype, const String& name, ReadyState readyState, bool requiresConsumer) | 37 // The disposer pattern actually makes the deletion of the extra data happen |
| 38 // earlier and not later. The disposer makes sure that the extra data is |
| 39 // destructed in weak processing which is run before sweeping and therefore |
| 40 // all the objects are still alive and can be touched. |
| 41 // |
| 42 // FIXME: Oilpan: This disposer pattern is duplicated in a lot of places. |
| 43 // We should create a good abstraction class for this and remove the code duplic
ation. |
| 44 class MediaStreamSourceDisposer { |
| 45 public: |
| 46 explicit MediaStreamSourceDisposer(MediaStreamSource& source) : m_source(sou
rce) { } |
| 47 ~MediaStreamSourceDisposer() |
| 48 { |
| 49 m_source.dispose(); |
| 50 } |
| 51 |
| 52 private: |
| 53 MediaStreamSource& m_source; |
| 54 }; |
| 55 |
| 56 typedef HeapHashMap<WeakMember<MediaStreamSource>, OwnPtr<MediaStreamSourceDispo
ser> > SourceDisposers; |
| 57 |
| 58 static SourceDisposers& sourceDisposers() |
38 { | 59 { |
39 return adoptRef(new MediaStreamSource(id, type, name, readyState, requiresCo
nsumer)); | 60 DEFINE_STATIC_LOCAL(Persistent<SourceDisposers>, disposers, (new SourceDispo
sers)); |
| 61 return *disposers; |
| 62 } |
| 63 |
| 64 MediaStreamSource* MediaStreamSource::create(const String& id, Type type, const
String& name, ReadyState readyState, bool requiresConsumer) |
| 65 { |
| 66 return new MediaStreamSource(id, type, name, readyState, requiresConsumer); |
40 } | 67 } |
41 | 68 |
42 MediaStreamSource::MediaStreamSource(const String& id, Type type, const String&
name, ReadyState readyState, bool requiresConsumer) | 69 MediaStreamSource::MediaStreamSource(const String& id, Type type, const String&
name, ReadyState readyState, bool requiresConsumer) |
43 : m_id(id) | 70 : m_id(id) |
44 , m_type(type) | 71 , m_type(type) |
45 , m_name(name) | 72 , m_name(name) |
46 , m_readyState(readyState) | 73 , m_readyState(readyState) |
47 , m_requiresConsumer(requiresConsumer) | 74 , m_requiresConsumer(requiresConsumer) |
48 { | 75 { |
| 76 sourceDisposers().add(this, adoptPtr(new MediaStreamSourceDisposer(*this))); |
| 77 } |
| 78 |
| 79 void MediaStreamSource::dispose() |
| 80 { |
| 81 m_extraData = nullptr; |
49 } | 82 } |
50 | 83 |
51 void MediaStreamSource::setReadyState(ReadyState readyState) | 84 void MediaStreamSource::setReadyState(ReadyState readyState) |
52 { | 85 { |
53 if (m_readyState != ReadyStateEnded && m_readyState != readyState) { | 86 if (m_readyState != ReadyStateEnded && m_readyState != readyState) { |
54 m_readyState = readyState; | 87 m_readyState = readyState; |
55 for (Vector<Observer*>::iterator i = m_observers.begin(); i != m_observe
rs.end(); ++i) | 88 for (HeapHashSet<WeakMember<Observer> >::iterator i = m_observers.begin(
); i != m_observers.end(); ++i) |
56 (*i)->sourceChangedState(); | 89 (*i)->sourceChangedState(); |
57 } | 90 } |
58 } | 91 } |
59 | 92 |
60 void MediaStreamSource::addObserver(MediaStreamSource::Observer* observer) | 93 void MediaStreamSource::addObserver(MediaStreamSource::Observer* observer) |
61 { | 94 { |
62 m_observers.append(observer); | 95 ASSERT(!m_observers.contains(observer)); |
63 } | 96 m_observers.add(observer); |
64 | |
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 } | 97 } |
71 | 98 |
72 void MediaStreamSource::addAudioConsumer(AudioDestinationConsumer* consumer) | 99 void MediaStreamSource::addAudioConsumer(AudioDestinationConsumer* consumer) |
73 { | 100 { |
74 ASSERT(m_requiresConsumer); | 101 ASSERT(m_requiresConsumer); |
75 MutexLocker locker(m_audioConsumersLock); | 102 MutexLocker locker(m_audioConsumersLock); |
76 m_audioConsumers.add(consumer); | 103 m_audioConsumers.add(consumer); |
77 } | 104 } |
78 | 105 |
79 bool MediaStreamSource::removeAudioConsumer(AudioDestinationConsumer* consumer) | 106 bool MediaStreamSource::removeAudioConsumer(AudioDestinationConsumer* consumer) |
(...skipping 16 matching lines...) Expand all Loading... |
96 } | 123 } |
97 | 124 |
98 void MediaStreamSource::consumeAudio(AudioBus* bus, size_t numberOfFrames) | 125 void MediaStreamSource::consumeAudio(AudioBus* bus, size_t numberOfFrames) |
99 { | 126 { |
100 ASSERT(m_requiresConsumer); | 127 ASSERT(m_requiresConsumer); |
101 MutexLocker locker(m_audioConsumersLock); | 128 MutexLocker locker(m_audioConsumersLock); |
102 for (HeapHashSet<Member<AudioDestinationConsumer> >::iterator it = m_audioCo
nsumers.begin(); it != m_audioConsumers.end(); ++it) | 129 for (HeapHashSet<Member<AudioDestinationConsumer> >::iterator it = m_audioCo
nsumers.begin(); it != m_audioConsumers.end(); ++it) |
103 (*it)->consumeAudio(bus, numberOfFrames); | 130 (*it)->consumeAudio(bus, numberOfFrames); |
104 } | 131 } |
105 | 132 |
| 133 void MediaStreamSource::trace(Visitor* visitor) |
| 134 { |
| 135 visitor->trace(m_observers); |
| 136 visitor->trace(m_audioConsumers); |
| 137 } |
| 138 |
106 } // namespace blink | 139 } // namespace blink |
| 140 |
OLD | NEW |