Chromium Code Reviews| 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. | |
|
awong
2010/11/16 00:04:58
Add comment on threadsafety of this class.
Sergey Ulanov
2010/11/16 01:25:09
Done.
| |
| 14 | |
| 15 #ifndef REMOTING_BASE_COMPOUND_BUFFER_H_ | |
| 16 #define REMOTING_BASE_COMPOUND_BUFFER_H_ | |
| 17 | |
| 18 #include <deque> | |
| 19 | |
| 20 #include "base/basictypes.h" | |
| 21 #include "base/ref_counted.h" | |
| 22 #include "google/protobuf/io/zero_copy_stream.h" | |
| 23 | |
| 24 namespace net { | |
| 25 class IOBuffer; | |
| 26 class IOBufferWithSize; | |
| 27 } // namespace net | |
| 28 | |
| 29 namespace remoting { | |
| 30 | |
| 31 class CompoundBuffer { | |
| 32 public: | |
| 33 CompoundBuffer(); | |
| 34 ~CompoundBuffer(); | |
| 35 | |
| 36 void Clear(); | |
| 37 | |
| 38 // Adds new chunk to the buffer. |start| defines position of the chunk | |
| 39 // within the |buffer|. |size| is the size of the chunk that is being | |
| 40 // added, not size of the |buffer|. | |
| 41 void Append(net::IOBuffer* buffer, int size); | |
| 42 void Append(net::IOBuffer* buffer, const char* start, int size); | |
| 43 void Append(const CompoundBuffer& buffer); | |
| 44 void Prepend(net::IOBuffer* buffer, int size); | |
| 45 void Prepend(net::IOBuffer* buffer, const char* start, int size); | |
| 46 void Prepend(const CompoundBuffer& buffer); | |
| 47 | |
| 48 // Same as above, but creates new IOBuffer and copies the data. | |
| 49 void AppendCopyOf(const char* data, int data_size); | |
| 50 void PrependCopyOf(const char* data, int data_size); | |
| 51 | |
| 52 // Current size of the buffer. | |
| 53 int total_bytes() const { return total_bytes_; } | |
| 54 | |
| 55 // Locks the buffer. After the buffer is locked, no changes can be made | |
| 56 // to its content. | |
| 57 void Lock(); | |
|
awong
2010/11/16 00:04:58
You mean nothing can be appended to prepend right?
Sergey Ulanov
2010/11/16 01:25:09
Done.
| |
| 58 | |
| 59 // Returns true if content is locked. | |
| 60 bool locked() const { return locked_; } | |
| 61 | |
| 62 // Creates new IOBufferWithSize object and copies all data into it. | |
| 63 // Ownership of the result is given to the caller. | |
| 64 net::IOBufferWithSize* ToIOBufferWithSize() const; | |
| 65 | |
| 66 // Copies all data into given location. | |
| 67 void CopyTo(char* data, int data_size) const; | |
| 68 | |
| 69 // Clears the buffer, and initializes it with the interval from |buffer| | |
| 70 // starting at |start| and ending at |end|. The data itself isn't copied. | |
| 71 void CopyFrom(const CompoundBuffer& source, int start, int end); | |
| 72 | |
| 73 private: | |
| 74 friend class CompoundBufferInputStream; | |
| 75 | |
| 76 struct DataChunk { | |
| 77 DataChunk(net::IOBuffer* buffer, const char* start, int size); | |
| 78 | |
| 79 scoped_refptr<net::IOBuffer> buffer; | |
| 80 const char* start; | |
| 81 int size; | |
| 82 }; | |
| 83 typedef std::deque<DataChunk> DataChunkList; | |
| 84 | |
| 85 DataChunkList chunks_; | |
| 86 int total_bytes_; | |
| 87 bool locked_; | |
| 88 | |
| 89 DISALLOW_COPY_AND_ASSIGN(CompoundBuffer); | |
| 90 }; | |
| 91 | |
| 92 class CompoundBufferInputStream | |
| 93 : public google::protobuf::io::ZeroCopyInputStream { | |
| 94 public: | |
| 95 // Caller keeps ownership of |buffer|. |buffer| must be locked. | |
| 96 explicit CompoundBufferInputStream(const CompoundBuffer* buffer); | |
| 97 virtual ~CompoundBufferInputStream(); | |
| 98 | |
| 99 // google::protobuf::io::ZeroCopyInputStream interface. | |
| 100 virtual bool Next(const void** data, int* size); | |
| 101 virtual void BackUp(int count); | |
| 102 virtual bool Skip(int count); | |
| 103 virtual int64 ByteCount() const; | |
| 104 | |
| 105 private: | |
| 106 const CompoundBuffer* buffer_; | |
| 107 | |
| 108 size_t current_chunk_; | |
| 109 int current_chunk_position_; | |
| 110 int position_; | |
| 111 int last_returned_size_; | |
| 112 }; | |
| 113 | |
| 114 } // namespace remoting | |
| 115 | |
| 116 #endif // REMOTING_BASE_COMPOUND_BUFFER_H_ | |
| OLD | NEW |