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 "EventTarget.h" |
34 | #include "MediaCanStartListener.h" |
35 | #include "MediaProducer.h" |
36 | #include "MediaStreamPrivate.h" |
37 | #include "MediaStreamTrack.h" |
38 | #include "PlatformMediaSession.h" |
39 | #include "ScriptWrappable.h" |
40 | #include "Timer.h" |
41 | #include "URLRegistry.h" |
42 | #include <wtf/HashMap.h> |
43 | #include <wtf/LoggerHelper.h> |
44 | #include <wtf/RefCounted.h> |
45 | #include <wtf/RefPtr.h> |
46 | |
47 | namespace WebCore { |
48 | |
49 | class Document; |
50 | |
51 | class MediaStream final |
52 | : public URLRegistrable |
53 | , public EventTargetWithInlineData |
54 | , public ActiveDOMObject |
55 | , public MediaStreamTrack::Observer |
56 | , public MediaStreamPrivate::Observer |
57 | , private MediaCanStartListener |
58 | , private PlatformMediaSessionClient |
59 | #if !RELEASE_LOG_DISABLED |
60 | , private LoggerHelper |
61 | #endif |
62 | , public RefCounted<MediaStream> { |
63 | WTF_MAKE_ISO_ALLOCATED(MediaStream); |
64 | public: |
65 | class Observer { |
66 | public: |
67 | virtual ~Observer() = default; |
68 | virtual void didAddOrRemoveTrack() = 0; |
69 | }; |
70 | |
71 | static Ref<MediaStream> create(ScriptExecutionContext&); |
72 | static Ref<MediaStream> create(ScriptExecutionContext&, MediaStream&); |
73 | static Ref<MediaStream> create(ScriptExecutionContext&, const MediaStreamTrackVector&); |
74 | static Ref<MediaStream> create(ScriptExecutionContext&, Ref<MediaStreamPrivate>&&); |
75 | virtual ~MediaStream(); |
76 | |
77 | String id() const { return m_private->id(); } |
78 | |
79 | void addTrack(MediaStreamTrack&); |
80 | void removeTrack(MediaStreamTrack&); |
81 | MediaStreamTrack* getTrackById(String); |
82 | |
83 | MediaStreamTrackVector getAudioTracks() const; |
84 | MediaStreamTrackVector getVideoTracks() const; |
85 | MediaStreamTrackVector getTracks() const; |
86 | |
87 | RefPtr<MediaStream> clone(); |
88 | |
89 | bool active() const { return m_isActive; } |
90 | bool muted() const { return m_private->muted(); } |
91 | |
92 | MediaStreamPrivate& privateStream() { return m_private.get(); } |
93 | |
94 | void startProducingData(); |
95 | void stopProducingData(); |
96 | |
97 | void endCaptureTracks(); |
98 | |
99 | // EventTarget |
100 | EventTargetInterface eventTargetInterface() const final { return MediaStreamEventTargetInterfaceType; } |
101 | ScriptExecutionContext* scriptExecutionContext() const final { return ContextDestructionObserver::scriptExecutionContext(); } |
102 | |
103 | using RefCounted<MediaStream>::ref; |
104 | using RefCounted<MediaStream>::deref; |
105 | |
106 | // URLRegistrable |
107 | URLRegistry& registry() const override; |
108 | |
109 | void addObserver(Observer*); |
110 | void removeObserver(Observer*); |
111 | |
112 | void addTrackFromPlatform(Ref<MediaStreamTrack>&&); |
113 | |
114 | Document* document() const; |
115 | |
116 | // ActiveDOMObject API. |
117 | bool hasPendingActivity() const final; |
118 | |
119 | enum class StreamModifier { DomAPI, Platform }; |
120 | bool internalAddTrack(Ref<MediaStreamTrack>&&, StreamModifier); |
121 | WEBCORE_EXPORT bool internalRemoveTrack(const String&, StreamModifier); |
122 | |
123 | protected: |
124 | MediaStream(ScriptExecutionContext&, const MediaStreamTrackVector&); |
125 | MediaStream(ScriptExecutionContext&, Ref<MediaStreamPrivate>&&); |
126 | |
127 | #if !RELEASE_LOG_DISABLED |
128 | const Logger& logger() const final { return m_logger.get(); } |
129 | const void* logIdentifier() const final { return m_logIdentifier; } |
130 | WTFLogChannel& logChannel() const final; |
131 | const char* logClassName() const final { return "MediaStream" ; } |
132 | #endif |
133 | |
134 | private: |
135 | |
136 | // EventTarget |
137 | void refEventTarget() final { ref(); } |
138 | void derefEventTarget() final { deref(); } |
139 | |
140 | // MediaStreamTrack::Observer |
141 | void trackDidEnd() final; |
142 | |
143 | // MediaStreamPrivate::Observer |
144 | void activeStatusChanged() final; |
145 | void didAddTrack(MediaStreamTrackPrivate&) final; |
146 | void didRemoveTrack(MediaStreamTrackPrivate&) final; |
147 | void characteristicsChanged() final; |
148 | |
149 | MediaProducer::MediaStateFlags mediaState() const; |
150 | |
151 | // MediaCanStartListener |
152 | void mediaCanStart(Document&) final; |
153 | |
154 | // PlatformMediaSessionClient |
155 | PlatformMediaSession::MediaType mediaType() const final; |
156 | PlatformMediaSession::MediaType presentationType() const final; |
157 | PlatformMediaSession::CharacteristicsFlags characteristics() const final; |
158 | void mayResumePlayback(bool shouldResume) final; |
159 | void suspendPlayback() final; |
160 | bool canReceiveRemoteControlCommands() const final { return false; } |
161 | void didReceiveRemoteControlCommand(PlatformMediaSession::RemoteControlCommandType, const PlatformMediaSession::RemoteCommandArgument*) final { } |
162 | bool supportsSeeking() const final { return false; } |
163 | bool shouldOverrideBackgroundPlaybackRestriction(PlatformMediaSession::InterruptionType) const final { return false; } |
164 | String sourceApplicationIdentifier() const final; |
165 | bool canProduceAudio() const final; |
166 | Document* hostingDocument() const final { return document(); } |
167 | bool processingUserGestureForMedia() const final; |
168 | |
169 | // ActiveDOMObject API. |
170 | void stop() final; |
171 | const char* activeDOMObjectName() const final; |
172 | bool canSuspendForDocumentSuspension() const final; |
173 | |
174 | void updateActiveState(); |
175 | void activityEventTimerFired(); |
176 | void setIsActive(bool); |
177 | void statusDidChange(); |
178 | |
179 | MediaStreamTrackVector trackVectorForType(RealtimeMediaSource::Type) const; |
180 | |
181 | Ref<MediaStreamPrivate> m_private; |
182 | |
183 | HashMap<String, RefPtr<MediaStreamTrack>> m_trackSet; |
184 | |
185 | Vector<Observer*> m_observers; |
186 | std::unique_ptr<PlatformMediaSession> m_mediaSession; |
187 | |
188 | MediaProducer::MediaStateFlags m_state { MediaProducer::IsNotPlaying }; |
189 | |
190 | #if !RELEASE_LOG_DISABLED |
191 | Ref<Logger> m_logger; |
192 | const void* m_logIdentifier; |
193 | #endif |
194 | |
195 | bool m_isActive { false }; |
196 | bool m_isProducingData { false }; |
197 | bool m_isWaitingUntilMediaCanStart { false }; |
198 | }; |
199 | |
200 | } // namespace WebCore |
201 | |
202 | #endif // ENABLE(MEDIA_STREAM) |
203 | |