1/*
2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * Copyright (C) 2011, 2015 Ericsson AB. All rights reserved.
4 * Copyright (C) 2013-2019 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#pragma once
29
30#if ENABLE(MEDIA_STREAM)
31
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"
41#include <wtf/LoggerHelper.h>
42
43namespace WebCore {
44
45class AudioSourceProvider;
46class Document;
47
48struct MediaTrackConstraints;
49
50class MediaStreamTrack
51 : public RefCounted<MediaStreamTrack>
52 , public ActiveDOMObject
53 , public EventTargetWithInlineData
54 , public MediaProducer
55 , private MediaStreamTrackPrivate::Observer
56#if !RELEASE_LOG_DISABLED
57 , private LoggerHelper
58#endif
59{
60 WTF_MAKE_ISO_ALLOCATED(MediaStreamTrack);
61public:
62 class Observer {
63 public:
64 virtual ~Observer() = default;
65 virtual void trackDidEnd() = 0;
66 };
67
68 static Ref<MediaStreamTrack> create(ScriptExecutionContext&, Ref<MediaStreamTrackPrivate>&&);
69 virtual ~MediaStreamTrack();
70
71 virtual bool isCanvas() const { return false; }
72
73 const AtomString& kind() const;
74 WEBCORE_EXPORT const String& id() const;
75 const String& label() const;
76
77 const AtomString& contentHint() const;
78 void setContentHint(const String&);
79
80 bool enabled() const;
81 void setEnabled(bool);
82
83 bool muted() const;
84 void setMuted(MediaProducer::MutedStateFlags);
85
86 enum class State { Live, Ended };
87 State readyState() const;
88
89 bool ended() const;
90
91 virtual RefPtr<MediaStreamTrack> clone();
92
93 enum class StopMode { Silently, PostEvent };
94 void stopTrack(StopMode = StopMode::Silently);
95
96 bool isCaptureTrack() const { return m_isCaptureTrack; }
97
98 struct TrackSettings {
99 Optional<int> width;
100 Optional<int> height;
101 Optional<double> aspectRatio;
102 Optional<double> frameRate;
103 String facingMode;
104 Optional<double> volume;
105 Optional<int> sampleRate;
106 Optional<int> sampleSize;
107 Optional<bool> echoCancellation;
108 Optional<bool> displaySurface;
109 String logicalSurface;
110 String deviceId;
111 String groupId;
112 };
113 TrackSettings getSettings() const;
114
115 struct TrackCapabilities {
116 Optional<LongRange> width;
117 Optional<LongRange> height;
118 Optional<DoubleRange> aspectRatio;
119 Optional<DoubleRange> frameRate;
120 Optional<Vector<String>> facingMode;
121 Optional<DoubleRange> volume;
122 Optional<LongRange> sampleRate;
123 Optional<LongRange> sampleSize;
124 Optional<Vector<bool>> echoCancellation;
125 String deviceId;
126 String groupId;
127 };
128 TrackCapabilities getCapabilities() const;
129
130 const MediaTrackConstraints& getConstraints() const { return m_constraints; }
131 void applyConstraints(const Optional<MediaTrackConstraints>&, DOMPromiseDeferred<void>&&);
132
133 RealtimeMediaSource& source() const { return m_private->source(); }
134 MediaStreamTrackPrivate& privateTrack() { return m_private.get(); }
135
136 AudioSourceProvider* audioSourceProvider();
137
138 // MediaProducer
139 void pageMutedStateDidChange() final;
140 MediaProducer::MediaStateFlags mediaState() const final;
141
142 void addObserver(Observer&);
143 void removeObserver(Observer&);
144
145 using RefCounted::ref;
146 using RefCounted::deref;
147
148 // ActiveDOMObject API.
149 bool hasPendingActivity() const final;
150
151 void setIdForTesting(String&& id) { m_private->setIdForTesting(WTFMove(id)); }
152
153#if !RELEASE_LOG_DISABLED
154 const Logger& logger() const final { return m_private->logger(); }
155 const void* logIdentifier() const final { return m_private->logIdentifier(); }
156#endif
157
158protected:
159 MediaStreamTrack(ScriptExecutionContext&, Ref<MediaStreamTrackPrivate>&&);
160
161 ScriptExecutionContext* scriptExecutionContext() const final { return ActiveDOMObject::scriptExecutionContext(); }
162
163 Ref<MediaStreamTrackPrivate> m_private;
164
165private:
166 explicit MediaStreamTrack(MediaStreamTrack&);
167
168 void configureTrackRendering();
169
170 Document* document() const;
171
172 // ActiveDOMObject API.
173 void stop() final;
174 const char* activeDOMObjectName() const final;
175 bool canSuspendForDocumentSuspension() const final;
176
177 // EventTarget
178 void refEventTarget() final { ref(); }
179 void derefEventTarget() final { deref(); }
180 EventTargetInterface eventTargetInterface() const final { return MediaStreamTrackEventTargetInterfaceType; }
181
182 // MediaStreamTrackPrivate::Observer
183 void trackStarted(MediaStreamTrackPrivate&) final;
184 void trackEnded(MediaStreamTrackPrivate&) final;
185 void trackMutedChanged(MediaStreamTrackPrivate&) final;
186 void trackSettingsChanged(MediaStreamTrackPrivate&) final;
187 void trackEnabledChanged(MediaStreamTrackPrivate&) final;
188
189#if !RELEASE_LOG_DISABLED
190 const char* logClassName() const final { return "MediaStreamTrack"; }
191 WTFLogChannel& logChannel() const final;
192#endif
193
194 Vector<Observer*> m_observers;
195
196 MediaTrackConstraints m_constraints;
197 Optional<DOMPromiseDeferred<void>> m_promise;
198 GenericTaskQueue<ScriptExecutionContext> m_taskQueue;
199 GenericTaskQueue<Timer> m_eventTaskQueue;
200
201 bool m_ended { false };
202 bool m_isCaptureTrack { false };
203};
204
205typedef Vector<RefPtr<MediaStreamTrack>> MediaStreamTrackVector;
206
207} // namespace WebCore
208
209#endif // ENABLE(MEDIA_STREAM)
210