ADTSDemuxer.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef ADTS_DEMUXER_H_
  6. #define ADTS_DEMUXER_H_
  7. #include "mozilla/Attributes.h"
  8. #include "mozilla/Maybe.h"
  9. #include "MediaDataDemuxer.h"
  10. #include "MediaResource.h"
  11. #include "mp4_demuxer/ByteReader.h"
  12. namespace mozilla {
  13. namespace adts {
  14. class Frame;
  15. class FrameParser;
  16. }
  17. class ADTSTrackDemuxer;
  18. class ADTSDemuxer : public MediaDataDemuxer {
  19. public:
  20. // MediaDataDemuxer interface.
  21. explicit ADTSDemuxer(MediaResource* aSource);
  22. RefPtr<InitPromise> Init() override;
  23. bool HasTrackType(TrackInfo::TrackType aType) const override;
  24. uint32_t GetNumberTracks(TrackInfo::TrackType aType) const override;
  25. already_AddRefed<MediaTrackDemuxer> GetTrackDemuxer(
  26. TrackInfo::TrackType aType, uint32_t aTrackNumber) override;
  27. bool IsSeekable() const override;
  28. private:
  29. bool InitInternal();
  30. RefPtr<MediaResource> mSource;
  31. RefPtr<ADTSTrackDemuxer> mTrackDemuxer;
  32. };
  33. class ADTSTrackDemuxer : public MediaTrackDemuxer {
  34. public:
  35. explicit ADTSTrackDemuxer(MediaResource* aSource);
  36. // Initializes the track demuxer by reading the first frame for meta data.
  37. // Returns initialization success state.
  38. bool Init();
  39. // Returns the total stream length if known, -1 otherwise.
  40. int64_t StreamLength() const;
  41. // Returns the estimated stream duration, or a 0-duration if unknown.
  42. media::TimeUnit Duration() const;
  43. // Returns the estimated duration up to the given frame number,
  44. // or a 0-duration if unknown.
  45. media::TimeUnit Duration(int64_t aNumFrames) const;
  46. // MediaTrackDemuxer interface.
  47. UniquePtr<TrackInfo> GetInfo() const override;
  48. RefPtr<SeekPromise> Seek(media::TimeUnit aTime) override;
  49. RefPtr<SamplesPromise> GetSamples(int32_t aNumSamples = 1) override;
  50. void Reset() override;
  51. RefPtr<SkipAccessPointPromise> SkipToNextRandomAccessPoint(
  52. media::TimeUnit aTimeThreshold) override;
  53. int64_t GetResourceOffset() const override;
  54. media::TimeIntervals GetBuffered() override;
  55. private:
  56. // Destructor.
  57. ~ADTSTrackDemuxer();
  58. // Fast approximate seeking to given time.
  59. media::TimeUnit FastSeek(const media::TimeUnit& aTime);
  60. // Seeks by scanning the stream up to the given time for more accurate results.
  61. media::TimeUnit ScanUntil(const media::TimeUnit& aTime);
  62. // Finds the next valid frame and returns its byte range.
  63. const adts::Frame& FindNextFrame(bool findFirstFrame = false);
  64. // Skips the next frame given the provided byte range.
  65. bool SkipNextFrame(const adts::Frame& aFrame);
  66. // Returns the next ADTS frame, if available.
  67. already_AddRefed<MediaRawData> GetNextFrame(const adts::Frame& aFrame);
  68. // Updates post-read meta data.
  69. void UpdateState(const adts::Frame& aFrame);
  70. // Returns the frame index for the given offset.
  71. int64_t FrameIndexFromOffset(int64_t aOffset) const;
  72. // Returns the frame index for the given time.
  73. int64_t FrameIndexFromTime(const media::TimeUnit& aTime) const;
  74. // Reads aSize bytes into aBuffer from the source starting at aOffset.
  75. // Returns the actual size read.
  76. int32_t Read(uint8_t* aBuffer, int64_t aOffset, int32_t aSize);
  77. // Returns the average frame length derived from the previously parsed frames.
  78. double AverageFrameLength() const;
  79. // The (hopefully) ADTS resource.
  80. MediaResourceIndex mSource;
  81. // ADTS frame parser used to detect frames and extract side info.
  82. adts::FrameParser* mParser;
  83. // Current byte offset in the source stream.
  84. int64_t mOffset;
  85. // Total parsed frames.
  86. uint64_t mNumParsedFrames;
  87. // Current frame index.
  88. int64_t mFrameIndex;
  89. // Sum of parsed frames' lengths in bytes.
  90. uint64_t mTotalFrameLen;
  91. // Samples per frame metric derived from frame headers or 0 if none available.
  92. uint32_t mSamplesPerFrame;
  93. // Samples per second metric derived from frame headers or 0 if none available.
  94. uint32_t mSamplesPerSecond;
  95. // Channel count derived from frame headers or 0 if none available.
  96. uint32_t mChannels;
  97. // Audio track config info.
  98. UniquePtr<AudioInfo> mInfo;
  99. };
  100. } // mozilla
  101. #endif // !ADTS_DEMUXER_H_