1 | /* |
2 | * Copyright (C) 2014 Cable Television Labs Inc. All rights reserved. |
3 | * Copyright (C) 2014-2017 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 COMPUTER, 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 COMPUTER, 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 | #include "config.h" |
28 | #include "InbandDataTextTrack.h" |
29 | |
30 | #if ENABLE(VIDEO_TRACK) |
31 | |
32 | #include "DataCue.h" |
33 | #include "HTMLMediaElement.h" |
34 | #include "InbandTextTrackPrivate.h" |
35 | #include <wtf/IsoMallocInlines.h> |
36 | |
37 | namespace WebCore { |
38 | |
39 | WTF_MAKE_ISO_ALLOCATED_IMPL(InbandDataTextTrack); |
40 | |
41 | inline InbandDataTextTrack::InbandDataTextTrack(ScriptExecutionContext& context, TextTrackClient& client, InbandTextTrackPrivate& trackPrivate) |
42 | : InbandTextTrack(context, client, trackPrivate) |
43 | { |
44 | } |
45 | |
46 | Ref<InbandDataTextTrack> InbandDataTextTrack::create(ScriptExecutionContext& context, TextTrackClient& client, InbandTextTrackPrivate& trackPrivate) |
47 | { |
48 | return adoptRef(*new InbandDataTextTrack(context, client, trackPrivate)); |
49 | } |
50 | |
51 | InbandDataTextTrack::~InbandDataTextTrack() = default; |
52 | |
53 | void InbandDataTextTrack::addDataCue(const MediaTime& start, const MediaTime& end, const void* data, unsigned length) |
54 | { |
55 | addCue(DataCue::create(*scriptExecutionContext(), start, end, data, length)); |
56 | } |
57 | |
58 | #if ENABLE(DATACUE_VALUE) |
59 | |
60 | void InbandDataTextTrack::addDataCue(const MediaTime& start, const MediaTime& end, Ref<SerializedPlatformRepresentation>&& platformValue, const String& type) |
61 | { |
62 | if (m_incompleteCueMap.contains(platformValue.ptr())) |
63 | return; |
64 | |
65 | auto cue = DataCue::create(*scriptExecutionContext(), start, end, platformValue.copyRef(), type); |
66 | if (hasCue(cue.ptr(), TextTrackCue::IgnoreDuration)) { |
67 | INFO_LOG(LOGIDENTIFIER, "ignoring already added cue: " , cue.get()); |
68 | return; |
69 | } |
70 | |
71 | if (end.isPositiveInfinite() && mediaElement()) { |
72 | cue->setEndTime(mediaElement()->durationMediaTime()); |
73 | m_incompleteCueMap.add(WTFMove(platformValue), cue.copyRef()); |
74 | } |
75 | |
76 | INFO_LOG(LOGIDENTIFIER, cue.get()); |
77 | |
78 | addCue(WTFMove(cue)); |
79 | } |
80 | |
81 | void InbandDataTextTrack::updateDataCue(const MediaTime& start, const MediaTime& inEnd, SerializedPlatformRepresentation& platformValue) |
82 | { |
83 | RefPtr<DataCue> cue = m_incompleteCueMap.get(&platformValue); |
84 | if (!cue) |
85 | return; |
86 | |
87 | cue->willChange(); |
88 | |
89 | MediaTime end = inEnd; |
90 | if (end.isPositiveInfinite() && mediaElement()) |
91 | end = mediaElement()->durationMediaTime(); |
92 | else |
93 | m_incompleteCueMap.remove(&platformValue); |
94 | |
95 | INFO_LOG(LOGIDENTIFIER, "was start = " , cue->startMediaTime(), ", end = " , cue->endMediaTime(), ", will be start = " , start, ", end = " , end); |
96 | |
97 | cue->setStartTime(start); |
98 | cue->setEndTime(end); |
99 | |
100 | cue->didChange(); |
101 | } |
102 | |
103 | void InbandDataTextTrack::removeDataCue(const MediaTime&, const MediaTime&, SerializedPlatformRepresentation& platformValue) |
104 | { |
105 | if (auto cue = m_incompleteCueMap.take(&platformValue)) { |
106 | INFO_LOG(LOGIDENTIFIER, "removing: " , *cue); |
107 | InbandTextTrack::removeCue(*cue); |
108 | } |
109 | } |
110 | |
111 | ExceptionOr<void> InbandDataTextTrack::removeCue(TextTrackCue& cue) |
112 | { |
113 | ASSERT(cue.cueType() == TextTrackCue::Data); |
114 | |
115 | m_incompleteCueMap.remove(const_cast<SerializedPlatformRepresentation*>(toDataCue(&cue)->platformValue())); |
116 | |
117 | return InbandTextTrack::removeCue(cue); |
118 | } |
119 | |
120 | #endif |
121 | |
122 | } // namespace WebCore |
123 | |
124 | #endif |
125 | |