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_key_frame
, 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_key_frame
, Type type
,
120 // Decode timestamp. If not explicitly set, or set to kNoTimestamp(), the
121 // value will be taken from the normal timestamp.
122 DecodeTimestamp
GetDecodeTimestamp() const;
123 void SetDecodeTimestamp(DecodeTimestamp timestamp
);
125 // Gets/sets the ID of the decoder config associated with this buffer.
126 int GetConfigId() const;
127 void SetConfigId(int config_id
);
129 // Returns the config ID of this buffer if it has no splice buffers or
130 // |index| is out of range. Otherwise returns the config ID for the
131 // buffer in |splice_buffers_| at position |index|.
132 int GetSpliceBufferConfigId(size_t index
) const;
134 // Gets the parser's media type associated with this buffer. Value is
135 // meaningless for EOS buffers.
136 Type
type() const { return type_
; }
138 // Gets the parser's track ID associated with this buffer. Value is
139 // meaningless for EOS buffers.
140 TrackId
track_id() const { return track_id_
; }
142 // Converts this buffer to a splice buffer. |pre_splice_buffers| must not
143 // have any EOS buffers, must not have any splice buffers, nor must have any
144 // buffer with preroll.
146 // |pre_splice_buffers| will be deep copied and each copy's splice_timestamp()
147 // will be set to this buffer's splice_timestamp(). A copy of |this|, with a
148 // splice_timestamp() of kNoTimestamp(), will be added to the end of
149 // |splice_buffers_|.
151 // See the Audio Splice Frame Algorithm in the MSE specification for details.
152 typedef StreamParser::BufferQueue BufferQueue
;
153 void ConvertToSpliceBuffer(const BufferQueue
& pre_splice_buffers
);
154 const BufferQueue
& splice_buffers() const { return splice_buffers_
; }
156 // Specifies a buffer which must be decoded prior to this one to ensure this
157 // buffer can be accurately decoded. The given buffer must be of the same
158 // type, must not be a splice buffer, must not have any discard padding, and
159 // must not be an end of stream buffer. |preroll| is not copied.
161 // It's expected that this preroll buffer will be discarded entirely post
162 // decoding. As such it's discard_padding() will be set to kInfiniteDuration.
164 // All future timestamp, decode timestamp, config id, or track id changes to
165 // this buffer will be applied to the preroll buffer as well.
166 void SetPrerollBuffer(const scoped_refptr
<StreamParserBuffer
>& preroll
);
167 const scoped_refptr
<StreamParserBuffer
>& preroll_buffer() {
168 return preroll_buffer_
;
171 void set_timestamp(base::TimeDelta timestamp
) override
;
174 StreamParserBuffer(const uint8
* data
, int data_size
,
175 const uint8
* side_data
, int side_data_size
,
176 bool is_key_frame
, Type type
,
178 ~StreamParserBuffer() override
;
180 DecodeTimestamp decode_timestamp_
;
184 BufferQueue splice_buffers_
;
185 scoped_refptr
<StreamParserBuffer
> preroll_buffer_
;
187 DISALLOW_COPY_AND_ASSIGN(StreamParserBuffer
);
192 #endif // MEDIA_BASE_STREAM_PARSER_BUFFER_H_