Home | History | Annotate | Download | only in mediastream
      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 ANY
     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 ANY
     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 ON
     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, MediaStreamComponent* component)
     45 {
     46     MediaStreamTrack* track = adoptRefCountedGarbageCollectedWillBeNoop(new MediaStreamTrack(context, component));
     47     track->suspendIfNeeded();
     48     return track;
     49 }
     50 
     51 MediaStreamTrack::MediaStreamTrack(ExecutionContext* context, MediaStreamComponent* 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     m_component->source()->removeObserver(this);
     64 }
     65 
     66 String MediaStreamTrack::kind() const
     67 {
     68     DEFINE_STATIC_LOCAL(String, audioKind, ("audio"));
     69     DEFINE_STATIC_LOCAL(String, videoKind, ("video"));
     70 
     71     switch (m_component->source()->type()) {
     72     case MediaStreamSource::TypeAudio:
     73         return audioKind;
     74     case MediaStreamSource::TypeVideo:
     75         return videoKind;
     76     }
     77 
     78     ASSERT_NOT_REACHED();
     79     return audioKind;
     80 }
     81 
     82 String MediaStreamTrack::id() const
     83 {
     84     return m_component->id();
     85 }
     86 
     87 String MediaStreamTrack::label() const
     88 {
     89     return m_component->source()->name();
     90 }
     91 
     92 bool MediaStreamTrack::enabled() const
     93 {
     94     return m_component->enabled();
     95 }
     96 
     97 void MediaStreamTrack::setEnabled(bool enabled)
     98 {
     99     if (enabled == m_component->enabled())
    100         return;
    101 
    102     m_component->setEnabled(enabled);
    103 
    104     if (!ended())
    105         MediaStreamCenter::instance().didSetMediaStreamTrackEnabled(m_component.get());
    106 }
    107 
    108 bool MediaStreamTrack::muted() const
    109 {
    110     return m_component->muted();
    111 }
    112 
    113 String MediaStreamTrack::readyState() const
    114 {
    115     if (ended())
    116         return "ended";
    117 
    118     switch (m_readyState) {
    119     case MediaStreamSource::ReadyStateLive:
    120         return "live";
    121     case MediaStreamSource::ReadyStateMuted:
    122         return "muted";
    123     case MediaStreamSource::ReadyStateEnded:
    124         return "ended";
    125     }
    126 
    127     ASSERT_NOT_REACHED();
    128     return String();
    129 }
    130 
    131 void MediaStreamTrack::getSources(ExecutionContext* context, MediaStreamTrackSourcesCallback* callback, ExceptionState& exceptionState)
    132 {
    133     LocalFrame* frame = toDocument(context)->frame();
    134     UserMediaController* userMedia = UserMediaController::from(frame);
    135     if (!userMedia) {
    136         exceptionState.throwDOMException(NotSupportedError, "No sources controller available; is this a detached window?");
    137         return;
    138     }
    139     MediaStreamTrackSourcesRequest* request = MediaStreamTrackSourcesRequestImpl::create(*context, callback);
    140     userMedia->requestSources(request);
    141 }
    142 
    143 void MediaStreamTrack::stopTrack(ExceptionState& exceptionState)
    144 {
    145     if (ended())
    146         return;
    147 
    148     m_readyState = MediaStreamSource::ReadyStateEnded;
    149     MediaStreamCenter::instance().didStopMediaStreamTrack(component());
    150     dispatchEvent(Event::create(EventTypeNames::ended));
    151     propagateTrackEnded();
    152 }
    153 
    154 MediaStreamTrack* MediaStreamTrack::clone(ExecutionContext* context)
    155 {
    156     RefPtr<MediaStreamComponent> clonedComponent = MediaStreamComponent::create(component()->source());
    157     MediaStreamTrack* clonedTrack = MediaStreamTrack::create(context, clonedComponent.get());
    158     MediaStreamCenter::instance().didCreateMediaStreamTrack(clonedComponent.get());
    159     return clonedTrack;
    160 }
    161 
    162 bool MediaStreamTrack::ended() const
    163 {
    164     return m_stopped || (m_readyState == MediaStreamSource::ReadyStateEnded);
    165 }
    166 
    167 void MediaStreamTrack::sourceChangedState()
    168 {
    169     if (ended())
    170         return;
    171 
    172     m_readyState = m_component->source()->readyState();
    173     switch (m_readyState) {
    174     case MediaStreamSource::ReadyStateLive:
    175         dispatchEvent(Event::create(EventTypeNames::unmute));
    176         break;
    177     case MediaStreamSource::ReadyStateMuted:
    178         dispatchEvent(Event::create(EventTypeNames::mute));
    179         break;
    180     case MediaStreamSource::ReadyStateEnded:
    181         dispatchEvent(Event::create(EventTypeNames::ended));
    182         propagateTrackEnded();
    183         break;
    184     }
    185 }
    186 
    187 void MediaStreamTrack::propagateTrackEnded()
    188 {
    189     RELEASE_ASSERT(!m_isIteratingRegisteredMediaStreams);
    190     m_isIteratingRegisteredMediaStreams = true;
    191     for (HeapHashSet<Member<MediaStream> >::iterator iter = m_registeredMediaStreams.begin(); iter != m_registeredMediaStreams.end(); ++iter)
    192         (*iter)->trackEnded();
    193     m_isIteratingRegisteredMediaStreams = false;
    194 }
    195 
    196 MediaStreamComponent* MediaStreamTrack::component()
    197 {
    198     return m_component.get();
    199 }
    200 
    201 void MediaStreamTrack::stop()
    202 {
    203     m_stopped = true;
    204 }
    205 
    206 PassOwnPtr<AudioSourceProvider> MediaStreamTrack::createWebAudioSource()
    207 {
    208     return MediaStreamCenter::instance().createWebAudioSourceFromMediaStreamTrack(component());
    209 }
    210 
    211 void MediaStreamTrack::registerMediaStream(MediaStream* mediaStream)
    212 {
    213     RELEASE_ASSERT(!m_isIteratingRegisteredMediaStreams);
    214     RELEASE_ASSERT(!m_registeredMediaStreams.contains(mediaStream));
    215     m_registeredMediaStreams.add(mediaStream);
    216 }
    217 
    218 void MediaStreamTrack::unregisterMediaStream(MediaStream* mediaStream)
    219 {
    220     RELEASE_ASSERT(!m_isIteratingRegisteredMediaStreams);
    221     HeapHashSet<Member<MediaStream> >::iterator iter = m_registeredMediaStreams.find(mediaStream);
    222     RELEASE_ASSERT(iter != m_registeredMediaStreams.end());
    223     m_registeredMediaStreams.remove(iter);
    224 }
    225 
    226 const AtomicString& MediaStreamTrack::interfaceName() const
    227 {
    228     return EventTargetNames::MediaStreamTrack;
    229 }
    230 
    231 ExecutionContext* MediaStreamTrack::executionContext() const
    232 {
    233     return ActiveDOMObject::executionContext();
    234 }
    235 
    236 void MediaStreamTrack::trace(Visitor* visitor)
    237 {
    238     visitor->trace(m_registeredMediaStreams);
    239     EventTargetWithInlineData::trace(visitor);
    240 }
    241 
    242 } // namespace blink
    243