1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
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/. */
7 #ifndef ADTS_DEMUXER_H_
8 #define ADTS_DEMUXER_H_
10 #include "mozilla/Attributes.h"
11 #include "mozilla/Maybe.h"
12 #include "MediaDataDemuxer.h"
13 #include "MediaResource.h"
18 class ADTSTrackDemuxer
;
20 DDLoggedTypeDeclNameAndBase(ADTSDemuxer
, MediaDataDemuxer
);
22 class ADTSDemuxer
: public MediaDataDemuxer
,
23 public DecoderDoctorLifeLogger
<ADTSDemuxer
> {
25 // MediaDataDemuxer interface.
26 explicit ADTSDemuxer(MediaResource
* aSource
);
27 RefPtr
<InitPromise
> Init() override
;
28 uint32_t GetNumberTracks(TrackInfo::TrackType aType
) const override
;
29 already_AddRefed
<MediaTrackDemuxer
> GetTrackDemuxer(
30 TrackInfo::TrackType aType
, uint32_t aTrackNumber
) override
;
31 bool IsSeekable() const override
;
33 // Return true if a valid ADTS frame header could be found.
34 static bool ADTSSniffer(const uint8_t* aData
, const uint32_t aLength
);
39 RefPtr
<MediaResource
> mSource
;
40 RefPtr
<ADTSTrackDemuxer
> mTrackDemuxer
;
43 DDLoggedTypeNameAndBase(ADTSTrackDemuxer
, MediaTrackDemuxer
);
45 class ADTSTrackDemuxer
: public MediaTrackDemuxer
,
46 public DecoderDoctorLifeLogger
<ADTSTrackDemuxer
> {
48 explicit ADTSTrackDemuxer(MediaResource
* aSource
);
50 // Initializes the track demuxer by reading the first frame for meta data.
51 // Returns initialization success state.
54 // Returns the total stream length if known, -1 otherwise.
55 int64_t StreamLength() const;
57 // Returns the estimated stream duration, or a 0-duration if unknown.
58 media::TimeUnit
Duration() const;
60 // Returns the estimated duration up to the given frame number,
61 // or a 0-duration if unknown.
62 media::TimeUnit
Duration(int64_t aNumFrames
) const;
64 // MediaTrackDemuxer interface.
65 UniquePtr
<TrackInfo
> GetInfo() const override
;
66 RefPtr
<SeekPromise
> Seek(const media::TimeUnit
& aTime
) override
;
67 RefPtr
<SamplesPromise
> GetSamples(int32_t aNumSamples
= 1) override
;
68 void Reset() override
;
69 RefPtr
<SkipAccessPointPromise
> SkipToNextRandomAccessPoint(
70 const media::TimeUnit
& aTimeThreshold
) override
;
71 int64_t GetResourceOffset() const override
;
72 media::TimeIntervals
GetBuffered() override
;
78 // Fast approximate seeking to given time.
79 media::TimeUnit
FastSeek(const media::TimeUnit
& aTime
);
81 // Seeks by scanning the stream up to the given time for more accurate
83 media::TimeUnit
ScanUntil(const media::TimeUnit
& aTime
);
85 // Finds the next valid frame and returns its byte range.
86 const ADTS::Frame
& FindNextFrame(bool findFirstFrame
= false);
88 // Skips the next frame given the provided byte range.
89 bool SkipNextFrame(const ADTS::Frame
& aFrame
);
91 // Returns the next ADTS frame, if available.
92 already_AddRefed
<MediaRawData
> GetNextFrame(const ADTS::Frame
& aFrame
);
94 // Updates post-read meta data.
95 void UpdateState(const ADTS::Frame
& aFrame
);
97 // Returns the frame index for the given offset.
98 int64_t FrameIndexFromOffset(uint64_t aOffset
) const;
100 // Returns the frame index for the given time.
101 int64_t FrameIndexFromTime(const media::TimeUnit
& aTime
) const;
103 // Reads aSize bytes into aBuffer from the source starting at aOffset.
104 // Returns the actual size read.
105 uint32_t Read(uint8_t* aBuffer
, int64_t aOffset
, int32_t aSize
);
107 // Returns the average frame length derived from the previously parsed frames.
108 double AverageFrameLength() const;
110 // The (hopefully) ADTS resource.
111 MediaResourceIndex mSource
;
113 // ADTS frame parser used to detect frames and extract side info.
114 ADTS::FrameParser
* mParser
;
116 // Current byte offset in the source stream.
119 // Total parsed frames.
120 uint64_t mNumParsedFrames
;
122 // Current frame index.
125 // Sum of parsed frames' lengths in bytes.
126 uint64_t mTotalFrameLen
;
128 // Samples per frame metric derived from frame headers or 0 if none available.
129 uint32_t mSamplesPerFrame
;
131 // Samples per second metric derived from frame headers or 0 if none
133 uint32_t mSamplesPerSecond
;
135 // Channel count derived from frame headers or 0 if none available.
138 // Audio track config info.
139 UniquePtr
<AudioInfo
> mInfo
;
141 // Amount of pre-roll time when seeking.
142 // AAC encoder delay is by default 2112 audio frames.
143 media::TimeUnit mPreRoll
;
146 } // namespace mozilla
148 #endif // !ADTS_DEMUXER_H_