Improve use of NeverDestroyed
[WebKit-https.git] / Source / WebCore / platform / mock / MockRealtimeMediaSource.cpp
1 /*
2  * Copyright (C) 2015 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 Google Inc. 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 "MockRealtimeMediaSource.h"
33
34 #if ENABLE(MEDIA_STREAM)
35
36 #include "CaptureDevice.h"
37 #include "Logging.h"
38 #include "MediaConstraints.h"
39 #include "NotImplemented.h"
40 #include "RealtimeMediaSourceSettings.h"
41 #include <math.h>
42 #include <wtf/CurrentTime.h>
43 #include <wtf/NeverDestroyed.h>
44 #include <wtf/text/StringView.h>
45
46 namespace WebCore {
47
48 Vector<CaptureDevice>& MockRealtimeMediaSource::audioDevices()
49 {
50     static auto info = makeNeverDestroyed([] {
51         Vector<CaptureDevice> vector;
52
53         auto captureDevice = CaptureDevice("239c24b0-2b15-11e3-8224-0800200c9a66", CaptureDevice::DeviceType::Audio, "Mock audio device 1");
54         captureDevice.setEnabled(true);
55         vector.append(WTFMove(captureDevice));
56
57         captureDevice = CaptureDevice("239c24b1-2b15-11e3-8224-0800200c9a66", CaptureDevice::DeviceType::Audio, "Mock audio device 2");
58         captureDevice.setEnabled(true);
59         vector.append(WTFMove(captureDevice));
60
61         return vector;
62     }());
63     return info;
64 }
65
66 Vector<CaptureDevice>& MockRealtimeMediaSource::videoDevices()
67 {
68     static auto info = makeNeverDestroyed([] {
69         Vector<CaptureDevice> vector;
70
71         auto captureDevice = CaptureDevice("239c24b2-2b15-11e3-8224-0800200c9a66", CaptureDevice::DeviceType::Video, "Mock video device 1");
72         captureDevice.setEnabled(true);
73         vector.append(WTFMove(captureDevice));
74
75         captureDevice = CaptureDevice("239c24b3-2b15-11e3-8224-0800200c9a66", CaptureDevice::DeviceType::Video, "Mock video device 2");
76         captureDevice.setEnabled(true);
77         vector.append(WTFMove(captureDevice));
78
79         return vector;
80     }());
81     return info;
82 }
83
84 MockRealtimeMediaSource::MockRealtimeMediaSource(const String& id, RealtimeMediaSource::Type type, const String& name)
85     : BaseRealtimeMediaSourceClass(id, type, name)
86 {
87     switch (type) {
88     case RealtimeMediaSource::Type::Audio:
89         m_deviceIndex = name == audioDevices()[0].label() ? 0 : 1;
90         setPersistentID(String(audioDevices()[m_deviceIndex].persistentId()));
91         return;
92     case RealtimeMediaSource::Type::Video:
93         m_deviceIndex = name == videoDevices()[0].label() ? 0 : 1;
94         setPersistentID(String(videoDevices()[m_deviceIndex].persistentId()));
95         return;
96     case RealtimeMediaSource::Type::None:
97         ASSERT_NOT_REACHED();
98     }
99 }
100
101 void MockRealtimeMediaSource::initializeCapabilities()
102 {
103     m_capabilities = std::make_unique<RealtimeMediaSourceCapabilities>(supportedConstraints());
104     m_capabilities->setDeviceId(id());
105     initializeCapabilities(*m_capabilities.get());
106 }
107
108 const RealtimeMediaSourceCapabilities& MockRealtimeMediaSource::capabilities() const
109 {
110     if (!m_capabilities)
111         const_cast<MockRealtimeMediaSource&>(*this).initializeCapabilities();
112     return *m_capabilities;
113 }
114
115 void MockRealtimeMediaSource::initializeSettings()
116 {
117     if (m_currentSettings.deviceId().isEmpty()) {
118         m_currentSettings.setSupportedConstraints(supportedConstraints());
119         m_currentSettings.setDeviceId(id());
120     }
121
122     updateSettings(m_currentSettings);
123 }
124
125 const RealtimeMediaSourceSettings& MockRealtimeMediaSource::settings() const
126 {
127     const_cast<MockRealtimeMediaSource&>(*this).initializeSettings();
128     return m_currentSettings;
129 }
130
131 RealtimeMediaSourceSupportedConstraints& MockRealtimeMediaSource::supportedConstraints()
132 {
133     if (m_supportedConstraints.supportsDeviceId())
134         return m_supportedConstraints;
135
136     m_supportedConstraints.setSupportsDeviceId(true);
137     initializeSupportedConstraints(m_supportedConstraints);
138
139     return m_supportedConstraints;
140 }
141
142 } // namespace WebCore
143
144 #endif // ENABLE(MEDIA_STREAM)