Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / formats / mp4 / track_run_iterator.h
blobfa82cc1e6372c8ea9d014ea3bdecfc272c5f5e60
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.
5 #ifndef MEDIA_FORMATS_MP4_TRACK_RUN_ITERATOR_H_
6 #define MEDIA_FORMATS_MP4_TRACK_RUN_ITERATOR_H_
8 #include <vector>
10 #include "base/memory/scoped_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/formats/mp4/cenc.h"
18 namespace media {
20 class DecryptConfig;
22 namespace mp4 {
24 base::TimeDelta MEDIA_EXPORT TimeDeltaFromRational(int64 numer, int64 denom);
25 DecodeTimestamp MEDIA_EXPORT DecodeTimestampFromRational(int64 numer,
26 int64 denom);
28 struct SampleInfo;
29 struct TrackRunInfo;
31 class MEDIA_EXPORT TrackRunIterator {
32 public:
33 // Create a new TrackRunIterator. A reference to |moov| will be retained for
34 // the lifetime of this object.
35 TrackRunIterator(const Movie* moov, const scoped_refptr<MediaLog>& media_log);
36 ~TrackRunIterator();
38 // Sets up the iterator to handle all the runs from the current fragment.
39 bool Init(const MovieFragment& moof);
41 // Returns true if the properties of the current run or sample are valid.
42 bool IsRunValid() const;
43 bool IsSampleValid() const;
45 // Advance the properties to refer to the next run or sample. Requires that
46 // the current sample be valid.
47 void AdvanceRun();
48 void AdvanceSample();
50 // Returns true if this track run has auxiliary information and has not yet
51 // been cached. Only valid if IsRunValid().
52 bool AuxInfoNeedsToBeCached();
54 // Caches the CENC data from the given buffer. |buf| must be a buffer starting
55 // at the offset given by cenc_offset(), with a |size| of at least
56 // cenc_size(). Returns true on success, false on error.
57 bool CacheAuxInfo(const uint8* buf, int size);
59 // Returns the maximum buffer location at which no data earlier in the stream
60 // will be required in order to read the current or any subsequent sample. You
61 // may clear all data up to this offset before reading the current sample
62 // safely. Result is in the same units as offset() (for Media Source this is
63 // in bytes past the the head of the MOOF box).
64 int64 GetMaxClearOffset();
66 // Property of the current run. Only valid if IsRunValid().
67 uint32 track_id() const;
68 int64 aux_info_offset() const;
69 int aux_info_size() const;
70 bool is_encrypted() const;
71 bool is_audio() const;
72 // Only one is valid, based on the value of is_audio().
73 const AudioSampleEntry& audio_description() const;
74 const VideoSampleEntry& video_description() const;
76 // Properties of the current sample. Only valid if IsSampleValid().
77 int64 sample_offset() const;
78 int sample_size() const;
79 DecodeTimestamp dts() const;
80 base::TimeDelta cts() const;
81 base::TimeDelta duration() const;
82 bool is_keyframe() const;
84 // Only call when is_encrypted() is true and AuxInfoNeedsToBeCached() is
85 // false. Result is owned by caller.
86 scoped_ptr<DecryptConfig> GetDecryptConfig();
88 private:
89 void ResetRun();
90 const TrackEncryption& track_encryption() const;
92 uint32 GetGroupDescriptionIndex(uint32 sample_index) const;
94 // Sample encryption information.
95 bool IsSampleEncrypted(size_t sample_index) const;
96 uint8 GetIvSize(size_t sample_index) const;
97 const std::vector<uint8>& GetKeyId(size_t sample_index) const;
99 const Movie* moov_;
100 scoped_refptr<MediaLog> media_log_;
102 std::vector<TrackRunInfo> runs_;
103 std::vector<TrackRunInfo>::const_iterator run_itr_;
104 std::vector<SampleInfo>::const_iterator sample_itr_;
106 std::vector<FrameCENCInfo> cenc_info_;
108 int64 sample_dts_;
109 int64 sample_offset_;
111 DISALLOW_COPY_AND_ASSIGN(TrackRunIterator);
114 } // namespace mp4
115 } // namespace media
117 #endif // MEDIA_FORMATS_MP4_TRACK_RUN_ITERATOR_H_