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 #ifndef MediaStream_h
     27 #define MediaStream_h
     28 
     29 #include "bindings/v8/ScriptWrappable.h"
     30 #include "core/dom/ContextLifecycleObserver.h"
     31 #include "core/dom/EventTarget.h"
     32 #include "core/html/URLRegistry.h"
     33 #include "core/platform/Timer.h"
     34 #include "core/platform/mediastream/MediaStreamDescriptor.h"
     35 #include "modules/mediastream/MediaStreamTrack.h"
     36 #include "wtf/RefCounted.h"
     37 #include "wtf/RefPtr.h"
     38 
     39 namespace WebCore {
     40 
     41 class ExceptionState;
     42 
     43 class MediaStream : public RefCounted<MediaStream>, public ScriptWrappable, public URLRegistrable, public MediaStreamDescriptorClient, public EventTarget, public ContextLifecycleObserver {
     44 public:
     45     static PassRefPtr<MediaStream> create(ScriptExecutionContext*);
     46     static PassRefPtr<MediaStream> create(ScriptExecutionContext*, PassRefPtr<MediaStream>);
     47     static PassRefPtr<MediaStream> create(ScriptExecutionContext*, const MediaStreamTrackVector&);
     48     static PassRefPtr<MediaStream> create(ScriptExecutionContext*, PassRefPtr<MediaStreamDescriptor>);
     49     virtual ~MediaStream();
     50 
     51     // DEPRECATED
     52     String label() const { return m_descriptor->id(); }
     53 
     54     String id() const { return m_descriptor->id(); }
     55 
     56     void addTrack(PassRefPtr<MediaStreamTrack>, ExceptionState&);
     57     void removeTrack(PassRefPtr<MediaStreamTrack>, ExceptionState&);
     58     MediaStreamTrack* getTrackById(String);
     59 
     60     MediaStreamTrackVector getAudioTracks() const { return m_audioTracks; }
     61     MediaStreamTrackVector getVideoTracks() const { return m_videoTracks; }
     62 
     63     bool ended() const;
     64     void stop();
     65 
     66     DEFINE_ATTRIBUTE_EVENT_LISTENER(ended);
     67     DEFINE_ATTRIBUTE_EVENT_LISTENER(addtrack);
     68     DEFINE_ATTRIBUTE_EVENT_LISTENER(removetrack);
     69 
     70     // MediaStreamDescriptorClient
     71     virtual void trackEnded() OVERRIDE;
     72     virtual void streamEnded() OVERRIDE;
     73 
     74     MediaStreamDescriptor* descriptor() const { return m_descriptor.get(); }
     75 
     76     // EventTarget
     77     virtual const AtomicString& interfaceName() const OVERRIDE;
     78     virtual ScriptExecutionContext* scriptExecutionContext() const OVERRIDE;
     79 
     80     using RefCounted<MediaStream>::ref;
     81     using RefCounted<MediaStream>::deref;
     82 
     83     // URLRegistrable
     84     virtual URLRegistry& registry() const OVERRIDE;
     85 
     86 protected:
     87     MediaStream(ScriptExecutionContext*, PassRefPtr<MediaStreamDescriptor>);
     88 
     89     // EventTarget
     90     virtual EventTargetData* eventTargetData() OVERRIDE;
     91     virtual EventTargetData* ensureEventTargetData() OVERRIDE;
     92 
     93     // ContextLifecycleObserver
     94     virtual void contextDestroyed();
     95 
     96 private:
     97     // EventTarget
     98     virtual void refEventTarget() OVERRIDE { ref(); }
     99     virtual void derefEventTarget() OVERRIDE { deref(); }
    100 
    101     // MediaStreamDescriptorClient
    102     virtual void addRemoteTrack(MediaStreamComponent*) OVERRIDE;
    103     virtual void removeRemoteTrack(MediaStreamComponent*) OVERRIDE;
    104 
    105     void scheduleDispatchEvent(PassRefPtr<Event>);
    106     void scheduledEventTimerFired(Timer<MediaStream>*);
    107 
    108     bool m_stopped;
    109 
    110     EventTargetData m_eventTargetData;
    111 
    112     MediaStreamTrackVector m_audioTracks;
    113     MediaStreamTrackVector m_videoTracks;
    114     RefPtr<MediaStreamDescriptor> m_descriptor;
    115 
    116     Timer<MediaStream> m_scheduledEventTimer;
    117     Vector<RefPtr<Event> > m_scheduledEvents;
    118 };
    119 
    120 typedef Vector<RefPtr<MediaStream> > MediaStreamVector;
    121 
    122 } // namespace WebCore
    123 
    124 #endif // MediaStream_h
    125