[MediaStream] RealtimeMediaSource should be able to vend hashed IDs
[WebKit-https.git] / Source / WebCore / platform / mediastream / mac / RealtimeMediaSourceCenterMac.cpp
1 /*
2  * Copyright (C) 2013-2016 Apple, Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer
12  *    in the documentation and/or other materials provided with the
13  *    distribution.
14  * 3. Neither the name of Ericsson nor the names of its contributors
15  *    may be used to endorse or promote products derived from this
16  *    software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32
33 #if ENABLE(MEDIA_STREAM)
34 #include "RealtimeMediaSourceCenterMac.h"
35
36 #include "AVAudioSessionCaptureDeviceManager.h"
37 #include "AVCaptureDeviceManager.h"
38 #include "AVVideoCaptureSource.h"
39 #include "CoreAudioCaptureDeviceManager.h"
40 #include "CoreAudioCaptureSource.h"
41 #include "DisplayCaptureManagerCocoa.h"
42 #include "Logging.h"
43 #include "MediaStreamPrivate.h"
44 #include "ScreenDisplayCaptureSourceMac.h"
45 #include <wtf/MainThread.h>
46
47 namespace WebCore {
48
49 class VideoCaptureSourceFactoryMac final : public VideoCaptureFactory {
50 public:
51     CaptureSourceOrError createVideoCaptureSource(const CaptureDevice& device, String&& hashSalt, const MediaConstraints* constraints) final
52     {
53         ASSERT(device.type() == CaptureDevice::DeviceType::Camera);
54         return AVVideoCaptureSource::create(String { device.persistentId() }, WTFMove(hashSalt), constraints);
55     }
56
57 #if PLATFORM(IOS)
58 private:
59     void setVideoCapturePageState(bool interrupted, bool pageMuted)
60     {
61         if (activeSource())
62             activeSource()->setInterrupted(interrupted, pageMuted);
63     }
64 #endif
65 };
66
67 class DisplayCaptureSourceFactoryMac final : public DisplayCaptureFactory {
68 public:
69     CaptureSourceOrError createDisplayCaptureSource(const CaptureDevice& device, const MediaConstraints* constraints) final
70     {
71 #if PLATFORM(IOS)
72         UNUSED_PARAM(device);
73         UNUSED_PARAM(constraints);
74 #endif
75         switch (device.type()) {
76         case CaptureDevice::DeviceType::Screen:
77 #if PLATFORM(MAC)
78             return ScreenDisplayCaptureSourceMac::create(String { device.persistentId() }, constraints);
79 #endif
80         case CaptureDevice::DeviceType::Window:
81 #if PLATFORM(MAC)
82             return WindowDisplayCaptureSourceMac::create(String { device.persistentId() }, constraints);
83 #endif
84         case CaptureDevice::DeviceType::Application:
85         case CaptureDevice::DeviceType::Browser:
86         case CaptureDevice::DeviceType::Microphone:
87         case CaptureDevice::DeviceType::Camera:
88         case CaptureDevice::DeviceType::Unknown:
89             ASSERT_NOT_REACHED();
90             break;
91         }
92
93         return { };
94     }
95 };
96
97 VideoCaptureFactory& RealtimeMediaSourceCenterMac::videoCaptureSourceFactory()
98 {
99     static NeverDestroyed<VideoCaptureSourceFactoryMac> factory;
100     return factory.get();
101 }
102
103 DisplayCaptureFactory& RealtimeMediaSourceCenterMac::displayCaptureSourceFactory()
104 {
105     static NeverDestroyed<DisplayCaptureSourceFactoryMac> factory;
106     return factory.get();
107 }
108
109 AudioCaptureFactory& RealtimeMediaSourceCenterMac::audioCaptureSourceFactory()
110 {
111     return RealtimeMediaSourceCenterMac::singleton().audioFactory();
112 }
113
114 RealtimeMediaSourceCenterMac& RealtimeMediaSourceCenterMac::singleton()
115 {
116     ASSERT(isMainThread());
117     static NeverDestroyed<RealtimeMediaSourceCenterMac> center;
118     return center;
119 }
120
121 RealtimeMediaSourceCenter& RealtimeMediaSourceCenter::platformCenter()
122 {
123     return RealtimeMediaSourceCenterMac::singleton();
124 }
125
126 RealtimeMediaSourceCenterMac::RealtimeMediaSourceCenterMac() = default;
127
128 RealtimeMediaSourceCenterMac::~RealtimeMediaSourceCenterMac() = default;
129
130
131 AudioCaptureFactory& RealtimeMediaSourceCenterMac::audioFactory()
132 {
133     if (m_audioFactoryOverride)
134         return *m_audioFactoryOverride;
135
136     return CoreAudioCaptureSource::factory();
137 }
138
139 VideoCaptureFactory& RealtimeMediaSourceCenterMac::videoFactory()
140 {
141     return videoCaptureSourceFactory();
142 }
143
144 DisplayCaptureFactory& RealtimeMediaSourceCenterMac::displayCaptureFactory()
145 {
146     return displayCaptureSourceFactory();
147 }
148
149 CaptureDeviceManager& RealtimeMediaSourceCenterMac::audioCaptureDeviceManager()
150 {
151 #if PLATFORM(MAC)
152     return CoreAudioCaptureDeviceManager::singleton();
153 #else
154     return AVAudioSessionCaptureDeviceManager::singleton();
155 #endif
156 }
157
158 CaptureDeviceManager& RealtimeMediaSourceCenterMac::videoCaptureDeviceManager()
159 {
160     return AVCaptureDeviceManager::singleton();
161 }
162
163 CaptureDeviceManager& RealtimeMediaSourceCenterMac::displayCaptureDeviceManager()
164 {
165     return DisplayCaptureManagerCocoa::singleton();
166 }
167
168 } // namespace WebCore
169
170 #endif // ENABLE(MEDIA_STREAM)