tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

ADTSDemuxer.h (4720B)


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