Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / media / base / stream_parser_buffer.h
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_BASE_STREAM_PARSER_BUFFER_H_
6 #define MEDIA_BASE_STREAM_PARSER_BUFFER_H_
7
8 #include <deque>
9
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
15 namespace media {
16
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 {
22  public:
23   DecodeTimestamp() {}
24   DecodeTimestamp(const DecodeTimestamp& rhs) : ts_(rhs.ts_) { }
25   DecodeTimestamp& operator=(const DecodeTimestamp& rhs) {
26     if (&rhs != this)
27       ts_ = rhs.ts_;
28     return *this;
29   }
30
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_; }
39
40   base::TimeDelta operator-(const DecodeTimestamp& rhs) const {
41     return ts_ - rhs.ts_;
42   }
43
44   DecodeTimestamp& operator+=(const base::TimeDelta& rhs) {
45     ts_ += rhs;
46     return *this;
47   }
48
49   DecodeTimestamp& operator-=(const base::TimeDelta& rhs)  {
50     ts_ -= rhs;
51     return *this;
52   }
53
54   DecodeTimestamp operator+(const base::TimeDelta& rhs) const {
55     return DecodeTimestamp(ts_ + rhs);
56   }
57
58   DecodeTimestamp operator-(const base::TimeDelta& rhs) const {
59     return DecodeTimestamp(ts_ - rhs);
60   }
61
62   int64 operator/(const base::TimeDelta& rhs) const {
63     return ts_ / rhs;
64   }
65
66   static DecodeTimestamp FromSecondsD(double seconds) {
67     return DecodeTimestamp(base::TimeDelta::FromSecondsD(seconds));
68   }
69
70   static DecodeTimestamp FromMilliseconds(int64 milliseconds) {
71     return DecodeTimestamp(base::TimeDelta::FromMilliseconds(milliseconds));
72   }
73
74   static DecodeTimestamp FromMicroseconds(int64 microseconds) {
75     return DecodeTimestamp(base::TimeDelta::FromMicroseconds(microseconds));
76   }
77
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);
82   }
83
84   double InSecondsF() const { return ts_.InSecondsF(); }
85   int64 InMilliseconds() const { return ts_.InMilliseconds(); }
86   int64 InMicroseconds() const { return ts_.InMicroseconds(); }
87
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_; }
91
92  private:
93   explicit DecodeTimestamp(base::TimeDelta timestamp) : ts_(timestamp) { }
94
95   base::TimeDelta ts_;
96 };
97
98 MEDIA_EXPORT extern inline DecodeTimestamp kNoDecodeTimestamp() {
99   return DecodeTimestamp::FromPresentationTime(kNoTimestamp());
100 }
101
102 class MEDIA_EXPORT StreamParserBuffer : public DecoderBuffer {
103  public:
104   // Value used to signal an invalid decoder config ID.
105   enum { kInvalidConfigId = -1 };
106
107   typedef DemuxerStream::Type Type;
108   typedef StreamParser::TrackId TrackId;
109
110   static scoped_refptr<StreamParserBuffer> CreateEOSBuffer();
111
112   static scoped_refptr<StreamParserBuffer> CopyFrom(
113       const uint8* data, int data_size, bool is_keyframe, Type type,
114       TrackId track_id);
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,
118       TrackId track_id);
119   bool IsKeyframe() const { return is_keyframe_; }
120
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);
125
126   // Gets/sets the ID of the decoder config associated with this buffer.
127   int GetConfigId() const;
128   void SetConfigId(int config_id);
129
130   // Gets the parser's media type associated with this buffer. Value is
131   // meaningless for EOS buffers.
132   Type type() const { return type_; }
133
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_; }
137
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.
141   //
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_|.
146   //
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_; }
151
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.
156   //
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.
159   //
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_;
165   }
166
167   virtual void set_timestamp(base::TimeDelta timestamp) OVERRIDE;
168
169  private:
170   StreamParserBuffer(const uint8* data, int data_size,
171                      const uint8* side_data, int side_data_size,
172                      bool is_keyframe, Type type,
173                      TrackId track_id);
174   virtual ~StreamParserBuffer();
175
176   bool is_keyframe_;
177   DecodeTimestamp decode_timestamp_;
178   int config_id_;
179   Type type_;
180   TrackId track_id_;
181   BufferQueue splice_buffers_;
182   scoped_refptr<StreamParserBuffer> preroll_buffer_;
183
184   DISALLOW_COPY_AND_ASSIGN(StreamParserBuffer);
185 };
186
187 }  // namespace media
188
189 #endif  // MEDIA_BASE_STREAM_PARSER_BUFFER_H_