| 1 | /* |
| 2 | * Copyright (C) 2013 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 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #ifndef RealtimeMediaSourceCapabilities_h |
| 27 | #define RealtimeMediaSourceCapabilities_h |
| 28 | |
| 29 | #if ENABLE(MEDIA_STREAM) |
| 30 | |
| 31 | #include "RealtimeMediaSourceSettings.h" |
| 32 | #include <wtf/NeverDestroyed.h> |
| 33 | #include <wtf/RefCounted.h> |
| 34 | #include <wtf/Vector.h> |
| 35 | #include <wtf/text/AtomicString.h> |
| 36 | |
| 37 | namespace WebCore { |
| 38 | |
| 39 | class CapabilityValueOrRange { |
| 40 | public: |
| 41 | |
| 42 | enum Type { |
| 43 | Undefined, |
| 44 | Double, |
| 45 | ULong, |
| 46 | DoubleRange, |
| 47 | ULongRange, |
| 48 | }; |
| 49 | Type type() const { return m_type; } |
| 50 | |
| 51 | union ValueUnion { |
| 52 | int asInt; |
| 53 | double asDouble; |
| 54 | }; |
| 55 | |
| 56 | CapabilityValueOrRange() |
| 57 | : m_type(Undefined) |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | CapabilityValueOrRange(double value) |
| 62 | : m_type(Double) |
| 63 | { |
| 64 | m_minOrValue.asDouble = value; |
| 65 | } |
| 66 | |
| 67 | CapabilityValueOrRange(int value) |
| 68 | : m_type(ULong) |
| 69 | { |
| 70 | m_minOrValue.asInt = value; |
| 71 | } |
| 72 | |
| 73 | CapabilityValueOrRange(double min, double max) |
| 74 | : m_type(DoubleRange) |
| 75 | { |
| 76 | m_minOrValue.asDouble = min; |
| 77 | m_max.asDouble = max; |
| 78 | } |
| 79 | |
| 80 | CapabilityValueOrRange(int min, int max) |
| 81 | : m_type(ULongRange) |
| 82 | { |
| 83 | m_minOrValue.asInt = min; |
| 84 | m_max.asInt = max; |
| 85 | } |
| 86 | |
| 87 | const ValueUnion& rangeMin() const |
| 88 | { |
| 89 | ASSERT(m_type == DoubleRange || m_type == ULongRange); |
| 90 | return m_minOrValue; |
| 91 | } |
| 92 | |
| 93 | const ValueUnion& rangeMax() const |
| 94 | { |
| 95 | ASSERT(m_type == DoubleRange || m_type == ULongRange); |
| 96 | return m_max; |
| 97 | } |
| 98 | |
| 99 | const ValueUnion& value() const |
| 100 | { |
| 101 | ASSERT(m_type == Double || m_type == ULong); |
| 102 | return m_minOrValue; |
| 103 | } |
| 104 | |
| 105 | template<class Encoder> void encode(Encoder&) const; |
| 106 | template<class Decoder> static bool decode(Decoder&, CapabilityValueOrRange&); |
| 107 | |
| 108 | private: |
| 109 | ValueUnion m_minOrValue; |
| 110 | ValueUnion m_max; |
| 111 | Type m_type; |
| 112 | }; |
| 113 | |
| 114 | template<class Encoder> |
| 115 | void CapabilityValueOrRange::encode(Encoder& encoder) const |
| 116 | { |
| 117 | encoder.encodeFixedLengthData(reinterpret_cast<const uint8_t*>(&m_minOrValue), sizeof(ValueUnion), alignof(ValueUnion)); |
| 118 | encoder.encodeFixedLengthData(reinterpret_cast<const uint8_t*>(&m_max), sizeof(ValueUnion), alignof(ValueUnion)); |
| 119 | encoder.encodeEnum(m_type); |
| 120 | } |
| 121 | |
| 122 | template<class Decoder> |
| 123 | bool CapabilityValueOrRange::decode(Decoder& decoder, CapabilityValueOrRange& valueOrRange) |
| 124 | { |
| 125 | return decoder.decodeFixedLengthData(reinterpret_cast<uint8_t*>(&valueOrRange.m_minOrValue), sizeof(ValueUnion), alignof(ValueUnion)) |
| 126 | && decoder.decodeFixedLengthData(reinterpret_cast<uint8_t*>(&valueOrRange.m_max), sizeof(ValueUnion), alignof(ValueUnion)) |
| 127 | && decoder.decodeEnum(valueOrRange.m_type); |
| 128 | } |
| 129 | |
| 130 | class RealtimeMediaSourceCapabilities { |
| 131 | public: |
| 132 | RealtimeMediaSourceCapabilities() = default; |
| 133 | RealtimeMediaSourceCapabilities(const RealtimeMediaSourceSupportedConstraints& supportedConstraints) |
| 134 | : m_supportedConstraints(supportedConstraints) |
| 135 | { |
| 136 | } |
| 137 | |
| 138 | ~RealtimeMediaSourceCapabilities() = default; |
| 139 | |
| 140 | static const RealtimeMediaSourceCapabilities& emptyCapabilities() |
| 141 | { |
| 142 | static NeverDestroyed<RealtimeMediaSourceCapabilities> emptyCapabilities; |
| 143 | return emptyCapabilities; |
| 144 | } |
| 145 | |
| 146 | bool supportsWidth() const { return m_supportedConstraints.supportsWidth(); } |
| 147 | const CapabilityValueOrRange& width() const { return m_width; } |
| 148 | void setWidth(const CapabilityValueOrRange& width) { m_width = width; } |
| 149 | |
| 150 | bool supportsHeight() const { return m_supportedConstraints.supportsHeight(); } |
| 151 | const CapabilityValueOrRange& height() const { return m_height; } |
| 152 | void setHeight(const CapabilityValueOrRange& height) { m_height = height; } |
| 153 | |
| 154 | bool supportsFrameRate() const { return m_supportedConstraints.supportsFrameRate(); } |
| 155 | const CapabilityValueOrRange& frameRate() const { return m_frameRate; } |
| 156 | void setFrameRate(const CapabilityValueOrRange& frameRate) { m_frameRate = frameRate; } |
| 157 | |
| 158 | bool supportsFacingMode() const { return m_supportedConstraints.supportsFacingMode(); } |
| 159 | const Vector<RealtimeMediaSourceSettings::VideoFacingMode>& facingMode() const { return m_facingMode; } |
| 160 | void addFacingMode(RealtimeMediaSourceSettings::VideoFacingMode mode) { m_facingMode.append(mode); } |
| 161 | |
| 162 | bool supportsAspectRatio() const { return m_supportedConstraints.supportsAspectRatio(); } |
| 163 | const CapabilityValueOrRange& aspectRatio() const { return m_aspectRatio; } |
| 164 | void setAspectRatio(const CapabilityValueOrRange& aspectRatio) { m_aspectRatio = aspectRatio; } |
| 165 | |
| 166 | bool supportsVolume() const { return m_supportedConstraints.supportsVolume(); } |
| 167 | const CapabilityValueOrRange& volume() const { return m_volume; } |
| 168 | void setVolume(const CapabilityValueOrRange& volume) { m_volume = volume; } |
| 169 | |
| 170 | bool supportsSampleRate() const { return m_supportedConstraints.supportsSampleRate(); } |
| 171 | const CapabilityValueOrRange& sampleRate() const { return m_sampleRate; } |
| 172 | void setSampleRate(const CapabilityValueOrRange& sampleRate) { m_sampleRate = sampleRate; } |
| 173 | |
| 174 | bool supportsSampleSize() const { return m_supportedConstraints.supportsSampleSize(); } |
| 175 | const CapabilityValueOrRange& sampleSize() const { return m_sampleSize; } |
| 176 | void setSampleSize(const CapabilityValueOrRange& sampleSize) { m_sampleSize = sampleSize; } |
| 177 | |
| 178 | enum class EchoCancellation { |
| 179 | ReadOnly = 0, |
| 180 | ReadWrite = 1, |
| 181 | }; |
| 182 | bool supportsEchoCancellation() const { return m_supportedConstraints.supportsEchoCancellation(); } |
| 183 | EchoCancellation echoCancellation() const { return m_echoCancellation; } |
| 184 | void setEchoCancellation(EchoCancellation echoCancellation) { m_echoCancellation = echoCancellation; } |
| 185 | |
| 186 | bool supportsDeviceId() const { return m_supportedConstraints.supportsDeviceId(); } |
| 187 | const AtomicString& deviceId() const { return m_deviceId; } |
| 188 | void setDeviceId(const AtomicString& id) { m_deviceId = id; } |
| 189 | |
| 190 | bool supportsGroupId() const { return m_supportedConstraints.supportsGroupId(); } |
| 191 | const AtomicString& groupId() const { return m_groupId; } |
| 192 | void setGroupId(const AtomicString& id) { m_groupId = id; } |
| 193 | |
| 194 | const RealtimeMediaSourceSupportedConstraints& supportedConstraints() const { return m_supportedConstraints; } |
| 195 | void setSupportedConstraints(const RealtimeMediaSourceSupportedConstraints& constraints) { m_supportedConstraints = constraints; } |
| 196 | |
| 197 | template<class Encoder> void encode(Encoder&) const; |
| 198 | template<class Decoder> static bool decode(Decoder&, RealtimeMediaSourceCapabilities&); |
| 199 | |
| 200 | private: |
| 201 | CapabilityValueOrRange m_width; |
| 202 | CapabilityValueOrRange m_height; |
| 203 | CapabilityValueOrRange m_aspectRatio; |
| 204 | CapabilityValueOrRange m_frameRate; |
| 205 | Vector<RealtimeMediaSourceSettings::VideoFacingMode> m_facingMode; |
| 206 | CapabilityValueOrRange m_volume; |
| 207 | CapabilityValueOrRange m_sampleRate; |
| 208 | CapabilityValueOrRange m_sampleSize; |
| 209 | EchoCancellation m_echoCancellation; |
| 210 | AtomicString m_deviceId; |
| 211 | AtomicString m_groupId; |
| 212 | |
| 213 | RealtimeMediaSourceSupportedConstraints m_supportedConstraints; |
| 214 | }; |
| 215 | |
| 216 | template<class Encoder> |
| 217 | void RealtimeMediaSourceCapabilities::encode(Encoder& encoder) const |
| 218 | { |
| 219 | encoder << m_width |
| 220 | << m_height |
| 221 | << m_aspectRatio |
| 222 | << m_frameRate |
| 223 | << m_facingMode |
| 224 | << m_volume |
| 225 | << m_sampleRate |
| 226 | << m_sampleSize |
| 227 | << m_deviceId |
| 228 | << m_groupId |
| 229 | << m_supportedConstraints; |
| 230 | encoder.encodeEnum(m_echoCancellation); |
| 231 | } |
| 232 | |
| 233 | template<class Decoder> |
| 234 | bool RealtimeMediaSourceCapabilities::decode(Decoder& decoder, RealtimeMediaSourceCapabilities& capabilities) |
| 235 | { |
| 236 | return decoder.decode(capabilities.m_width) |
| 237 | && decoder.decode(capabilities.m_height) |
| 238 | && decoder.decode(capabilities.m_aspectRatio) |
| 239 | && decoder.decode(capabilities.m_frameRate) |
| 240 | && decoder.decode(capabilities.m_facingMode) |
| 241 | && decoder.decode(capabilities.m_volume) |
| 242 | && decoder.decode(capabilities.m_sampleRate) |
| 243 | && decoder.decode(capabilities.m_sampleSize) |
| 244 | && decoder.decode(capabilities.m_deviceId) |
| 245 | && decoder.decode(capabilities.m_groupId) |
| 246 | && decoder.decode(capabilities.m_supportedConstraints) |
| 247 | && decoder.decodeEnum(capabilities.m_echoCancellation); |
| 248 | } |
| 249 | |
| 250 | } // namespace WebCore |
| 251 | |
| 252 | #endif // RealtimeMediaSourceCapabilities_h |
| 253 | |
| 254 | #endif |
| 255 | |