Backed out changeset 06f41c22f3a6 (bug 1888460) for causing linux xpcshell failures...
[gecko.git] / dom / media / webm / NesteggPacketHolder.h
blob7c74f752d35f82b1f6ab328f8f2b272407383d2a
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #if !defined(NesteggPacketHolder_h_)
7 # define NesteggPacketHolder_h_
9 # include <deque>
10 # include <stdint.h>
11 # include "nsAutoRef.h"
12 # include "nestegg/nestegg.h"
14 namespace mozilla {
16 // Holds a nestegg_packet, and its file offset. This is needed so we
17 // know the offset in the file we've played up to, in order to calculate
18 // whether it's likely we can play through to the end without needing
19 // to stop to buffer, given the current download rate.
20 class NesteggPacketHolder {
21 public:
22 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(NesteggPacketHolder)
23 NesteggPacketHolder()
24 : mPacket(nullptr),
25 mOffset(-1),
26 mTimestamp(-1),
27 mDuration(-1),
28 mTrack(0),
29 mIsKeyframe(false) {}
31 bool Init(nestegg_packet* aPacket, int64_t aOffset, unsigned aTrack,
32 bool aIsKeyframe) {
33 uint64_t timestamp_ns;
34 if (nestegg_packet_tstamp(aPacket, &timestamp_ns) == -1) {
35 return false;
38 // We store the timestamp as signed microseconds so that it's easily
39 // comparable to other timestamps we have in the system.
40 mTimestamp = timestamp_ns / 1000;
41 mPacket = aPacket;
42 mOffset = aOffset;
43 mTrack = aTrack;
44 mIsKeyframe = aIsKeyframe;
46 uint64_t duration_ns;
47 if (!nestegg_packet_duration(aPacket, &duration_ns)) {
48 mDuration = duration_ns / 1000;
50 return true;
53 nestegg_packet* Packet() {
54 MOZ_ASSERT(IsInitialized());
55 return mPacket;
57 int64_t Offset() {
58 MOZ_ASSERT(IsInitialized());
59 return mOffset;
61 int64_t Timestamp() {
62 MOZ_ASSERT(IsInitialized());
63 return mTimestamp;
65 int64_t Duration() {
66 MOZ_ASSERT(IsInitialized());
67 return mDuration;
69 unsigned Track() {
70 MOZ_ASSERT(IsInitialized());
71 return mTrack;
73 bool IsKeyframe() {
74 MOZ_ASSERT(IsInitialized());
75 return mIsKeyframe;
78 private:
79 ~NesteggPacketHolder() { nestegg_free_packet(mPacket); }
81 bool IsInitialized() { return mOffset >= 0; }
83 nestegg_packet* mPacket;
85 // Offset in bytes. This is the offset of the end of the Block
86 // which contains the packet.
87 int64_t mOffset;
89 // Packet presentation timestamp in microseconds.
90 int64_t mTimestamp;
92 // Packet duration in microseconds; -1 if unknown or retrieval failed.
93 int64_t mDuration;
95 // Track ID.
96 unsigned mTrack;
98 // Does this packet contain a keyframe?
99 bool mIsKeyframe;
101 // Copy constructor and assignment operator not implemented. Don't use them!
102 NesteggPacketHolder(const NesteggPacketHolder& aOther);
103 NesteggPacketHolder& operator=(NesteggPacketHolder const& aOther);
106 // Queue for holding nestegg packets.
107 class WebMPacketQueue {
108 public:
109 int32_t GetSize() { return mQueue.size(); }
111 void Push(NesteggPacketHolder* aItem) { mQueue.push_back(aItem); }
113 void PushFront(NesteggPacketHolder* aItem) {
114 mQueue.push_front(std::move(aItem));
117 already_AddRefed<NesteggPacketHolder> PopFront() {
118 RefPtr<NesteggPacketHolder> result = std::move(mQueue.front());
119 mQueue.pop_front();
120 return result.forget();
123 void Reset() {
124 while (!mQueue.empty()) {
125 mQueue.pop_front();
129 private:
130 std::deque<RefPtr<NesteggPacketHolder>> mQueue;
133 } // namespace mozilla
135 #endif