a328cd3a83ecbfca049e54abf70de9d78faf331b
[WebKit.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 #include "RealtimeMediaSourceCenter.h"
33
34 #if ENABLE(MEDIA_STREAM)
35 #include "AVCaptureDeviceManager.h"
36 #include "AVVideoCaptureSource.h"
37 #include "CoreAudioCaptureSource.h"
38 #include "DisplayCaptureManagerCocoa.h"
39 #include "Logging.h"
40 #include "MediaStreamPrivate.h"
41 #include "RuntimeEnabledFeatures.h"
42 #include "ScreenDisplayCaptureSourceMac.h"
43 #include "WindowDisplayCaptureSourceMac.h"
44 #include <wtf/MainThread.h>
45
46 namespace WebCore {
47
48 class VideoCaptureSourceFactoryMac final : public VideoCaptureFactory {
49 public:
50     CaptureSourceOrError createVideoCaptureSource(const CaptureDevice& device, String&& hashSalt, const MediaConstraints* constraints) final
51     {
52         ASSERT(device.type() == CaptureDevice::DeviceType::Camera);
53         return AVVideoCaptureSource::create(String { device.persistentId() }, WTFMove(hashSalt), constraints);
54     }
55
56 private:
57 #if PLATFORM(IOS_FAMILY)
58     void setVideoCapturePageState(bool interrupted, bool pageMuted)
59     {
60         if (activeSource())
61             activeSource()->setInterrupted(interrupted, pageMuted);
62     }
63 #endif
64
65     CaptureDeviceManager& videoCaptureDeviceManager() { return AVCaptureDeviceManager::singleton(); }
66 };
67
68 class DisplayCaptureSourceFactoryMac final : public DisplayCaptureFactory {
69 public:
70     CaptureSourceOrError createDisplayCaptureSource(const CaptureDevice& device, const MediaConstraints* constraints) final
71     {
72 #if PLATFORM(IOS_FAMILY)
73         UNUSED_PARAM(device);
74         UNUSED_PARAM(constraints);
75 #endif
76         switch (device.type()) {
77         case CaptureDevice::DeviceType::Screen:
78 #if PLATFORM(MAC)
79             return ScreenDisplayCaptureSourceMac::create(String { device.persistentId() }, constraints);
80 #endif
81         case CaptureDevice::DeviceType::Window:
82 #if PLATFORM(MAC)
83             return WindowDisplayCaptureSourceMac::create(String { device.persistentId() }, constraints);
84 #endif
85         case CaptureDevice::DeviceType::Microphone:
86         case CaptureDevice::DeviceType::Camera:
87         case CaptureDevice::DeviceType::Unknown:
88             ASSERT_NOT_REACHED();
89             break;
90         }
91
92         return { };
93     }
94 private:
95     CaptureDeviceManager& displayCaptureDeviceManager() { return DisplayCaptureManagerCocoa::singleton(); }
96 };
97
98 AudioCaptureFactory& RealtimeMediaSourceCenter::defaultAudioCaptureFactory()
99 {
100     return CoreAudioCaptureSource::factory();
101 }
102
103 VideoCaptureFactory& RealtimeMediaSourceCenter::defaultVideoCaptureFactory()
104 {
105     static NeverDestroyed<VideoCaptureSourceFactoryMac> factory;
106     return factory.get();
107 }
108
109 DisplayCaptureFactory& RealtimeMediaSourceCenter::defaultDisplayCaptureFactory()
110 {
111     static NeverDestroyed<DisplayCaptureSourceFactoryMac> factory;
112     return factory.get();
113 }
114
115 } // namespace WebCore
116
117 #endif // ENABLE(MEDIA_STREAM)