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 | #pragma once |
27 | |
28 | #include <wtf/MediaTime.h> |
29 | #include <wtf/RefPtr.h> |
30 | #include <wtf/StdMap.h> |
31 | |
32 | namespace WebCore { |
33 | |
34 | class MediaSample; |
35 | class SampleMap; |
36 | |
37 | class PresentationOrderSampleMap { |
38 | friend class SampleMap; |
39 | public: |
40 | using MapType = StdMap<MediaTime, RefPtr<MediaSample>>; |
41 | typedef MapType::iterator iterator; |
42 | typedef MapType::const_iterator const_iterator; |
43 | typedef MapType::reverse_iterator reverse_iterator; |
44 | typedef MapType::const_reverse_iterator const_reverse_iterator; |
45 | typedef std::pair<iterator, iterator> iterator_range; |
46 | typedef MapType::value_type value_type; |
47 | |
48 | iterator begin() { return m_samples.begin(); } |
49 | const_iterator begin() const { return m_samples.begin(); } |
50 | iterator end() { return m_samples.end(); } |
51 | const_iterator end() const { return m_samples.end(); } |
52 | reverse_iterator rbegin() { return m_samples.rbegin(); } |
53 | const_reverse_iterator rbegin() const { return m_samples.rbegin(); } |
54 | reverse_iterator rend() { return m_samples.rend(); } |
55 | const_reverse_iterator rend() const { return m_samples.rend(); } |
56 | |
57 | size_t size() const { return m_samples.size(); } |
58 | |
59 | WEBCORE_EXPORT iterator findSampleWithPresentationTime(const MediaTime&); |
60 | WEBCORE_EXPORT iterator findSampleContainingPresentationTime(const MediaTime&); |
61 | WEBCORE_EXPORT iterator findSampleContainingOrAfterPresentationTime(const MediaTime&); |
62 | WEBCORE_EXPORT iterator findSampleStartingOnOrAfterPresentationTime(const MediaTime&); |
63 | WEBCORE_EXPORT iterator findSampleStartingAfterPresentationTime(const MediaTime&); |
64 | WEBCORE_EXPORT reverse_iterator reverseFindSampleContainingPresentationTime(const MediaTime&); |
65 | WEBCORE_EXPORT reverse_iterator reverseFindSampleBeforePresentationTime(const MediaTime&); |
66 | WEBCORE_EXPORT iterator_range findSamplesBetweenPresentationTimes(const MediaTime&, const MediaTime&); |
67 | WEBCORE_EXPORT iterator_range findSamplesBetweenPresentationTimesFromEnd(const MediaTime&, const MediaTime&); |
68 | |
69 | private: |
70 | MapType m_samples; |
71 | }; |
72 | |
73 | class DecodeOrderSampleMap { |
74 | friend class SampleMap; |
75 | public: |
76 | typedef std::pair<MediaTime, MediaTime> KeyType; |
77 | using MapType = StdMap<KeyType, RefPtr<MediaSample>>; |
78 | typedef MapType::iterator iterator; |
79 | typedef MapType::const_iterator const_iterator; |
80 | typedef MapType::reverse_iterator reverse_iterator; |
81 | typedef MapType::const_reverse_iterator const_reverse_iterator; |
82 | typedef std::pair<iterator, iterator> iterator_range; |
83 | typedef std::pair<reverse_iterator, reverse_iterator> reverse_iterator_range; |
84 | typedef MapType::value_type value_type; |
85 | |
86 | iterator begin() { return m_samples.begin(); } |
87 | const_iterator begin() const { return m_samples.begin(); } |
88 | iterator end() { return m_samples.end(); } |
89 | const_iterator end() const { return m_samples.end(); } |
90 | reverse_iterator rbegin() { return m_samples.rbegin(); } |
91 | const_reverse_iterator rbegin() const { return m_samples.rbegin(); } |
92 | reverse_iterator rend() { return m_samples.rend(); } |
93 | const_reverse_iterator rend() const { return m_samples.rend(); } |
94 | |
95 | WEBCORE_EXPORT iterator findSampleWithDecodeKey(const KeyType&); |
96 | WEBCORE_EXPORT reverse_iterator reverseFindSampleWithDecodeKey(const KeyType&); |
97 | WEBCORE_EXPORT reverse_iterator findSyncSamplePriorToPresentationTime(const MediaTime&, const MediaTime& threshold = MediaTime::positiveInfiniteTime()); |
98 | WEBCORE_EXPORT reverse_iterator findSyncSamplePriorToDecodeIterator(reverse_iterator); |
99 | WEBCORE_EXPORT iterator findSyncSampleAfterPresentationTime(const MediaTime&, const MediaTime& threshold = MediaTime::positiveInfiniteTime()); |
100 | WEBCORE_EXPORT iterator findSyncSampleAfterDecodeIterator(iterator); |
101 | WEBCORE_EXPORT reverse_iterator_range findDependentSamples(MediaSample*); |
102 | WEBCORE_EXPORT iterator_range findSamplesBetweenDecodeKeys(const KeyType&, const KeyType&); |
103 | |
104 | private: |
105 | MapType m_samples; |
106 | PresentationOrderSampleMap m_presentationOrder; |
107 | }; |
108 | |
109 | class SampleMap { |
110 | public: |
111 | SampleMap() = default; |
112 | |
113 | WEBCORE_EXPORT bool empty() const; |
114 | size_t size() const { return m_decodeOrder.m_samples.size(); } |
115 | WEBCORE_EXPORT void clear(); |
116 | WEBCORE_EXPORT void addSample(MediaSample&); |
117 | WEBCORE_EXPORT void removeSample(MediaSample*); |
118 | size_t sizeInBytes() const { return m_totalSize; } |
119 | |
120 | template<typename I> |
121 | void addRange(I begin, I end); |
122 | |
123 | DecodeOrderSampleMap& decodeOrder() { return m_decodeOrder; } |
124 | const DecodeOrderSampleMap& decodeOrder() const { return m_decodeOrder; } |
125 | PresentationOrderSampleMap& presentationOrder() { return m_decodeOrder.m_presentationOrder; } |
126 | const PresentationOrderSampleMap& presentationOrder() const { return m_decodeOrder.m_presentationOrder; } |
127 | |
128 | private: |
129 | DecodeOrderSampleMap m_decodeOrder; |
130 | size_t m_totalSize { 0 }; |
131 | }; |
132 | |
133 | template<typename I> |
134 | inline void SampleMap::addRange(I begin, I end) |
135 | { |
136 | for (I iter = begin; iter != end; ++iter) |
137 | addSample(*iter->second); |
138 | } |
139 | |
140 | } // namespace WebCore |
141 | |