Coverage Report

Created: 2018-09-25 14:53

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