Add media stream release logging
[WebKit-https.git] / Source / WebCore / platform / mediastream / RealtimeMediaSourceSettings.cpp
1 /*
2  * Copyright (C) 2013-2019 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
33 #if ENABLE(MEDIA_STREAM)
34 #include "RealtimeMediaSourceSettings.h"
35
36 #include <wtf/NeverDestroyed.h>
37
38 namespace WebCore {
39
40 String RealtimeMediaSourceSettings::facingMode(RealtimeMediaSourceSettings::VideoFacingMode mode)
41 {
42     static const NeverDestroyed<String> values[] = {
43         MAKE_STATIC_STRING_IMPL("unknown"),
44         MAKE_STATIC_STRING_IMPL("user"),
45         MAKE_STATIC_STRING_IMPL("environment"),
46         MAKE_STATIC_STRING_IMPL("left"),
47         MAKE_STATIC_STRING_IMPL("right"),
48     };
49     static_assert(static_cast<size_t>(RealtimeMediaSourceSettings::VideoFacingMode::Unknown) == 0, "RealtimeMediaSourceSettings::VideoFacingMode::Unknown is not 0 as expected");
50     static_assert(static_cast<size_t>(RealtimeMediaSourceSettings::VideoFacingMode::User) == 1, "RealtimeMediaSourceSettings::VideoFacingMode::User is not 1 as expected");
51     static_assert(static_cast<size_t>(RealtimeMediaSourceSettings::VideoFacingMode::Environment) == 2, "RealtimeMediaSourceSettings::VideoFacingMode::Environment is not 2 as expected");
52     static_assert(static_cast<size_t>(RealtimeMediaSourceSettings::VideoFacingMode::Left) == 3, "RealtimeMediaSourceSettings::VideoFacingMode::Left is not 3 as expected");
53     static_assert(static_cast<size_t>(RealtimeMediaSourceSettings::VideoFacingMode::Right) == 4, "RealtimeMediaSourceSettings::VideoFacingMode::Right is not 4 as expected");
54     ASSERT(static_cast<size_t>(mode) < WTF_ARRAY_LENGTH(values));
55     return values[static_cast<size_t>(mode)];
56 }
57
58 RealtimeMediaSourceSettings::VideoFacingMode RealtimeMediaSourceSettings::videoFacingModeEnum(const String& mode)
59 {
60     if (mode == "user")
61         return RealtimeMediaSourceSettings::VideoFacingMode::User;
62     if (mode == "environment")
63         return RealtimeMediaSourceSettings::VideoFacingMode::Environment;
64     if (mode == "left")
65         return RealtimeMediaSourceSettings::VideoFacingMode::Left;
66     if (mode == "right")
67         return RealtimeMediaSourceSettings::VideoFacingMode::Right;
68
69     return RealtimeMediaSourceSettings::Unknown;
70 }
71
72 String RealtimeMediaSourceSettings::convertFlagsToString(const OptionSet<RealtimeMediaSourceSettings::Flag> flags)
73 {
74     StringBuilder builder;
75
76     builder.append("[ ");
77     for (auto flag : flags) {
78         if (!builder.isEmpty())
79             builder.append(", ");
80
81         switch (flag) {
82         case RealtimeMediaSourceSettings::Width:
83             builder.append("Width");
84             break;
85         case RealtimeMediaSourceSettings::Height:
86             builder.append("Height");
87             break;
88         case RealtimeMediaSourceSettings::AspectRatio:
89             builder.append("AspectRatio");
90             break;
91         case RealtimeMediaSourceSettings::FrameRate:
92             builder.append("FrameRate");
93             break;
94         case RealtimeMediaSourceSettings::FacingMode:
95             builder.append("FacingMode");
96             break;
97         case RealtimeMediaSourceSettings::Volume:
98             builder.append("Volume");
99             break;
100         case RealtimeMediaSourceSettings::SampleRate:
101             builder.append("SampleRate");
102             break;
103         case RealtimeMediaSourceSettings::SampleSize:
104             builder.append("SampleSize");
105             break;
106         case RealtimeMediaSourceSettings::EchoCancellation:
107             builder.append("EchoCancellation");
108             break;
109         case RealtimeMediaSourceSettings::DeviceId:
110             builder.append("DeviceId");
111             break;
112         case RealtimeMediaSourceSettings::GroupId:
113             builder.append("GroupId");
114             break;
115         case RealtimeMediaSourceSettings::Label:
116             builder.append("Label");
117             break;
118         case RealtimeMediaSourceSettings::DisplaySurface:
119             builder.append("DisplaySurface");
120             break;
121         case RealtimeMediaSourceSettings::LogicalSurface:
122             builder.append("LogicalSurface");
123             break;
124         }
125     }
126     builder.append(" ]");
127
128     return builder.toString();
129 }
130
131 OptionSet<RealtimeMediaSourceSettings::Flag> RealtimeMediaSourceSettings::difference(const RealtimeMediaSourceSettings& that) const
132 {
133     OptionSet<RealtimeMediaSourceSettings::Flag> difference;
134
135     if (width() != that.width())
136         difference.add(RealtimeMediaSourceSettings::Width);
137     if (height() != that.height())
138         difference.add(RealtimeMediaSourceSettings::Height);
139     if (aspectRatio() != that.aspectRatio())
140         difference.add(RealtimeMediaSourceSettings::AspectRatio);
141     if (frameRate() != that.frameRate())
142         difference.add(RealtimeMediaSourceSettings::FrameRate);
143     if (facingMode() != that.facingMode())
144         difference.add(RealtimeMediaSourceSettings::FacingMode);
145     if (volume() != that.volume())
146         difference.add(RealtimeMediaSourceSettings::Volume);
147     if (sampleRate() != that.sampleRate())
148         difference.add(RealtimeMediaSourceSettings::SampleRate);
149     if (sampleSize() != that.sampleSize())
150         difference.add(RealtimeMediaSourceSettings::SampleSize);
151     if (echoCancellation() != that.echoCancellation())
152         difference.add(RealtimeMediaSourceSettings::EchoCancellation);
153     if (deviceId() != that.deviceId())
154         difference.add(RealtimeMediaSourceSettings::DeviceId);
155     if (groupId() != that.groupId())
156         difference.add(RealtimeMediaSourceSettings::GroupId);
157     if (label() != that.label())
158         difference.add(RealtimeMediaSourceSettings::Label);
159     if (displaySurface() != that.displaySurface())
160         difference.add(RealtimeMediaSourceSettings::DisplaySurface);
161     if (logicalSurface() != that.logicalSurface())
162         difference.add(RealtimeMediaSourceSettings::LogicalSurface);
163
164     return difference;
165 }
166
167 String convertEnumerationToString(RealtimeMediaSourceSettings::VideoFacingMode enumerationValue)
168 {
169     static const NeverDestroyed<String> values[] = {
170         MAKE_STATIC_STRING_IMPL("Unknown"),
171         MAKE_STATIC_STRING_IMPL("User"),
172         MAKE_STATIC_STRING_IMPL("Environment"),
173         MAKE_STATIC_STRING_IMPL("Left"),
174         MAKE_STATIC_STRING_IMPL("Right"),
175     };
176     static_assert(static_cast<size_t>(RealtimeMediaSourceSettings::VideoFacingMode::Unknown) == 0, "RealtimeMediaSourceSettings::VideoFacingMode::Unknown is not 0 as expected");
177     static_assert(static_cast<size_t>(RealtimeMediaSourceSettings::VideoFacingMode::User) == 1, "RealtimeMediaSourceSettings::VideoFacingMode::User is not 1 as expected");
178     static_assert(static_cast<size_t>(RealtimeMediaSourceSettings::VideoFacingMode::Environment) == 2, "RealtimeMediaSourceSettings::VideoFacingMode::Environment is not 2 as expected");
179     static_assert(static_cast<size_t>(RealtimeMediaSourceSettings::VideoFacingMode::Left) == 3, "RealtimeMediaSourceSettings::VideoFacingMode::Left is not 3 as expected");
180     static_assert(static_cast<size_t>(RealtimeMediaSourceSettings::VideoFacingMode::Right) == 4, "RealtimeMediaSourceSettings::VideoFacingMode::Right is not 4 as expected");
181     ASSERT(static_cast<size_t>(enumerationValue) < WTF_ARRAY_LENGTH(values));
182     return values[static_cast<size_t>(enumerationValue)];
183 }
184
185 } // namespace WebCore
186
187 #endif // ENABLE(MEDIA_STREAM)