winlin

Merge branch 'srs.master'

@@ -388,7 +388,8 @@ if [ $SRS_EXPORT_LIBRTMP_PROJECT = NO ]; then @@ -388,7 +388,8 @@ if [ $SRS_EXPORT_LIBRTMP_PROJECT = NO ]; then
388 "srs_app_thread" "srs_app_bandwidth" "srs_app_st" "srs_app_log" "srs_app_config" 388 "srs_app_thread" "srs_app_bandwidth" "srs_app_st" "srs_app_log" "srs_app_config"
389 "srs_app_pithy_print" "srs_app_reload" "srs_app_http_api" "srs_app_http_conn" "srs_app_http_hooks" 389 "srs_app_pithy_print" "srs_app_reload" "srs_app_http_api" "srs_app_http_conn" "srs_app_http_hooks"
390 "srs_app_json" "srs_app_ingest" "srs_app_ffmpeg" "srs_app_utility" "srs_app_dvr" "srs_app_edge" 390 "srs_app_json" "srs_app_ingest" "srs_app_ffmpeg" "srs_app_utility" "srs_app_dvr" "srs_app_edge"
391 - "srs_app_kbps" "srs_app_heartbeat" "srs_app_empty" "srs_app_http_client" "srs_app_avc_aac") 391 + "srs_app_kbps" "srs_app_heartbeat" "srs_app_empty" "srs_app_http_client" "srs_app_avc_aac"
  392 + "srs_app_pipe")
392 APP_INCS="src/app"; MODULE_DIR=${APP_INCS} . auto/modules.sh 393 APP_INCS="src/app"; MODULE_DIR=${APP_INCS} . auto/modules.sh
393 APP_OBJS="${MODULE_OBJS[@]}" 394 APP_OBJS="${MODULE_OBJS[@]}"
394 fi 395 fi
  1 +/*
  2 +The MIT License (MIT)
  3 +
  4 +Copyright (c) 2013-2014 winlin
  5 +
  6 +Permission is hereby granted, free of charge, to any person obtaining a copy of
  7 +this software and associated documentation files (the "Software"), to deal in
  8 +the Software without restriction, including without limitation the rights to
  9 +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  10 +the Software, and to permit persons to whom the Software is furnished to do so,
  11 +subject to the following conditions:
  12 +
  13 +The above copyright notice and this permission notice shall be included in all
  14 +copies or substantial portions of the Software.
  15 +
  16 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18 +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19 +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20 +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21 +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22 +*/
  23 +
  24 +#include <srs_app_pipe.hpp>
  25 +
  26 +#include <unistd.h>
  27 +
  28 +#include <srs_kernel_error.hpp>
  29 +#include <srs_kernel_log.hpp>
  30 +
  31 +SrsPipe::SrsPipe()
  32 +{
  33 + fds[0] = fds[1] = 0;
  34 + read_stfd = write_stfd = NULL;
  35 + _already_written = false;
  36 +}
  37 +
  38 +SrsPipe::~SrsPipe()
  39 +{
  40 + srs_close_stfd(read_stfd);
  41 + srs_close_stfd(write_stfd);
  42 +}
  43 +
  44 +int SrsPipe::initialize()
  45 +{
  46 + int ret = ERROR_SUCCESS;
  47 +
  48 + if (pipe(fds) < 0) {
  49 + ret = ERROR_SYSTEM_CREATE_PIPE;
  50 + srs_error("create pipe failed. ret=%d", ret);
  51 + return ret;
  52 + }
  53 +
  54 + if ((read_stfd = st_netfd_open(fds[0])) == NULL) {
  55 + ret = ERROR_SYSTEM_CREATE_PIPE;
  56 + srs_error("open read pipe failed. ret=%d", ret);
  57 + return ret;
  58 + }
  59 +
  60 + if ((write_stfd = st_netfd_open(fds[1])) == NULL) {
  61 + ret = ERROR_SYSTEM_CREATE_PIPE;
  62 + srs_error("open write pipe failed. ret=%d", ret);
  63 + return ret;
  64 + }
  65 +
  66 + return ret;
  67 +}
  68 +
  69 +bool SrsPipe::already_written()
  70 +{
  71 + return _already_written;
  72 +}
  73 +
  74 +int SrsPipe::active()
  75 +{
  76 + int ret = ERROR_SUCCESS;
  77 +
  78 + int v = 0;
  79 + if (st_write(read_stfd, &v, sizeof(int), ST_UTIME_NO_TIMEOUT) != sizeof(int)) {
  80 + ret = ERROR_SYSTEM_WRITE_PIPE;
  81 + srs_error("write pipe failed. ret=%d", ret);
  82 + return ret;
  83 + }
  84 +
  85 + _already_written = true;
  86 +
  87 + return ret;
  88 +}
  89 +
  90 +int SrsPipe::reset()
  91 +{
  92 + int ret = ERROR_SUCCESS;
  93 +
  94 + int v;
  95 + if (st_read(read_stfd, &v, sizeof(int), ST_UTIME_NO_TIMEOUT) != sizeof(int)) {
  96 + ret = ERROR_SYSTEM_READ_PIPE;
  97 + srs_error("read pipe failed. ret=%d", ret);
  98 + return ret;
  99 + }
  100 +
  101 + _already_written = false;
  102 +
  103 + return ret;
  104 +}
  105 +
  1 +/*
  2 +The MIT License (MIT)
  3 +
  4 +Copyright (c) 2013-2014 winlin
  5 +
  6 +Permission is hereby granted, free of charge, to any person obtaining a copy of
  7 +this software and associated documentation files (the "Software"), to deal in
  8 +the Software without restriction, including without limitation the rights to
  9 +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  10 +the Software, and to permit persons to whom the Software is furnished to do so,
  11 +subject to the following conditions:
  12 +
  13 +The above copyright notice and this permission notice shall be included in all
  14 +copies or substantial portions of the Software.
  15 +
  16 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18 +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19 +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20 +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21 +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22 +*/
  23 +
  24 +#ifndef SRS_APP_PIPE_HPP
  25 +#define SRS_APP_PIPE_HPP
  26 +
  27 +/*
  28 +#include <srs_app_pipe.hpp>
  29 +*/
  30 +
  31 +#include <srs_core.hpp>
  32 +
  33 +#include <srs_app_st.hpp>
  34 +
  35 +/**
  36 +* convert something to io,
  37 +* for example, signal or SrsConsumer event.
  38 +* for performance issue, @see: https://github.com/winlinvip/simple-rtmp-server/issues/194
  39 +*/
  40 +class SrsPipe
  41 +{
  42 +private:
  43 + int fds[2];
  44 + st_netfd_t read_stfd;
  45 + st_netfd_t write_stfd;
  46 + /**
  47 + * for the event based service,
  48 + * for example, the consumer only care whether there is data writen in pipe,
  49 + * and the source will not write to pipe when pipe is already writen.
  50 + */
  51 + bool _already_written;
  52 +public:
  53 + SrsPipe();
  54 + virtual ~SrsPipe();
  55 +public:
  56 + /**
  57 + * initialize pipes, open fds.
  58 + */
  59 + virtual int initialize();
  60 +public:
  61 + /**
  62 + * for event based service, whether already writen data.
  63 + */
  64 + virtual bool already_written();
  65 + /**
  66 + * for event based service,
  67 + * write an int to pipe and set the pipe to active.
  68 + */
  69 + virtual int active();
  70 + /**
  71 + * for event based service,
  72 + * read an int from pipe and reset the pipe to deactive.
  73 + */
  74 + virtual int reset();
  75 +};
  76 +
  77 +#endif
  78 +
@@ -88,6 +88,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -88,6 +88,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
88 #define ERROR_SYSTEM_FILE_SEEK 1049 88 #define ERROR_SYSTEM_FILE_SEEK 1049
89 #define ERROR_SYSTEM_IO_INVALID 1050 89 #define ERROR_SYSTEM_IO_INVALID 1050
90 #define ERROR_ST_EXCEED_THREADS 1051 90 #define ERROR_ST_EXCEED_THREADS 1051
  91 +#define ERROR_SYSTEM_READ_PIPE 1052
  92 +#define ERROR_SYSTEM_WRITE_PIPE 1053
91 93
92 /////////////////////////////////////////////////////// 94 ///////////////////////////////////////////////////////
93 // RTMP protocol error. 95 // RTMP protocol error.
@@ -92,6 +92,8 @@ file @@ -92,6 +92,8 @@ file
92 ..\app\srs_app_kbps.cpp, 92 ..\app\srs_app_kbps.cpp,
93 ..\app\srs_app_log.hpp, 93 ..\app\srs_app_log.hpp,
94 ..\app\srs_app_log.cpp, 94 ..\app\srs_app_log.cpp,
  95 + ..\app\srs_app_pipe.hpp,
  96 + ..\app\srs_app_pipe.cpp,
95 ..\app\srs_app_refer.hpp, 97 ..\app\srs_app_refer.hpp,
96 ..\app\srs_app_refer.cpp, 98 ..\app\srs_app_refer.cpp,
97 ..\app\srs_app_reload.hpp, 99 ..\app\srs_app_reload.hpp,