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 FLAC_DEMUXER_H_
8 : #define FLAC_DEMUXER_H_
9 :
10 : #include "mozilla/Attributes.h"
11 : #include "MediaDataDemuxer.h"
12 : #include "MediaResource.h"
13 : namespace mozilla {
14 :
15 : namespace flac {
16 : class Frame;
17 : class FrameParser;
18 : }
19 : class FlacTrackDemuxer;
20 :
21 :
22 0 : class FlacDemuxer : public MediaDataDemuxer
23 : {
24 : public:
25 : // MediaDataDemuxer interface.
26 : explicit FlacDemuxer(MediaResource* aSource);
27 : RefPtr<InitPromise> Init() override;
28 : bool HasTrackType(TrackInfo::TrackType aType) const override;
29 : uint32_t GetNumberTracks(TrackInfo::TrackType aType) const override;
30 : already_AddRefed<MediaTrackDemuxer> GetTrackDemuxer(
31 : TrackInfo::TrackType aType, uint32_t aTrackNumber) override;
32 : bool IsSeekable() const override;
33 :
34 : // Return true if a valid flac frame header could be found.
35 : static bool FlacSniffer(const uint8_t* aData, const uint32_t aLength);
36 :
37 : private:
38 : bool InitInternal();
39 :
40 : RefPtr<MediaResource> mSource;
41 : RefPtr<FlacTrackDemuxer> mTrackDemuxer;
42 : };
43 :
44 : class FlacTrackDemuxer : public MediaTrackDemuxer
45 : {
46 : public:
47 : explicit FlacTrackDemuxer(MediaResource* aSource);
48 :
49 : // Initializes the track demuxer by reading the first frame for meta data.
50 : // Returns initialization success state.
51 : bool Init();
52 :
53 : // MediaTrackDemuxer interface.
54 : UniquePtr<TrackInfo> GetInfo() const override;
55 : RefPtr<SeekPromise> Seek(const media::TimeUnit& aTime) override;
56 : RefPtr<SamplesPromise> GetSamples(int32_t aNumSamples = 1) override;
57 : void Reset() override;
58 : int64_t GetResourceOffset() const override;
59 : media::TimeIntervals GetBuffered() override;
60 : RefPtr<SkipAccessPointPromise> SkipToNextRandomAccessPoint(
61 : const media::TimeUnit& aTimeThreshold) override;
62 :
63 : bool IsSeekable() const;
64 :
65 : private:
66 : // Destructor.
67 : ~FlacTrackDemuxer();
68 :
69 : // Returns the estimated stream duration, or a 0-duration if unknown.
70 : media::TimeUnit Duration() const;
71 : media::TimeUnit TimeAtEnd();
72 :
73 : // Fast approximate seeking to given time.
74 : media::TimeUnit FastSeek(const media::TimeUnit& aTime);
75 :
76 : // Seeks by scanning the stream up to the given time for more accurate
77 : // results.
78 : media::TimeUnit ScanUntil(const media::TimeUnit& aTime);
79 :
80 : // Finds the next valid frame and return it.
81 : const flac::Frame& FindNextFrame();
82 :
83 : // Returns the next ADTS frame, if available.
84 : already_AddRefed<MediaRawData> GetNextFrame(const flac::Frame& aFrame);
85 :
86 : // Reads aSize bytes into aBuffer from the source starting at aOffset.
87 : // Returns the actual size read.
88 : int32_t Read(uint8_t* aBuffer, int64_t aOffset, int32_t aSize);
89 :
90 : // Returns the average frame length derived from the previously parsed frames.
91 : double AverageFrameLength() const;
92 :
93 : // The (hopefully) Flac resource.
94 : MediaResourceIndex mSource;
95 :
96 : // Flac frame parser used to detect frames and extract side info.
97 : nsAutoPtr<flac::FrameParser> mParser;
98 :
99 : // Total duration of parsed frames.
100 : media::TimeUnit mParsedFramesDuration;
101 :
102 : // Sum of parsed frames' lengths in bytes.
103 : uint64_t mTotalFrameLen;
104 :
105 : // Audio track config info.
106 : UniquePtr<AudioInfo> mInfo;
107 : };
108 :
109 : } // mozilla
110 :
111 : #endif // !FLAC_DEMUXER_H_
|