/src/mozilla-central/dom/media/wave/WaveDemuxer.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
2 | | * Licence, 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 WAV_DEMUXER_H_ |
6 | | #define WAV_DEMUXER_H_ |
7 | | |
8 | | #include "MediaDataDemuxer.h" |
9 | | #include "MediaResource.h" |
10 | | |
11 | | namespace mozilla { |
12 | | class BufferReader; |
13 | | |
14 | | static const uint32_t FRMT_CODE = 0x666d7420; |
15 | | static const uint32_t DATA_CODE = 0x64617461; |
16 | | static const uint32_t LIST_CODE = 0x4c495354; |
17 | | static const uint32_t INFO_CODE = 0x494e464f; |
18 | | |
19 | | static const uint8_t RIFF[4] = {'R', 'I', 'F', 'F'}; |
20 | | static const uint8_t WAVE[4] = {'W', 'A', 'V', 'E'}; |
21 | | |
22 | | static const uint16_t RIFF_CHUNK_SIZE = 12; |
23 | | static const uint16_t CHUNK_HEAD_SIZE = 8; |
24 | | static const uint16_t FMT_CHUNK_MIN_SIZE = 16; |
25 | | static const uint16_t DATA_CHUNK_SIZE = 768; |
26 | | |
27 | | class WAVTrackDemuxer; |
28 | | |
29 | | DDLoggedTypeDeclNameAndBase(WAVDemuxer, MediaDataDemuxer); |
30 | | DDLoggedTypeNameAndBase(WAVTrackDemuxer, MediaTrackDemuxer); |
31 | | |
32 | | class WAVDemuxer |
33 | | : public MediaDataDemuxer |
34 | | , public DecoderDoctorLifeLogger<WAVDemuxer> |
35 | | { |
36 | | public: |
37 | | // MediaDataDemuxer interface. |
38 | | explicit WAVDemuxer(MediaResource* aSource); |
39 | | RefPtr<InitPromise> Init() override; |
40 | | uint32_t GetNumberTracks(TrackInfo::TrackType aType) const override; |
41 | | already_AddRefed<MediaTrackDemuxer> GetTrackDemuxer( |
42 | | TrackInfo::TrackType aType, uint32_t aTrackNumber) override; |
43 | | bool IsSeekable() const override; |
44 | | |
45 | | private: |
46 | | // Synchronous Initialization. |
47 | | bool InitInternal(); |
48 | | |
49 | | MediaResourceIndex mSource; |
50 | | RefPtr<WAVTrackDemuxer> mTrackDemuxer; |
51 | | }; |
52 | | |
53 | | class RIFFParser |
54 | | { |
55 | | private: |
56 | | class RIFFHeader; |
57 | | public: |
58 | | const RIFFHeader& RiffHeader() const; |
59 | | |
60 | | Result<uint32_t, nsresult> Parse(BufferReader& aReader); |
61 | | |
62 | | void Reset(); |
63 | | |
64 | | private: |
65 | | class RIFFHeader |
66 | | { |
67 | | public: |
68 | | RIFFHeader(); |
69 | | void Reset(); |
70 | | |
71 | | bool IsValid() const; |
72 | | bool IsValid(int aPos) const; |
73 | | |
74 | | bool ParseNext(uint8_t c); |
75 | | |
76 | | private: |
77 | | bool Update(uint8_t c); |
78 | | |
79 | | uint8_t mRaw[RIFF_CHUNK_SIZE]; |
80 | | |
81 | | int mPos; |
82 | | }; |
83 | | |
84 | | RIFFHeader mRiffHeader; |
85 | | }; |
86 | | |
87 | | class HeaderParser |
88 | | { |
89 | | private: |
90 | | class ChunkHeader; |
91 | | public: |
92 | | const ChunkHeader& GiveHeader() const; |
93 | | |
94 | | Result<uint32_t, nsresult> Parse(BufferReader& aReader); |
95 | | |
96 | | void Reset(); |
97 | | |
98 | | private: |
99 | | class ChunkHeader |
100 | | { |
101 | | public: |
102 | | ChunkHeader(); |
103 | | void Reset(); |
104 | | |
105 | | bool IsValid() const; |
106 | | |
107 | | uint32_t ChunkName() const; |
108 | | uint32_t ChunkSize() const; |
109 | | |
110 | | bool ParseNext(uint8_t c); |
111 | | |
112 | | private: |
113 | | void Update(uint8_t c); |
114 | | |
115 | | uint8_t mRaw[CHUNK_HEAD_SIZE]; |
116 | | |
117 | | int mPos; |
118 | | }; |
119 | | |
120 | | ChunkHeader mHeader; |
121 | | }; |
122 | | |
123 | | class FormatParser |
124 | | { |
125 | | private: |
126 | | class FormatChunk; |
127 | | public: |
128 | | const FormatChunk& FmtChunk() const; |
129 | | |
130 | | Result<uint32_t, nsresult> Parse(BufferReader& aReader); |
131 | | |
132 | | void Reset(); |
133 | | |
134 | | private: |
135 | | class FormatChunk |
136 | | { |
137 | | public: |
138 | | FormatChunk(); |
139 | | void Reset(); |
140 | | |
141 | | uint16_t WaveFormat() const; |
142 | | uint16_t Channels() const; |
143 | | uint32_t SampleRate() const; |
144 | | uint16_t FrameSize() const; |
145 | | uint16_t SampleFormat() const; |
146 | | |
147 | | bool IsValid() const; |
148 | | bool ParseNext(uint8_t c); |
149 | | |
150 | | private: |
151 | | void Update(uint8_t c); |
152 | | |
153 | | uint8_t mRaw[FMT_CHUNK_MIN_SIZE]; |
154 | | |
155 | | int mPos; |
156 | | }; |
157 | | |
158 | | FormatChunk mFmtChunk; |
159 | | }; |
160 | | |
161 | | class DataParser |
162 | | { |
163 | | private: |
164 | | class DataChunk; |
165 | | public: |
166 | | DataParser(); |
167 | | |
168 | | const DataChunk& CurrentChunk() const; |
169 | | |
170 | | void Reset(); |
171 | | |
172 | | private: |
173 | | class DataChunk |
174 | | { |
175 | | public: |
176 | | void Reset(); |
177 | | private: |
178 | | int mPos; // To Check Alignment |
179 | | }; |
180 | | |
181 | | DataChunk mChunk; |
182 | | }; |
183 | | |
184 | | class WAVTrackDemuxer |
185 | | : public MediaTrackDemuxer |
186 | | , public DecoderDoctorLifeLogger<WAVTrackDemuxer> |
187 | | { |
188 | | public: |
189 | | explicit WAVTrackDemuxer(MediaResource* aSource); |
190 | | |
191 | | bool Init(); |
192 | | |
193 | | int64_t StreamLength() const; |
194 | | |
195 | | media::TimeUnit Duration() const; |
196 | | media::TimeUnit Duration(int64_t aNumDataChunks) const; |
197 | | media::TimeUnit DurationFromBytes(uint32_t aNumBytes) const; |
198 | | |
199 | | media::TimeUnit SeekPosition() const; |
200 | | |
201 | | RefPtr<MediaRawData> DemuxSample(); |
202 | | |
203 | | // MediaTrackDemuxer interface. |
204 | | UniquePtr<TrackInfo> GetInfo() const override; |
205 | | RefPtr<SeekPromise> Seek(const media::TimeUnit& aTime) override; |
206 | | RefPtr<SamplesPromise> GetSamples(int32_t aNumSamples) override; |
207 | | void Reset() override; |
208 | | RefPtr<SkipAccessPointPromise> SkipToNextRandomAccessPoint( |
209 | | const media::TimeUnit& aTimeThreshold) override; |
210 | | int64_t GetResourceOffset() const override; |
211 | | media::TimeIntervals GetBuffered() override; |
212 | | |
213 | | private: |
214 | 0 | ~WAVTrackDemuxer() { } |
215 | | |
216 | | media::TimeUnit FastSeek(const media::TimeUnit& aTime); |
217 | | media::TimeUnit ScanUntil(const media::TimeUnit& aTime); |
218 | | |
219 | | MediaByteRange FindNextChunk(); |
220 | | |
221 | | MediaByteRange FindChunkHeader(); |
222 | | MediaByteRange FindRIFFHeader(); |
223 | | MediaByteRange FindFmtChunk(); |
224 | | MediaByteRange FindListChunk(); |
225 | | MediaByteRange FindInfoTag(); |
226 | | |
227 | | bool RIFFParserInit(); |
228 | | bool HeaderParserInit(); |
229 | | bool FmtChunkParserInit(); |
230 | | bool ListChunkParserInit(uint32_t aChunkSize); |
231 | | |
232 | | bool SkipNextChunk(const MediaByteRange& aRange); |
233 | | |
234 | | already_AddRefed<MediaRawData> GetNextChunk(const MediaByteRange& aRange); |
235 | | already_AddRefed<MediaRawData> GetFileHeader(const MediaByteRange& aRange); |
236 | | |
237 | | void UpdateState(const MediaByteRange& aRange); |
238 | | |
239 | | int64_t OffsetFromChunkIndex(int64_t aChunkIndex) const; |
240 | | int64_t ChunkIndexFromOffset(int64_t aOffet) const; |
241 | | int64_t ChunkIndexFromTime(const media::TimeUnit& aTime) const; |
242 | | |
243 | | int32_t Read(uint8_t* aBuffer, int64_t aOffset, int32_t aSize); |
244 | | |
245 | | MediaResourceIndex mSource; |
246 | | |
247 | | DataParser mParser; |
248 | | RIFFParser mRIFFParser; |
249 | | HeaderParser mHeaderParser; |
250 | | |
251 | | FormatParser mFmtParser; |
252 | | // ListChunkParser mListChunkParser; |
253 | | |
254 | | int64_t mOffset; |
255 | | int64_t mFirstChunkOffset; |
256 | | |
257 | | uint32_t mNumParsedChunks; |
258 | | int32_t mChunkIndex; |
259 | | |
260 | | uint32_t mDataLength; |
261 | | uint64_t mTotalChunkLen; |
262 | | |
263 | | int32_t mSamplesPerChunk; |
264 | | int32_t mSamplesPerSecond; |
265 | | |
266 | | int32_t mChannels; |
267 | | int32_t mSampleFormat; |
268 | | |
269 | | UniquePtr<AudioInfo> mInfo; |
270 | | }; |
271 | | |
272 | | } // namespace mozilla |
273 | | |
274 | | #endif |