2 * Copyright (C) 2011 Google Inc. All rights reserved.
3 * Copyright (C) 2011, 2015 Ericsson AB. All rights reserved.
4 * Copyright (C) 2013-2017 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 "EventTarget.h"
34 #include "ExceptionBase.h"
35 #include "MediaCanStartListener.h"
36 #include "MediaProducer.h"
37 #include "MediaStreamPrivate.h"
38 #include "MediaStreamTrack.h"
39 #include "PlatformMediaSession.h"
40 #include "ScriptWrappable.h"
42 #include "URLRegistry.h"
43 #include <wtf/HashMap.h>
44 #include <wtf/RefCounted.h>
45 #include <wtf/RefPtr.h>
51 class MediaStream final
52 : public URLRegistrable
53 , public EventTargetWithInlineData
54 , public ActiveDOMObject
55 , public MediaStreamTrack::Observer
56 , public MediaStreamPrivate::Observer
57 , private MediaProducer
58 , private MediaCanStartListener
59 , private PlatformMediaSessionClient
60 , public RefCounted<MediaStream> {
64 virtual ~Observer() { }
65 virtual void didAddOrRemoveTrack() = 0;
68 static Ref<MediaStream> create(ScriptExecutionContext&);
69 static Ref<MediaStream> create(ScriptExecutionContext&, MediaStream&);
70 static Ref<MediaStream> create(ScriptExecutionContext&, const MediaStreamTrackVector&);
71 static Ref<MediaStream> create(ScriptExecutionContext&, Ref<MediaStreamPrivate>&&);
72 virtual ~MediaStream();
74 String id() const { return m_private->id(); }
76 void addTrack(MediaStreamTrack&);
77 void removeTrack(MediaStreamTrack&);
78 MediaStreamTrack* getTrackById(String);
80 MediaStreamTrackVector getAudioTracks() const;
81 MediaStreamTrackVector getVideoTracks() const;
82 MediaStreamTrackVector getTracks() const;
84 RefPtr<MediaStream> clone();
86 bool active() const { return m_isActive; }
87 bool muted() const { return m_private->muted(); }
89 MediaStreamPrivate& privateStream() { return m_private.get(); }
91 void startProducingData();
92 void stopProducingData();
94 void endCaptureTracks();
97 EventTargetInterface eventTargetInterface() const final { return MediaStreamEventTargetInterfaceType; }
98 ScriptExecutionContext* scriptExecutionContext() const final { return ContextDestructionObserver::scriptExecutionContext(); }
100 using RefCounted<MediaStream>::ref;
101 using RefCounted<MediaStream>::deref;
104 URLRegistry& registry() const override;
106 void addObserver(Observer*);
107 void removeObserver(Observer*);
109 void addTrackFromPlatform(Ref<MediaStreamTrack>&&);
111 Document* document() const;
113 // ActiveDOMObject API.
114 bool hasPendingActivity() const final;
116 enum class StreamModifier { DomAPI, Platform };
117 bool internalAddTrack(Ref<MediaStreamTrack>&&, StreamModifier);
118 WEBCORE_EXPORT bool internalRemoveTrack(const String&, StreamModifier);
121 MediaStream(ScriptExecutionContext&, const MediaStreamTrackVector&);
122 MediaStream(ScriptExecutionContext&, Ref<MediaStreamPrivate>&&);
127 void refEventTarget() final { ref(); }
128 void derefEventTarget() final { deref(); }
130 // MediaStreamTrack::Observer
131 void trackDidEnd() final;
133 // MediaStreamPrivate::Observer
134 void activeStatusChanged() final;
135 void didAddTrack(MediaStreamTrackPrivate&) final;
136 void didRemoveTrack(MediaStreamTrackPrivate&) final;
137 void characteristicsChanged() final;
140 void pageMutedStateDidChange() final;
141 MediaProducer::MediaStateFlags mediaState() const final;
143 // MediaCanStartListener
144 void mediaCanStart(Document&) final;
146 // PlatformMediaSessionClient
147 PlatformMediaSession::MediaType mediaType() const final;
148 PlatformMediaSession::MediaType presentationType() const final;
149 PlatformMediaSession::CharacteristicsFlags characteristics() const final;
150 void mayResumePlayback(bool shouldResume) final;
151 void suspendPlayback() final;
152 bool canReceiveRemoteControlCommands() const final { return false; }
153 void didReceiveRemoteControlCommand(PlatformMediaSession::RemoteControlCommandType, const PlatformMediaSession::RemoteCommandArgument*) final { }
154 bool supportsSeeking() const final { return false; }
155 bool shouldOverrideBackgroundPlaybackRestriction(PlatformMediaSession::InterruptionType) const final { return false; }
156 String sourceApplicationIdentifier() const final;
157 bool canProduceAudio() const final;
158 const Document* hostingDocument() const final { return document(); }
159 bool processingUserGestureForMedia() const final;
161 // ActiveDOMObject API.
163 const char* activeDOMObjectName() const final;
164 bool canSuspendForDocumentSuspension() const final;
166 void scheduleActiveStateChange();
167 void activityEventTimerFired();
168 void setIsActive(bool);
169 void statusDidChange();
171 MediaStreamTrackVector trackVectorForType(RealtimeMediaSource::Type) const;
173 Ref<MediaStreamPrivate> m_private;
175 HashMap<String, RefPtr<MediaStreamTrack>> m_trackSet;
177 Timer m_activityEventTimer;
178 Vector<Ref<Event>> m_scheduledActivityEvents;
180 Vector<Observer*> m_observers;
181 std::unique_ptr<PlatformMediaSession> m_mediaSession;
183 MediaStateFlags m_state { IsNotPlaying };
185 bool m_isActive { false };
186 bool m_isProducingData { false };
187 bool m_isWaitingUntilMediaCanStart { false };
190 } // namespace WebCore
192 #endif // ENABLE(MEDIA_STREAM)