1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-15 04:42:04 +00:00

refine params nameing and SrsStream.

This commit is contained in:
winlin 2015-07-28 18:08:11 +08:00
parent 8d86eb6516
commit bfe0f97edd
7 changed files with 57 additions and 54 deletions

View file

@ -252,7 +252,7 @@ int SrsFileReader::read(void* buf, size_t count, ssize_t* pnread)
// TODO: FIXME: use st_read. // TODO: FIXME: use st_read.
if ((nread = ::read(fd, buf, count)) < 0) { if ((nread = ::read(fd, buf, count)) < 0) {
ret = ERROR_SYSTEM_FILE_READ; ret = ERROR_SYSTEM_FILE_READ;
srs_error("read from file %s failed. ret=%d", _file.c_str(), ret); srs_error("read from file %s failed. ret=%d", path.c_str(), ret);
return ret; return ret;
} }

View file

@ -40,7 +40,7 @@ using namespace std;
SrsMp3Encoder::SrsMp3Encoder() SrsMp3Encoder::SrsMp3Encoder()
{ {
_fs = NULL; writer = NULL;
tag_stream = new SrsStream(); tag_stream = new SrsStream();
} }
@ -49,19 +49,19 @@ SrsMp3Encoder::~SrsMp3Encoder()
srs_freep(tag_stream); srs_freep(tag_stream);
} }
int SrsMp3Encoder::initialize(SrsFileWriter* fs) int SrsMp3Encoder::initialize(SrsFileWriter* fw)
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
srs_assert(fs); srs_assert(fw);
if (!fs->is_open()) { if (!fw->is_open()) {
ret = ERROR_KERNEL_MP3_STREAM_CLOSED; ret = ERROR_KERNEL_MP3_STREAM_CLOSED;
srs_warn("stream is not open for encoder. ret=%d", ret); srs_warn("stream is not open for encoder. ret=%d", ret);
return ret; return ret;
} }
_fs = fs; writer = fw;
return ret; return ret;
} }
@ -78,7 +78,7 @@ int SrsMp3Encoder::write_header()
(char)0x00, (char)0x00, (char)0x00, (char)0x00, // FrameSize (char)0x00, (char)0x00, (char)0x00, (char)0x00, // FrameSize
(char)0x00, (char)0x00 // Flags (char)0x00, (char)0x00 // Flags
}; };
return _fs->write(id3, sizeof(id3), NULL); return writer->write(id3, sizeof(id3), NULL);
} }
int SrsMp3Encoder::write_audio(int64_t timestamp, char* data, int size) int SrsMp3Encoder::write_audio(int64_t timestamp, char* data, int size)
@ -122,6 +122,6 @@ int SrsMp3Encoder::write_audio(int64_t timestamp, char* data, int size)
return ret; return ret;
} }
return _fs->write(data + stream->pos(), size - stream->pos(), NULL); return writer->write(data + stream->pos(), size - stream->pos(), NULL);
} }

View file

@ -33,7 +33,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
class SrsStream; class SrsStream;
class SrsFileWriter; class SrsFileWriter;
class SrsFileReader;
/** /**
* encode data to aac file. * encode data to aac file.
@ -41,7 +40,7 @@ class SrsFileReader;
class SrsMp3Encoder class SrsMp3Encoder
{ {
private: private:
SrsFileWriter* _fs; SrsFileWriter* writer;
private: private:
SrsStream* tag_stream; SrsStream* tag_stream;
public: public:
@ -51,9 +50,9 @@ public:
/** /**
* initialize the underlayer file stream. * initialize the underlayer file stream.
* @remark user can initialize multiple times to encode multiple mp3 files. * @remark user can initialize multiple times to encode multiple mp3 files.
* @remark, user must free the fs, mp3 encoder never close/free it. * @remark, user must free the @param fw, mp3 encoder never close/free it.
*/ */
virtual int initialize(SrsFileWriter* fs); virtual int initialize(SrsFileWriter* fw);
public: public:
/** /**
* write mp3 id3 v2.3 header. * write mp3 id3 v2.3 header.

View file

@ -31,8 +31,8 @@ using namespace std;
SrsStream::SrsStream() SrsStream::SrsStream()
{ {
p = _bytes = NULL; p = bytes = NULL;
_size = 0; nb_bytes = 0;
// 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());
@ -42,24 +42,24 @@ SrsStream::~SrsStream()
{ {
} }
int SrsStream::initialize(char* bytes, int size) int SrsStream::initialize(char* b, int nb)
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
if (!bytes) { if (!b) {
ret = ERROR_KERNEL_STREAM_INIT; ret = ERROR_KERNEL_STREAM_INIT;
srs_error("stream param bytes must not be NULL. ret=%d", ret); srs_error("stream param bytes must not be NULL. ret=%d", ret);
return ret; return ret;
} }
if (size <= 0) { if (nb <= 0) {
ret = ERROR_KERNEL_STREAM_INIT; ret = ERROR_KERNEL_STREAM_INIT;
srs_error("stream param size must be positive. ret=%d", ret); srs_error("stream param size must be positive. ret=%d", ret);
return ret; return ret;
} }
_size = size; nb_bytes = nb;
p = _bytes = bytes; p = bytes = b;
srs_info("init stream ok, size=%d", size); srs_info("init stream ok, size=%d", size);
return ret; return ret;
@ -67,29 +67,29 @@ int SrsStream::initialize(char* bytes, int size)
char* SrsStream::data() char* SrsStream::data()
{ {
return _bytes; return bytes;
} }
int SrsStream::size() int SrsStream::size()
{ {
return _size; return nb_bytes;
} }
int SrsStream::pos() int SrsStream::pos()
{ {
return p - _bytes; return (int)(p - bytes);
} }
bool SrsStream::empty() bool SrsStream::empty()
{ {
return !_bytes || (p >= _bytes + _size); return !bytes || (p >= bytes + nb_bytes);
} }
bool SrsStream::require(int required_size) bool SrsStream::require(int required_size)
{ {
srs_assert(required_size > 0); srs_assert(required_size > 0);
return required_size <= _size - (p - _bytes); return required_size <= nb_bytes - (p - bytes);
} }
void SrsStream::skip(int size) void SrsStream::skip(int size)
@ -111,7 +111,7 @@ int16_t SrsStream::read_2bytes()
srs_assert(require(2)); srs_assert(require(2));
int16_t value; int16_t value;
pp = (char*)&value; char* pp = (char*)&value;
pp[1] = *p++; pp[1] = *p++;
pp[0] = *p++; pp[0] = *p++;
@ -123,7 +123,7 @@ int32_t SrsStream::read_3bytes()
srs_assert(require(3)); srs_assert(require(3));
int32_t value = 0x00; int32_t value = 0x00;
pp = (char*)&value; char* pp = (char*)&value;
pp[2] = *p++; pp[2] = *p++;
pp[1] = *p++; pp[1] = *p++;
pp[0] = *p++; pp[0] = *p++;
@ -136,7 +136,7 @@ int32_t SrsStream::read_4bytes()
srs_assert(require(4)); srs_assert(require(4));
int32_t value; int32_t value;
pp = (char*)&value; char* pp = (char*)&value;
pp[3] = *p++; pp[3] = *p++;
pp[2] = *p++; pp[2] = *p++;
pp[1] = *p++; pp[1] = *p++;
@ -150,7 +150,7 @@ int64_t SrsStream::read_8bytes()
srs_assert(require(8)); srs_assert(require(8));
int64_t value; int64_t value;
pp = (char*)&value; char* pp = (char*)&value;
pp[7] = *p++; pp[7] = *p++;
pp[6] = *p++; pp[6] = *p++;
pp[5] = *p++; pp[5] = *p++;
@ -195,7 +195,7 @@ void SrsStream::write_2bytes(int16_t value)
{ {
srs_assert(require(2)); srs_assert(require(2));
pp = (char*)&value; char* pp = (char*)&value;
*p++ = pp[1]; *p++ = pp[1];
*p++ = pp[0]; *p++ = pp[0];
} }
@ -204,7 +204,7 @@ void SrsStream::write_4bytes(int32_t value)
{ {
srs_assert(require(4)); srs_assert(require(4));
pp = (char*)&value; char* pp = (char*)&value;
*p++ = pp[3]; *p++ = pp[3];
*p++ = pp[2]; *p++ = pp[2];
*p++ = pp[1]; *p++ = pp[1];
@ -215,7 +215,7 @@ void SrsStream::write_3bytes(int32_t value)
{ {
srs_assert(require(3)); srs_assert(require(3));
pp = (char*)&value; char* pp = (char*)&value;
*p++ = pp[2]; *p++ = pp[2];
*p++ = pp[1]; *p++ = pp[1];
*p++ = pp[0]; *p++ = pp[0];
@ -225,7 +225,7 @@ void SrsStream::write_8bytes(int64_t value)
{ {
srs_assert(require(8)); srs_assert(require(8));
pp = (char*)&value; char* pp = (char*)&value;
*p++ = pp[7]; *p++ = pp[7];
*p++ = pp[6]; *p++ = pp[6];
*p++ = pp[5]; *p++ = pp[5];
@ -238,7 +238,7 @@ void SrsStream::write_8bytes(int64_t value)
void SrsStream::write_string(string value) void SrsStream::write_string(string value)
{ {
srs_assert(require(value.length())); srs_assert(require((int)value.length()));
memcpy(p, value.data(), value.length()); memcpy(p, value.data(), value.length());
p += value.length(); p += value.length();

View file

@ -41,23 +41,25 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
class SrsStream class SrsStream
{ {
private: private:
// current position at bytes.
char* p; char* p;
char* pp; // the bytes data for stream to read or write.
char* _bytes; char* bytes;
int _size; // the total number of bytes.
int nb_bytes;
public: public:
SrsStream(); SrsStream();
virtual ~SrsStream(); virtual ~SrsStream();
public: public:
/** /**
* initialize the stream from bytes. * initialize the stream from bytes.
* @bytes, the bytes to convert from/to basic types. * @b, the bytes to convert from/to basic types.
* @size, the size of bytes. * @nb, the size of bytes, total number of bytes for stream.
* @remark, stream never free the bytes, user must free it. * @remark, stream never free the bytes, user must free it.
* @remark, return error when bytes NULL. * @remark, return error when bytes NULL.
* @remark, return error when size is not positive. * @remark, return error when size is not positive.
*/ */
virtual int initialize(char* bytes, int size); virtual int initialize(char* b, int nb);
// get the status of stream // get the status of stream
public: public:
/** /**

View file

@ -2697,11 +2697,11 @@ SrsTSMuxer::~SrsTSMuxer()
close(); close();
} }
int SrsTSMuxer::open(string _path) int SrsTSMuxer::open(string p)
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
path = _path; path = p;
close(); close();
@ -3048,7 +3048,7 @@ int SrsTsCache::do_cache_avc(SrsAvcAacCodec* codec, SrsCodecSample* sample)
SrsTsEncoder::SrsTsEncoder() SrsTsEncoder::SrsTsEncoder()
{ {
_fs = NULL; writer = NULL;
codec = new SrsAvcAacCodec(); codec = new SrsAvcAacCodec();
sample = new SrsCodecSample(); sample = new SrsCodecSample();
cache = new SrsTsCache(); cache = new SrsTsCache();
@ -3065,22 +3065,22 @@ SrsTsEncoder::~SrsTsEncoder()
srs_freep(context); srs_freep(context);
} }
int SrsTsEncoder::initialize(SrsFileWriter* fs) int SrsTsEncoder::initialize(SrsFileWriter* fw)
{ {
int ret = ERROR_SUCCESS; int ret = ERROR_SUCCESS;
srs_assert(fs); srs_assert(fw);
if (!fs->is_open()) { if (!fw->is_open()) {
ret = ERROR_KERNEL_FLV_STREAM_CLOSED; ret = ERROR_KERNEL_FLV_STREAM_CLOSED;
srs_warn("stream is not open for encoder. ret=%d", ret); srs_warn("stream is not open for encoder. ret=%d", ret);
return ret; return ret;
} }
_fs = fs; writer = fw;
srs_freep(muxer); srs_freep(muxer);
muxer = new SrsTSMuxer(fs, context, SrsCodecAudioAAC, SrsCodecVideoAVC); muxer = new SrsTSMuxer(fw, context, SrsCodecAudioAAC, SrsCodecVideoAVC);
if ((ret = muxer->open("")) != ERROR_SUCCESS) { if ((ret = muxer->open("")) != ERROR_SUCCESS) {
return ret; return ret;

View file

@ -1561,8 +1561,9 @@ public:
public: public:
/** /**
* open the writer, donot write the PSI of ts. * open the writer, donot write the PSI of ts.
* @param p a string indicates the path of ts file to mux to.
*/ */
virtual int open(std::string _path); virtual int open(std::string p);
/** /**
* when open ts, we donot write the header(PSI), * when open ts, we donot write the header(PSI),
* for user may need to update the acodec to mp3 or others, * for user may need to update the acodec to mp3 or others,
@ -1625,7 +1626,7 @@ private:
class SrsTsEncoder class SrsTsEncoder
{ {
private: private:
SrsFileWriter* _fs; SrsFileWriter* writer;
private: private:
SrsAvcAacCodec* codec; SrsAvcAacCodec* codec;
SrsCodecSample* sample; SrsCodecSample* sample;
@ -1638,8 +1639,9 @@ public:
public: public:
/** /**
* initialize the underlayer file stream. * initialize the underlayer file stream.
* @param fw the writer to use for ts encoder, user must free it.
*/ */
virtual int initialize(SrsFileWriter* fs); virtual int initialize(SrsFileWriter* fw);
public: public:
/** /**
* write audio/video packet. * write audio/video packet.