Line data Source code
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_
8 :
9 : #include <stdint.h>
10 : #include "nsAutoRef.h"
11 : #include "nestegg/nestegg.h"
12 :
13 : namespace mozilla {
14 :
15 : // Holds a nestegg_packet, and its file offset. This is needed so we
16 : // know the offset in the file we've played up to, in order to calculate
17 : // whether it's likely we can play through to the end without needing
18 : // to stop to buffer, given the current download rate.
19 : class NesteggPacketHolder {
20 : public:
21 0 : NS_INLINE_DECL_THREADSAFE_REFCOUNTING(NesteggPacketHolder)
22 0 : NesteggPacketHolder()
23 0 : : mPacket(nullptr)
24 : , mOffset(-1)
25 : , mTimestamp(-1)
26 : , mDuration(-1)
27 0 : , mIsKeyframe(false) {}
28 :
29 0 : bool Init(nestegg_packet* aPacket, int64_t aOffset, unsigned aTrack, bool aIsKeyframe)
30 : {
31 : uint64_t timestamp_ns;
32 0 : if (nestegg_packet_tstamp(aPacket, ×tamp_ns) == -1) {
33 0 : return false;
34 : }
35 :
36 : // We store the timestamp as signed microseconds so that it's easily
37 : // comparable to other timestamps we have in the system.
38 0 : mTimestamp = timestamp_ns / 1000;
39 0 : mPacket = aPacket;
40 0 : mOffset = aOffset;
41 0 : mTrack = aTrack;
42 0 : mIsKeyframe = aIsKeyframe;
43 :
44 : uint64_t duration_ns;
45 0 : if (!nestegg_packet_duration(aPacket, &duration_ns)) {
46 0 : mDuration = duration_ns / 1000;
47 : }
48 0 : return true;
49 : }
50 :
51 0 : nestegg_packet* Packet() { MOZ_ASSERT(IsInitialized()); return mPacket; }
52 0 : int64_t Offset() { MOZ_ASSERT(IsInitialized()); return mOffset; }
53 0 : int64_t Timestamp() { MOZ_ASSERT(IsInitialized()); return mTimestamp; }
54 0 : int64_t Duration() { MOZ_ASSERT(IsInitialized()); return mDuration; }
55 0 : unsigned Track() { MOZ_ASSERT(IsInitialized()); return mTrack; }
56 : bool IsKeyframe() { MOZ_ASSERT(IsInitialized()); return mIsKeyframe; }
57 :
58 : private:
59 0 : ~NesteggPacketHolder()
60 0 : {
61 0 : nestegg_free_packet(mPacket);
62 0 : }
63 :
64 0 : bool IsInitialized() { return mOffset >= 0; }
65 :
66 : nestegg_packet* mPacket;
67 :
68 : // Offset in bytes. This is the offset of the end of the Block
69 : // which contains the packet.
70 : int64_t mOffset;
71 :
72 : // Packet presentation timestamp in microseconds.
73 : int64_t mTimestamp;
74 :
75 : // Packet duration in microseconds; -1 if unknown or retrieval failed.
76 : int64_t mDuration;
77 :
78 : // Track ID.
79 : unsigned mTrack;
80 :
81 : // Does this packet contain a keyframe?
82 : bool mIsKeyframe;
83 :
84 : // Copy constructor and assignment operator not implemented. Don't use them!
85 : NesteggPacketHolder(const NesteggPacketHolder &aOther);
86 : NesteggPacketHolder& operator= (NesteggPacketHolder const& aOther);
87 : };
88 :
89 : // Queue for holding nestegg packets.
90 0 : class WebMPacketQueue {
91 : public:
92 0 : int32_t GetSize() {
93 0 : return mQueue.size();
94 : }
95 :
96 : void Push(NesteggPacketHolder* aItem) {
97 : mQueue.push_back(aItem);
98 : }
99 :
100 0 : void PushFront(NesteggPacketHolder* aItem) {
101 0 : mQueue.push_front(Move(aItem));
102 0 : }
103 :
104 0 : already_AddRefed<NesteggPacketHolder> PopFront() {
105 0 : RefPtr<NesteggPacketHolder> result = mQueue.front().forget();
106 0 : mQueue.pop_front();
107 0 : return result.forget();
108 : }
109 :
110 0 : void Reset() {
111 0 : while (!mQueue.empty()) {
112 0 : mQueue.pop_front();
113 : }
114 0 : }
115 :
116 : private:
117 : std::deque<RefPtr<NesteggPacketHolder>> mQueue;
118 : };
119 :
120 :
121 : } // namespace mozilla
122 :
123 : #endif
124 :
|