From f9756ea14c336e6cf122b81246affcec40b3fffa Mon Sep 17 00:00:00 2001 From: winlin Date: Tue, 11 Nov 2014 16:09:44 +0800 Subject: [PATCH] add srs pipe for bug #194 --- trunk/configure | 3 +- trunk/src/app/srs_app_pipe.cpp | 105 ++++++++++++++++++++++++++ trunk/src/app/srs_app_pipe.hpp | 78 +++++++++++++++++++ trunk/src/kernel/srs_kernel_error.hpp | 2 + trunk/src/srs/srs.upp | 2 + 5 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 trunk/src/app/srs_app_pipe.cpp create mode 100644 trunk/src/app/srs_app_pipe.hpp diff --git a/trunk/configure b/trunk/configure index a380aba72..6d5f6aaf0 100755 --- a/trunk/configure +++ b/trunk/configure @@ -388,7 +388,8 @@ if [ $SRS_EXPORT_LIBRTMP_PROJECT = NO ]; then "srs_app_thread" "srs_app_bandwidth" "srs_app_st" "srs_app_log" "srs_app_config" "srs_app_pithy_print" "srs_app_reload" "srs_app_http_api" "srs_app_http_conn" "srs_app_http_hooks" "srs_app_json" "srs_app_ingest" "srs_app_ffmpeg" "srs_app_utility" "srs_app_dvr" "srs_app_edge" - "srs_app_kbps" "srs_app_heartbeat" "srs_app_empty" "srs_app_http_client" "srs_app_avc_aac") + "srs_app_kbps" "srs_app_heartbeat" "srs_app_empty" "srs_app_http_client" "srs_app_avc_aac" + "srs_app_pipe") APP_INCS="src/app"; MODULE_DIR=${APP_INCS} . auto/modules.sh APP_OBJS="${MODULE_OBJS[@]}" fi diff --git a/trunk/src/app/srs_app_pipe.cpp b/trunk/src/app/srs_app_pipe.cpp new file mode 100644 index 000000000..56823ffe0 --- /dev/null +++ b/trunk/src/app/srs_app_pipe.cpp @@ -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 + +#include + +#include +#include + +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; +} + diff --git a/trunk/src/app/srs_app_pipe.hpp b/trunk/src/app/srs_app_pipe.hpp new file mode 100644 index 000000000..088bf32fa --- /dev/null +++ b/trunk/src/app/srs_app_pipe.hpp @@ -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 +*/ + +#include + +#include + +/** +* 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 + diff --git a/trunk/src/kernel/srs_kernel_error.hpp b/trunk/src/kernel/srs_kernel_error.hpp index 21ca885de..ee877c4ab 100644 --- a/trunk/src/kernel/srs_kernel_error.hpp +++ b/trunk/src/kernel/srs_kernel_error.hpp @@ -88,6 +88,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #define ERROR_SYSTEM_FILE_SEEK 1049 #define ERROR_SYSTEM_IO_INVALID 1050 #define ERROR_ST_EXCEED_THREADS 1051 +#define ERROR_SYSTEM_READ_PIPE 1052 +#define ERROR_SYSTEM_WRITE_PIPE 1053 /////////////////////////////////////////////////////// // RTMP protocol error. diff --git a/trunk/src/srs/srs.upp b/trunk/src/srs/srs.upp index 5fa0bfbf9..86aea57ed 100755 --- a/trunk/src/srs/srs.upp +++ b/trunk/src/srs/srs.upp @@ -92,6 +92,8 @@ file ..\app\srs_app_kbps.cpp, ..\app\srs_app_log.hpp, ..\app\srs_app_log.cpp, + ..\app\srs_app_pipe.hpp, + ..\app\srs_app_pipe.cpp, ..\app\srs_app_refer.hpp, ..\app\srs_app_refer.cpp, ..\app\srs_app_reload.hpp,