OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_BASE_DATA_BUFFER_H_ | 5 #ifndef MEDIA_BASE_DATA_BUFFER_H_ |
6 #define MEDIA_BASE_DATA_BUFFER_H_ | 6 #define MEDIA_BASE_DATA_BUFFER_H_ |
7 | 7 |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
12 #include "media/base/media_export.h" | 12 #include "media/base/media_export.h" |
13 | 13 |
14 namespace media { | 14 namespace media { |
15 | 15 |
16 // A simple buffer that takes ownership of the given data pointer or allocates | 16 // A simple buffer that takes ownership of the given data pointer or allocates |
17 // as necessary. | 17 // as necessary. |
18 // | 18 // |
19 // Unlike DecoderBuffer, allocations are assumed to be allocated with the | 19 // Unlike DecoderBuffer, allocations are assumed to be allocated with the |
20 // default memory allocator (i.e., new uint8[]). | 20 // default memory allocator (i.e., new uint8_t[]). |
21 // | 21 // |
22 // NOTE: It is illegal to call any method when end_of_stream() is true. | 22 // NOTE: It is illegal to call any method when end_of_stream() is true. |
23 class MEDIA_EXPORT DataBuffer : public base::RefCountedThreadSafe<DataBuffer> { | 23 class MEDIA_EXPORT DataBuffer : public base::RefCountedThreadSafe<DataBuffer> { |
24 public: | 24 public: |
25 // Allocates buffer of size |buffer_size| >= 0. | 25 // Allocates buffer of size |buffer_size| >= 0. |
26 explicit DataBuffer(int buffer_size); | 26 explicit DataBuffer(int buffer_size); |
27 | 27 |
28 // Assumes valid data of size |buffer_size|. | 28 // Assumes valid data of size |buffer_size|. |
29 DataBuffer(scoped_ptr<uint8[]> buffer, int buffer_size); | 29 DataBuffer(scoped_ptr<uint8_t[]> buffer, int buffer_size); |
30 | 30 |
31 // Create a DataBuffer whose |data_| is copied from |data|. | 31 // Create a DataBuffer whose |data_| is copied from |data|. |
32 // | 32 // |
33 // |data| must not be null and |size| must be >= 0. | 33 // |data| must not be null and |size| must be >= 0. |
34 static scoped_refptr<DataBuffer> CopyFrom(const uint8* data, int size); | 34 static scoped_refptr<DataBuffer> CopyFrom(const uint8_t* data, int size); |
35 | 35 |
36 // Create a DataBuffer indicating we've reached end of stream. | 36 // Create a DataBuffer indicating we've reached end of stream. |
37 // | 37 // |
38 // Calling any method other than end_of_stream() on the resulting buffer | 38 // Calling any method other than end_of_stream() on the resulting buffer |
39 // is disallowed. | 39 // is disallowed. |
40 static scoped_refptr<DataBuffer> CreateEOSBuffer(); | 40 static scoped_refptr<DataBuffer> CreateEOSBuffer(); |
41 | 41 |
42 base::TimeDelta timestamp() const { | 42 base::TimeDelta timestamp() const { |
43 DCHECK(!end_of_stream()); | 43 DCHECK(!end_of_stream()); |
44 return timestamp_; | 44 return timestamp_; |
45 } | 45 } |
46 | 46 |
47 void set_timestamp(const base::TimeDelta& timestamp) { | 47 void set_timestamp(const base::TimeDelta& timestamp) { |
48 DCHECK(!end_of_stream()); | 48 DCHECK(!end_of_stream()); |
49 timestamp_ = timestamp; | 49 timestamp_ = timestamp; |
50 } | 50 } |
51 | 51 |
52 base::TimeDelta duration() const { | 52 base::TimeDelta duration() const { |
53 DCHECK(!end_of_stream()); | 53 DCHECK(!end_of_stream()); |
54 return duration_; | 54 return duration_; |
55 } | 55 } |
56 | 56 |
57 void set_duration(const base::TimeDelta& duration) { | 57 void set_duration(const base::TimeDelta& duration) { |
58 DCHECK(!end_of_stream()); | 58 DCHECK(!end_of_stream()); |
59 duration_ = duration; | 59 duration_ = duration; |
60 } | 60 } |
61 | 61 |
62 const uint8* data() const { | 62 const uint8_t* data() const { |
63 DCHECK(!end_of_stream()); | 63 DCHECK(!end_of_stream()); |
64 return data_.get(); | 64 return data_.get(); |
65 } | 65 } |
66 | 66 |
67 uint8* writable_data() { | 67 uint8_t* writable_data() { |
68 DCHECK(!end_of_stream()); | 68 DCHECK(!end_of_stream()); |
69 return data_.get(); | 69 return data_.get(); |
70 } | 70 } |
71 | 71 |
72 // The size of valid data in bytes. | 72 // The size of valid data in bytes. |
73 // | 73 // |
74 // Setting this value beyond the buffer size is disallowed. | 74 // Setting this value beyond the buffer size is disallowed. |
75 int data_size() const { | 75 int data_size() const { |
76 DCHECK(!end_of_stream()); | 76 DCHECK(!end_of_stream()); |
77 return data_size_; | 77 return data_size_; |
78 } | 78 } |
79 | 79 |
80 void set_data_size(int data_size) { | 80 void set_data_size(int data_size) { |
81 DCHECK(!end_of_stream()); | 81 DCHECK(!end_of_stream()); |
82 CHECK_LE(data_size, buffer_size_); | 82 CHECK_LE(data_size, buffer_size_); |
83 data_size_ = data_size; | 83 data_size_ = data_size; |
84 } | 84 } |
85 | 85 |
86 // If there's no data in this buffer, it represents end of stream. | 86 // If there's no data in this buffer, it represents end of stream. |
87 bool end_of_stream() const { return data_ == NULL; } | 87 bool end_of_stream() const { return data_ == NULL; } |
88 | 88 |
89 protected: | 89 protected: |
90 friend class base::RefCountedThreadSafe<DataBuffer>; | 90 friend class base::RefCountedThreadSafe<DataBuffer>; |
91 | 91 |
92 // Allocates buffer of size |data_size|, copies [data,data+data_size) to | 92 // Allocates buffer of size |data_size|, copies [data,data+data_size) to |
93 // the allocated buffer and sets data size to |data_size|. | 93 // the allocated buffer and sets data size to |data_size|. |
94 // | 94 // |
95 // If |data| is null an end of stream buffer is created. | 95 // If |data| is null an end of stream buffer is created. |
96 DataBuffer(const uint8* data, int data_size); | 96 DataBuffer(const uint8_t* data, int data_size); |
97 | 97 |
98 virtual ~DataBuffer(); | 98 virtual ~DataBuffer(); |
99 | 99 |
100 private: | 100 private: |
101 base::TimeDelta timestamp_; | 101 base::TimeDelta timestamp_; |
102 base::TimeDelta duration_; | 102 base::TimeDelta duration_; |
103 | 103 |
104 scoped_ptr<uint8[]> data_; | 104 scoped_ptr<uint8_t[]> data_; |
105 int buffer_size_; | 105 int buffer_size_; |
106 int data_size_; | 106 int data_size_; |
107 | 107 |
108 DISALLOW_COPY_AND_ASSIGN(DataBuffer); | 108 DISALLOW_COPY_AND_ASSIGN(DataBuffer); |
109 }; | 109 }; |
110 | 110 |
111 } // namespace media | 111 } // namespace media |
112 | 112 |
113 #endif // MEDIA_BASE_DATA_BUFFER_H_ | 113 #endif // MEDIA_BASE_DATA_BUFFER_H_ |
OLD | NEW |