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/events/EventTarget.h"
     32 #include "core/html/URLRegistry.h"
     33 #include "core/platform/mediastream/MediaStreamDescriptor.h"
     34 #include "modules/mediastream/MediaStreamTrack.h"
     35 #include "platform/Timer.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 EventTargetWithInlineData, public ContextLifecycleObserver {
     44     REFCOUNTED_EVENT_TARGET(MediaStream);
     45 public:
     46     static PassRefPtr<MediaStream> create(ExecutionContext*);
     47     static PassRefPtr<MediaStream> create(ExecutionContext*, PassRefPtr<MediaStream>);
     48     static PassRefPtr<MediaStream> create(ExecutionContext*, const MediaStreamTrackVector&);
     49     static PassRefPtr<MediaStream> create(ExecutionContext*, PassRefPtr<MediaStreamDescriptor>);
     50     virtual ~MediaStream();
     51 
     52     // DEPRECATED
     53     String label() const { return m_descriptor->id(); }
     54 
     55     String id() const { return m_descriptor->id(); }
     56 
     57     void addTrack(PassRefPtr<MediaStreamTrack>, ExceptionState&);
     58     void removeTrack(PassRefPtr<MediaStreamTrack>, ExceptionState&);
     59     MediaStreamTrack* getTrackById(String);
     60 
     61     MediaStreamTrackVector getAudioTracks() const { return m_audioTracks; }
     62     MediaStreamTrackVector getVideoTracks() const { return m_videoTracks; }
     63 
     64     bool ended() const;
     65     void stop();
     66 
     67     DEFINE_ATTRIBUTE_EVENT_LISTENER(ended);
     68     DEFINE_ATTRIBUTE_EVENT_LISTENER(addtrack);
     69     DEFINE_ATTRIBUTE_EVENT_LISTENER(removetrack);
     70 
     71     // MediaStreamDescriptorClient
     72     virtual void trackEnded() OVERRIDE;
     73     virtual void streamEnded() OVERRIDE;
     74 
     75     MediaStreamDescriptor* descriptor() const { return m_descriptor.get(); }
     76 
     77     // EventTarget
     78     virtual const AtomicString& interfaceName() const OVERRIDE;
     79     virtual ExecutionContext* executionContext() const OVERRIDE;
     80 
     81     // URLRegistrable
     82     virtual URLRegistry& registry() const OVERRIDE;
     83 
     84 protected:
     85     MediaStream(ExecutionContext*, PassRefPtr<MediaStreamDescriptor>);
     86 
     87     // ContextLifecycleObserver
     88     virtual void contextDestroyed();
     89 
     90 private:
     91     // MediaStreamDescriptorClient
     92     virtual void addRemoteTrack(MediaStreamComponent*) OVERRIDE;
     93     virtual void removeRemoteTrack(MediaStreamComponent*) OVERRIDE;
     94 
     95     void scheduleDispatchEvent(PassRefPtr<Event>);
     96     void scheduledEventTimerFired(Timer<MediaStream>*);
     97 
     98     bool m_stopped;
     99 
    100     MediaStreamTrackVector m_audioTracks;
    101     MediaStreamTrackVector m_videoTracks;
    102     RefPtr<MediaStreamDescriptor> m_descriptor;
    103 
    104     Timer<MediaStream> m_scheduledEventTimer;
    105     Vector<RefPtr<Event> > m_scheduledEvents;
    106 };
    107 
    108 typedef Vector<RefPtr<MediaStream> > MediaStreamVector;
    109 
    110 } // namespace WebCore
    111 
    112 #endif // MediaStream_h
    113