mirror of
https://github.com/ossrs/srs.git
synced 2025-02-15 04:42:04 +00:00
Refactor kernel buffer
This commit is contained in:
parent
d045ce5d74
commit
7b5fa0e391
2 changed files with 35 additions and 77 deletions
|
@ -45,10 +45,10 @@ ISrsCodec::~ISrsCodec()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
SrsBuffer::SrsBuffer(char* b, int nb_b)
|
SrsBuffer::SrsBuffer(char* b, int nn)
|
||||||
{
|
{
|
||||||
p = bytes = b;
|
p = bytes = b;
|
||||||
nb_bytes = nb_b;
|
nb_bytes = nn;
|
||||||
|
|
||||||
// TODO: support both little and big endian.
|
// TODO: support both little and big endian.
|
||||||
srs_assert(srs_is_little_endian());
|
srs_assert(srs_is_little_endian());
|
||||||
|
|
|
@ -97,117 +97,75 @@ class SrsBuffer
|
||||||
private:
|
private:
|
||||||
// current position at bytes.
|
// current position at bytes.
|
||||||
char* p;
|
char* p;
|
||||||
// the bytes data for stream to read or write.
|
// the bytes data for buffer to read or write.
|
||||||
char* bytes;
|
char* bytes;
|
||||||
// the total number of bytes.
|
// the total number of bytes.
|
||||||
int nb_bytes;
|
int nb_bytes;
|
||||||
public:
|
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.
|
// @remark User must free the data b.
|
||||||
SrsBuffer(char* b, int nb_b);
|
SrsBuffer(char* b, int nn);
|
||||||
virtual ~SrsBuffer();
|
virtual ~SrsBuffer();
|
||||||
// get the status of stream
|
|
||||||
public:
|
public:
|
||||||
/**
|
// Get the data and head of buffer.
|
||||||
* get data of stream, set by initialize.
|
// current-bytes = head() = data() + pos()
|
||||||
* current bytes = data() + pos()
|
|
||||||
*/
|
|
||||||
inline char* data() { return bytes; }
|
inline char* data() { return bytes; }
|
||||||
inline char* head() { return p; }
|
inline char* head() { return p; }
|
||||||
/**
|
// Get the total size of buffer.
|
||||||
* the total stream size, set by initialize.
|
// left-bytes = size() - pos()
|
||||||
* left bytes = size() - pos().
|
|
||||||
*/
|
|
||||||
virtual int size();
|
virtual int size();
|
||||||
/**
|
void set_size(int v) { nb_bytes = v; }
|
||||||
* tell the current pos.
|
// Get the current buffer position.
|
||||||
*/
|
|
||||||
virtual int pos();
|
virtual int pos();
|
||||||
// Left bytes in buffer, total size() minus the current pos().
|
// Left bytes in buffer, total size() minus the current pos().
|
||||||
virtual int left();
|
virtual int left();
|
||||||
/**
|
// Whether buffer is empty.
|
||||||
* whether stream is empty.
|
|
||||||
* if empty, user should never read or write.
|
|
||||||
*/
|
|
||||||
virtual bool empty();
|
virtual bool empty();
|
||||||
/**
|
// Whether buffer is able to supply required size of bytes.
|
||||||
* whether required size is ok.
|
// @remark User should check buffer by require then do read/write.
|
||||||
* @return true if stream can read/write specified required_size bytes.
|
// @remark Assert the required_size is not negative.
|
||||||
* @remark assert required_size positive.
|
|
||||||
*/
|
|
||||||
virtual bool require(int required_size);
|
virtual bool require(int required_size);
|
||||||
// to change stream.
|
|
||||||
public:
|
public:
|
||||||
/**
|
// Skip some size.
|
||||||
* to skip some size.
|
// @param size can be any value. positive to forward; nagetive to backward.
|
||||||
* @param size can be any value. positive to forward; nagetive to backward.
|
// @remark to skip(pos()) to reset buffer.
|
||||||
* @remark to skip(pos()) to reset stream.
|
// @remark assert initialized, the data() not NULL.
|
||||||
* @remark assert initialized, the data() not NULL.
|
|
||||||
*/
|
|
||||||
virtual void skip(int size);
|
virtual void skip(int size);
|
||||||
public:
|
public:
|
||||||
/**
|
// Read 1bytes char from buffer.
|
||||||
* get 1bytes char from stream.
|
|
||||||
*/
|
|
||||||
virtual int8_t read_1bytes();
|
virtual int8_t read_1bytes();
|
||||||
/**
|
// Read 2bytes int from buffer.
|
||||||
* get 2bytes int from stream.
|
|
||||||
*/
|
|
||||||
virtual int16_t read_2bytes();
|
virtual int16_t read_2bytes();
|
||||||
/**
|
// Read 3bytes int from buffer.
|
||||||
* get 3bytes int from stream.
|
|
||||||
*/
|
|
||||||
virtual int32_t read_3bytes();
|
virtual int32_t read_3bytes();
|
||||||
/**
|
// Read 4bytes int from buffer.
|
||||||
* get 4bytes int from stream.
|
|
||||||
*/
|
|
||||||
virtual int32_t read_4bytes();
|
virtual int32_t read_4bytes();
|
||||||
/**
|
// Read 8bytes int from buffer.
|
||||||
* get 8bytes int from stream.
|
|
||||||
*/
|
|
||||||
virtual int64_t read_8bytes();
|
virtual int64_t read_8bytes();
|
||||||
/**
|
// Read string from buffer, length specifies by param len.
|
||||||
* get string from stream, length specifies by param len.
|
|
||||||
*/
|
|
||||||
virtual std::string read_string(int len);
|
virtual std::string read_string(int len);
|
||||||
/**
|
// Read bytes from buffer, length specifies by param len.
|
||||||
* get bytes from stream, length specifies by param len.
|
|
||||||
*/
|
|
||||||
virtual void read_bytes(char* data, int size);
|
virtual void read_bytes(char* data, int size);
|
||||||
public:
|
public:
|
||||||
/**
|
// Write 1bytes char to buffer.
|
||||||
* write 1bytes char to stream.
|
|
||||||
*/
|
|
||||||
virtual void write_1bytes(int8_t value);
|
virtual void write_1bytes(int8_t value);
|
||||||
/**
|
// Write 2bytes int to buffer.
|
||||||
* write 2bytes int to stream.
|
|
||||||
*/
|
|
||||||
virtual void write_2bytes(int16_t value);
|
virtual void write_2bytes(int16_t value);
|
||||||
/**
|
// Write 4bytes int to buffer.
|
||||||
* write 4bytes int to stream.
|
|
||||||
*/
|
|
||||||
virtual void write_4bytes(int32_t value);
|
virtual void write_4bytes(int32_t value);
|
||||||
/**
|
// Write 3bytes int to buffer.
|
||||||
* write 3bytes int to stream.
|
|
||||||
*/
|
|
||||||
virtual void write_3bytes(int32_t value);
|
virtual void write_3bytes(int32_t value);
|
||||||
/**
|
// Write 8bytes int to buffer.
|
||||||
* write 8bytes int to stream.
|
|
||||||
*/
|
|
||||||
virtual void write_8bytes(int64_t value);
|
virtual void write_8bytes(int64_t value);
|
||||||
/**
|
// Write string to buffer
|
||||||
* write string to stream
|
|
||||||
*/
|
|
||||||
virtual void write_string(std::string value);
|
virtual void write_string(std::string value);
|
||||||
/**
|
// Write bytes to buffer
|
||||||
* write bytes to stream
|
|
||||||
*/
|
|
||||||
virtual void write_bytes(char* data, int size);
|
virtual void write_bytes(char* data, int size);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* the bit stream, base on SrsBuffer,
|
* the bit buffer, base on SrsBuffer,
|
||||||
* for exmaple, the h.264 avc stream is bit stream.
|
* for exmaple, the h.264 avc buffer is bit buffer.
|
||||||
*/
|
*/
|
||||||
class SrsBitBuffer
|
class SrsBitBuffer
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue