965229efdd625903147623c9318eb332d73c3017
[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.append(&observer);
88 }
89
90 void MediaStreamPrivate::removeObserver(MediaStreamPrivate::Observer& observer)
91 {
92     size_t pos = m_observers.find(&observer);
93     if (pos != notFound)
94         m_observers.remove(pos);
95 }
96
97 MediaStreamTrackPrivateVector MediaStreamPrivate::tracks() const
98 {
99     return copyToVector(m_trackSet.values());
100 }
101
102 void MediaStreamPrivate::updateActiveState(NotifyClientOption notifyClientOption)
103 {
104     bool newActiveState = false;
105     for (auto& track : m_trackSet.values()) {
106         if (!track->ended()) {
107             newActiveState = true;
108             break;
109         }
110     }
111
112     updateActiveVideoTrack();
113
114     // A stream is active if it has at least one un-ended track.
115     if (newActiveState == m_isActive)
116         return;
117
118     m_isActive = newActiveState;
119
120     if (notifyClientOption == NotifyClientOption::Notify) {
121         for (auto& observer : m_observers)
122             observer->activeStatusChanged();
123     }
124 }
125
126 void MediaStreamPrivate::addTrack(RefPtr<MediaStreamTrackPrivate>&& track, NotifyClientOption notifyClientOption)
127 {
128     if (m_trackSet.contains(track->id()))
129         return;
130
131     track->addObserver(*this);
132     m_trackSet.add(track->id(), track);
133
134     if (notifyClientOption == NotifyClientOption::Notify) {
135         for (auto& observer : m_observers)
136             observer->didAddTrack(*track.get());
137     }
138
139     updateActiveState(notifyClientOption);
140     characteristicsChanged();
141 }
142
143 void MediaStreamPrivate::removeTrack(MediaStreamTrackPrivate& track, NotifyClientOption notifyClientOption)
144 {
145     if (!m_trackSet.remove(track.id()))
146         return;
147
148     track.removeObserver(*this);
149
150     if (notifyClientOption == NotifyClientOption::Notify) {
151         for (auto& observer : m_observers)
152             observer->didRemoveTrack(track);
153     }
154
155     updateActiveState(NotifyClientOption::Notify);
156     characteristicsChanged();
157 }
158
159 void MediaStreamPrivate::startProducingData()
160 {
161     for (auto& track : m_trackSet.values())
162         track->startProducingData();
163 }
164
165 void MediaStreamPrivate::stopProducingData()
166 {
167     for (auto& track : m_trackSet.values())
168         track->stopProducingData();
169 }
170
171 bool MediaStreamPrivate::isProducingData() const
172 {
173     for (auto& track : m_trackSet.values()) {
174         if (track->isProducingData())
175             return true;
176     }
177     return false;
178 }
179
180 void MediaStreamPrivate::setCaptureTracksMuted(bool muted)
181 {
182     for (auto& track : m_trackSet.values()) {
183         if (track->isCaptureTrack())
184             track->setMuted(muted);
185     }
186 }
187
188 bool MediaStreamPrivate::hasVideo() const
189 {
190     for (auto& track : m_trackSet.values()) {
191         if (track->type() == RealtimeMediaSource::Type::Video && track->enabled() && !track->ended())
192             return true;
193     }
194     return false;
195 }
196
197 bool MediaStreamPrivate::hasAudio() const
198 {
199     for (auto& track : m_trackSet.values()) {
200         if (track->type() == RealtimeMediaSource::Type::Audio && track->enabled() && !track->ended())
201             return true;
202     }
203     return false;
204 }
205
206 bool MediaStreamPrivate::hasCaptureVideoSource() const
207 {
208     for (auto& track : m_trackSet.values()) {
209         if (track->type() == RealtimeMediaSource::Type::Video && track->isCaptureTrack())
210             return true;
211     }
212     return false;
213 }
214
215 bool MediaStreamPrivate::hasCaptureAudioSource() const
216 {
217     for (auto& track : m_trackSet.values()) {
218         if (track->type() == RealtimeMediaSource::Type::Audio && track->isCaptureTrack())
219             return true;
220     }
221     return false;
222 }
223
224 bool MediaStreamPrivate::muted() const
225 {
226     for (auto& track : m_trackSet.values()) {
227         if (!track->muted() && !track->ended())
228             return false;
229     }
230     return true;
231 }
232
233 FloatSize MediaStreamPrivate::intrinsicSize() const
234 {
235     FloatSize size;
236
237     if (m_activeVideoTrack) {
238         const RealtimeMediaSourceSettings& setting = m_activeVideoTrack->settings();
239         size.setWidth(setting.width());
240         size.setHeight(setting.height());
241     }
242
243     return size;
244 }
245
246 void MediaStreamPrivate::updateActiveVideoTrack()
247 {
248     m_activeVideoTrack = nullptr;
249     for (auto& track : m_trackSet.values()) {
250         if (!track->ended() && track->type() == RealtimeMediaSource::Type::Video) {
251             m_activeVideoTrack = track.get();
252             break;
253         }
254     }
255 }
256
257 void MediaStreamPrivate::characteristicsChanged()
258 {
259     for (auto& observer : m_observers)
260         observer->characteristicsChanged();
261 }
262
263 void MediaStreamPrivate::trackMutedChanged(MediaStreamTrackPrivate&)
264 {
265     scheduleDeferredTask([this] {
266         characteristicsChanged();
267     });
268 }
269
270 void MediaStreamPrivate::trackSettingsChanged(MediaStreamTrackPrivate&)
271 {
272     characteristicsChanged();
273 }
274
275 void MediaStreamPrivate::trackEnabledChanged(MediaStreamTrackPrivate&)
276 {
277     updateActiveVideoTrack();
278
279     scheduleDeferredTask([this] {
280         characteristicsChanged();
281     });
282 }
283
284 void MediaStreamPrivate::trackStarted(MediaStreamTrackPrivate&)
285 {
286     scheduleDeferredTask([this] {
287         characteristicsChanged();
288     });
289 }
290
291 void MediaStreamPrivate::trackEnded(MediaStreamTrackPrivate&)
292 {
293     scheduleDeferredTask([this] {
294         updateActiveState(NotifyClientOption::Notify);
295         characteristicsChanged();
296     });
297 }
298
299 void MediaStreamPrivate::scheduleDeferredTask(Function<void ()>&& function)
300 {
301     ASSERT(function);
302     callOnMainThread([weakThis = makeWeakPtr(*this), function = WTFMove(function)] {
303         if (!weakThis)
304             return;
305
306         function();
307     });
308 }
309
310 void MediaStreamPrivate::monitorOrientation(OrientationNotifier& notifier)
311 {
312     for (auto& track : m_trackSet.values()) {
313         if (track->source().isCaptureSource() && track->type() == RealtimeMediaSource::Type::Video)
314             track->source().monitorOrientation(notifier);
315     }
316 }
317
318 } // namespace WebCore
319
320 #endif // ENABLE(MEDIA_STREAM)