1/*
2 * Copyright (C) 2014 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#include "config.h"
27
28#if ENABLE(VIDEO) && ENABLE(DATACUE_VALUE) && (USE(AVFOUNDATION) || PLATFORM(IOS))
29#include "InbandMetadataTextTrackPrivateAVF.h"
30
31#include "InbandTextTrackPrivateClient.h"
32#include "Logging.h"
33#include <CoreMedia/CoreMedia.h>
34#include <wtf/PassOwnPtr.h>
35#include <wtf/text/CString.h>
36#include <wtf/text/WTFString.h>
37#include <wtf/unicode/CharacterNames.h>
38
39namespace WebCore {
40
41PassRefPtr<InbandMetadataTextTrackPrivateAVF> InbandMetadataTextTrackPrivateAVF::create(InbandTextTrackPrivate::Kind kind, InbandTextTrackPrivate::CueFormat cueFormat, const AtomicString& id)
42{
43    return adoptRef(new InbandMetadataTextTrackPrivateAVF(kind, cueFormat, id));
44}
45
46InbandMetadataTextTrackPrivateAVF::InbandMetadataTextTrackPrivateAVF(InbandTextTrackPrivate::Kind kind, InbandTextTrackPrivate::CueFormat cueFormat, const AtomicString& id)
47    : InbandTextTrackPrivate(cueFormat)
48    , m_kind(kind)
49    , m_id(id)
50    , m_currentCueStartTime(0)
51{
52}
53
54InbandMetadataTextTrackPrivateAVF::~InbandMetadataTextTrackPrivateAVF()
55{
56}
57
58#if ENABLE(DATACUE_VALUE)
59void InbandMetadataTextTrackPrivateAVF::addDataCue(double start, double end, PassRefPtr<SerializedPlatformRepresentation> prpCueData, const String& type)
60{
61    ASSERT(cueFormat() == Data);
62    if (!client())
63        return;
64
65    RefPtr<SerializedPlatformRepresentation> cueData = prpCueData;
66    m_currentCueStartTime = start;
67    if (end == std::numeric_limits<double>::infinity())
68        m_incompleteCues.append(new IncompleteMetaDataCue(start, cueData));
69    client()->addDataCue(this, start, end, cueData, type);
70}
71
72void InbandMetadataTextTrackPrivateAVF::updatePendingCueEndTimes(double time)
73{
74    if (time >= m_currentCueStartTime) {
75        for (size_t i = 0; i < m_incompleteCues.size(); i++) {
76            IncompleteMetaDataCue* partialCue = m_incompleteCues[i];
77
78            LOG(Media, "InbandMetadataTextTrackPrivateAVF::addDataCue(%p) - updating cue: start=%.2f, end=%.2f", this, partialCue->startTime(), time);
79            client()->updateDataCue(this, partialCue->startTime(), time, partialCue->cueData());
80        }
81    } else
82        LOG(Media, "InbandMetadataTextTrackPrivateAVF::addDataCue negative length cue(s) ignored: start=%.2f, end=%.2f\n", m_currentCueStartTime, time);
83
84    m_incompleteCues.resize(0);
85    m_currentCueStartTime = 0;
86}
87#endif
88
89void InbandMetadataTextTrackPrivateAVF::flushPartialCues()
90{
91    if (m_currentCueStartTime && m_incompleteCues.size())
92        LOG(Media, "InbandMetadataTextTrackPrivateAVF::resetCueValues flushing incomplete data for cues: start=%.2f\n", m_currentCueStartTime);
93
94    if (client()) {
95        for (size_t i = 0; i < m_incompleteCues.size(); i++) {
96            IncompleteMetaDataCue* partialCue = m_incompleteCues[i];
97            client()->removeDataCue(this, partialCue->startTime(), std::numeric_limits<double>::infinity(), partialCue->cueData());
98        }
99    }
100
101    m_incompleteCues.resize(0);
102    m_currentCueStartTime = 0;
103}
104
105} // namespace WebCore
106
107#endif // ENABLE(VIDEO) && (USE(AVFOUNDATION) || PLATFORM(IOS))
108