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 MediaCanStartListener
58 , private PlatformMediaSessionClient
59 , public RefCounted<MediaStream> {
63 virtual ~Observer() { }
64 virtual void didAddOrRemoveTrack() = 0;
67 static Ref<MediaStream> create(ScriptExecutionContext&);
68 static Ref<MediaStream> create(ScriptExecutionContext&, MediaStream&);
69 static Ref<MediaStream> create(ScriptExecutionContext&, const MediaStreamTrackVector&);
70 static Ref<MediaStream> create(ScriptExecutionContext&, Ref<MediaStreamPrivate>&&);
71 virtual ~MediaStream();
73 String id() const { return m_private->id(); }
75 void addTrack(MediaStreamTrack&);
76 void removeTrack(MediaStreamTrack&);
77 MediaStreamTrack* getTrackById(String);
79 MediaStreamTrackVector getAudioTracks() const;
80 MediaStreamTrackVector getVideoTracks() const;
81 MediaStreamTrackVector getTracks() const;
83 RefPtr<MediaStream> clone();
85 bool active() const { return m_isActive; }
86 bool muted() const { return m_private->muted(); }
88 MediaStreamPrivate& privateStream() { return m_private.get(); }
90 void startProducingData();
91 void stopProducingData();
93 void endCaptureTracks();
96 EventTargetInterface eventTargetInterface() const final { return MediaStreamEventTargetInterfaceType; }
97 ScriptExecutionContext* scriptExecutionContext() const final { return ContextDestructionObserver::scriptExecutionContext(); }
99 using RefCounted<MediaStream>::ref;
100 using RefCounted<MediaStream>::deref;
103 URLRegistry& registry() const override;
105 void addObserver(Observer*);
106 void removeObserver(Observer*);
108 void addTrackFromPlatform(Ref<MediaStreamTrack>&&);
110 Document* document() const;
112 // ActiveDOMObject API.
113 bool hasPendingActivity() const final;
115 enum class StreamModifier { DomAPI, Platform };
116 bool internalAddTrack(Ref<MediaStreamTrack>&&, StreamModifier);
117 WEBCORE_EXPORT bool internalRemoveTrack(const String&, StreamModifier);
120 MediaStream(ScriptExecutionContext&, const MediaStreamTrackVector&);
121 MediaStream(ScriptExecutionContext&, Ref<MediaStreamPrivate>&&);
126 void refEventTarget() final { ref(); }
127 void derefEventTarget() final { deref(); }
129 // MediaStreamTrack::Observer
130 void trackDidEnd() final;
132 // MediaStreamPrivate::Observer
133 void activeStatusChanged() final;
134 void didAddTrack(MediaStreamTrackPrivate&) final;
135 void didRemoveTrack(MediaStreamTrackPrivate&) final;
136 void characteristicsChanged() final;
138 MediaProducer::MediaStateFlags mediaState() const;
140 // MediaCanStartListener
141 void mediaCanStart(Document&) final;
143 // PlatformMediaSessionClient
144 PlatformMediaSession::MediaType mediaType() const final;
145 PlatformMediaSession::MediaType presentationType() const final;
146 PlatformMediaSession::CharacteristicsFlags characteristics() const final;
147 void mayResumePlayback(bool shouldResume) final;
148 void suspendPlayback() final;
149 bool canReceiveRemoteControlCommands() const final { return false; }
150 void didReceiveRemoteControlCommand(PlatformMediaSession::RemoteControlCommandType, const PlatformMediaSession::RemoteCommandArgument*) final { }
151 bool supportsSeeking() const final { return false; }
152 bool shouldOverrideBackgroundPlaybackRestriction(PlatformMediaSession::InterruptionType) const final { return false; }
153 String sourceApplicationIdentifier() const final;
154 bool canProduceAudio() const final;
155 const Document* hostingDocument() const final { return document(); }
156 bool processingUserGestureForMedia() const final;
158 // ActiveDOMObject API.
160 const char* activeDOMObjectName() const final;
161 bool canSuspendForDocumentSuspension() const final;
163 void scheduleActiveStateChange();
164 void activityEventTimerFired();
165 void setIsActive(bool);
166 void statusDidChange();
168 MediaStreamTrackVector trackVectorForType(RealtimeMediaSource::Type) const;
170 Ref<MediaStreamPrivate> m_private;
172 HashMap<String, RefPtr<MediaStreamTrack>> m_trackSet;
174 Timer m_activityEventTimer;
175 Vector<Ref<Event>> m_scheduledActivityEvents;
177 Vector<Observer*> m_observers;
178 std::unique_ptr<PlatformMediaSession> m_mediaSession;
180 MediaProducer::MediaStateFlags m_state { MediaProducer::IsNotPlaying };
182 bool m_isActive { false };
183 bool m_isProducingData { false };
184 bool m_isWaitingUntilMediaCanStart { false };
187 } // namespace WebCore
189 #endif // ENABLE(MEDIA_STREAM)