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_DATA_BUFFER_H_
6 #define MEDIA_BASE_DATA_BUFFER_H_
8 #include "base/logging.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/time/time.h"
12 #include "media/base/media_export.h"
16 // A simple buffer that takes ownership of the given data pointer or allocates
19 // Unlike DecoderBuffer, allocations are assumed to be allocated with the
20 // default memory allocator (i.e., new uint8[]).
22 // NOTE: It is illegal to call any method when end_of_stream() is true.
23 class MEDIA_EXPORT DataBuffer
: public base::RefCountedThreadSafe
<DataBuffer
> {
25 // Allocates buffer of size |buffer_size| >= 0.
26 explicit DataBuffer(int buffer_size
);
28 // Assumes valid data of size |buffer_size|.
29 DataBuffer(scoped_ptr
<uint8
[]> buffer
, int buffer_size
);
31 // Create a DataBuffer whose |data_| is copied from |data|.
33 // |data| must not be null and |size| must be >= 0.
34 static scoped_refptr
<DataBuffer
> CopyFrom(const uint8
* data
, int size
);
36 // Create a DataBuffer indicating we've reached end of stream.
38 // Calling any method other than end_of_stream() on the resulting buffer
40 static scoped_refptr
<DataBuffer
> CreateEOSBuffer();
42 base::TimeDelta
timestamp() const {
43 DCHECK(!end_of_stream());
47 void set_timestamp(const base::TimeDelta
& timestamp
) {
48 DCHECK(!end_of_stream());
49 timestamp_
= timestamp
;
52 base::TimeDelta
duration() const {
53 DCHECK(!end_of_stream());
57 void set_duration(const base::TimeDelta
& duration
) {
58 DCHECK(!end_of_stream());
62 const uint8
* data() const {
63 DCHECK(!end_of_stream());
67 uint8
* writable_data() {
68 DCHECK(!end_of_stream());
72 // The size of valid data in bytes.
74 // Setting this value beyond the buffer size is disallowed.
75 int data_size() const {
76 DCHECK(!end_of_stream());
80 void set_data_size(int data_size
) {
81 DCHECK(!end_of_stream());
82 CHECK_LE(data_size
, buffer_size_
);
83 data_size_
= data_size
;
86 // If there's no data in this buffer, it represents end of stream.
87 bool end_of_stream() const { return data_
== NULL
; }
90 friend class base::RefCountedThreadSafe
<DataBuffer
>;
92 // Allocates buffer of size |data_size|, copies [data,data+data_size) to
93 // the allocated buffer and sets data size to |data_size|.
95 // If |data| is null an end of stream buffer is created.
96 DataBuffer(const uint8
* data
, int data_size
);
98 virtual ~DataBuffer();
101 base::TimeDelta timestamp_
;
102 base::TimeDelta duration_
;
104 scoped_ptr
<uint8
[]> data_
;
108 DISALLOW_COPY_AND_ASSIGN(DataBuffer
);
113 #endif // MEDIA_BASE_DATA_BUFFER_H_