track_run_iterator.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef MEDIA_FORMATS_MP4_TRACK_RUN_ITERATOR_H_
  5. #define MEDIA_FORMATS_MP4_TRACK_RUN_ITERATOR_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <memory>
  9. #include <vector>
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/time/time.h"
  12. #include "media/base/media_export.h"
  13. #include "media/base/media_log.h"
  14. #include "media/base/stream_parser_buffer.h"
  15. #include "media/formats/mp4/box_definitions.h"
  16. #include "media/media_buildflags.h"
  17. namespace media {
  18. class DecryptConfig;
  19. namespace mp4 {
  20. base::TimeDelta MEDIA_EXPORT TimeDeltaFromRational(int64_t numer,
  21. int64_t denom);
  22. DecodeTimestamp MEDIA_EXPORT DecodeTimestampFromRational(int64_t numer,
  23. int64_t denom);
  24. struct SampleInfo;
  25. struct TrackRunInfo;
  26. class MEDIA_EXPORT TrackRunIterator {
  27. public:
  28. // Create a new TrackRunIterator. A reference to |moov| will be retained for
  29. // the lifetime of this object.
  30. TrackRunIterator(const Movie* moov, MediaLog* media_log);
  31. TrackRunIterator(const TrackRunIterator&) = delete;
  32. TrackRunIterator& operator=(const TrackRunIterator&) = delete;
  33. ~TrackRunIterator();
  34. // Sets up the iterator to handle all the runs from the current fragment.
  35. bool Init(const MovieFragment& moof);
  36. // Returns true if the properties of the current run or sample are valid.
  37. bool IsRunValid() const;
  38. bool IsSampleValid() const;
  39. // Advance the properties to refer to the next run or sample. These return
  40. // |false| on failure, but note that advancing to the end (IsRunValid() or
  41. // IsSampleValid() return false) is not a failure, and the properties are not
  42. // guaranteed to be consistent in that case.
  43. bool AdvanceRun();
  44. bool AdvanceSample();
  45. // Returns true if this track run has auxiliary information and has not yet
  46. // been cached. Only valid if IsRunValid().
  47. bool AuxInfoNeedsToBeCached();
  48. // Caches the CENC data from the given buffer. |buf| must be a buffer starting
  49. // at the offset given by cenc_offset(), with a |size| of at least
  50. // cenc_size(). Returns true on success, false on error.
  51. bool CacheAuxInfo(const uint8_t* buf, int size);
  52. // Returns the maximum buffer location at which no data earlier in the stream
  53. // will be required in order to read the current or any subsequent sample. You
  54. // may clear all data up to this offset before reading the current sample
  55. // safely. Result is in the same units as offset() (for Media Source this is
  56. // in bytes past the the head of the MOOF box).
  57. int64_t GetMaxClearOffset();
  58. // Property of the current run. Only valid if IsRunValid().
  59. uint32_t track_id() const;
  60. int64_t aux_info_offset() const;
  61. int aux_info_size() const;
  62. bool is_encrypted() const;
  63. bool is_audio() const;
  64. // Only one is valid, based on the value of is_audio().
  65. const AudioSampleEntry& audio_description() const;
  66. const VideoSampleEntry& video_description() const;
  67. // Properties of the current sample. Only valid if IsSampleValid().
  68. int64_t sample_offset() const;
  69. uint32_t sample_size() const;
  70. DecodeTimestamp dts() const;
  71. base::TimeDelta cts() const;
  72. base::TimeDelta duration() const;
  73. bool is_keyframe() const;
  74. // Only call when is_encrypted() is true and AuxInfoNeedsToBeCached() is
  75. // false. Result is owned by caller.
  76. std::unique_ptr<DecryptConfig> GetDecryptConfig();
  77. private:
  78. bool UpdateCts();
  79. bool ResetRun();
  80. const ProtectionSchemeInfo& protection_scheme_info() const;
  81. const TrackEncryption& track_encryption() const;
  82. uint32_t GetGroupDescriptionIndex(uint32_t sample_index) const;
  83. // Sample encryption information.
  84. bool IsSampleEncrypted(size_t sample_index) const;
  85. uint8_t GetIvSize(size_t sample_index) const;
  86. const std::vector<uint8_t>& GetKeyId(size_t sample_index) const;
  87. bool ApplyConstantIv(size_t sample_index, SampleEncryptionEntry* entry) const;
  88. raw_ptr<const Movie> moov_;
  89. raw_ptr<MediaLog> media_log_;
  90. std::vector<TrackRunInfo> runs_;
  91. std::vector<TrackRunInfo>::const_iterator run_itr_;
  92. std::vector<SampleInfo>::const_iterator sample_itr_;
  93. int64_t sample_dts_;
  94. int64_t sample_cts_;
  95. int64_t sample_offset_;
  96. };
  97. } // namespace mp4
  98. } // namespace media
  99. #endif // MEDIA_FORMATS_MP4_TRACK_RUN_ITERATOR_H_