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.
5 #ifndef MEDIA_BASE_STREAM_PARSER_BUFFER_H_
6 #define MEDIA_BASE_STREAM_PARSER_BUFFER_H_
10 #include "media/base/decoder_buffer.h"
11 #include "media/base/demuxer_stream.h"
12 #include "media/base/media_export.h"
13 #include "media/base/stream_parser.h"
17 // Simple wrapper around base::TimeDelta that represents a decode timestamp.
18 // Making DecodeTimestamp a different type makes it easier to determine whether
19 // code is operating on presentation or decode timestamps and makes conversions
20 // between the two types explicit and easy to spot.
21 class DecodeTimestamp {
24 DecodeTimestamp(const DecodeTimestamp& rhs) : ts_(rhs.ts_) { }
25 DecodeTimestamp& operator=(const DecodeTimestamp& rhs) {
31 // Only operators that are actually used by the code have been defined.
32 // Reviewers should pay close attention to the addition of new operators.
33 bool operator<(const DecodeTimestamp& rhs) const { return ts_ < rhs.ts_; }
34 bool operator>(const DecodeTimestamp& rhs) const { return ts_ > rhs.ts_; }
35 bool operator==(const DecodeTimestamp& rhs) const { return ts_ == rhs.ts_; }
36 bool operator!=(const DecodeTimestamp& rhs) const { return ts_ != rhs.ts_; }
37 bool operator>=(const DecodeTimestamp& rhs) const { return ts_ >= rhs.ts_; }
38 bool operator<=(const DecodeTimestamp& rhs) const { return ts_ <= rhs.ts_; }
40 base::TimeDelta operator-(const DecodeTimestamp& rhs) const {
44 DecodeTimestamp& operator+=(const base::TimeDelta& rhs) {
49 DecodeTimestamp& operator-=(const base::TimeDelta& rhs) {
54 DecodeTimestamp operator+(const base::TimeDelta& rhs) const {
55 return DecodeTimestamp(ts_ + rhs);
58 DecodeTimestamp operator-(const base::TimeDelta& rhs) const {
59 return DecodeTimestamp(ts_ - rhs);
62 int64 operator/(const base::TimeDelta& rhs) const {
66 static DecodeTimestamp FromSecondsD(double seconds) {
67 return DecodeTimestamp(base::TimeDelta::FromSecondsD(seconds));
70 static DecodeTimestamp FromMilliseconds(int64 milliseconds) {
71 return DecodeTimestamp(base::TimeDelta::FromMilliseconds(milliseconds));
74 static DecodeTimestamp FromMicroseconds(int64 microseconds) {
75 return DecodeTimestamp(base::TimeDelta::FromMicroseconds(microseconds));
78 // This method is used to explicitly call out when presentation timestamps
79 // are being converted to a decode timestamp.
80 static DecodeTimestamp FromPresentationTime(base::TimeDelta timestamp) {
81 return DecodeTimestamp(timestamp);
84 double InSecondsF() const { return ts_.InSecondsF(); }
85 int64 InMilliseconds() const { return ts_.InMilliseconds(); }
86 int64 InMicroseconds() const { return ts_.InMicroseconds(); }
88 // TODO(acolwell): Remove once all the hacks are gone. This method is called
89 // by hacks where a decode time is being used as a presentation time.
90 base::TimeDelta ToPresentationTime() const { return ts_; }
93 explicit DecodeTimestamp(base::TimeDelta timestamp) : ts_(timestamp) { }
98 MEDIA_EXPORT extern inline DecodeTimestamp kNoDecodeTimestamp() {
99 return DecodeTimestamp::FromPresentationTime(kNoTimestamp());
102 class MEDIA_EXPORT StreamParserBuffer : public DecoderBuffer {
104 // Value used to signal an invalid decoder config ID.
105 enum { kInvalidConfigId = -1 };
107 typedef DemuxerStream::Type Type;
108 typedef StreamParser::TrackId TrackId;
110 static scoped_refptr<StreamParserBuffer> CreateEOSBuffer();
112 static scoped_refptr<StreamParserBuffer> CopyFrom(
113 const uint8* data, int data_size, bool is_keyframe, Type type,
115 static scoped_refptr<StreamParserBuffer> CopyFrom(
116 const uint8* data, int data_size,
117 const uint8* side_data, int side_data_size, bool is_keyframe, Type type,
119 bool IsKeyframe() const { return is_keyframe_; }
121 // Decode timestamp. If not explicitly set, or set to kNoTimestamp(), the
122 // value will be taken from the normal timestamp.
123 DecodeTimestamp GetDecodeTimestamp() const;
124 void SetDecodeTimestamp(DecodeTimestamp timestamp);
126 // Gets/sets the ID of the decoder config associated with this buffer.
127 int GetConfigId() const;
128 void SetConfigId(int config_id);
130 // Gets the parser's media type associated with this buffer. Value is
131 // meaningless for EOS buffers.
132 Type type() const { return type_; }
134 // Gets the parser's track ID associated with this buffer. Value is
135 // meaningless for EOS buffers.
136 TrackId track_id() const { return track_id_; }
138 // Converts this buffer to a splice buffer. |pre_splice_buffers| must not
139 // have any EOS buffers, must not have any splice buffers, nor must have any
140 // buffer with preroll.
142 // |pre_splice_buffers| will be deep copied and each copy's splice_timestamp()
143 // will be set to this buffer's splice_timestamp(). A copy of |this|, with a
144 // splice_timestamp() of kNoTimestamp(), will be added to the end of
145 // |splice_buffers_|.
147 // See the Audio Splice Frame Algorithm in the MSE specification for details.
148 typedef StreamParser::BufferQueue BufferQueue;
149 void ConvertToSpliceBuffer(const BufferQueue& pre_splice_buffers);
150 const BufferQueue& splice_buffers() const { return splice_buffers_; }
152 // Specifies a buffer which must be decoded prior to this one to ensure this
153 // buffer can be accurately decoded. The given buffer must be of the same
154 // type, must not be a splice buffer, must not have any discard padding, and
155 // must not be an end of stream buffer. |preroll| is not copied.
157 // It's expected that this preroll buffer will be discarded entirely post
158 // decoding. As such it's discard_padding() will be set to kInfiniteDuration.
160 // All future timestamp, decode timestamp, config id, or track id changes to
161 // this buffer will be applied to the preroll buffer as well.
162 void SetPrerollBuffer(const scoped_refptr<StreamParserBuffer>& preroll);
163 const scoped_refptr<StreamParserBuffer>& preroll_buffer() {
164 return preroll_buffer_;
167 virtual void set_timestamp(base::TimeDelta timestamp) OVERRIDE;
170 StreamParserBuffer(const uint8* data, int data_size,
171 const uint8* side_data, int side_data_size,
172 bool is_keyframe, Type type,
174 virtual ~StreamParserBuffer();
177 DecodeTimestamp decode_timestamp_;
181 BufferQueue splice_buffers_;
182 scoped_refptr<StreamParserBuffer> preroll_buffer_;
184 DISALLOW_COPY_AND_ASSIGN(StreamParserBuffer);
189 #endif // MEDIA_BASE_STREAM_PARSER_BUFFER_H_