OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (C) 2011 Google Inc. All rights reserved. | |
3 * Copyright (C) 2011 Ericsson AB. All rights reserved. | |
4 * | |
5 * Redistribution and use in source and binary forms, with or without | |
6 * modification, are permitted provided that the following conditions | |
7 * are met: | |
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 in the | |
12 * documentation and/or other materials provided with the distribution. | |
13 * | |
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND AN
Y | |
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
17 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR AN
Y | |
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND O
N | |
21 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
23 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
24 */ | |
25 | |
26 #include "config.h" | |
27 #include "modules/mediastream/MediaStreamTrack.h" | |
28 | |
29 #include "bindings/core/v8/ExceptionMessages.h" | |
30 #include "core/dom/Document.h" | |
31 #include "core/dom/ExceptionCode.h" | |
32 #include "core/dom/ExecutionContext.h" | |
33 #include "core/events/Event.h" | |
34 #include "modules/mediastream/MediaStream.h" | |
35 #include "modules/mediastream/MediaStreamTrackSourcesCallback.h" | |
36 #include "modules/mediastream/MediaStreamTrackSourcesRequestImpl.h" | |
37 #include "modules/mediastream/UserMediaController.h" | |
38 #include "platform/mediastream/MediaStreamCenter.h" | |
39 #include "platform/mediastream/MediaStreamComponent.h" | |
40 #include "public/platform/WebSourceInfo.h" | |
41 | |
42 namespace blink { | |
43 | |
44 MediaStreamTrack* MediaStreamTrack::create(ExecutionContext* context, MediaStrea
mComponent* component) | |
45 { | |
46 MediaStreamTrack* track = new MediaStreamTrack(context, component); | |
47 track->suspendIfNeeded(); | |
48 return track; | |
49 } | |
50 | |
51 MediaStreamTrack::MediaStreamTrack(ExecutionContext* context, MediaStreamCompone
nt* component) | |
52 : ActiveDOMObject(context) | |
53 , m_readyState(MediaStreamSource::ReadyStateLive) | |
54 , m_isIteratingRegisteredMediaStreams(false) | |
55 , m_stopped(false) | |
56 , m_component(component) | |
57 { | |
58 m_component->source()->addObserver(this); | |
59 } | |
60 | |
61 MediaStreamTrack::~MediaStreamTrack() | |
62 { | |
63 } | |
64 | |
65 String MediaStreamTrack::kind() const | |
66 { | |
67 DEFINE_STATIC_LOCAL(String, audioKind, ("audio")); | |
68 DEFINE_STATIC_LOCAL(String, videoKind, ("video")); | |
69 | |
70 switch (m_component->source()->type()) { | |
71 case MediaStreamSource::TypeAudio: | |
72 return audioKind; | |
73 case MediaStreamSource::TypeVideo: | |
74 return videoKind; | |
75 } | |
76 | |
77 ASSERT_NOT_REACHED(); | |
78 return audioKind; | |
79 } | |
80 | |
81 String MediaStreamTrack::id() const | |
82 { | |
83 return m_component->id(); | |
84 } | |
85 | |
86 String MediaStreamTrack::label() const | |
87 { | |
88 return m_component->source()->name(); | |
89 } | |
90 | |
91 bool MediaStreamTrack::enabled() const | |
92 { | |
93 return m_component->enabled(); | |
94 } | |
95 | |
96 void MediaStreamTrack::setEnabled(bool enabled) | |
97 { | |
98 if (enabled == m_component->enabled()) | |
99 return; | |
100 | |
101 m_component->setEnabled(enabled); | |
102 | |
103 if (!ended()) | |
104 MediaStreamCenter::instance().didSetMediaStreamTrackEnabled(m_component.
get()); | |
105 } | |
106 | |
107 bool MediaStreamTrack::muted() const | |
108 { | |
109 return m_component->muted(); | |
110 } | |
111 | |
112 String MediaStreamTrack::readyState() const | |
113 { | |
114 if (ended()) | |
115 return "ended"; | |
116 | |
117 switch (m_readyState) { | |
118 case MediaStreamSource::ReadyStateLive: | |
119 return "live"; | |
120 case MediaStreamSource::ReadyStateMuted: | |
121 return "muted"; | |
122 case MediaStreamSource::ReadyStateEnded: | |
123 return "ended"; | |
124 } | |
125 | |
126 ASSERT_NOT_REACHED(); | |
127 return String(); | |
128 } | |
129 | |
130 void MediaStreamTrack::getSources(ExecutionContext* context, MediaStreamTrackSou
rcesCallback* callback, ExceptionState& exceptionState) | |
131 { | |
132 LocalFrame* frame = toDocument(context)->frame(); | |
133 UserMediaController* userMedia = UserMediaController::from(frame); | |
134 if (!userMedia) { | |
135 exceptionState.throwDOMException(NotSupportedError, "No sources controll
er available; is this a detached window?"); | |
136 return; | |
137 } | |
138 MediaStreamTrackSourcesRequest* request = MediaStreamTrackSourcesRequestImpl
::create(*context, callback); | |
139 userMedia->requestSources(request); | |
140 } | |
141 | |
142 void MediaStreamTrack::stopTrack(ExceptionState& exceptionState) | |
143 { | |
144 if (ended()) | |
145 return; | |
146 | |
147 m_readyState = MediaStreamSource::ReadyStateEnded; | |
148 MediaStreamCenter::instance().didStopMediaStreamTrack(component()); | |
149 dispatchEvent(Event::create(EventTypeNames::ended)); | |
150 propagateTrackEnded(); | |
151 } | |
152 | |
153 MediaStreamTrack* MediaStreamTrack::clone(ExecutionContext* context) | |
154 { | |
155 MediaStreamComponent* clonedComponent = MediaStreamComponent::create(compone
nt()->source()); | |
156 MediaStreamTrack* clonedTrack = MediaStreamTrack::create(context, clonedComp
onent); | |
157 MediaStreamCenter::instance().didCreateMediaStreamTrack(clonedComponent); | |
158 return clonedTrack; | |
159 } | |
160 | |
161 bool MediaStreamTrack::ended() const | |
162 { | |
163 return m_stopped || (m_readyState == MediaStreamSource::ReadyStateEnded); | |
164 } | |
165 | |
166 void MediaStreamTrack::sourceChangedState() | |
167 { | |
168 if (ended()) | |
169 return; | |
170 | |
171 m_readyState = m_component->source()->readyState(); | |
172 switch (m_readyState) { | |
173 case MediaStreamSource::ReadyStateLive: | |
174 m_component->setMuted(false); | |
175 dispatchEvent(Event::create(EventTypeNames::unmute)); | |
176 break; | |
177 case MediaStreamSource::ReadyStateMuted: | |
178 m_component->setMuted(true); | |
179 dispatchEvent(Event::create(EventTypeNames::mute)); | |
180 break; | |
181 case MediaStreamSource::ReadyStateEnded: | |
182 dispatchEvent(Event::create(EventTypeNames::ended)); | |
183 propagateTrackEnded(); | |
184 break; | |
185 } | |
186 } | |
187 | |
188 void MediaStreamTrack::propagateTrackEnded() | |
189 { | |
190 RELEASE_ASSERT(!m_isIteratingRegisteredMediaStreams); | |
191 m_isIteratingRegisteredMediaStreams = true; | |
192 for (HeapHashSet<Member<MediaStream> >::iterator iter = m_registeredMediaStr
eams.begin(); iter != m_registeredMediaStreams.end(); ++iter) | |
193 (*iter)->trackEnded(); | |
194 m_isIteratingRegisteredMediaStreams = false; | |
195 } | |
196 | |
197 MediaStreamComponent* MediaStreamTrack::component() | |
198 { | |
199 return m_component.get(); | |
200 } | |
201 | |
202 void MediaStreamTrack::stop() | |
203 { | |
204 m_stopped = true; | |
205 } | |
206 | |
207 PassOwnPtr<AudioSourceProvider> MediaStreamTrack::createWebAudioSource() | |
208 { | |
209 return MediaStreamCenter::instance().createWebAudioSourceFromMediaStreamTrac
k(component()); | |
210 } | |
211 | |
212 void MediaStreamTrack::registerMediaStream(MediaStream* mediaStream) | |
213 { | |
214 RELEASE_ASSERT(!m_isIteratingRegisteredMediaStreams); | |
215 RELEASE_ASSERT(!m_registeredMediaStreams.contains(mediaStream)); | |
216 m_registeredMediaStreams.add(mediaStream); | |
217 } | |
218 | |
219 void MediaStreamTrack::unregisterMediaStream(MediaStream* mediaStream) | |
220 { | |
221 RELEASE_ASSERT(!m_isIteratingRegisteredMediaStreams); | |
222 HeapHashSet<Member<MediaStream> >::iterator iter = m_registeredMediaStreams.
find(mediaStream); | |
223 RELEASE_ASSERT(iter != m_registeredMediaStreams.end()); | |
224 m_registeredMediaStreams.remove(iter); | |
225 } | |
226 | |
227 const AtomicString& MediaStreamTrack::interfaceName() const | |
228 { | |
229 return EventTargetNames::MediaStreamTrack; | |
230 } | |
231 | |
232 ExecutionContext* MediaStreamTrack::executionContext() const | |
233 { | |
234 return ActiveDOMObject::executionContext(); | |
235 } | |
236 | |
237 void MediaStreamTrack::trace(Visitor* visitor) | |
238 { | |
239 visitor->trace(m_registeredMediaStreams); | |
240 visitor->trace(m_component); | |
241 EventTargetWithInlineData::trace(visitor); | |
242 MediaStreamSource::Observer::trace(visitor); | |
243 } | |
244 | |
245 } // namespace blink | |
OLD | NEW |