1/*
2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * Copyright (C) 2011 Ericsson AB. All rights reserved.
4 * Copyright (C) 2013 Apple Inc. All rights reserved.
5 * Copyright (C) 2013 Nokia Corporation and/or its subsidiary(-ies).
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1.  Redistributions of source code must retain the above copyright
11 *     notice, this list of conditions and the following disclaimer.
12 * 2.  Redistributions in binary form must reproduce the above copyright
13 *     notice, this list of conditions and the following disclaimer in the
14 *     documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef MediaStream_h
29#define MediaStream_h
30
31#if ENABLE(MEDIA_STREAM)
32
33#include "ContextDestructionObserver.h"
34#include "EventTarget.h"
35#include "ExceptionBase.h"
36#include "MediaStreamPrivate.h"
37#include "MediaStreamTrack.h"
38#include "ScriptWrappable.h"
39#include "Timer.h"
40#include "URLRegistry.h"
41#include <wtf/RefCounted.h>
42#include <wtf/RefPtr.h>
43
44namespace WebCore {
45
46class MediaStreamCenter;
47
48class MediaStream final : public RefCounted<MediaStream>, public URLRegistrable, public ScriptWrappable, public MediaStreamPrivateClient, public EventTargetWithInlineData, public ContextDestructionObserver {
49public:
50    class Observer {
51    public:
52        virtual void didAddOrRemoveTrack() = 0;
53    };
54
55    static PassRefPtr<MediaStream> create(ScriptExecutionContext&);
56    static PassRefPtr<MediaStream> create(ScriptExecutionContext&, PassRefPtr<MediaStream>);
57    static PassRefPtr<MediaStream> create(ScriptExecutionContext&, const Vector<RefPtr<MediaStreamTrack>>&);
58    static PassRefPtr<MediaStream> create(ScriptExecutionContext&, PassRefPtr<MediaStreamPrivate>);
59    virtual ~MediaStream();
60
61    String id() const { return m_private->id(); }
62
63    void addTrack(PassRefPtr<MediaStreamTrack>, ExceptionCode&);
64    void removeTrack(PassRefPtr<MediaStreamTrack>, ExceptionCode&);
65    MediaStreamTrack* getTrackById(String);
66
67    Vector<RefPtr<MediaStreamTrack>> getAudioTracks() const { return m_audioTracks; }
68    Vector<RefPtr<MediaStreamTrack>> getVideoTracks() const { return m_videoTracks; }
69    Vector<RefPtr<MediaStreamTrack>> getTracks();
70
71    PassRefPtr<MediaStream> clone();
72
73    DEFINE_ATTRIBUTE_EVENT_LISTENER(addtrack);
74    DEFINE_ATTRIBUTE_EVENT_LISTENER(removetrack);
75
76    bool active() const;
77    void setActive(bool);
78
79    DEFINE_ATTRIBUTE_EVENT_LISTENER(active);
80    DEFINE_ATTRIBUTE_EVENT_LISTENER(inactive);
81
82    MediaStreamPrivate* privateStream() const { return m_private.get(); }
83
84    // EventTarget
85    virtual EventTargetInterface eventTargetInterface() const final { return MediaStreamEventTargetInterfaceType; }
86    virtual ScriptExecutionContext* scriptExecutionContext() const final { return ContextDestructionObserver::scriptExecutionContext(); }
87
88    using RefCounted<MediaStream>::ref;
89    using RefCounted<MediaStream>::deref;
90
91    // URLRegistrable
92    virtual URLRegistry& registry() const override;
93
94    void addObserver(Observer*);
95    void removeObserver(Observer*);
96
97protected:
98    MediaStream(ScriptExecutionContext&, PassRefPtr<MediaStreamPrivate>);
99
100    // ContextDestructionObserver
101    virtual void contextDestroyed() override final;
102
103private:
104    // EventTarget
105    virtual void refEventTarget() override final { ref(); }
106    virtual void derefEventTarget() override final { deref(); }
107
108    // MediaStreamPrivateClient
109    virtual void trackDidEnd() override final;
110    virtual void setStreamIsActive(bool) override final;
111    virtual void addRemoteSource(MediaStreamSource*) override final;
112    virtual void removeRemoteSource(MediaStreamSource*) override final;
113    virtual void addRemoteTrack(MediaStreamTrackPrivate*) override final;
114    virtual void removeRemoteTrack(MediaStreamTrackPrivate*) override final;
115
116    bool removeTrack(PassRefPtr<MediaStreamTrack>);
117    bool addTrack(PassRefPtr<MediaStreamTrack>);
118
119    bool haveTrackWithSource(PassRefPtr<MediaStreamSource>);
120
121    void scheduleDispatchEvent(PassRefPtr<Event>);
122    void scheduledEventTimerFired(Timer<MediaStream>*);
123
124    void cloneMediaStreamTrackVector(Vector<RefPtr<MediaStreamTrack>>&, const Vector<RefPtr<MediaStreamTrack>>&);
125
126    Vector<RefPtr<MediaStreamTrack>>* trackVectorForType(MediaStreamSource::Type);
127
128    RefPtr<MediaStreamPrivate> m_private;
129    Vector<RefPtr<MediaStreamTrack>> m_audioTracks;
130    Vector<RefPtr<MediaStreamTrack>> m_videoTracks;
131
132    Timer<MediaStream> m_scheduledEventTimer;
133    Vector<RefPtr<Event>> m_scheduledEvents;
134
135    Vector<Observer*> m_observers;
136};
137
138} // namespace WebCore
139
140#endif // ENABLE(MEDIA_STREAM)
141
142#endif // MediaStream_h
143