1/*
2 * Copyright (C) 2013 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. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef SampleMap_h
27#define SampleMap_h
28
29#if ENABLE(MEDIA_SOURCE)
30
31#include <map>
32#include <wtf/MediaTime.h>
33#include <wtf/RefPtr.h>
34
35namespace WebCore {
36
37class MediaSample;
38class SampleMap;
39
40class PresentationOrderSampleMap {
41    friend class SampleMap;
42public:
43    typedef std::map<MediaTime, RefPtr<MediaSample>> MapType;
44    typedef MapType::iterator iterator;
45    typedef MapType::reverse_iterator reverse_iterator;
46    typedef std::pair<iterator, iterator> iterator_range;
47
48    iterator begin() { return m_samples.begin(); }
49    iterator end() { return m_samples.end(); }
50    reverse_iterator rbegin() { return m_samples.rbegin(); }
51    reverse_iterator rend() { return m_samples.rend(); }
52
53    iterator findSampleWithPresentationTime(const MediaTime&);
54    iterator findSampleContainingPresentationTime(const MediaTime&);
55    iterator findSampleOnOrAfterPresentationTime(const MediaTime&);
56    reverse_iterator reverseFindSampleContainingPresentationTime(const MediaTime&);
57    reverse_iterator reverseFindSampleBeforePresentationTime(const MediaTime&);
58    iterator_range findSamplesBetweenPresentationTimes(const MediaTime&, const MediaTime&);
59    iterator_range findSamplesWithinPresentationRange(const MediaTime&, const MediaTime&);
60    iterator_range findSamplesWithinPresentationRangeFromEnd(const MediaTime&, const MediaTime&);
61
62private:
63    MapType m_samples;
64};
65
66class DecodeOrderSampleMap {
67    friend class SampleMap;
68public:
69    typedef std::pair<MediaTime, MediaTime> KeyType;
70    typedef std::map<KeyType, RefPtr<MediaSample>> MapType;
71    typedef MapType::iterator iterator;
72    typedef MapType::reverse_iterator reverse_iterator;
73    typedef std::pair<reverse_iterator, reverse_iterator> reverse_iterator_range;
74
75    iterator begin() { return m_samples.begin(); }
76    iterator end() { return m_samples.end(); }
77    reverse_iterator rbegin() { return m_samples.rbegin(); }
78    reverse_iterator rend() { return m_samples.rend(); }
79
80    iterator findSampleWithDecodeKey(const KeyType&);
81    reverse_iterator reverseFindSampleWithDecodeKey(const KeyType&);
82    reverse_iterator findSyncSamplePriorToPresentationTime(const MediaTime&, const MediaTime& threshold = MediaTime::positiveInfiniteTime());
83    reverse_iterator findSyncSamplePriorToDecodeIterator(reverse_iterator);
84    iterator findSyncSampleAfterPresentationTime(const MediaTime&, const MediaTime& threshold = MediaTime::positiveInfiniteTime());
85    iterator findSyncSampleAfterDecodeIterator(iterator);
86    reverse_iterator_range findDependentSamples(MediaSample*);
87
88private:
89    MapType m_samples;
90    PresentationOrderSampleMap m_presentationOrder;
91};
92
93class SampleMap {
94public:
95    SampleMap()
96        : m_totalSize(0)
97    {
98    }
99
100    bool empty() const;
101    void clear();
102    void addSample(PassRefPtr<MediaSample>);
103    void removeSample(MediaSample*);
104    size_t sizeInBytes() const { return m_totalSize; }
105
106    template<typename I>
107    void addRange(I begin, I end);
108
109    DecodeOrderSampleMap& decodeOrder() { return m_decodeOrder; }
110    const DecodeOrderSampleMap& decodeOrder() const { return m_decodeOrder; }
111    PresentationOrderSampleMap& presentationOrder() { return m_decodeOrder.m_presentationOrder; }
112    const PresentationOrderSampleMap& presentationOrder() const { return m_decodeOrder.m_presentationOrder; }
113
114private:
115    DecodeOrderSampleMap m_decodeOrder;
116    size_t m_totalSize;
117};
118
119template<typename I>
120void SampleMap::addRange(I begin, I end)
121{
122    for (I iter = begin; iter != end; ++iter)
123        addSample(iter->second);
124}
125
126}
127
128#endif
129
130#endif // SampleMap_h
131