Line data Source code
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/. */
6 :
7 : #ifndef ADTS_DEMUXER_H_
8 : #define ADTS_DEMUXER_H_
9 :
10 : #include "mozilla/Attributes.h"
11 : #include "mozilla/Maybe.h"
12 : #include "MediaDataDemuxer.h"
13 : #include "MediaResource.h"
14 : #include "mp4_demuxer/ByteReader.h"
15 :
16 : namespace mozilla {
17 :
18 : namespace adts {
19 : class Frame;
20 : class FrameParser;
21 : }
22 :
23 : class ADTSTrackDemuxer;
24 :
25 0 : class ADTSDemuxer : public MediaDataDemuxer
26 : {
27 : public:
28 : // MediaDataDemuxer interface.
29 : explicit ADTSDemuxer(MediaResource* aSource);
30 : RefPtr<InitPromise> Init() override;
31 : bool HasTrackType(TrackInfo::TrackType aType) const override;
32 : uint32_t GetNumberTracks(TrackInfo::TrackType aType) const override;
33 : already_AddRefed<MediaTrackDemuxer>
34 : GetTrackDemuxer(TrackInfo::TrackType aType, uint32_t aTrackNumber) override;
35 : bool IsSeekable() const override;
36 :
37 : // Return true if a valid ADTS frame header could be found.
38 : static bool ADTSSniffer(const uint8_t* aData, const uint32_t aLength);
39 :
40 : private:
41 : bool InitInternal();
42 :
43 : RefPtr<MediaResource> mSource;
44 : RefPtr<ADTSTrackDemuxer> mTrackDemuxer;
45 : };
46 :
47 : class ADTSTrackDemuxer : public MediaTrackDemuxer
48 : {
49 : public:
50 : explicit ADTSTrackDemuxer(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 : // MediaTrackDemuxer interface.
67 : UniquePtr<TrackInfo> GetInfo() const override;
68 : RefPtr<SeekPromise> Seek(const media::TimeUnit& aTime) override;
69 : RefPtr<SamplesPromise> GetSamples(int32_t aNumSamples = 1) override;
70 : void Reset() override;
71 : RefPtr<SkipAccessPointPromise> SkipToNextRandomAccessPoint(
72 : const media::TimeUnit& aTimeThreshold) override;
73 : int64_t GetResourceOffset() const override;
74 : media::TimeIntervals GetBuffered() override;
75 :
76 : private:
77 : // Destructor.
78 : ~ADTSTrackDemuxer();
79 :
80 : // Fast approximate seeking to given time.
81 : media::TimeUnit FastSeek(const media::TimeUnit& aTime);
82 :
83 : // Seeks by scanning the stream up to the given time for more accurate results.
84 : media::TimeUnit ScanUntil(const media::TimeUnit& aTime);
85 :
86 : // Finds the next valid frame and returns its byte range.
87 : const adts::Frame& FindNextFrame(bool findFirstFrame = false);
88 :
89 : // Skips the next frame given the provided byte range.
90 : bool SkipNextFrame(const adts::Frame& aFrame);
91 :
92 : // Returns the next ADTS frame, if available.
93 : already_AddRefed<MediaRawData> GetNextFrame(const adts::Frame& aFrame);
94 :
95 : // Updates post-read meta data.
96 : void UpdateState(const adts::Frame& aFrame);
97 :
98 : // Returns the frame index for the given offset.
99 : int64_t FrameIndexFromOffset(int64_t aOffset) const;
100 :
101 : // Returns the frame index for the given time.
102 : int64_t FrameIndexFromTime(const media::TimeUnit& aTime) const;
103 :
104 : // Reads aSize bytes into aBuffer from the source starting at aOffset.
105 : // Returns the actual size read.
106 : int32_t Read(uint8_t* aBuffer, int64_t aOffset, int32_t aSize);
107 :
108 : // Returns the average frame length derived from the previously parsed frames.
109 : double AverageFrameLength() const;
110 :
111 : // The (hopefully) ADTS resource.
112 : MediaResourceIndex mSource;
113 :
114 : // ADTS frame parser used to detect frames and extract side info.
115 : adts::FrameParser* mParser;
116 :
117 : // Current byte offset in the source stream.
118 : int64_t mOffset;
119 :
120 : // Total parsed frames.
121 : uint64_t mNumParsedFrames;
122 :
123 : // Current frame index.
124 : int64_t mFrameIndex;
125 :
126 : // Sum of parsed frames' lengths in bytes.
127 : uint64_t mTotalFrameLen;
128 :
129 : // Samples per frame metric derived from frame headers or 0 if none available.
130 : uint32_t mSamplesPerFrame;
131 :
132 : // Samples per second metric derived from frame headers or 0 if none available.
133 : uint32_t mSamplesPerSecond;
134 :
135 : // Channel count derived from frame headers or 0 if none available.
136 : uint32_t mChannels;
137 :
138 : // Audio track config info.
139 : UniquePtr<AudioInfo> mInfo;
140 : };
141 :
142 : } // mozilla
143 :
144 : #endif // !ADTS_DEMUXER_H_
|