Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/media/mp3/MP3Demuxer.h
Line
Count
Source (jump to first uncovered line)
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
3
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
#ifndef MP3_DEMUXER_H_
6
#define MP3_DEMUXER_H_
7
8
#include "MediaDataDemuxer.h"
9
#include "MediaResource.h"
10
#include "MP3FrameParser.h"
11
12
namespace mozilla {
13
14
class MP3TrackDemuxer;
15
16
DDLoggedTypeDeclNameAndBase(MP3Demuxer, MediaDataDemuxer);
17
DDLoggedTypeNameAndBase(MP3TrackDemuxer, MediaTrackDemuxer);
18
19
class MP3Demuxer
20
  : public MediaDataDemuxer
21
  , public DecoderDoctorLifeLogger<MP3Demuxer>
22
{
23
public:
24
  // MediaDataDemuxer interface.
25
  explicit MP3Demuxer(MediaResource* aSource);
26
  RefPtr<InitPromise> Init() override;
27
  uint32_t GetNumberTracks(TrackInfo::TrackType aType) const override;
28
  already_AddRefed<MediaTrackDemuxer> GetTrackDemuxer(
29
      TrackInfo::TrackType aType, uint32_t aTrackNumber) override;
30
  bool IsSeekable() const override;
31
  void NotifyDataArrived() override;
32
  void NotifyDataRemoved() override;
33
34
private:
35
  // Synchronous initialization.
36
  bool InitInternal();
37
38
  RefPtr<MediaResource> mSource;
39
  RefPtr<MP3TrackDemuxer> mTrackDemuxer;
40
};
41
42
// The MP3 demuxer used to extract MPEG frames and side information out of
43
// MPEG streams.
44
class MP3TrackDemuxer
45
  : public MediaTrackDemuxer
46
  , public DecoderDoctorLifeLogger<MP3TrackDemuxer>
47
{
48
public:
49
  // Constructor, expecting a valid media resource.
50
  explicit MP3TrackDemuxer(MediaResource* aSource);
51
52
  // Initializes the track demuxer by reading the first frame for meta data.
53
  // Returns initialization success state.
54
  bool Init();
55
56
  // Returns the total stream length if known, -1 otherwise.
57
  int64_t StreamLength() const;
58
59
  // Returns the estimated stream duration, or a 0-duration if unknown.
60
  media::TimeUnit Duration() const;
61
62
  // Returns the estimated duration up to the given frame number,
63
  // or a 0-duration if unknown.
64
  media::TimeUnit Duration(int64_t aNumFrames) const;
65
66
  // Returns the estimated current seek position time.
67
  media::TimeUnit SeekPosition() const;
68
69
  const FrameParser::Frame& LastFrame() const;
70
  RefPtr<MediaRawData> DemuxSample();
71
72
  const ID3Parser::ID3Header& ID3Header() const;
73
  const FrameParser::VBRHeader& VBRInfo() const;
74
75
  // MediaTrackDemuxer interface.
76
  UniquePtr<TrackInfo> GetInfo() const override;
77
  RefPtr<SeekPromise> Seek(const media::TimeUnit& aTime) override;
78
  RefPtr<SamplesPromise> GetSamples(int32_t aNumSamples = 1) override;
79
  void Reset() override;
80
  RefPtr<SkipAccessPointPromise> SkipToNextRandomAccessPoint(
81
    const media::TimeUnit& aTimeThreshold) override;
82
  int64_t GetResourceOffset() const override;
83
  media::TimeIntervals GetBuffered() override;
84
85
private:
86
  // Destructor.
87
0
  ~MP3TrackDemuxer() {}
88
89
  // Fast approximate seeking to given time.
90
  media::TimeUnit FastSeek(const media::TimeUnit& aTime);
91
92
  // Seeks by scanning the stream up to the given time for more accurate results.
93
  media::TimeUnit ScanUntil(const media::TimeUnit& aTime);
94
95
  // Finds the first valid frame and returns its byte range if found
96
  // or a null-byte range otherwise.
97
  MediaByteRange FindFirstFrame();
98
99
  // Finds the next valid frame and returns its byte range if found
100
  // or a null-byte range otherwise.
101
  MediaByteRange FindNextFrame();
102
103
  // Skips the next frame given the provided byte range.
104
  bool SkipNextFrame(const MediaByteRange& aRange);
105
106
  // Returns the next MPEG frame, if available.
107
  already_AddRefed<MediaRawData> GetNextFrame(const MediaByteRange& aRange);
108
109
  // Updates post-read meta data.
110
  void UpdateState(const MediaByteRange& aRange);
111
112
  // Returns the estimated offset for the given frame index.
113
  int64_t OffsetFromFrameIndex(int64_t aFrameIndex) const;
114
115
  // Returns the estimated frame index for the given offset.
116
  int64_t FrameIndexFromOffset(int64_t aOffset) const;
117
118
  // Returns the estimated frame index for the given time.
119
  int64_t FrameIndexFromTime(const media::TimeUnit& aTime) const;
120
121
  // Reads aSize bytes into aBuffer from the source starting at aOffset.
122
  // Returns the actual size read.
123
  int32_t Read(uint8_t* aBuffer, int64_t aOffset, int32_t aSize);
124
125
  // Returns the average frame length derived from the previously parsed frames.
126
  double AverageFrameLength() const;
127
128
  // The (hopefully) MPEG resource.
129
  MediaResourceIndex mSource;
130
131
  // MPEG frame parser used to detect frames and extract side info.
132
  FrameParser mParser;
133
134
  // Whether we've locked onto a valid sequence of frames or not.
135
  bool mFrameLock;
136
137
  // Current byte offset in the source stream.
138
  int64_t mOffset;
139
140
  // Byte offset of the begin of the first frame, or 0 if none parsed yet.
141
  int64_t mFirstFrameOffset;
142
143
  // Total parsed frames.
144
  uint64_t mNumParsedFrames;
145
146
  // Current frame index.
147
  int64_t mFrameIndex;
148
149
  // Sum of parsed frames' lengths in bytes.
150
  uint64_t mTotalFrameLen;
151
152
  // Samples per frame metric derived from frame headers or 0 if none available.
153
  int32_t mSamplesPerFrame;
154
155
  // Samples per second metric derived from frame headers or 0 if none available.
156
  int32_t mSamplesPerSecond;
157
158
  // Channel count derived from frame headers or 0 if none available.
159
  int32_t mChannels;
160
161
  // Audio track config info.
162
  UniquePtr<AudioInfo> mInfo;
163
};
164
165
} // namespace mozilla
166
167
#endif