Add media stream release logging
[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-2019 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     ALWAYS_LOG(LOGIDENTIFIER, track->logIdentifier());
140
141     track->addObserver(*this);
142     m_trackSet.add(track->id(), track);
143
144     if (notifyClientOption == NotifyClientOption::Notify) {
145         forEachObserver([&track](auto& observer) {
146             observer.didAddTrack(*track.get());
147         });
148     }
149
150     updateActiveState(notifyClientOption);
151     characteristicsChanged();
152 }
153
154 void MediaStreamPrivate::removeTrack(MediaStreamTrackPrivate& track, NotifyClientOption notifyClientOption)
155 {
156     if (!m_trackSet.remove(track.id()))
157         return;
158
159     ALWAYS_LOG(LOGIDENTIFIER, track.logIdentifier());
160     track.removeObserver(*this);
161
162     if (notifyClientOption == NotifyClientOption::Notify) {
163         forEachObserver([&track](auto& observer) {
164             observer.didRemoveTrack(track);
165         });
166     }
167
168     updateActiveState(NotifyClientOption::Notify);
169     characteristicsChanged();
170 }
171
172 void MediaStreamPrivate::startProducingData()
173 {
174     ALWAYS_LOG(LOGIDENTIFIER);
175     for (auto& track : m_trackSet.values())
176         track->startProducingData();
177 }
178
179 void MediaStreamPrivate::stopProducingData()
180 {
181     ALWAYS_LOG(LOGIDENTIFIER);
182     for (auto& track : m_trackSet.values())
183         track->stopProducingData();
184 }
185
186 bool MediaStreamPrivate::isProducingData() const
187 {
188     for (auto& track : m_trackSet.values()) {
189         if (track->isProducingData())
190             return true;
191     }
192     return false;
193 }
194
195 void MediaStreamPrivate::setCaptureTracksMuted(bool muted)
196 {
197     ALWAYS_LOG(LOGIDENTIFIER, muted);
198     for (auto& track : m_trackSet.values()) {
199         if (track->isCaptureTrack())
200             track->setMuted(muted);
201     }
202 }
203
204 bool MediaStreamPrivate::hasVideo() const
205 {
206     for (auto& track : m_trackSet.values()) {
207         if (track->type() == RealtimeMediaSource::Type::Video && track->enabled() && !track->ended())
208             return true;
209     }
210     return false;
211 }
212
213 bool MediaStreamPrivate::hasAudio() const
214 {
215     for (auto& track : m_trackSet.values()) {
216         if (track->type() == RealtimeMediaSource::Type::Audio && track->enabled() && !track->ended())
217             return true;
218     }
219     return false;
220 }
221
222 bool MediaStreamPrivate::hasCaptureVideoSource() const
223 {
224     for (auto& track : m_trackSet.values()) {
225         if (track->type() == RealtimeMediaSource::Type::Video && track->isCaptureTrack())
226             return true;
227     }
228     return false;
229 }
230
231 bool MediaStreamPrivate::hasCaptureAudioSource() const
232 {
233     for (auto& track : m_trackSet.values()) {
234         if (track->type() == RealtimeMediaSource::Type::Audio && track->isCaptureTrack())
235             return true;
236     }
237     return false;
238 }
239
240 bool MediaStreamPrivate::muted() const
241 {
242     for (auto& track : m_trackSet.values()) {
243         if (!track->muted() && !track->ended())
244             return false;
245     }
246     return true;
247 }
248
249 FloatSize MediaStreamPrivate::intrinsicSize() const
250 {
251     FloatSize size;
252
253     if (m_activeVideoTrack) {
254         const RealtimeMediaSourceSettings& setting = m_activeVideoTrack->settings();
255         size.setWidth(setting.width());
256         size.setHeight(setting.height());
257     }
258
259     return size;
260 }
261
262 void MediaStreamPrivate::updateActiveVideoTrack()
263 {
264     m_activeVideoTrack = nullptr;
265     for (auto& track : m_trackSet.values()) {
266         if (!track->ended() && track->type() == RealtimeMediaSource::Type::Video) {
267             m_activeVideoTrack = track.get();
268             break;
269         }
270     }
271 }
272
273 void MediaStreamPrivate::characteristicsChanged()
274 {
275     forEachObserver([](auto& observer) {
276         observer.characteristicsChanged();
277     });
278 }
279
280 void MediaStreamPrivate::trackMutedChanged(MediaStreamTrackPrivate& track)
281 {
282 #if RELEASE_LOG_DISABLED
283     UNUSED_PARAM(track);
284 #endif
285
286     ALWAYS_LOG(LOGIDENTIFIER, track.logIdentifier(), " ", track.muted());
287     scheduleDeferredTask([this] {
288         characteristicsChanged();
289     });
290 }
291
292 void MediaStreamPrivate::trackSettingsChanged(MediaStreamTrackPrivate&)
293 {
294     characteristicsChanged();
295 }
296
297 void MediaStreamPrivate::trackEnabledChanged(MediaStreamTrackPrivate& track)
298 {
299 #if RELEASE_LOG_DISABLED
300     UNUSED_PARAM(track);
301 #endif
302
303     ALWAYS_LOG(LOGIDENTIFIER, track.logIdentifier(), " ", track.enabled());
304     updateActiveVideoTrack();
305
306     scheduleDeferredTask([this] {
307         characteristicsChanged();
308     });
309 }
310
311 void MediaStreamPrivate::trackStarted(MediaStreamTrackPrivate& track)
312 {
313 #if RELEASE_LOG_DISABLED
314     UNUSED_PARAM(track);
315 #endif
316
317     ALWAYS_LOG(LOGIDENTIFIER, track.logIdentifier());
318     scheduleDeferredTask([this] {
319         characteristicsChanged();
320     });
321 }
322
323 void MediaStreamPrivate::trackEnded(MediaStreamTrackPrivate& track)
324 {
325 #if RELEASE_LOG_DISABLED
326     UNUSED_PARAM(track);
327 #endif
328
329     ALWAYS_LOG(LOGIDENTIFIER, track.logIdentifier());
330     scheduleDeferredTask([this] {
331         updateActiveState(NotifyClientOption::Notify);
332         characteristicsChanged();
333     });
334 }
335
336 void MediaStreamPrivate::scheduleDeferredTask(Function<void ()>&& function)
337 {
338     ASSERT(function);
339     callOnMainThread([weakThis = makeWeakPtr(*this), function = WTFMove(function)] {
340         if (!weakThis)
341             return;
342
343         function();
344     });
345 }
346
347 void MediaStreamPrivate::monitorOrientation(OrientationNotifier& notifier)
348 {
349     for (auto& track : m_trackSet.values()) {
350         if (track->source().isCaptureSource() && track->type() == RealtimeMediaSource::Type::Video)
351             track->source().monitorOrientation(notifier);
352     }
353 }
354
355 #if !RELEASE_LOG_DISABLED
356 void MediaStreamPrivate::setLogger(const Logger& newLogger, const void* newLogIdentifier)
357 {
358     m_logger = &newLogger;
359     m_logIdentifier = newLogIdentifier;
360     ALWAYS_LOG(LOGIDENTIFIER);
361 }
362
363 WTFLogChannel& MediaStreamPrivate::logChannel() const
364 {
365     return LogWebRTC;
366 }
367 #endif
368
369 } // namespace WebCore
370
371 #endif // ENABLE(MEDIA_STREAM)