| 1 | /* |
| 2 | * Copyright (C) 2012-2017 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 | #if ENABLE(VIDEO_TRACK) |
| 29 | |
| 30 | #include "Color.h" |
| 31 | #include "TrackPrivateBase.h" |
| 32 | #include <wtf/JSONValues.h> |
| 33 | #include <wtf/MediaTime.h> |
| 34 | |
| 35 | #if ENABLE(DATACUE_VALUE) |
| 36 | #include "SerializedPlatformRepresentation.h" |
| 37 | #endif |
| 38 | |
| 39 | namespace WebCore { |
| 40 | |
| 41 | class InbandTextTrackPrivate; |
| 42 | class ISOWebVTTCue; |
| 43 | |
| 44 | class GenericCueData : public RefCounted<GenericCueData> { |
| 45 | public: |
| 46 | static Ref<GenericCueData> create() { return adoptRef(*new GenericCueData); } |
| 47 | |
| 48 | MediaTime startTime() const { return m_startTime; } |
| 49 | void setStartTime(const MediaTime& startTime) { m_startTime = startTime; } |
| 50 | |
| 51 | MediaTime endTime() const { return m_endTime; } |
| 52 | void setEndTime(const MediaTime& endTime) { m_endTime = endTime; } |
| 53 | |
| 54 | const String& id() const { return m_id; } |
| 55 | void setId(const String& id) { m_id = id; } |
| 56 | |
| 57 | const String& content() const { return m_content; } |
| 58 | void setContent(const String& content) { m_content = content; } |
| 59 | |
| 60 | double line() const { return m_line; } |
| 61 | void setLine(double line) { m_line = line; } |
| 62 | |
| 63 | double position() const { return m_position; } |
| 64 | void setPosition(double position) { m_position = position; } |
| 65 | |
| 66 | double size() const { return m_size; } |
| 67 | void setSize(double size) { m_size = size; } |
| 68 | |
| 69 | enum Alignment { None, Start, Middle, End }; |
| 70 | Alignment align() const { return m_align; } |
| 71 | void setAlign(Alignment align) { m_align = align; } |
| 72 | |
| 73 | const String& fontName() const { return m_fontName; } |
| 74 | void setFontName(const String& fontName) { m_fontName = fontName; } |
| 75 | |
| 76 | double baseFontSize() const { return m_baseFontSize; } |
| 77 | void setBaseFontSize(double baseFontSize) { m_baseFontSize = baseFontSize; } |
| 78 | |
| 79 | double relativeFontSize() const { return m_relativeFontSize; } |
| 80 | void setRelativeFontSize(double relativeFontSize) { m_relativeFontSize = relativeFontSize; } |
| 81 | |
| 82 | const Color& foregroundColor() const { return m_foregroundColor; } |
| 83 | void setForegroundColor(const Color& color) { m_foregroundColor = color; } |
| 84 | |
| 85 | const Color& backgroundColor() const { return m_backgroundColor; } |
| 86 | void setBackgroundColor(const Color& color) { m_backgroundColor = color; } |
| 87 | |
| 88 | const Color& highlightColor() const { return m_highlightColor; } |
| 89 | void setHighlightColor(const Color& color) { m_highlightColor = color; } |
| 90 | |
| 91 | enum Status { Uninitialized, Partial, Complete }; |
| 92 | Status status() { return m_status; } |
| 93 | void setStatus(Status status) { m_status = status; } |
| 94 | |
| 95 | bool doesExtendCueData(const GenericCueData&) const; |
| 96 | |
| 97 | String toJSONString() const; |
| 98 | |
| 99 | private: |
| 100 | GenericCueData() = default; |
| 101 | |
| 102 | MediaTime m_startTime; |
| 103 | MediaTime m_endTime; |
| 104 | String m_id; |
| 105 | String m_content; |
| 106 | double m_line { -1 }; |
| 107 | double m_position { -1 }; |
| 108 | double m_size { -1 }; |
| 109 | Alignment m_align { None }; |
| 110 | String m_fontName; |
| 111 | double m_baseFontSize { 0 }; |
| 112 | double m_relativeFontSize { 0 }; |
| 113 | Color m_foregroundColor; |
| 114 | Color m_backgroundColor; |
| 115 | Color m_highlightColor; |
| 116 | Status m_status { Uninitialized }; |
| 117 | }; |
| 118 | |
| 119 | inline String GenericCueData::toJSONString() const |
| 120 | { |
| 121 | auto object = JSON::Object::create(); |
| 122 | |
| 123 | #if !LOG_DISABLED |
| 124 | object->setString("text"_s , m_content); |
| 125 | #endif |
| 126 | object->setDouble("start"_s , m_startTime.toDouble()); |
| 127 | object->setDouble("end"_s , m_endTime.toDouble()); |
| 128 | |
| 129 | const char* status; |
| 130 | switch (m_status) { |
| 131 | case GenericCueData::Uninitialized: |
| 132 | status = "Uninitialized" ; |
| 133 | break; |
| 134 | case GenericCueData::Partial: |
| 135 | status = "Partial" ; |
| 136 | break; |
| 137 | case GenericCueData::Complete: |
| 138 | status = "Complete" ; |
| 139 | break; |
| 140 | } |
| 141 | object->setString("status" , status); |
| 142 | |
| 143 | if (!m_id.isEmpty()) |
| 144 | object->setString("id" , m_id); |
| 145 | |
| 146 | if (m_line > 0) |
| 147 | object->setDouble("line"_s , m_line); |
| 148 | |
| 149 | if (m_size > 0) |
| 150 | object->setDouble("size"_s , m_size); |
| 151 | |
| 152 | if (m_position > 0) |
| 153 | object->setDouble("position"_s , m_position); |
| 154 | |
| 155 | if (m_align != None) { |
| 156 | const char* align; |
| 157 | switch (m_align) { |
| 158 | case GenericCueData::Start: |
| 159 | align = "Start" ; |
| 160 | break; |
| 161 | case GenericCueData::Middle: |
| 162 | align = "Middle" ; |
| 163 | break; |
| 164 | case GenericCueData::End: |
| 165 | align = "End" ; |
| 166 | break; |
| 167 | case GenericCueData::None: |
| 168 | align = "None" ; |
| 169 | break; |
| 170 | } |
| 171 | object->setString("align"_s , align); |
| 172 | } |
| 173 | |
| 174 | if (m_foregroundColor.isValid()) |
| 175 | object->setString("foregroundColor"_s , m_foregroundColor.serialized()); |
| 176 | |
| 177 | if (m_backgroundColor.isValid()) |
| 178 | object->setString("backgroundColor"_s , m_backgroundColor.serialized()); |
| 179 | |
| 180 | if (m_highlightColor.isValid()) |
| 181 | object->setString("highlightColor"_s , m_highlightColor.serialized()); |
| 182 | |
| 183 | if (m_baseFontSize) |
| 184 | object->setDouble("baseFontSize"_s , m_baseFontSize); |
| 185 | |
| 186 | if (m_relativeFontSize) |
| 187 | object->setDouble("relativeFontSize"_s , m_relativeFontSize); |
| 188 | |
| 189 | if (!m_fontName.isEmpty()) |
| 190 | object->setString("font"_s , m_fontName); |
| 191 | |
| 192 | return object->toJSONString(); |
| 193 | } |
| 194 | |
| 195 | inline bool GenericCueData::doesExtendCueData(const GenericCueData& other) const |
| 196 | { |
| 197 | if (m_relativeFontSize != other.m_relativeFontSize) |
| 198 | return false; |
| 199 | if (m_baseFontSize != other.m_baseFontSize) |
| 200 | return false; |
| 201 | if (m_position != other.m_position) |
| 202 | return false; |
| 203 | if (m_line != other.m_line) |
| 204 | return false; |
| 205 | if (m_size != other.m_size) |
| 206 | return false; |
| 207 | if (m_align != other.m_align) |
| 208 | return false; |
| 209 | if (m_foregroundColor != other.m_foregroundColor) |
| 210 | return false; |
| 211 | if (m_backgroundColor != other.m_backgroundColor) |
| 212 | return false; |
| 213 | if (m_highlightColor != other.m_highlightColor) |
| 214 | return false; |
| 215 | if (m_fontName != other.m_fontName) |
| 216 | return false; |
| 217 | if (m_id != other.m_id) |
| 218 | return false; |
| 219 | if (m_content != other.m_content) |
| 220 | return false; |
| 221 | |
| 222 | return true; |
| 223 | } |
| 224 | |
| 225 | class InbandTextTrackPrivateClient : public TrackPrivateBaseClient { |
| 226 | public: |
| 227 | virtual ~InbandTextTrackPrivateClient() = default; |
| 228 | |
| 229 | virtual void addDataCue(const MediaTime& start, const MediaTime& end, const void*, unsigned) = 0; |
| 230 | |
| 231 | #if ENABLE(DATACUE_VALUE) |
| 232 | virtual void addDataCue(const MediaTime& start, const MediaTime& end, Ref<SerializedPlatformRepresentation>&&, const String&) = 0; |
| 233 | virtual void updateDataCue(const MediaTime& start, const MediaTime& end, SerializedPlatformRepresentation&) = 0; |
| 234 | virtual void removeDataCue(const MediaTime& start, const MediaTime& end, SerializedPlatformRepresentation&) = 0; |
| 235 | #endif |
| 236 | |
| 237 | virtual void addGenericCue(GenericCueData&) = 0; |
| 238 | virtual void updateGenericCue(GenericCueData&) = 0; |
| 239 | virtual void removeGenericCue(GenericCueData&) = 0; |
| 240 | |
| 241 | virtual void parseWebVTTFileHeader(String&&) { ASSERT_NOT_REACHED(); } |
| 242 | virtual void parseWebVTTCueData(const char* data, unsigned length) = 0; |
| 243 | virtual void parseWebVTTCueData(const ISOWebVTTCue&) = 0; |
| 244 | }; |
| 245 | |
| 246 | } // namespace WebCore |
| 247 | |
| 248 | namespace WTF { |
| 249 | |
| 250 | template<typename Type> |
| 251 | struct LogArgument; |
| 252 | |
| 253 | template <> |
| 254 | struct LogArgument<WebCore::GenericCueData> { |
| 255 | static String toString(const WebCore::GenericCueData& cue) |
| 256 | { |
| 257 | return cue.toJSONString(); |
| 258 | } |
| 259 | }; |
| 260 | |
| 261 | } |
| 262 | |
| 263 | #endif |
| 264 | |