tor-browser

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

MP4Metadata.h (3319B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, 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 MP4METADATA_H_
      6 #define MP4METADATA_H_
      7 
      8 #include <type_traits>
      9 
     10 #include "ByteStream.h"
     11 #include "DecoderData.h"
     12 #include "MediaData.h"
     13 #include "MediaInfo.h"
     14 #include "MediaResult.h"
     15 #include "SampleIterator.h"
     16 #include "mozilla/UniquePtr.h"
     17 #include "mp4parse.h"
     18 
     19 namespace mozilla {
     20 
     21 DDLoggedTypeDeclName(MP4Metadata);
     22 
     23 // The memory owner in mIndice.indices is rust mp4 parser, so lifetime of this
     24 // class SHOULD NOT longer than rust parser.
     25 class IndiceWrapper {
     26 public:
     27  size_t Length() const;
     28 
     29  bool GetIndice(size_t aIndex, MP4SampleIndex::Indice& aIndice) const;
     30 
     31  explicit IndiceWrapper(Mp4parseByteData& aRustIndice);
     32 
     33 protected:
     34  Mp4parseByteData mIndice;
     35 };
     36 
     37 struct FreeMP4Parser {
     38  void operator()(Mp4parseParser* aPtr) { mp4parse_free(aPtr); }
     39 };
     40 
     41 // Wrap an Stream to remember the read offset.
     42 class StreamAdaptor {
     43 public:
     44  explicit StreamAdaptor(ByteStream* aSource) : mSource(aSource), mOffset(0) {}
     45 
     46  ~StreamAdaptor() = default;
     47 
     48  nsresult Read(uint8_t* buffer, uintptr_t size, size_t* bytes_read);
     49 
     50 private:
     51  ByteStream* mSource;
     52  CheckedInt<size_t> mOffset;
     53 };
     54 
     55 class MP4Metadata : public DecoderDoctorLifeLogger<MP4Metadata> {
     56 public:
     57  explicit MP4Metadata(ByteStream* aSource);
     58  ~MP4Metadata();
     59 
     60  // Simple template class containing a MediaResult and another type.
     61  template <typename T>
     62  class ResultAndType {
     63   public:
     64    template <typename M2, typename T2>
     65    ResultAndType(M2&& aM, T2&& aT)
     66        : mResult(std::forward<M2>(aM)), mT(std::forward<T2>(aT)) {}
     67    ResultAndType(const ResultAndType&) = default;
     68    ResultAndType& operator=(const ResultAndType&) = default;
     69    ResultAndType(ResultAndType&&) = default;
     70    ResultAndType& operator=(ResultAndType&&) = default;
     71 
     72    mozilla::MediaResult& Result() { return mResult; }
     73    T& Ref() { return mT; }
     74 
     75   private:
     76    mozilla::MediaResult mResult;
     77    std::decay_t<T> mT;
     78  };
     79 
     80  using ResultAndByteBuffer = ResultAndType<RefPtr<mozilla::MediaByteBuffer>>;
     81  static ResultAndByteBuffer Metadata(ByteStream* aSource);
     82 
     83  static constexpr uint32_t NumberTracksError() { return UINT32_MAX; }
     84  using ResultAndTrackCount = ResultAndType<uint32_t>;
     85  ResultAndTrackCount GetNumberTracks(
     86      mozilla::TrackInfo::TrackType aType) const;
     87 
     88  using ResultAndTrackInfo =
     89      ResultAndType<mozilla::UniquePtr<mozilla::TrackInfo>>;
     90  ResultAndTrackInfo GetTrackInfo(mozilla::TrackInfo::TrackType aType,
     91                                  size_t aTrackNumber) const;
     92 
     93  bool CanSeek() const;
     94 
     95  using ResultAndCryptoFile = ResultAndType<const CryptoFile*>;
     96  ResultAndCryptoFile Crypto() const;
     97 
     98  using ResultAndIndice = ResultAndType<mozilla::UniquePtr<IndiceWrapper>>;
     99  ResultAndIndice GetTrackIndice(uint32_t aTrackId) const;
    100 
    101  nsresult Parse();
    102 
    103 private:
    104  void UpdateCrypto();
    105  Maybe<uint32_t> TrackTypeToGlobalTrackIndex(
    106      mozilla::TrackInfo::TrackType aType, size_t aTrackNumber) const;
    107 
    108  CryptoFile mCrypto;
    109  RefPtr<ByteStream> mSource;
    110  StreamAdaptor mSourceAdaptor;
    111  mozilla::UniquePtr<Mp4parseParser, FreeMP4Parser> mParser;
    112 };
    113 
    114 }  // namespace mozilla
    115 
    116 #endif  // MP4METADATA_H_