2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * Copyright (C) 2011, 2015 Ericsson AB. All rights reserved.
4 * Copyright (C) 2013-2016 Apple Inc. All rights reserved.
5 * Copyright (C) 2013 Nokia Corporation and/or its subsidiary(-ies).
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
30 #if ENABLE(MEDIA_STREAM)
32 #include "ActiveDOMObject.h"
33 #include "DoubleRange.h"
34 #include "EventTarget.h"
35 #include "GenericTaskQueue.h"
36 #include "JSDOMPromiseDeferred.h"
37 #include "LongRange.h"
38 #include "MediaProducer.h"
39 #include "MediaStreamTrackPrivate.h"
40 #include "MediaTrackConstraints.h"
44 class AudioSourceProvider;
47 struct MediaTrackConstraints;
49 class MediaStreamTrack :
50 public RefCounted<MediaStreamTrack>,
51 public ActiveDOMObject,
52 public EventTargetWithInlineData,
53 private MediaProducer,
54 private MediaStreamTrackPrivate::Observer {
58 virtual ~Observer() { }
59 virtual void trackDidEnd() = 0;
62 static Ref<MediaStreamTrack> create(ScriptExecutionContext&, Ref<MediaStreamTrackPrivate>&&);
63 virtual ~MediaStreamTrack();
65 virtual bool isCanvas() const { return false; }
67 const AtomicString& kind() const;
68 WEBCORE_EXPORT const String& id() const;
69 const String& label() const;
72 void setEnabled(bool);
76 enum class State { Live, Ended };
77 State readyState() const;
81 Ref<MediaStreamTrack> clone();
83 enum class StopMode { Silently, PostEvent };
84 void stopTrack(StopMode = StopMode::Silently);
86 bool isCaptureTrack() const { return m_private->isCaptureTrack(); }
88 struct TrackSettings {
89 std::optional<int> width;
90 std::optional<int> height;
91 std::optional<double> aspectRatio;
92 std::optional<double> frameRate;
94 std::optional<double> volume;
95 std::optional<int> sampleRate;
96 std::optional<int> sampleSize;
97 std::optional<bool> echoCancellation;
101 WEBCORE_EXPORT TrackSettings getSettings() const;
103 struct TrackCapabilities {
104 std::optional<LongRange> width;
105 std::optional<LongRange> height;
106 std::optional<DoubleRange> aspectRatio;
107 std::optional<DoubleRange> frameRate;
108 std::optional<Vector<String>> facingMode;
109 std::optional<DoubleRange> volume;
110 std::optional<LongRange> sampleRate;
111 std::optional<LongRange> sampleSize;
112 std::optional<Vector<bool>> echoCancellation;
116 TrackCapabilities getCapabilities() const;
118 const MediaTrackConstraints& getConstraints() const { return m_constraints; }
119 void applyConstraints(const std::optional<MediaTrackConstraints>&, DOMPromiseDeferred<void>&&);
121 RealtimeMediaSource& source() const { return m_private->source(); }
122 MediaStreamTrackPrivate& privateTrack() { return m_private.get(); }
124 AudioSourceProvider* audioSourceProvider();
127 void pageMutedStateDidChange() final;
128 MediaProducer::MediaStateFlags mediaState() const final;
130 void addObserver(Observer&);
131 void removeObserver(Observer&);
133 using RefCounted::ref;
134 using RefCounted::deref;
136 // ActiveDOMObject API.
137 bool hasPendingActivity() const final;
140 MediaStreamTrack(ScriptExecutionContext&, Ref<MediaStreamTrackPrivate>&&);
143 explicit MediaStreamTrack(MediaStreamTrack&);
145 void configureTrackRendering();
147 Document* document() const;
149 // ActiveDOMObject API.
151 const char* activeDOMObjectName() const final;
152 bool canSuspendForDocumentSuspension() const final;
155 void refEventTarget() final { ref(); }
156 void derefEventTarget() final { deref(); }
157 EventTargetInterface eventTargetInterface() const final { return MediaStreamTrackEventTargetInterfaceType; }
158 ScriptExecutionContext* scriptExecutionContext() const final { return ActiveDOMObject::scriptExecutionContext(); }
160 // MediaStreamTrackPrivate::Observer
161 void trackStarted(MediaStreamTrackPrivate&) final;
162 void trackEnded(MediaStreamTrackPrivate&) final;
163 void trackMutedChanged(MediaStreamTrackPrivate&) final;
164 void trackSettingsChanged(MediaStreamTrackPrivate&) final;
165 void trackEnabledChanged(MediaStreamTrackPrivate&) final;
167 WeakPtr<MediaStreamTrack> createWeakPtr() { return m_weakPtrFactory.createWeakPtr(); }
169 Vector<Observer*> m_observers;
170 Ref<MediaStreamTrackPrivate> m_private;
172 MediaTrackConstraints m_constraints;
173 std::optional<DOMPromiseDeferred<void>> m_promise;
174 WeakPtrFactory<MediaStreamTrack> m_weakPtrFactory;
175 GenericTaskQueue<ScriptExecutionContext> m_taskQueue;
177 bool m_ended { false };
180 typedef Vector<RefPtr<MediaStreamTrack>> MediaStreamTrackVector;
182 } // namespace WebCore
184 #endif // ENABLE(MEDIA_STREAM)