1/*
2 * Copyright (C) 2011 Ericsson AB. All rights reserved.
3 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Copyright (C) 2013-2018 Apple Inc. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
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
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * 3. Neither the name of Ericsson nor the names of its contributors
17 * may be used to endorse or promote products derived from this
18 * software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#pragma once
34
35#if ENABLE(MEDIA_STREAM)
36
37#include "ExceptionOr.h"
38#include "MediaStreamRequest.h"
39#include "RealtimeMediaSource.h"
40#include "RealtimeMediaSourceFactory.h"
41#include "RealtimeMediaSourceSupportedConstraints.h"
42#include <wtf/Function.h>
43#include <wtf/RefPtr.h>
44#include <wtf/text/WTFString.h>
45
46namespace WebCore {
47
48class CaptureDevice;
49class CaptureDeviceManager;
50class RealtimeMediaSourceSettings;
51class RealtimeMediaSourceSupportedConstraints;
52class TrackSourceInfo;
53
54struct MediaConstraints;
55
56class RealtimeMediaSourceCenter {
57public:
58 ~RealtimeMediaSourceCenter();
59
60 WEBCORE_EXPORT static RealtimeMediaSourceCenter& singleton();
61
62 using ValidConstraintsHandler = Function<void(Vector<CaptureDevice>&& audioDeviceUIDs, Vector<CaptureDevice>&& videoDeviceUIDs, String&&)>;
63 using InvalidConstraintsHandler = Function<void(const String& invalidConstraint)>;
64 WEBCORE_EXPORT void validateRequestConstraints(ValidConstraintsHandler&&, InvalidConstraintsHandler&&, const MediaStreamRequest&, String&&);
65
66 using NewMediaStreamHandler = Function<void(RefPtr<MediaStreamPrivate>&&)>;
67 void createMediaStream(Ref<const Logger>&&, NewMediaStreamHandler&&, String&&, CaptureDevice&& audioDevice, CaptureDevice&& videoDevice, const MediaStreamRequest&);
68
69 WEBCORE_EXPORT Vector<CaptureDevice> getMediaStreamDevices();
70
71 const RealtimeMediaSourceSupportedConstraints& supportedConstraints() { return m_supportedConstraints; }
72
73 WEBCORE_EXPORT AudioCaptureFactory& audioCaptureFactory();
74 WEBCORE_EXPORT void setAudioCaptureFactory(AudioCaptureFactory&);
75 WEBCORE_EXPORT void unsetAudioCaptureFactory(AudioCaptureFactory&);
76
77 WEBCORE_EXPORT VideoCaptureFactory& videoCaptureFactory();
78 WEBCORE_EXPORT void setVideoCaptureFactory(VideoCaptureFactory&);
79 WEBCORE_EXPORT void unsetVideoCaptureFactory(VideoCaptureFactory&);
80
81 WEBCORE_EXPORT DisplayCaptureFactory& displayCaptureFactory();
82 WEBCORE_EXPORT void setDisplayCaptureFactory(DisplayCaptureFactory&);
83 WEBCORE_EXPORT void unsetDisplayCaptureFactory(DisplayCaptureFactory&);
84
85 WEBCORE_EXPORT String hashStringWithSalt(const String& id, const String& hashSalt);
86
87 WEBCORE_EXPORT void setDevicesChangedObserver(std::function<void()>&&);
88
89 void setCapturePageState(bool interrupted, bool pageMuted);
90
91 void captureDevicesChanged();
92
93 WEBCORE_EXPORT static bool shouldInterruptAudioOnPageVisibilityChange();
94
95private:
96 RealtimeMediaSourceCenter();
97 friend class NeverDestroyed<RealtimeMediaSourceCenter>;
98
99 AudioCaptureFactory& defaultAudioCaptureFactory();
100 VideoCaptureFactory& defaultVideoCaptureFactory();
101 DisplayCaptureFactory& defaultDisplayCaptureFactory();
102
103 struct DeviceInfo {
104 unsigned fitnessScore;
105 CaptureDevice device;
106 };
107
108 void getDisplayMediaDevices(const MediaStreamRequest&, Vector<DeviceInfo>&, String&);
109 void getUserMediaDevices(const MediaStreamRequest&, String&&, Vector<DeviceInfo>& audioDevices, Vector<DeviceInfo>& videoDevices, String&);
110
111 RealtimeMediaSourceSupportedConstraints m_supportedConstraints;
112
113 WTF::Function<void()> m_deviceChangedObserver;
114
115 AudioCaptureFactory* m_audioCaptureFactoryOverride { nullptr };
116 VideoCaptureFactory* m_videoCaptureFactoryOverride { nullptr };
117 DisplayCaptureFactory* m_displayCaptureFactoryOverride { nullptr };
118
119 bool m_shouldInterruptAudioOnPageVisibilityChange { false };
120};
121
122} // namespace WebCore
123
124#endif // ENABLE(MEDIA_STREAM)
125
126