From 7b5fa0e3912b08e7119b70f8f5752913a754972b Mon Sep 17 00:00:00 2001 From: winlin Date: Thu, 30 Apr 2020 20:46:45 +0800 Subject: [PATCH] Refactor kernel buffer --- trunk/src/kernel/srs_kernel_buffer.cpp | 4 +- trunk/src/kernel/srs_kernel_buffer.hpp | 108 ++++++++----------------- 2 files changed, 35 insertions(+), 77 deletions(-) diff --git a/trunk/src/kernel/srs_kernel_buffer.cpp b/trunk/src/kernel/srs_kernel_buffer.cpp index 9279a04ff..7f62d6b5a 100644 --- a/trunk/src/kernel/srs_kernel_buffer.cpp +++ b/trunk/src/kernel/srs_kernel_buffer.cpp @@ -45,10 +45,10 @@ ISrsCodec::~ISrsCodec() { } -SrsBuffer::SrsBuffer(char* b, int nb_b) +SrsBuffer::SrsBuffer(char* b, int nn) { p = bytes = b; - nb_bytes = nb_b; + nb_bytes = nn; // TODO: support both little and big endian. srs_assert(srs_is_little_endian()); diff --git a/trunk/src/kernel/srs_kernel_buffer.hpp b/trunk/src/kernel/srs_kernel_buffer.hpp index b46de136c..ce8852c87 100644 --- a/trunk/src/kernel/srs_kernel_buffer.hpp +++ b/trunk/src/kernel/srs_kernel_buffer.hpp @@ -97,117 +97,75 @@ class SrsBuffer private: // current position at bytes. char* p; - // the bytes data for stream to read or write. + // the bytes data for buffer to read or write. char* bytes; // the total number of bytes. int nb_bytes; public: - // Initialize buffer with data b and size nb_b. + // Create buffer with data b and size nn. // @remark User must free the data b. - SrsBuffer(char* b, int nb_b); + SrsBuffer(char* b, int nn); virtual ~SrsBuffer(); -// get the status of stream public: - /** - * get data of stream, set by initialize. - * current bytes = data() + pos() - */ + // Get the data and head of buffer. + // current-bytes = head() = data() + pos() inline char* data() { return bytes; } inline char* head() { return p; } - /** - * the total stream size, set by initialize. - * left bytes = size() - pos(). - */ + // Get the total size of buffer. + // left-bytes = size() - pos() virtual int size(); - /** - * tell the current pos. - */ + void set_size(int v) { nb_bytes = v; } + // Get the current buffer position. virtual int pos(); // Left bytes in buffer, total size() minus the current pos(). virtual int left(); - /** - * whether stream is empty. - * if empty, user should never read or write. - */ + // Whether buffer is empty. virtual bool empty(); - /** - * whether required size is ok. - * @return true if stream can read/write specified required_size bytes. - * @remark assert required_size positive. - */ + // Whether buffer is able to supply required size of bytes. + // @remark User should check buffer by require then do read/write. + // @remark Assert the required_size is not negative. virtual bool require(int required_size); - // to change stream. public: - /** - * to skip some size. - * @param size can be any value. positive to forward; nagetive to backward. - * @remark to skip(pos()) to reset stream. - * @remark assert initialized, the data() not NULL. - */ + // Skip some size. + // @param size can be any value. positive to forward; nagetive to backward. + // @remark to skip(pos()) to reset buffer. + // @remark assert initialized, the data() not NULL. virtual void skip(int size); public: - /** - * get 1bytes char from stream. - */ + // Read 1bytes char from buffer. virtual int8_t read_1bytes(); - /** - * get 2bytes int from stream. - */ + // Read 2bytes int from buffer. virtual int16_t read_2bytes(); - /** - * get 3bytes int from stream. - */ + // Read 3bytes int from buffer. virtual int32_t read_3bytes(); - /** - * get 4bytes int from stream. - */ + // Read 4bytes int from buffer. virtual int32_t read_4bytes(); - /** - * get 8bytes int from stream. - */ + // Read 8bytes int from buffer. virtual int64_t read_8bytes(); - /** - * get string from stream, length specifies by param len. - */ + // Read string from buffer, length specifies by param len. virtual std::string read_string(int len); - /** - * get bytes from stream, length specifies by param len. - */ + // Read bytes from buffer, length specifies by param len. virtual void read_bytes(char* data, int size); public: - /** - * write 1bytes char to stream. - */ + // Write 1bytes char to buffer. virtual void write_1bytes(int8_t value); - /** - * write 2bytes int to stream. - */ + // Write 2bytes int to buffer. virtual void write_2bytes(int16_t value); - /** - * write 4bytes int to stream. - */ + // Write 4bytes int to buffer. virtual void write_4bytes(int32_t value); - /** - * write 3bytes int to stream. - */ + // Write 3bytes int to buffer. virtual void write_3bytes(int32_t value); - /** - * write 8bytes int to stream. - */ + // Write 8bytes int to buffer. virtual void write_8bytes(int64_t value); - /** - * write string to stream - */ + // Write string to buffer virtual void write_string(std::string value); - /** - * write bytes to stream - */ + // Write bytes to buffer virtual void write_bytes(char* data, int size); }; /** - * the bit stream, base on SrsBuffer, - * for exmaple, the h.264 avc stream is bit stream. + * the bit buffer, base on SrsBuffer, + * for exmaple, the h.264 avc buffer is bit buffer. */ class SrsBitBuffer {