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