diff options
author | ralphl@chromium.org <ralphl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-03-16 19:54:34 +0000 |
---|---|---|
committer | ralphl@chromium.org <ralphl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-03-16 19:54:34 +0000 |
commit | 839807ce1e6e42917b330cc394ce938aa9ddd065 (patch) | |
tree | 618bc492650f10f52e70e0f74059a9dd9327f6f1 | |
parent | d56bcd21c5842c72ec0a8cd14c910e1dd4ed7048 (diff) | |
download | chromium_src-839807ce1e6e42917b330cc394ce938aa9ddd065.zip chromium_src-839807ce1e6e42917b330cc394ce938aa9ddd065.tar.gz chromium_src-839807ce1e6e42917b330cc394ce938aa9ddd065.tar.bz2 |
Buffer interface is now uint8*. Slight change to way data buffers are created. Caller passes no parameters to the
constructor of a data buffer now. GetWritableData() method is responsible for allocating memory.
Review URL: http://codereview.chromium.org/46015
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@11760 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | media/base/buffers.h | 17 | ||||
-rwxr-xr-x | media/base/data_buffer.cc | 33 | ||||
-rwxr-xr-x | media/base/data_buffer.h | 10 | ||||
-rw-r--r-- | media/base/data_buffer_unittest.cc | 47 | ||||
-rw-r--r-- | media/filters/audio_renderer_base.cc | 2 | ||||
-rw-r--r-- | media/filters/ffmpeg_demuxer.cc | 4 |
6 files changed, 58 insertions, 55 deletions
diff --git a/media/base/buffers.h b/media/base/buffers.h index dddc5d3..901a8a2 100644 --- a/media/base/buffers.h +++ b/media/base/buffers.h @@ -97,7 +97,7 @@ class StreamSample : public base::RefCountedThreadSafe<StreamSample> { class Buffer : public StreamSample { public: // Returns a read only pointer to the buffer data. - virtual const char* GetData() const = 0; + virtual const uint8* GetData() const = 0; // Returns the size of valid data in bytes. virtual size_t GetDataSize() const = 0; @@ -106,15 +106,18 @@ class Buffer : public StreamSample { class WritableBuffer : public Buffer { public: - // Returns a read-write pointer to the buffer data. - virtual char* GetWritableData() = 0; + // Returns a read-write pointer to the buffer data. When this method is + // called, any pointers previously returned from this method are invalid, and + // any data previously written to the buffer is invalid. The buffer size + // is guaranteed to be at least the size of |buffer_size|. The size + // that the GetDataSize() method will return is set to |buffer_size|. + // If, after filling the buffer, the caller wants to set the size to a smaller + // value then they can call the SetDataSize() method. + virtual uint8* GetWritableData(size_t buffer_size) = 0; // Updates the size of valid data in bytes, which must be less than or equal - // to GetBufferSize. + // to the |buffer_size| passed to GetWritableData(). virtual void SetDataSize(size_t data_size) = 0; - - // Returns the maximum allocated size for this buffer. - virtual size_t GetBufferSize() const = 0; }; diff --git a/media/base/data_buffer.cc b/media/base/data_buffer.cc index cdad4d7..b88790f 100755 --- a/media/base/data_buffer.cc +++ b/media/base/data_buffer.cc @@ -7,24 +7,17 @@ namespace media { -DataBuffer::DataBuffer(char* data, size_t buffer_size, size_t data_size, - const base::TimeDelta& timestamp, - const base::TimeDelta& duration) - : data_(data), - buffer_size_(buffer_size), - data_size_(data_size) { - DCHECK(data); - DCHECK(buffer_size >= 0); - DCHECK(data_size <= buffer_size); - SetTimestamp(timestamp); - SetDuration(duration); +DataBuffer::DataBuffer() + : data_(NULL), + buffer_size_(0), + data_size_(0) { } DataBuffer::~DataBuffer() { delete [] data_; } -const char* DataBuffer::GetData() const { +const uint8* DataBuffer::GetData() const { return data_; } @@ -32,15 +25,23 @@ size_t DataBuffer::GetDataSize() const { return data_size_; } -char* DataBuffer::GetWritableData() { +uint8* DataBuffer::GetWritableData(size_t buffer_size) { + if (buffer_size > buffer_size_) { + delete [] data_; + data_ = new uint8[buffer_size]; + if (!data_) { + NOTREACHED(); + buffer_size = 0; + } + buffer_size_ = buffer_size; + } + data_size_ = buffer_size; return data_; } -size_t DataBuffer::GetBufferSize() const { - return buffer_size_; -} void DataBuffer::SetDataSize(size_t data_size) { + DCHECK(data_size <= buffer_size_); data_size_ = data_size; } diff --git a/media/base/data_buffer.h b/media/base/data_buffer.h index 9ee72e9..8099d14 100755 --- a/media/base/data_buffer.h +++ b/media/base/data_buffer.h @@ -16,23 +16,21 @@ namespace media { class DataBuffer : public WritableBuffer { public: - DataBuffer(char* data, size_t buffer_size, size_t data_size, - const base::TimeDelta& timestamp, const base::TimeDelta& duration); + DataBuffer(); // Buffer implementation. - virtual const char* GetData() const; + virtual const uint8* GetData() const; virtual size_t GetDataSize() const; // WritableBuffer implementation. - virtual char* GetWritableData(); - virtual size_t GetBufferSize() const; + virtual uint8* GetWritableData(size_t buffer_size); virtual void SetDataSize(size_t data_size); protected: virtual ~DataBuffer(); private: - char* data_; + uint8* data_; size_t buffer_size_; size_t data_size_; }; diff --git a/media/base/data_buffer_unittest.cc b/media/base/data_buffer_unittest.cc index b0a45a1..21334f8 100644 --- a/media/base/data_buffer_unittest.cc +++ b/media/base/data_buffer_unittest.cc @@ -9,7 +9,6 @@ using media::DataBuffer; TEST(DataBufferTest, Basic) { - const size_t kBufferSize = 32; const char kData[] = "hello"; const size_t kDataSize = arraysize(kData); const char kNewData[] = "chromium"; @@ -20,18 +19,13 @@ TEST(DataBufferTest, Basic) { const base::TimeDelta kDurationB = base::TimeDelta::FromMicroseconds(5678); // Create our buffer and copy some data into it. - char* data = new char[kBufferSize]; - ASSERT_TRUE(data); - size_t copied = base::strlcpy(data, kData, kBufferSize); - EXPECT_EQ(kDataSize, copied + 1); - // Create a DataBuffer. - scoped_refptr<DataBuffer> buffer; - buffer = new DataBuffer(data, kBufferSize, kDataSize, - kTimestampA, kDurationA); - ASSERT_TRUE(buffer.get()); + scoped_refptr<DataBuffer> buffer = new DataBuffer(); + ASSERT_TRUE(buffer); // Test StreamSample implementation. + buffer->SetTimestamp(kTimestampA); + buffer->SetDuration(kDurationA); EXPECT_TRUE(kTimestampA == buffer->GetTimestamp()); EXPECT_TRUE(kDurationA == buffer->GetDuration()); EXPECT_FALSE(buffer->IsEndOfStream()); @@ -41,19 +35,6 @@ TEST(DataBufferTest, Basic) { EXPECT_TRUE(kTimestampB == buffer->GetTimestamp()); EXPECT_TRUE(kDurationB == buffer->GetDuration()); - // Test Buffer implementation. - ASSERT_EQ(data, buffer->GetData()); - EXPECT_EQ(kDataSize, buffer->GetDataSize()); - EXPECT_STREQ(kData, buffer->GetData()); - - // Test WritableBuffer implementation. - ASSERT_EQ(data, buffer->GetWritableData()); - EXPECT_EQ(kBufferSize, buffer->GetBufferSize()); - copied = base::strlcpy(data, kNewData, kBufferSize); - EXPECT_EQ(kNewDataSize, copied + 1); - buffer->SetDataSize(kNewDataSize); - EXPECT_EQ(kNewDataSize, buffer->GetDataSize()); - buffer->SetEndOfStream(true); EXPECT_TRUE(buffer->IsEndOfStream()); buffer->SetEndOfStream(false); @@ -62,4 +43,24 @@ TEST(DataBufferTest, Basic) { EXPECT_TRUE(buffer->IsDiscontinuous()); buffer->SetDiscontinuous(false); EXPECT_FALSE(buffer->IsDiscontinuous()); + + // Test WritableBuffer implementation. + EXPECT_FALSE(buffer->GetData()); + uint8* data = buffer->GetWritableData(kDataSize); + ASSERT_TRUE(data); + ASSERT_EQ(buffer->GetDataSize(), kDataSize); + memcpy(data, kData, kDataSize); + const uint8* read_only_data = buffer->GetData(); + ASSERT_EQ(data, read_only_data); + ASSERT_EQ(0, memcmp(read_only_data, kData, kDataSize)); + + data = buffer->GetWritableData(kNewDataSize + 10); + ASSERT_TRUE(data); + ASSERT_EQ(buffer->GetDataSize(), kNewDataSize + 10); + memcpy(data, kNewData, kNewDataSize); + read_only_data = buffer->GetData(); + buffer->SetDataSize(kNewDataSize); + EXPECT_EQ(buffer->GetDataSize(), kNewDataSize); + ASSERT_EQ(data, read_only_data); + EXPECT_EQ(0, memcmp(read_only_data, kNewData, kNewDataSize)); } diff --git a/media/filters/audio_renderer_base.cc b/media/filters/audio_renderer_base.cc index b661837..548586e 100644 --- a/media/filters/audio_renderer_base.cc +++ b/media/filters/audio_renderer_base.cc @@ -94,7 +94,7 @@ size_t AudioRendererBase::FillBuffer(uint8* dest, size_t len) { Buffer* buffer = queue_.front(); // Determine how much to copy. - const char* data = buffer->GetData() + data_offset_; + const uint8* data = buffer->GetData() + data_offset_; size_t data_len = buffer->GetDataSize() - data_offset_; data_len = std::min(len, data_len); diff --git a/media/filters/ffmpeg_demuxer.cc b/media/filters/ffmpeg_demuxer.cc index b0288a2..472ec5bd 100644 --- a/media/filters/ffmpeg_demuxer.cc +++ b/media/filters/ffmpeg_demuxer.cc @@ -30,8 +30,8 @@ class AVPacketBuffer : public Buffer { } // Buffer implementation. - virtual const char* GetData() const { - return reinterpret_cast<const char*>(packet_->data); + virtual const uint8* GetData() const { + return reinterpret_cast<const uint8*>(packet_->data); } virtual size_t GetDataSize() const { |