1/*
2 * Copyright (C) 2014 Cable Television Labs Inc. All rights reserved.
3 * Copyright (C) 2014 Apple Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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 in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#pragma once
28
29#if ENABLE(VIDEO_TRACK)
30
31#include "SerializedPlatformRepresentation.h"
32#include "TextTrackCue.h"
33#include <JavaScriptCore/ArrayBuffer.h>
34#include <JavaScriptCore/JSCJSValue.h>
35#include <wtf/MediaTime.h>
36#include <wtf/TypeCasts.h>
37
38namespace WebCore {
39
40class ScriptExecutionContext;
41
42class DataCue final : public TextTrackCue {
43 WTF_MAKE_ISO_ALLOCATED(DataCue);
44public:
45 static Ref<DataCue> create(ScriptExecutionContext& context, const MediaTime& start, const MediaTime& end, ArrayBuffer& data)
46 {
47 return adoptRef(*new DataCue(context, start, end, data, emptyString()));
48 }
49
50 static Ref<DataCue> create(ScriptExecutionContext& context, const MediaTime& start, const MediaTime& end, const void* data, unsigned length)
51 {
52 return adoptRef(*new DataCue(context, start, end, data, length));
53 }
54
55 static Ref<DataCue> create(ScriptExecutionContext& context, const MediaTime& start, const MediaTime& end, ArrayBuffer& data, const String& type)
56 {
57 return adoptRef(*new DataCue(context, start, end, data, type));
58 }
59
60 static Ref<DataCue> create(ScriptExecutionContext& context, const MediaTime& start, const MediaTime& end, RefPtr<SerializedPlatformRepresentation>&& platformValue, const String& type)
61 {
62 return adoptRef(*new DataCue(context, start, end, WTFMove(platformValue), type));
63 }
64
65 static Ref<DataCue> create(ScriptExecutionContext& context, double start, double end, ArrayBuffer& data)
66 {
67 return adoptRef(*new DataCue(context, MediaTime::createWithDouble(start), MediaTime::createWithDouble(end), data, emptyString()));
68 }
69 static Ref<DataCue> create(ScriptExecutionContext& context, double start, double end, JSC::JSValue value, const String& type)
70 {
71 return adoptRef(*new DataCue(context, MediaTime::createWithDouble(start), MediaTime::createWithDouble(end), value, type));
72 }
73
74 virtual ~DataCue();
75 CueType cueType() const override { return Data; }
76
77 RefPtr<JSC::ArrayBuffer> data() const;
78 void setData(JSC::ArrayBuffer&);
79
80 const SerializedPlatformRepresentation* platformValue() const { return m_platformValue.get(); }
81
82 JSC::JSValue value(JSC::ExecState&) const;
83 void setValue(JSC::ExecState&, JSC::JSValue);
84
85 String type() const { return m_type; }
86 void setType(const String& type) { m_type = type; }
87
88 bool isEqual(const TextTrackCue&, CueMatchRules) const override;
89 bool cueContentsMatch(const TextTrackCue&) const override;
90 bool doesExtendCue(const TextTrackCue&) const override;
91
92 String toJSONString() const;
93
94private:
95 DataCue(ScriptExecutionContext&, const MediaTime& start, const MediaTime& end, ArrayBuffer&, const String&);
96 DataCue(ScriptExecutionContext&, const MediaTime& start, const MediaTime& end, const void*, unsigned);
97 DataCue(ScriptExecutionContext&, const MediaTime& start, const MediaTime& end, RefPtr<SerializedPlatformRepresentation>&&, const String&);
98 DataCue(ScriptExecutionContext&, const MediaTime& start, const MediaTime& end, JSC::JSValue, const String&);
99
100 JSC::JSValue valueOrNull() const;
101
102 RefPtr<ArrayBuffer> m_data;
103 String m_type;
104 RefPtr<SerializedPlatformRepresentation> m_platformValue;
105 JSC::JSValue m_value;
106};
107
108DataCue* toDataCue(TextTrackCue*);
109const DataCue* toDataCue(const TextTrackCue*);
110
111} // namespace WebCore
112
113namespace WTF {
114
115template<typename Type>
116struct LogArgument;
117
118template <>
119struct LogArgument<WebCore::DataCue> {
120 static String toString(const WebCore::DataCue& cue)
121 {
122 return cue.toJSONString();
123 }
124};
125
126}
127
128SPECIALIZE_TYPE_TRAITS_BEGIN(WebCore::DataCue)
129static bool isType(const WebCore::TextTrackCue& cue) { return cue.cueType() == WebCore::TextTrackCue::Data; }
130SPECIALIZE_TYPE_TRAITS_END()
131
132#endif
133