1/*
2 * Copyright (C) 2011, 2015 Ericsson AB. All rights reserved.
3 * Copyright (C) 2012 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
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer
15 * in the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of Ericsson nor the names of its contributors
18 * may be used to endorse or promote products derived from this
19 * 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#ifndef MediaStreamPrivate_h
35#define MediaStreamPrivate_h
36
37#if ENABLE(MEDIA_STREAM)
38
39#include "FloatSize.h"
40#include "MediaStreamTrack.h"
41#include "MediaStreamTrackPrivate.h"
42#include <wtf/Function.h>
43#include <wtf/HashMap.h>
44#include <wtf/LoggerHelper.h>
45#include <wtf/MediaTime.h>
46#include <wtf/RefCounted.h>
47#include <wtf/RefPtr.h>
48#include <wtf/UUID.h>
49#include <wtf/Vector.h>
50#include <wtf/WeakPtr.h>
51
52namespace WebCore {
53
54class MediaStream;
55class OrientationNotifier;
56
57class MediaStreamPrivate final
58 : public MediaStreamTrackPrivate::Observer
59 , public RefCounted<MediaStreamPrivate>
60 , public CanMakeWeakPtr<MediaStreamPrivate>
61#if !RELEASE_LOG_DISABLED
62 , private LoggerHelper
63#endif
64{
65public:
66 class Observer {
67 public:
68 virtual ~Observer() = default;
69
70 virtual void characteristicsChanged() { }
71 virtual void activeStatusChanged() { }
72 virtual void didAddTrack(MediaStreamTrackPrivate&) { }
73 virtual void didRemoveTrack(MediaStreamTrackPrivate&) { }
74 };
75
76 static Ref<MediaStreamPrivate> create(Ref<RealtimeMediaSource>&&);
77 static Ref<MediaStreamPrivate> create(const Vector<Ref<RealtimeMediaSource>>& audioSources, const Vector<Ref<RealtimeMediaSource>>& videoSources);
78 static Ref<MediaStreamPrivate> create(const MediaStreamTrackPrivateVector& tracks, String&& id = createCanonicalUUIDString()) { return adoptRef(*new MediaStreamPrivate(tracks, WTFMove(id))); }
79
80 virtual ~MediaStreamPrivate();
81
82 enum class NotifyClientOption { Notify, DontNotify };
83
84 void addObserver(Observer&);
85 void removeObserver(Observer&);
86
87 String id() const { return m_id; }
88
89 MediaStreamTrackPrivateVector tracks() const;
90 MediaStreamTrackPrivate* activeVideoTrack() { return m_activeVideoTrack; }
91
92 bool active() const { return m_isActive; }
93 void updateActiveState(NotifyClientOption);
94
95 void addTrack(RefPtr<MediaStreamTrackPrivate>&&, NotifyClientOption = NotifyClientOption::Notify);
96 void removeTrack(MediaStreamTrackPrivate&, NotifyClientOption = NotifyClientOption::Notify);
97
98 void startProducingData();
99 void stopProducingData();
100 bool isProducingData() const;
101
102 bool hasVideo() const;
103 bool hasAudio() const;
104 bool muted() const;
105
106 bool hasCaptureVideoSource() const;
107 bool hasCaptureAudioSource() const;
108
109 FloatSize intrinsicSize() const;
110
111 void monitorOrientation(OrientationNotifier&);
112
113#if !RELEASE_LOG_DISABLED
114 void setLogger(const Logger&, const void*);
115#endif
116
117private:
118 MediaStreamPrivate(const MediaStreamTrackPrivateVector&, String&&);
119
120 // MediaStreamTrackPrivate::Observer
121 void trackStarted(MediaStreamTrackPrivate&) override;
122 void trackEnded(MediaStreamTrackPrivate&) override;
123 void trackMutedChanged(MediaStreamTrackPrivate&) override;
124 void trackSettingsChanged(MediaStreamTrackPrivate&) override;
125 void trackEnabledChanged(MediaStreamTrackPrivate&) override;
126
127 void characteristicsChanged();
128 void updateActiveVideoTrack();
129
130 void scheduleDeferredTask(Function<void ()>&&);
131 void forEachObserver(const WTF::Function<void(Observer&)>&) const;
132
133#if !RELEASE_LOG_DISABLED
134 const Logger& logger() const final { ASSERT(m_logger); return *m_logger.get(); }
135 const void* logIdentifier() const final { return m_logIdentifier; }
136 const char* logClassName() const final { return "MediaStreamPrivate"; }
137 WTFLogChannel& logChannel() const final;
138
139 RefPtr<const Logger> m_logger;
140 const void* m_logIdentifier;
141#endif
142
143 HashSet<Observer*> m_observers;
144 String m_id;
145 MediaStreamTrackPrivate* m_activeVideoTrack { nullptr };
146 HashMap<String, RefPtr<MediaStreamTrackPrivate>> m_trackSet;
147 bool m_isActive { false };
148};
149
150typedef Vector<RefPtr<MediaStreamPrivate>> MediaStreamPrivateVector;
151
152} // namespace WebCore
153
154#endif // ENABLE(MEDIA_STREAM)
155
156#endif // MediaStreamPrivate_h
157