1// Copyright (c) 2012 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_MP4_TRACK_RUN_ITERATOR_H_
6#define MEDIA_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/mp4/box_definitions.h"
15#include "media/mp4/cenc.h"
16
17namespace media {
18
19class DecryptConfig;
20
21namespace mp4 {
22
23using base::TimeDelta;
24base::TimeDelta MEDIA_EXPORT TimeDeltaFromRational(int64 numer, int64 denom);
25
26struct SampleInfo;
27struct TrackRunInfo;
28
29class MEDIA_EXPORT TrackRunIterator {
30 public:
31  // Create a new TrackRunIterator. A reference to |moov| will be retained for
32  // the lifetime of this object.
33  TrackRunIterator(const Movie* moov, const LogCB& log_cb);
34  ~TrackRunIterator();
35
36  // Sets up the iterator to handle all the runs from the current fragment.
37  bool Init(const MovieFragment& moof);
38
39  // Returns true if the properties of the current run or sample are valid.
40  bool IsRunValid() const;
41  bool IsSampleValid() const;
42
43  // Advance the properties to refer to the next run or sample. Requires that
44  // the current sample be valid.
45  void AdvanceRun();
46  void AdvanceSample();
47
48  // Returns true if this track run has auxiliary information and has not yet
49  // been cached. Only valid if IsRunValid().
50  bool AuxInfoNeedsToBeCached();
51
52  // Caches the CENC data from the given buffer. |buf| must be a buffer starting
53  // at the offset given by cenc_offset(), with a |size| of at least
54  // cenc_size(). Returns true on success, false on error.
55  bool CacheAuxInfo(const uint8* buf, int size);
56
57  // Returns the maximum buffer location at which no data earlier in the stream
58  // will be required in order to read the current or any subsequent sample. You
59  // may clear all data up to this offset before reading the current sample
60  // safely. Result is in the same units as offset() (for Media Source this is
61  // in bytes past the the head of the MOOF box).
62  int64 GetMaxClearOffset();
63
64  // Property of the current run. Only valid if IsRunValid().
65  uint32 track_id() const;
66  int64 aux_info_offset() const;
67  int aux_info_size() const;
68  bool is_encrypted() const;
69  bool is_audio() const;
70  // Only one is valid, based on the value of is_audio().
71  const AudioSampleEntry& audio_description() const;
72  const VideoSampleEntry& video_description() const;
73
74  // Properties of the current sample. Only valid if IsSampleValid().
75  int64 sample_offset() const;
76  int sample_size() const;
77  TimeDelta dts() const;
78  TimeDelta cts() const;
79  TimeDelta duration() const;
80  bool is_keyframe() const;
81
82  // Only call when is_encrypted() is true and AuxInfoNeedsToBeCached() is
83  // false. Result is owned by caller.
84  scoped_ptr<DecryptConfig> GetDecryptConfig();
85
86 private:
87  void ResetRun();
88  const TrackEncryption& track_encryption() const;
89
90  const Movie* moov_;
91  LogCB log_cb_;
92
93  std::vector<TrackRunInfo> runs_;
94  std::vector<TrackRunInfo>::const_iterator run_itr_;
95  std::vector<SampleInfo>::const_iterator sample_itr_;
96
97  std::vector<FrameCENCInfo> cenc_info_;
98
99  int64 sample_dts_;
100  int64 sample_offset_;
101
102  DISALLOW_COPY_AND_ASSIGN(TrackRunIterator);
103};
104
105}  // namespace mp4
106}  // namespace media
107
108#endif  // MEDIA_MP4_TRACK_RUN_ITERATOR_H_
109