[MediaStream] Add Mac screen capture source
[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 RealtimeMediaSource::VideoCaptureFactory
50 {
51 public:
52     CaptureSourceOrError createVideoCaptureSource(const CaptureDevice& device, const MediaConstraints* constraints) final
53     {
54         switch (device.type()) {
55         case CaptureDevice::DeviceType::Camera:
56             return AVVideoCaptureSource::create(device.persistentId(), constraints);
57             break;
58         case CaptureDevice::DeviceType::Screen:
59 #if PLATFORM(MAC)
60             return ScreenDisplayCaptureSourceMac::create(device.persistentId(), constraints);
61             break;
62 #endif
63         case CaptureDevice::DeviceType::Application:
64         case CaptureDevice::DeviceType::Window:
65         case CaptureDevice::DeviceType::Browser:
66         case CaptureDevice::DeviceType::Microphone:
67         case CaptureDevice::DeviceType::Unknown:
68             ASSERT_NOT_REACHED();
69             break;
70         }
71
72         return { };
73     }
74
75 #if PLATFORM(IOS)
76 private:
77     void setVideoCapturePageState(bool interrupted, bool pageMuted)
78     {
79         if (activeSource())
80             activeSource()->setInterrupted(interrupted, pageMuted);
81     }
82 #endif
83 };
84
85 RealtimeMediaSource::VideoCaptureFactory& RealtimeMediaSourceCenterMac::videoCaptureSourceFactory()
86 {
87     static NeverDestroyed<VideoCaptureSourceFactoryMac> factory;
88     return factory.get();
89 }
90
91 RealtimeMediaSource::AudioCaptureFactory& RealtimeMediaSourceCenterMac::audioCaptureSourceFactory()
92 {
93     return RealtimeMediaSourceCenterMac::singleton().audioFactory();
94 }
95
96 RealtimeMediaSourceCenterMac& RealtimeMediaSourceCenterMac::singleton()
97 {
98     ASSERT(isMainThread());
99     static NeverDestroyed<RealtimeMediaSourceCenterMac> center;
100     return center;
101 }
102
103 RealtimeMediaSourceCenter& RealtimeMediaSourceCenter::platformCenter()
104 {
105     return RealtimeMediaSourceCenterMac::singleton();
106 }
107
108 RealtimeMediaSourceCenterMac::RealtimeMediaSourceCenterMac() = default;
109
110 RealtimeMediaSourceCenterMac::~RealtimeMediaSourceCenterMac() = default;
111
112
113 RealtimeMediaSource::AudioCaptureFactory& RealtimeMediaSourceCenterMac::audioFactory()
114 {
115     if (m_audioFactoryOverride)
116         return *m_audioFactoryOverride;
117
118     return CoreAudioCaptureSource::factory();
119 }
120
121 RealtimeMediaSource::VideoCaptureFactory& RealtimeMediaSourceCenterMac::videoFactory()
122 {
123     return videoCaptureSourceFactory();
124 }
125
126 CaptureDeviceManager& RealtimeMediaSourceCenterMac::audioCaptureDeviceManager()
127 {
128 #if PLATFORM(MAC)
129     return CoreAudioCaptureDeviceManager::singleton();
130 #else
131     return AVAudioSessionCaptureDeviceManager::singleton();
132 #endif
133 }
134
135 CaptureDeviceManager& RealtimeMediaSourceCenterMac::videoCaptureDeviceManager()
136 {
137     return AVCaptureDeviceManager::singleton();
138 }
139
140 CaptureDeviceManager& RealtimeMediaSourceCenterMac::displayCaptureDeviceManager()
141 {
142     return DisplayCaptureManagerCocoa::singleton();
143 }
144
145 } // namespace WebCore
146
147 #endif // ENABLE(MEDIA_STREAM)