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"
14 #include "media/base/timestamp_constants.h"
18 // Simple wrapper around base::TimeDelta that represents a decode timestamp.
19 // Making DecodeTimestamp a different type makes it easier to determine whether
20 // code is operating on presentation or decode timestamps and makes conversions
21 // between the two types explicit and easy to spot.
22 class DecodeTimestamp
{
25 DecodeTimestamp(const DecodeTimestamp
& rhs
) : ts_(rhs
.ts_
) { }
26 DecodeTimestamp
& operator=(const DecodeTimestamp
& rhs
) {
32 // Only operators that are actually used by the code have been defined.
33 // Reviewers should pay close attention to the addition of new operators.
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_
; }
39 bool operator<=(const DecodeTimestamp
& rhs
) const { return ts_
<= rhs
.ts_
; }
41 base::TimeDelta
operator-(const DecodeTimestamp
& rhs
) const {
45 DecodeTimestamp
& operator+=(const base::TimeDelta
& rhs
) {
50 DecodeTimestamp
& operator-=(const base::TimeDelta
& rhs
) {
55 DecodeTimestamp
operator+(const base::TimeDelta
& rhs
) const {
56 return DecodeTimestamp(ts_
+ rhs
);
59 DecodeTimestamp
operator-(const base::TimeDelta
& rhs
) const {
60 return DecodeTimestamp(ts_
- rhs
);
63 int64
operator/(const base::TimeDelta
& rhs
) const {
67 static DecodeTimestamp
FromSecondsD(double seconds
) {
68 return DecodeTimestamp(base::TimeDelta::FromSecondsD(seconds
));
71 static DecodeTimestamp
FromMilliseconds(int64 milliseconds
) {
72 return DecodeTimestamp(base::TimeDelta::FromMilliseconds(milliseconds
));
75 static DecodeTimestamp
FromMicroseconds(int64 microseconds
) {
76 return DecodeTimestamp(base::TimeDelta::FromMicroseconds(microseconds
));
79 // This method is used to explicitly call out when presentation timestamps
80 // are being converted to a decode timestamp.
81 static DecodeTimestamp
FromPresentationTime(base::TimeDelta timestamp
) {
82 return DecodeTimestamp(timestamp
);
85 double InSecondsF() const { return ts_
.InSecondsF(); }
86 int64
InMilliseconds() const { return ts_
.InMilliseconds(); }
87 int64
InMicroseconds() const { return ts_
.InMicroseconds(); }
89 // TODO(acolwell): Remove once all the hacks are gone. This method is called
90 // by hacks where a decode time is being used as a presentation time.
91 base::TimeDelta
ToPresentationTime() const { return ts_
; }
94 explicit DecodeTimestamp(base::TimeDelta timestamp
) : ts_(timestamp
) { }
99 MEDIA_EXPORT
extern inline DecodeTimestamp
kNoDecodeTimestamp() {
100 return DecodeTimestamp::FromPresentationTime(kNoTimestamp());
103 class MEDIA_EXPORT StreamParserBuffer
: public DecoderBuffer
{
105 // Value used to signal an invalid decoder config ID.
106 enum { kInvalidConfigId
= -1 };
108 typedef DemuxerStream::Type Type
;
109 typedef StreamParser::TrackId TrackId
;
111 static scoped_refptr
<StreamParserBuffer
> CreateEOSBuffer();
113 static scoped_refptr
<StreamParserBuffer
> CopyFrom(
114 const uint8
* data
, int data_size
, bool is_key_frame
, Type type
,
116 static scoped_refptr
<StreamParserBuffer
> CopyFrom(
117 const uint8
* data
, int data_size
,
118 const uint8
* side_data
, int side_data_size
, bool is_key_frame
, Type type
,
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 // Returns the config ID of this buffer if it has no splice buffers or
131 // |index| is out of range. Otherwise returns the config ID for the
132 // buffer in |splice_buffers_| at position |index|.
133 int GetSpliceBufferConfigId(size_t index
) const;
135 // Gets the parser's media type associated with this buffer. Value is
136 // meaningless for EOS buffers.
137 Type
type() const { return type_
; }
138 const char* GetTypeName() const;
140 // Gets the parser's track ID associated with this buffer. Value is
141 // meaningless for EOS buffers.
142 TrackId
track_id() const { return track_id_
; }
144 // Converts this buffer to a splice buffer. |pre_splice_buffers| must not
145 // have any EOS buffers, must not have any splice buffers, nor must have any
146 // buffer with preroll.
148 // |pre_splice_buffers| will be deep copied and each copy's splice_timestamp()
149 // will be set to this buffer's splice_timestamp(). A copy of |this|, with a
150 // splice_timestamp() of kNoTimestamp(), will be added to the end of
151 // |splice_buffers_|.
153 // See the Audio Splice Frame Algorithm in the MSE specification for details.
154 typedef StreamParser::BufferQueue BufferQueue
;
155 void ConvertToSpliceBuffer(const BufferQueue
& pre_splice_buffers
);
156 const BufferQueue
& splice_buffers() const { return splice_buffers_
; }
158 // Specifies a buffer which must be decoded prior to this one to ensure this
159 // buffer can be accurately decoded. The given buffer must be of the same
160 // type, must not be a splice buffer, must not have any discard padding, and
161 // must not be an end of stream buffer. |preroll| is not copied.
163 // It's expected that this preroll buffer will be discarded entirely post
164 // decoding. As such it's discard_padding() will be set to kInfiniteDuration.
166 // All future timestamp, decode timestamp, config id, or track id changes to
167 // this buffer will be applied to the preroll buffer as well.
168 void SetPrerollBuffer(const scoped_refptr
<StreamParserBuffer
>& preroll
);
169 const scoped_refptr
<StreamParserBuffer
>& preroll_buffer() {
170 return preroll_buffer_
;
173 void set_timestamp(base::TimeDelta timestamp
) override
;
175 bool is_duration_estimated() const { return is_duration_estimated_
; }
177 void set_is_duration_estimated(bool is_estimated
) {
178 is_duration_estimated_
= is_estimated
;
182 StreamParserBuffer(const uint8
* data
, int data_size
,
183 const uint8
* side_data
, int side_data_size
,
184 bool is_key_frame
, Type type
,
186 ~StreamParserBuffer() override
;
188 DecodeTimestamp decode_timestamp_
;
192 BufferQueue splice_buffers_
;
193 scoped_refptr
<StreamParserBuffer
> preroll_buffer_
;
194 bool is_duration_estimated_
;
196 DISALLOW_COPY_AND_ASSIGN(StreamParserBuffer
);
201 #endif // MEDIA_BASE_STREAM_PARSER_BUFFER_H_