OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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 // CompoundBuffer implements a data buffer that is composed of several pieces, |
| 6 // each stored in a refcounted IOBuffer. It is needed for encoding/decoding |
| 7 // video pipeline to represent data packet and minimize data copying. |
| 8 // It is particularly useful for splitting data between multiple RTP packets |
| 9 // and assembling them into one buffer on the receiving side. |
| 10 // |
| 11 // CompoundBufferInputStream implements ZeroCopyInputStream interface |
| 12 // to be used by protobuf to decode data stored in CompoundBuffer into |
| 13 // a protocol buffer message. |
| 14 // |
| 15 // Mutations to the buffer are not thread-safe. Immutability can be ensured |
| 16 // with the Lock() method. |
| 17 |
| 18 #ifndef REMOTING_BASE_COMPOUND_BUFFER_H_ |
| 19 #define REMOTING_BASE_COMPOUND_BUFFER_H_ |
| 20 |
| 21 #include <deque> |
| 22 |
| 23 #include "base/basictypes.h" |
| 24 #include "base/ref_counted.h" |
| 25 #include "google/protobuf/io/zero_copy_stream.h" |
| 26 |
| 27 namespace net { |
| 28 class IOBuffer; |
| 29 class IOBufferWithSize; |
| 30 } // namespace net |
| 31 |
| 32 namespace remoting { |
| 33 |
| 34 class CompoundBuffer { |
| 35 public: |
| 36 CompoundBuffer(); |
| 37 ~CompoundBuffer(); |
| 38 |
| 39 void Clear(); |
| 40 |
| 41 // Adds new chunk to the buffer. |start| defines position of the chunk |
| 42 // within the |buffer|. |size| is the size of the chunk that is being |
| 43 // added, not size of the |buffer|. |
| 44 void Append(net::IOBuffer* buffer, int size); |
| 45 void Append(net::IOBuffer* buffer, const char* start, int size); |
| 46 void Append(const CompoundBuffer& buffer); |
| 47 void Prepend(net::IOBuffer* buffer, int size); |
| 48 void Prepend(net::IOBuffer* buffer, const char* start, int size); |
| 49 void Prepend(const CompoundBuffer& buffer); |
| 50 |
| 51 // Same as above, but creates new IOBuffer and copies the data. |
| 52 void AppendCopyOf(const char* data, int data_size); |
| 53 void PrependCopyOf(const char* data, int data_size); |
| 54 |
| 55 // Current size of the buffer. |
| 56 int total_bytes() const { return total_bytes_; } |
| 57 |
| 58 // Locks the buffer. After the buffer is locked, no data can be |
| 59 // added or removed (content can still be changed if some other |
| 60 // object holds reference to the IOBuffer objects). |
| 61 void Lock(); |
| 62 |
| 63 // Returns true if content is locked. |
| 64 bool locked() const { return locked_; } |
| 65 |
| 66 // Creates new IOBufferWithSize object and copies all data into it. |
| 67 // Ownership of the result is given to the caller. |
| 68 net::IOBufferWithSize* ToIOBufferWithSize() const; |
| 69 |
| 70 // Copies all data into given location. |
| 71 void CopyTo(char* data, int data_size) const; |
| 72 |
| 73 // Clears the buffer, and initializes it with the interval from |buffer| |
| 74 // starting at |start| and ending at |end|. The data itself isn't copied. |
| 75 void CopyFrom(const CompoundBuffer& source, int start, int end); |
| 76 |
| 77 private: |
| 78 friend class CompoundBufferInputStream; |
| 79 |
| 80 struct DataChunk { |
| 81 DataChunk(net::IOBuffer* buffer, const char* start, int size); |
| 82 |
| 83 scoped_refptr<net::IOBuffer> buffer; |
| 84 const char* start; |
| 85 int size; |
| 86 }; |
| 87 typedef std::deque<DataChunk> DataChunkList; |
| 88 |
| 89 DataChunkList chunks_; |
| 90 int total_bytes_; |
| 91 bool locked_; |
| 92 |
| 93 DISALLOW_COPY_AND_ASSIGN(CompoundBuffer); |
| 94 }; |
| 95 |
| 96 class CompoundBufferInputStream |
| 97 : public google::protobuf::io::ZeroCopyInputStream { |
| 98 public: |
| 99 // Caller keeps ownership of |buffer|. |buffer| must be locked. |
| 100 explicit CompoundBufferInputStream(const CompoundBuffer* buffer); |
| 101 virtual ~CompoundBufferInputStream(); |
| 102 |
| 103 // google::protobuf::io::ZeroCopyInputStream interface. |
| 104 virtual bool Next(const void** data, int* size); |
| 105 virtual void BackUp(int count); |
| 106 virtual bool Skip(int count); |
| 107 virtual int64 ByteCount() const; |
| 108 |
| 109 private: |
| 110 const CompoundBuffer* buffer_; |
| 111 |
| 112 size_t current_chunk_; |
| 113 int current_chunk_position_; |
| 114 int position_; |
| 115 int last_returned_size_; |
| 116 }; |
| 117 |
| 118 } // namespace remoting |
| 119 |
| 120 #endif // REMOTING_BASE_COMPOUND_BUFFER_H_ |
OLD | NEW |