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
5#ifndef MEDIA_FORMATS_MP4_TRACK_RUN_ITERATOR_H_
6#define MEDIA_FORMATS_MP4_TRACK_RUN_ITERATOR_H_
7
8#include <vector>
9
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"
17
18namespace media {
19
20class DecryptConfig;
21
22namespace mp4 {
23
24base::TimeDelta MEDIA_EXPORT TimeDeltaFromRational(int64 numer, int64 denom);
25DecodeTimestamp MEDIA_EXPORT DecodeTimestampFromRational(int64 numer,
26                                                         int64 denom);
27
28struct SampleInfo;
29struct TrackRunInfo;
30
31class 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 LogCB& log_cb);
36  ~TrackRunIterator();
37
38  // Sets up the iterator to handle all the runs from the current fragment.
39  bool Init(const MovieFragment& moof);
40
41  // Returns true if the properties of the current run or sample are valid.
42  bool IsRunValid() const;
43  bool IsSampleValid() const;
44
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();
49
50  // Returns true if this track run has auxiliary information and has not yet
51  // been cached. Only valid if IsRunValid().
52  bool AuxInfoNeedsToBeCached();
53
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);
58
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();
65
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;
75
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;
83  bool is_random_access_point() const;
84
85  // Only call when is_encrypted() is true and AuxInfoNeedsToBeCached() is
86  // false. Result is owned by caller.
87  scoped_ptr<DecryptConfig> GetDecryptConfig();
88
89 private:
90  void ResetRun();
91  const TrackEncryption& track_encryption() const;
92
93  uint32 GetGroupDescriptionIndex(uint32 sample_index) const;
94  const CencSampleEncryptionInfoEntry& GetSampleEncryptionInfoEntry(
95      uint32 group_description_index) const;
96
97  // Sample encryption information.
98  bool IsSampleEncrypted(size_t sample_index) const;
99  uint8 GetIvSize(size_t sample_index) const;
100  const std::vector<uint8>& GetKeyId(size_t sample_index) const;
101
102  const Movie* moov_;
103  LogCB log_cb_;
104
105  std::vector<TrackRunInfo> runs_;
106  std::vector<TrackRunInfo>::const_iterator run_itr_;
107  std::vector<SampleInfo>::const_iterator sample_itr_;
108
109  std::vector<FrameCENCInfo> cenc_info_;
110
111  int64 sample_dts_;
112  int64 sample_offset_;
113
114  DISALLOW_COPY_AND_ASSIGN(TrackRunIterator);
115};
116
117}  // namespace mp4
118}  // namespace media
119
120#endif  // MEDIA_FORMATS_MP4_TRACK_RUN_ITERATOR_H_
121