bd8eb88bf3f9d884daca5f74b36b624c0bd0610a
[WebKit-https.git] / Source / WebCore / platform / mediastream / MediaStreamPrivate.cpp
1 /*
2  * Copyright (C) 2011, 2015 Ericsson AB. All rights reserved.
3  * Copyright (C) 2013 Google Inc. All rights reserved.
4  * Copyright (C) 2013 Nokia Corporation and/or its subsidiary(-ies).
5  * Copyright (C) 2015 Apple Inc. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are
9  * met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following disclaimer
15  * in the documentation and/or other materials provided with the
16  * distribution.
17  *     * Neither the name of Google Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived from
19  * this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include "config.h"
35 #include "MediaStreamPrivate.h"
36
37 #if ENABLE(MEDIA_STREAM)
38
39 #include "GraphicsContext.h"
40 #include "IntRect.h"
41 #include <wtf/MainThread.h>
42 #include <wtf/RefCounted.h>
43 #include <wtf/Vector.h>
44
45 namespace WebCore {
46
47 Ref<MediaStreamPrivate> MediaStreamPrivate::create(Ref<RealtimeMediaSource>&& source)
48 {
49     return MediaStreamPrivate::create(MediaStreamTrackPrivateVector::from(MediaStreamTrackPrivate::create(WTFMove(source))));
50 }
51
52 Ref<MediaStreamPrivate> MediaStreamPrivate::create(const Vector<Ref<RealtimeMediaSource>>& audioSources, const Vector<Ref<RealtimeMediaSource>>& videoSources)
53 {
54     MediaStreamTrackPrivateVector tracks;
55     tracks.reserveInitialCapacity(audioSources.size() + videoSources.size());
56
57     for (auto& source : audioSources)
58         tracks.uncheckedAppend(MediaStreamTrackPrivate::create(source.copyRef()));
59
60     for (auto& source : videoSources)
61         tracks.uncheckedAppend(MediaStreamTrackPrivate::create(source.copyRef()));
62
63     return MediaStreamPrivate::create(tracks);
64 }
65
66 MediaStreamPrivate::MediaStreamPrivate(const MediaStreamTrackPrivateVector& tracks, String&& id)
67     : m_id(WTFMove(id))
68 {
69     ASSERT(!m_id.isEmpty());
70
71     for (auto& track : tracks) {
72         track->addObserver(*this);
73         m_trackSet.add(track->id(), track);
74     }
75
76     updateActiveState(NotifyClientOption::DontNotify);
77 }
78
79 MediaStreamPrivate::~MediaStreamPrivate()
80 {
81     for (auto& track : m_trackSet.values())
82         track->removeObserver(*this);
83 }
84
85 void MediaStreamPrivate::addObserver(MediaStreamPrivate::Observer& observer)
86 {
87     m_observers.add(&observer);
88 }
89
90 void MediaStreamPrivate::removeObserver(MediaStreamPrivate::Observer& observer)
91 {
92     m_observers.remove(&observer);
93 }
94
95 void MediaStreamPrivate::forEachObserver(const WTF::Function<void(Observer&)>& apply) const
96 {
97     for (auto* observer : copyToVector(m_observers)) {
98         if (!m_observers.contains(observer))
99             continue;
100         apply(*observer);
101     }
102 }
103
104 MediaStreamTrackPrivateVector MediaStreamPrivate::tracks() const
105 {
106     return copyToVector(m_trackSet.values());
107 }
108
109 void MediaStreamPrivate::updateActiveState(NotifyClientOption notifyClientOption)
110 {
111     bool newActiveState = false;
112     for (auto& track : m_trackSet.values()) {
113         if (!track->ended()) {
114             newActiveState = true;
115             break;
116         }
117     }
118
119     updateActiveVideoTrack();
120
121     // A stream is active if it has at least one un-ended track.
122     if (newActiveState == m_isActive)
123         return;
124
125     m_isActive = newActiveState;
126
127     if (notifyClientOption == NotifyClientOption::Notify) {
128         forEachObserver([](auto& observer) {
129             observer.activeStatusChanged();
130         });
131     }
132 }
133
134 void MediaStreamPrivate::addTrack(RefPtr<MediaStreamTrackPrivate>&& track, NotifyClientOption notifyClientOption)
135 {
136     if (m_trackSet.contains(track->id()))
137         return;
138
139     track->addObserver(*this);
140     m_trackSet.add(track->id(), track);
141
142     if (notifyClientOption == NotifyClientOption::Notify) {
143         forEachObserver([&track](auto& observer) {
144             observer.didAddTrack(*track.get());
145         });
146     }
147
148     updateActiveState(notifyClientOption);
149     characteristicsChanged();
150 }
151
152 void MediaStreamPrivate::removeTrack(MediaStreamTrackPrivate& track, NotifyClientOption notifyClientOption)
153 {
154     if (!m_trackSet.remove(track.id()))
155         return;
156
157     track.removeObserver(*this);
158
159     if (notifyClientOption == NotifyClientOption::Notify) {
160         forEachObserver([&track](auto& observer) {
161             observer.didRemoveTrack(track);
162         });
163     }
164
165     updateActiveState(NotifyClientOption::Notify);
166     characteristicsChanged();
167 }
168
169 void MediaStreamPrivate::startProducingData()
170 {
171     for (auto& track : m_trackSet.values())
172         track->startProducingData();
173 }
174
175 void MediaStreamPrivate::stopProducingData()
176 {
177     for (auto& track : m_trackSet.values())
178         track->stopProducingData();
179 }
180
181 bool MediaStreamPrivate::isProducingData() const
182 {
183     for (auto& track : m_trackSet.values()) {
184         if (track->isProducingData())
185             return true;
186     }
187     return false;
188 }
189
190 void MediaStreamPrivate::setCaptureTracksMuted(bool muted)
191 {
192     for (auto& track : m_trackSet.values()) {
193         if (track->isCaptureTrack())
194             track->setMuted(muted);
195     }
196 }
197
198 bool MediaStreamPrivate::hasVideo() const
199 {
200     for (auto& track : m_trackSet.values()) {
201         if (track->type() == RealtimeMediaSource::Type::Video && track->enabled() && !track->ended())
202             return true;
203     }
204     return false;
205 }
206
207 bool MediaStreamPrivate::hasAudio() const
208 {
209     for (auto& track : m_trackSet.values()) {
210         if (track->type() == RealtimeMediaSource::Type::Audio && track->enabled() && !track->ended())
211             return true;
212     }
213     return false;
214 }
215
216 bool MediaStreamPrivate::hasCaptureVideoSource() const
217 {
218     for (auto& track : m_trackSet.values()) {
219         if (track->type() == RealtimeMediaSource::Type::Video && track->isCaptureTrack())
220             return true;
221     }
222     return false;
223 }
224
225 bool MediaStreamPrivate::hasCaptureAudioSource() const
226 {
227     for (auto& track : m_trackSet.values()) {
228         if (track->type() == RealtimeMediaSource::Type::Audio && track->isCaptureTrack())
229             return true;
230     }
231     return false;
232 }
233
234 bool MediaStreamPrivate::muted() const
235 {
236     for (auto& track : m_trackSet.values()) {
237         if (!track->muted() && !track->ended())
238             return false;
239     }
240     return true;
241 }
242
243 FloatSize MediaStreamPrivate::intrinsicSize() const
244 {
245     FloatSize size;
246
247     if (m_activeVideoTrack) {
248         const RealtimeMediaSourceSettings& setting = m_activeVideoTrack->settings();
249         size.setWidth(setting.width());
250         size.setHeight(setting.height());
251     }
252
253     return size;
254 }
255
256 void MediaStreamPrivate::updateActiveVideoTrack()
257 {
258     m_activeVideoTrack = nullptr;
259     for (auto& track : m_trackSet.values()) {
260         if (!track->ended() && track->type() == RealtimeMediaSource::Type::Video) {
261             m_activeVideoTrack = track.get();
262             break;
263         }
264     }
265 }
266
267 void MediaStreamPrivate::characteristicsChanged()
268 {
269     forEachObserver([](auto& observer) {
270         observer.characteristicsChanged();
271     });
272 }
273
274 void MediaStreamPrivate::trackMutedChanged(MediaStreamTrackPrivate&)
275 {
276     scheduleDeferredTask([this] {
277         characteristicsChanged();
278     });
279 }
280
281 void MediaStreamPrivate::trackSettingsChanged(MediaStreamTrackPrivate&)
282 {
283     characteristicsChanged();
284 }
285
286 void MediaStreamPrivate::trackEnabledChanged(MediaStreamTrackPrivate&)
287 {
288     updateActiveVideoTrack();
289
290     scheduleDeferredTask([this] {
291         characteristicsChanged();
292     });
293 }
294
295 void MediaStreamPrivate::trackStarted(MediaStreamTrackPrivate&)
296 {
297     scheduleDeferredTask([this] {
298         characteristicsChanged();
299     });
300 }
301
302 void MediaStreamPrivate::trackEnded(MediaStreamTrackPrivate&)
303 {
304     scheduleDeferredTask([this] {
305         updateActiveState(NotifyClientOption::Notify);
306         characteristicsChanged();
307     });
308 }
309
310 void MediaStreamPrivate::scheduleDeferredTask(Function<void ()>&& function)
311 {
312     ASSERT(function);
313     callOnMainThread([weakThis = makeWeakPtr(*this), function = WTFMove(function)] {
314         if (!weakThis)
315             return;
316
317         function();
318     });
319 }
320
321 void MediaStreamPrivate::monitorOrientation(OrientationNotifier& notifier)
322 {
323     for (auto& track : m_trackSet.values()) {
324         if (track->source().isCaptureSource() && track->type() == RealtimeMediaSource::Type::Video)
325             track->source().monitorOrientation(notifier);
326     }
327 }
328
329 } // namespace WebCore
330
331 #endif // ENABLE(MEDIA_STREAM)