1 | /* |
2 | * Copyright (C) 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 | * 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 | #pragma once |
27 | |
28 | #include <wtf/text/WTFString.h> |
29 | |
30 | namespace WebCore { |
31 | |
32 | class CaptureDevice { |
33 | public: |
34 | enum class DeviceType { Unknown, Microphone, Camera, Screen, Window }; |
35 | |
36 | CaptureDevice(const String& persistentId, DeviceType type, const String& label, const String& groupId = emptyString()) |
37 | : m_persistentId(persistentId) |
38 | , m_type(type) |
39 | , m_label(label) |
40 | , m_groupId(groupId) |
41 | { |
42 | } |
43 | |
44 | CaptureDevice() = default; |
45 | |
46 | const String& persistentId() const { return m_persistentId; } |
47 | |
48 | const String& label() const { return m_label; } |
49 | |
50 | const String& groupId() const { return m_groupId; } |
51 | |
52 | DeviceType type() const { return m_type; } |
53 | |
54 | bool enabled() const { return m_enabled; } |
55 | void setEnabled(bool enabled) { m_enabled = enabled; } |
56 | |
57 | explicit operator bool() const { return m_type != DeviceType::Unknown; } |
58 | |
59 | #if ENABLE(MEDIA_STREAM) |
60 | template<class Encoder> |
61 | void encode(Encoder& encoder) const |
62 | { |
63 | encoder << m_persistentId; |
64 | encoder << m_label; |
65 | encoder << m_groupId; |
66 | encoder << m_enabled; |
67 | encoder.encodeEnum(m_type); |
68 | } |
69 | |
70 | template <class Decoder> |
71 | static Optional<CaptureDevice> decode(Decoder& decoder) |
72 | { |
73 | Optional<String> persistentId; |
74 | decoder >> persistentId; |
75 | if (!persistentId) |
76 | return WTF::nullopt; |
77 | |
78 | Optional<String> label; |
79 | decoder >> label; |
80 | if (!label) |
81 | return WTF::nullopt; |
82 | |
83 | Optional<String> groupId; |
84 | decoder >> groupId; |
85 | if (!groupId) |
86 | return WTF::nullopt; |
87 | |
88 | Optional<bool> enabled; |
89 | decoder >> enabled; |
90 | if (!enabled) |
91 | return WTF::nullopt; |
92 | |
93 | Optional<CaptureDevice::DeviceType> type; |
94 | decoder >> type; |
95 | if (!type) |
96 | return WTF::nullopt; |
97 | |
98 | Optional<CaptureDevice> device = {{ WTFMove(*persistentId), WTFMove(*type), WTFMove(*label), WTFMove(*groupId) }}; |
99 | device->setEnabled(*enabled); |
100 | return device; |
101 | } |
102 | #endif |
103 | |
104 | private: |
105 | String m_persistentId; |
106 | DeviceType m_type { DeviceType::Unknown }; |
107 | String m_label; |
108 | String m_groupId; |
109 | bool m_enabled { false }; |
110 | }; |
111 | |
112 | } // namespace WebCore |
113 | |
114 | #if ENABLE(MEDIA_STREAM) |
115 | namespace WTF { |
116 | |
117 | template<> struct EnumTraits<WebCore::CaptureDevice::DeviceType> { |
118 | using values = EnumValues< |
119 | WebCore::CaptureDevice::DeviceType, |
120 | WebCore::CaptureDevice::DeviceType::Unknown, |
121 | WebCore::CaptureDevice::DeviceType::Microphone, |
122 | WebCore::CaptureDevice::DeviceType::Camera, |
123 | WebCore::CaptureDevice::DeviceType::Screen, |
124 | WebCore::CaptureDevice::DeviceType::Window |
125 | >; |
126 | }; |
127 | |
128 | } // namespace WTF |
129 | #endif |
130 | |
131 | |