Skip to content

Commit

Permalink
for #738, use reader and seeker for mp4 demuxer to seek for general m…
Browse files Browse the repository at this point in the history
…p4(ftyp-mdat-moov).
  • Loading branch information
winlinvip committed Feb 3, 2017
1 parent bbee16e commit 9d21a8b
Show file tree
Hide file tree
Showing 16 changed files with 235 additions and 99 deletions.
8 changes: 7 additions & 1 deletion trunk/src/app/srs_app_caster_flv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ void SrsHttpFileReader::skip(int64_t /*size*/)
{
}

int64_t SrsHttpFileReader::lseek(int64_t offset)
int64_t SrsHttpFileReader::seek2(int64_t offset)
{
return offset;
}
Expand Down Expand Up @@ -324,4 +324,10 @@ int SrsHttpFileReader::read(void* buf, size_t count, ssize_t* pnread)
return ret;
}

int SrsHttpFileReader::lseek(off_t offset, int whence, off_t* seeked)
{
// TODO: FIXME: Use HTTP range for seek.
return ERROR_SUCCESS;
}

#endif
7 changes: 2 additions & 5 deletions trunk/src/app/srs_app_caster_flv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,11 @@ class SrsHttpFileReader : public SrsFileReader
virtual bool is_open();
virtual int64_t tellg();
virtual void skip(int64_t size);
virtual int64_t lseek(int64_t offset);
virtual int64_t seek2(int64_t offset);
virtual int64_t filesize();
public:
/**
* read from file.
* @param pnread the output nb_read, NULL to ignore.
*/
virtual int read(void* buf, size_t count, ssize_t* pnread);
virtual int lseek(off_t offset, int whence, off_t* seeked);
};

#endif
Expand Down
6 changes: 3 additions & 3 deletions trunk/src/app/srs_app_dvr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ int SrsFlvSegment::update_flv_metadata()
}

// update the flesize.
fs->lseek(filesize_offset);
fs->seek2(filesize_offset);
if ((ret = fs->write(buf, SrsAmf0Size::number(), NULL)) != ERROR_SUCCESS) {
return ret;
}
Expand All @@ -385,13 +385,13 @@ int SrsFlvSegment::update_flv_metadata()
}

// update the duration
fs->lseek(duration_offset);
fs->seek2(duration_offset);
if ((ret = fs->write(buf, SrsAmf0Size::number(), NULL)) != ERROR_SUCCESS) {
return ret;
}

// reset the offset.
fs->lseek(cur);
fs->seek2(cur);

return ret;
}
Expand Down
4 changes: 2 additions & 2 deletions trunk/src/app/srs_app_http_static.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ int SrsVodStream::serve_flv_stream(ISrsHttpResponseWriter* w, ISrsHttpMessage* r
}

// write body.
if ((ret = ffd.lseek(offset)) != ERROR_SUCCESS) {
if ((ret = ffd.seek2(offset)) != ERROR_SUCCESS) {
return ret;
}

Expand Down Expand Up @@ -186,7 +186,7 @@ int SrsVodStream::serve_mp4_stream(ISrsHttpResponseWriter* w, ISrsHttpMessage* r
w->header()->set("Content-Range", content_range.str());

// write body.
fs.lseek(start);
fs.seek2(start);

// send data
if ((ret = copy(w, &fs, r, (int)left)) != ERROR_SUCCESS) {
Expand Down
5 changes: 2 additions & 3 deletions trunk/src/kernel/srs_kernel_error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#define ERROR_MP4_BOX_ILLEGAL_SCHEMA 3072
#define ERROR_MP4_BOX_STRING 3073
#define ERROR_MP4_BOX_ILLEGAL_BRAND 3074
#define ERROR_MP4_NOT_NON_SEEKABLE 3075
#define ERROR_MP4_ESDS_SL_Config 3076
#define ERROR_MP4_ILLEGAL_MOOV 3077
#define ERROR_MP4_ESDS_SL_Config 3075
#define ERROR_MP4_ILLEGAL_MOOV 3076

///////////////////////////////////////////////////////
// HTTP/StreamCaster/KAFKA protocol error.
Expand Down
30 changes: 28 additions & 2 deletions trunk/src/kernel/srs_kernel_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ bool SrsFileWriter::is_open()
return fd > 0;
}

void SrsFileWriter::lseek(int64_t offset)
void SrsFileWriter::seek2(int64_t offset)
{
::lseek(fd, (off_t)offset, SEEK_SET);
}
Expand Down Expand Up @@ -167,6 +167,19 @@ int SrsFileWriter::writev(const iovec* iov, int iovcnt, ssize_t* pnwrite)
return ret;
}

int SrsFileWriter::lseek(off_t offset, int whence, off_t* seeked)
{
off_t sk = ::lseek(fd, offset, whence);
if (sk < 0) {
return ERROR_SYSTEM_FILE_SEEK;
}

if (seeked) {
*seeked = sk;
}
return ERROR_SUCCESS;
}

SrsFileReader::SrsFileReader()
{
fd = -1;
Expand Down Expand Up @@ -231,7 +244,7 @@ void SrsFileReader::skip(int64_t size)
::lseek(fd, (off_t)size, SEEK_CUR);
}

int64_t SrsFileReader::lseek(int64_t offset)
int64_t SrsFileReader::seek2(int64_t offset)
{
return (int64_t)::lseek(fd, (off_t)offset, SEEK_SET);
}
Expand Down Expand Up @@ -268,3 +281,16 @@ int SrsFileReader::read(void* buf, size_t count, ssize_t* pnread)
return ret;
}

int SrsFileReader::lseek(off_t offset, int whence, off_t* seeked)
{
off_t sk = ::lseek(fd, offset, whence);
if (sk < 0) {
return ERROR_SYSTEM_FILE_SEEK;
}

if (seeked) {
*seeked = sk;
}
return ERROR_SUCCESS;
}

24 changes: 8 additions & 16 deletions trunk/src/kernel/srs_kernel_file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
/**
* file writer, to write to file.
*/
class SrsFileWriter : public ISrsWriter
class SrsFileWriter : public ISrsWriteSeeker
{
private:
std::string path;
Expand All @@ -67,25 +67,19 @@ class SrsFileWriter : public ISrsWriter
virtual void close();
public:
virtual bool is_open();
virtual void lseek(int64_t offset);
virtual void seek2(int64_t offset);
virtual int64_t tellg();
// Interface ISrsWriteSeeker
public:
/**
* write to file.
* @param pnwrite the output nb_write, NULL to ignore.
*/
virtual int write(void* buf, size_t count, ssize_t* pnwrite);
/**
* for the HTTP FLV, to writev to improve performance.
* @see https://github.com/ossrs/srs/issues/405
*/
virtual int writev(const iovec* iov, int iovcnt, ssize_t* pnwrite);
virtual int lseek(off_t offset, int whence, off_t* seeked);
};

/**
* file reader, to read from file.
*/
class SrsFileReader : public ISrsReader
class SrsFileReader : public ISrsReadSeeker
{
private:
std::string path;
Expand All @@ -109,14 +103,12 @@ class SrsFileReader : public ISrsReader
virtual bool is_open();
virtual int64_t tellg();
virtual void skip(int64_t size);
virtual int64_t lseek(int64_t offset);
virtual int64_t seek2(int64_t offset);
virtual int64_t filesize();
// Interface ISrsReadSeeker
public:
/**
* read from file.
* @param pnread the output nb_read, NULL to ignore.
*/
virtual int read(void* buf, size_t count, ssize_t* pnread);
virtual int lseek(off_t offset, int whence, off_t* seeked);
};

#endif
Expand Down
6 changes: 3 additions & 3 deletions trunk/src/kernel/srs_kernel_flv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -949,15 +949,15 @@ int SrsFlvVodStreamDecoder::read_sequence_header_summary(int64_t* pstart, int* p

// seek to the sequence header start offset.
if (av_sequence_offset_start > 0) {
reader->lseek(av_sequence_offset_start);
reader->seek2(av_sequence_offset_start);
*pstart = av_sequence_offset_start;
*psize = (int)(av_sequence_offset_end - av_sequence_offset_start);
}

return ret;
}

int SrsFlvVodStreamDecoder::lseek(int64_t offset)
int SrsFlvVodStreamDecoder::seek2(int64_t offset)
{
int ret = ERROR_SUCCESS;

Expand All @@ -969,7 +969,7 @@ int SrsFlvVodStreamDecoder::lseek(int64_t offset)
return ret;
}

if (reader->lseek(offset) < 0) {
if (reader->seek2(offset) < 0) {
ret = ERROR_SYSTEM_FILE_SEEK;
srs_warn("flv fast decoder seek error, "
"size=%"PRId64", offset=%"PRId64", ret=%d",
Expand Down
2 changes: 1 addition & 1 deletion trunk/src/kernel/srs_kernel_flv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ class SrsFlvVodStreamDecoder
/**
* for start offset, seed to this position and response flv stream.
*/
virtual int lseek(int64_t offset);
virtual int seek2(int64_t offset);
};

#endif
Expand Down
24 changes: 24 additions & 0 deletions trunk/src/kernel/srs_kernel_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ ISrsReader::~ISrsReader()
{
}

ISrsSeeker::ISrsSeeker()
{
}

ISrsSeeker::~ISrsSeeker()
{
}

ISrsReadSeeker::ISrsReadSeeker()
{
}

ISrsReadSeeker::~ISrsReadSeeker()
{
}

ISrsStreamWriter::ISrsStreamWriter()
{
}
Expand All @@ -55,3 +71,11 @@ ISrsWriter::~ISrsWriter()
{
}

ISrsWriteSeeker::ISrsWriteSeeker()
{
}

ISrsWriteSeeker::~ISrsWriteSeeker()
{
}

43 changes: 43 additions & 0 deletions trunk/src/kernel/srs_kernel_io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,37 @@ class ISrsReader
virtual int read(void* buf, size_t size, ssize_t* nread) = 0;
};

/**
* The seeker to seek with a device.
*/
class ISrsSeeker
{
public:
ISrsSeeker();
virtual ~ISrsSeeker();
public:
/**
* The lseek() function repositions the offset of the file descriptor fildes to the argument offset, according to the
* directive whence. lseek() repositions the file pointer fildes as follows:
* If whence is SEEK_SET, the offset is set to offset bytes.
* If whence is SEEK_CUR, the offset is set to its current location plus offset bytes.
* If whence is SEEK_END, the offset is set to the size of the file plus offset bytes.
* @param seeked Upon successful completion, lseek() returns the resulting offset location as measured in bytes from
* the beginning of the file. NULL to ignore.
*/
virtual int lseek(off_t offset, int whence, off_t* seeked) = 0;
};

/**
* The reader and seeker.
*/
class ISrsReadSeeker : virtual public ISrsReader, virtual public ISrsSeeker
{
public:
ISrsReadSeeker();
virtual ~ISrsReadSeeker();
};

/**
* The writer to write stream data to channel.
*/
Expand Down Expand Up @@ -79,6 +110,8 @@ class ISrsVectorWriter
/**
* write iov over writer.
* @nwrite the actual written bytes. NULL to ignore.
* @remark for the HTTP FLV, to writev to improve performance.
* @see https://github.com/ossrs/srs/issues/405
*/
virtual int writev(const iovec *iov, int iov_size, ssize_t* nwrite) = 0;
};
Expand All @@ -93,5 +126,15 @@ class ISrsWriter : virtual public ISrsStreamWriter, virtual public ISrsVectorWri
virtual ~ISrsWriter();
};

/**
* The writer and seeker.
*/
class ISrsWriteSeeker : virtual public ISrsWriter, virtual public ISrsSeeker
{
public:
ISrsWriteSeeker();
virtual ~ISrsWriteSeeker();
};

#endif

Loading

0 comments on commit 9d21a8b

Please sign in to comment.