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 MediaStreamTrack_h
29#define MediaStreamTrack_h
30
31#if ENABLE(MEDIA_STREAM)
32
33#include "ActiveDOMObject.h"
34#include "EventTarget.h"
35#include "MediaStreamSource.h"
36#include "MediaStreamTrackPrivate.h"
37#include "ScriptWrappable.h"
38#include <wtf/RefCounted.h>
39#include <wtf/RefPtr.h>
40#include <wtf/Vector.h>
41#include <wtf/text/WTFString.h>
42
43namespace WebCore {
44
45class Dictionary;
46class MediaConstraintsImpl;
47class MediaSourceStates;
48class MediaStreamTrackSourcesCallback;
49class MediaStreamCapabilities;
50class MediaTrackConstraints;
51
52class MediaStreamTrack : public RefCounted<MediaStreamTrack>, public ScriptWrappable, public ActiveDOMObject, public EventTargetWithInlineData, public MediaStreamTrackPrivateClient {
53public:
54    class Observer {
55    public:
56        virtual ~Observer() { }
57        virtual void trackDidEnd() = 0;
58    };
59
60    virtual ~MediaStreamTrack();
61
62    virtual const AtomicString& kind() const = 0;
63    const String& id() const;
64    const String& label() const;
65
66    bool enabled() const;
67    void setEnabled(bool);
68
69    bool muted() const;
70    bool readonly() const;
71    bool remote() const;
72    bool stopped() const;
73
74    const AtomicString& readyState() const;
75
76    static void getSources(ScriptExecutionContext*, PassRefPtr<MediaStreamTrackSourcesCallback>, ExceptionCode&);
77
78    RefPtr<MediaTrackConstraints> getConstraints() const;
79    RefPtr<MediaSourceStates> states() const;
80    RefPtr<MediaStreamCapabilities> getCapabilities() const;
81    void applyConstraints(const Dictionary&);
82    void applyConstraints(PassRefPtr<MediaConstraints>);
83
84    RefPtr<MediaStreamTrack> clone();
85    void stopProducingData();
86
87    DEFINE_ATTRIBUTE_EVENT_LISTENER(mute);
88    DEFINE_ATTRIBUTE_EVENT_LISTENER(unmute);
89    DEFINE_ATTRIBUTE_EVENT_LISTENER(started);
90    DEFINE_ATTRIBUTE_EVENT_LISTENER(ended);
91    DEFINE_ATTRIBUTE_EVENT_LISTENER(overconstrained);
92
93    MediaStreamSource* source() const { return m_privateTrack->source(); }
94    MediaStreamTrackPrivate& privateTrack() { return m_privateTrack.get(); }
95
96    bool ended() const;
97
98    void addObserver(Observer*);
99    void removeObserver(Observer*);
100
101    // EventTarget
102    virtual EventTargetInterface eventTargetInterface() const override final { return MediaStreamTrackEventTargetInterfaceType; }
103    virtual ScriptExecutionContext* scriptExecutionContext() const override final { return ActiveDOMObject::scriptExecutionContext(); }
104
105    using RefCounted<MediaStreamTrack>::ref;
106    using RefCounted<MediaStreamTrack>::deref;
107
108protected:
109    explicit MediaStreamTrack(MediaStreamTrack&);
110    MediaStreamTrack(ScriptExecutionContext&, MediaStreamTrackPrivate&, const Dictionary*);
111
112    void setSource(PassRefPtr<MediaStreamSource>);
113
114private:
115
116    void configureTrackRendering();
117    void trackDidEnd();
118    void scheduleEventDispatch(PassRefPtr<Event>);
119    void dispatchQueuedEvents();
120
121    // ActiveDOMObject
122    virtual void stop() override final;
123
124    // EventTarget
125    virtual void refEventTarget() override final { ref(); }
126    virtual void derefEventTarget() override final { deref(); }
127
128    // MediaStreamTrackPrivateClient
129    void trackReadyStateChanged();
130    void trackMutedChanged();
131    void trackEnabledChanged();
132
133    Vector<RefPtr<Event>> m_scheduledEvents;
134
135    RefPtr<MediaConstraintsImpl> m_constraints;
136    Mutex m_mutex;
137
138    Vector<Observer*> m_observers;
139
140    Ref<MediaStreamTrackPrivate> m_privateTrack;
141    bool m_eventDispatchScheduled;
142
143    bool m_stoppingTrack;
144};
145
146} // namespace WebCore
147
148#endif // ENABLE(MEDIA_STREAM)
149
150#endif // MediaStreamTrack_h
151