Shaka Player Embedded
streams.h
Go to the documentation of this file.
1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SHAKA_EMBEDDED_MEDIA_STREAMS_H_
16 #define SHAKA_EMBEDDED_MEDIA_STREAMS_H_
17 
18 #include <memory>
19 #include <type_traits>
20 #include <vector>
21 
22 #include "../macros.h"
23 #include "frames.h"
24 
25 namespace shaka {
26 namespace media {
27 
28 enum class FrameLocation : uint8_t {
32  Near,
34  After,
35 };
36 
37 
42  BufferedRange() = default;
43  BufferedRange(double start, double end) : start(start), end(end) {}
44 
45  bool operator==(const BufferedRange& other) const {
46  return start == other.start && end == other.end;
47  }
48  bool operator!=(const BufferedRange& other) const {
49  return !(*this == other);
50  }
51 
52  double start;
53  double end;
54 };
55 static_assert(std::is_pod<BufferedRange>::value, "BufferedRange should be POD");
56 
57 
70  public:
76  static constexpr const double kMaxGapSize = 0.15;
77 
78 
86  explicit StreamBase(bool order_by_dts);
87  ~StreamBase();
88 
90 
96  size_t CountFramesBetween(double start, double end) const;
97 
106  std::vector<BufferedRange> GetBufferedRanges() const;
107 
112  size_t EstimateSize() const;
113 
114 
127  void Remove(double start, double end);
128 
130  void Clear();
131 
132 
138  void DebugPrint(bool all_frames) const;
139 
140  protected:
150  std::shared_ptr<BaseFrame> GetFrameInternal(double time,
151  FrameLocation kind) const;
152 
163  void AddFrameInternal(std::shared_ptr<BaseFrame> frame);
164 
165  private:
166  void AssertRangesSorted() const;
167 
168  class Impl;
169  std::unique_ptr<Impl> impl_;
170 };
171 
172 
181 template <typename T, bool OrderByDts>
183  public:
184  Stream() : StreamBase(OrderByDts) {}
185 
187  void AddFrame(std::shared_ptr<T> frame) {
188  AddFrameInternal(frame);
189  }
190 
192  std::shared_ptr<T> GetFrame(
193  double time, FrameLocation kind = FrameLocation::After) const {
194  auto ret = GetFrameInternal(time, kind);
195  return std::shared_ptr<T>(ret, static_cast<T*>(ret.get()));
196  }
197 };
198 
199 
202 
203 } // namespace media
204 } // namespace shaka
205 
206 #endif // SHAKA_EMBEDDED_MEDIA_STREAMS_H_
#define SHAKA_EXPORT
Definition: macros.h:30
std::shared_ptr< shaka::media::DecodedFrame > frame
BufferedRange(double start, double end)
Definition: streams.h:43
#define SHAKA_NON_COPYABLE_OR_MOVABLE_TYPE(Type)
Definition: macros.h:51
bool operator!=(const BufferedRange &other) const
Definition: streams.h:48
bool operator==(const BufferedRange &other) const
Definition: streams.h:45
void AddFrame(std::shared_ptr< T > frame)
Definition: streams.h:187
std::shared_ptr< T > GetFrame(double time, FrameLocation kind=FrameLocation::After) const
Definition: streams.h:192