mirror of https://github.com/ossrs/srs.git
add srs pipe for bug #194
parent
2a55ae4317
commit
f9756ea14c
@ -0,0 +1,105 @@
|
||||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <srs_app_pipe.hpp>
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#include <srs_kernel_error.hpp>
|
||||
#include <srs_kernel_log.hpp>
|
||||
|
||||
SrsPipe::SrsPipe()
|
||||
{
|
||||
fds[0] = fds[1] = 0;
|
||||
read_stfd = write_stfd = NULL;
|
||||
_already_written = false;
|
||||
}
|
||||
|
||||
SrsPipe::~SrsPipe()
|
||||
{
|
||||
srs_close_stfd(read_stfd);
|
||||
srs_close_stfd(write_stfd);
|
||||
}
|
||||
|
||||
int SrsPipe::initialize()
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
if (pipe(fds) < 0) {
|
||||
ret = ERROR_SYSTEM_CREATE_PIPE;
|
||||
srs_error("create pipe failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if ((read_stfd = st_netfd_open(fds[0])) == NULL) {
|
||||
ret = ERROR_SYSTEM_CREATE_PIPE;
|
||||
srs_error("open read pipe failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if ((write_stfd = st_netfd_open(fds[1])) == NULL) {
|
||||
ret = ERROR_SYSTEM_CREATE_PIPE;
|
||||
srs_error("open write pipe failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool SrsPipe::already_written()
|
||||
{
|
||||
return _already_written;
|
||||
}
|
||||
|
||||
int SrsPipe::active()
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
int v = 0;
|
||||
if (st_write(read_stfd, &v, sizeof(int), ST_UTIME_NO_TIMEOUT) != sizeof(int)) {
|
||||
ret = ERROR_SYSTEM_WRITE_PIPE;
|
||||
srs_error("write pipe failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
_already_written = true;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int SrsPipe::reset()
|
||||
{
|
||||
int ret = ERROR_SUCCESS;
|
||||
|
||||
int v;
|
||||
if (st_read(read_stfd, &v, sizeof(int), ST_UTIME_NO_TIMEOUT) != sizeof(int)) {
|
||||
ret = ERROR_SYSTEM_READ_PIPE;
|
||||
srs_error("read pipe failed. ret=%d", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
_already_written = false;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
@ -0,0 +1,78 @@
|
||||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2013-2014 winlin
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef SRS_APP_PIPE_HPP
|
||||
#define SRS_APP_PIPE_HPP
|
||||
|
||||
/*
|
||||
#include <srs_app_pipe.hpp>
|
||||
*/
|
||||
|
||||
#include <srs_core.hpp>
|
||||
|
||||
#include <srs_app_st.hpp>
|
||||
|
||||
/**
|
||||
* convert something to io,
|
||||
* for example, signal or SrsConsumer event.
|
||||
* for performance issue, @see: https://github.com/winlinvip/simple-rtmp-server/issues/194
|
||||
*/
|
||||
class SrsPipe
|
||||
{
|
||||
private:
|
||||
int fds[2];
|
||||
st_netfd_t read_stfd;
|
||||
st_netfd_t write_stfd;
|
||||
/**
|
||||
* for the event based service,
|
||||
* for example, the consumer only care whether there is data writen in pipe,
|
||||
* and the source will not write to pipe when pipe is already writen.
|
||||
*/
|
||||
bool _already_written;
|
||||
public:
|
||||
SrsPipe();
|
||||
virtual ~SrsPipe();
|
||||
public:
|
||||
/**
|
||||
* initialize pipes, open fds.
|
||||
*/
|
||||
virtual int initialize();
|
||||
public:
|
||||
/**
|
||||
* for event based service, whether already writen data.
|
||||
*/
|
||||
virtual bool already_written();
|
||||
/**
|
||||
* for event based service,
|
||||
* write an int to pipe and set the pipe to active.
|
||||
*/
|
||||
virtual int active();
|
||||
/**
|
||||
* for event based service,
|
||||
* read an int from pipe and reset the pipe to deactive.
|
||||
*/
|
||||
virtual int reset();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue