Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/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
    , mIsKeyframe(false) {}
29
30
  bool Init(nestegg_packet* aPacket, int64_t aOffset, unsigned aTrack, bool aIsKeyframe)
31
  {
32
    uint64_t timestamp_ns;
33
    if (nestegg_packet_tstamp(aPacket, &timestamp_ns) == -1) {
34
      return false;
35
    }
36
37
    // We store the timestamp as signed microseconds so that it's easily
38
    // comparable to other timestamps we have in the system.
39
    mTimestamp = timestamp_ns / 1000;
40
    mPacket = aPacket;
41
    mOffset = aOffset;
42
    mTrack = aTrack;
43
    mIsKeyframe = aIsKeyframe;
44
45
    uint64_t duration_ns;
46
    if (!nestegg_packet_duration(aPacket, &duration_ns)) {
47
      mDuration = duration_ns / 1000;
48
    }
49
    return true;
50
  }
51
52
  nestegg_packet* Packet() { MOZ_ASSERT(IsInitialized()); return mPacket; }
53
  int64_t Offset() { MOZ_ASSERT(IsInitialized()); return mOffset; }
54
  int64_t Timestamp() { MOZ_ASSERT(IsInitialized()); return mTimestamp; }
55
  int64_t Duration() { MOZ_ASSERT(IsInitialized()); return mDuration; }
56
  unsigned Track() { MOZ_ASSERT(IsInitialized()); return mTrack; }
57
0
  bool IsKeyframe() { MOZ_ASSERT(IsInitialized()); return mIsKeyframe; }
58
59
private:
60
  ~NesteggPacketHolder()
61
  {
62
    nestegg_free_packet(mPacket);
63
  }
64
65
0
  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
  int32_t GetSize() {
94
    return mQueue.size();
95
  }
96
97
0
  void Push(NesteggPacketHolder* aItem) {
98
0
    mQueue.push_back(aItem);
99
0
  }
100
101
  void PushFront(NesteggPacketHolder* aItem) {
102
    mQueue.push_front(std::move(aItem));
103
  }
104
105
  already_AddRefed<NesteggPacketHolder> PopFront() {
106
    RefPtr<NesteggPacketHolder> result = mQueue.front().forget();
107
    mQueue.pop_front();
108
    return result.forget();
109
  }
110
111
  void Reset() {
112
    while (!mQueue.empty()) {
113
      mQueue.pop_front();
114
    }
115
  }
116
117
private:
118
  std::deque<RefPtr<NesteggPacketHolder>> mQueue;
119
};
120
121
122
} // namespace mozilla
123
124
#endif
125