winlin

change to 0.9.52, refine encoder and ffmpeg

@@ -23,6 +23,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -23,6 +23,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 23
24 #include <srs_app_encoder.hpp> 24 #include <srs_app_encoder.hpp>
25 25
  26 +#include <algorithm>
  27 +using namespace std;
  28 +
26 #include <srs_kernel_error.hpp> 29 #include <srs_kernel_error.hpp>
27 #include <srs_kernel_log.hpp> 30 #include <srs_kernel_log.hpp>
28 #include <srs_app_config.hpp> 31 #include <srs_app_config.hpp>
@@ -36,6 +39,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -36,6 +39,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 // when error, encoder sleep for a while and retry. 39 // when error, encoder sleep for a while and retry.
37 #define SRS_ENCODER_SLEEP_US (int64_t)(3*1000*1000LL) 40 #define SRS_ENCODER_SLEEP_US (int64_t)(3*1000*1000LL)
38 41
  42 +// for encoder to detect the dead loop
  43 +static std::vector<std::string> _transcoded_url;
  44 +
39 SrsEncoder::SrsEncoder() 45 SrsEncoder::SrsEncoder()
40 { 46 {
41 pthread = new SrsThread(this, SRS_ENCODER_SLEEP_US); 47 pthread = new SrsThread(this, SRS_ENCODER_SLEEP_US);
@@ -56,6 +62,7 @@ int SrsEncoder::on_publish(SrsRequest* req) @@ -56,6 +62,7 @@ int SrsEncoder::on_publish(SrsRequest* req)
56 ret = parse_scope_engines(req); 62 ret = parse_scope_engines(req);
57 63
58 // ignore the loop encoder 64 // ignore the loop encoder
  65 + // if got a loop, donot transcode the whole stream.
59 if (ret == ERROR_ENCODER_LOOP) { 66 if (ret == ERROR_ENCODER_LOOP) {
60 clear_engines(); 67 clear_engines();
61 ret = ERROR_SUCCESS; 68 ret = ERROR_SUCCESS;
@@ -126,6 +133,15 @@ void SrsEncoder::clear_engines() @@ -126,6 +133,15 @@ void SrsEncoder::clear_engines()
126 133
127 for (it = ffmpegs.begin(); it != ffmpegs.end(); ++it) { 134 for (it = ffmpegs.begin(); it != ffmpegs.end(); ++it) {
128 SrsFFMPEG* ffmpeg = *it; 135 SrsFFMPEG* ffmpeg = *it;
  136 +
  137 + std::string output = ffmpeg->output();
  138 +
  139 + std::vector<std::string>::iterator it;
  140 + it = std::find(_transcoded_url.begin(), _transcoded_url.end(), output);
  141 + if (it != _transcoded_url.end()) {
  142 + _transcoded_url.erase(it);
  143 + }
  144 +
129 srs_freep(ffmpeg); 145 srs_freep(ffmpeg);
130 } 146 }
131 147
@@ -147,7 +163,7 @@ int SrsEncoder::parse_scope_engines(SrsRequest* req) @@ -147,7 +163,7 @@ int SrsEncoder::parse_scope_engines(SrsRequest* req)
147 // parse vhost scope engines 163 // parse vhost scope engines
148 std::string scope = ""; 164 std::string scope = "";
149 if ((conf = _srs_config->get_transcode(req->vhost, scope)) != NULL) { 165 if ((conf = _srs_config->get_transcode(req->vhost, scope)) != NULL) {
150 - if ((ret = parse_transcode(req, conf)) != ERROR_SUCCESS) { 166 + if ((ret = parse_ffmpeg(req, conf)) != ERROR_SUCCESS) {
151 srs_error("parse vhost scope=%s transcode engines failed. " 167 srs_error("parse vhost scope=%s transcode engines failed. "
152 "ret=%d", scope.c_str(), ret); 168 "ret=%d", scope.c_str(), ret);
153 return ret; 169 return ret;
@@ -156,7 +172,7 @@ int SrsEncoder::parse_scope_engines(SrsRequest* req) @@ -156,7 +172,7 @@ int SrsEncoder::parse_scope_engines(SrsRequest* req)
156 // parse app scope engines 172 // parse app scope engines
157 scope = req->app; 173 scope = req->app;
158 if ((conf = _srs_config->get_transcode(req->vhost, scope)) != NULL) { 174 if ((conf = _srs_config->get_transcode(req->vhost, scope)) != NULL) {
159 - if ((ret = parse_transcode(req, conf)) != ERROR_SUCCESS) { 175 + if ((ret = parse_ffmpeg(req, conf)) != ERROR_SUCCESS) {
160 srs_error("parse app scope=%s transcode engines failed. " 176 srs_error("parse app scope=%s transcode engines failed. "
161 "ret=%d", scope.c_str(), ret); 177 "ret=%d", scope.c_str(), ret);
162 return ret; 178 return ret;
@@ -166,7 +182,7 @@ int SrsEncoder::parse_scope_engines(SrsRequest* req) @@ -166,7 +182,7 @@ int SrsEncoder::parse_scope_engines(SrsRequest* req)
166 scope += "/"; 182 scope += "/";
167 scope += req->stream; 183 scope += req->stream;
168 if ((conf = _srs_config->get_transcode(req->vhost, scope)) != NULL) { 184 if ((conf = _srs_config->get_transcode(req->vhost, scope)) != NULL) {
169 - if ((ret = parse_transcode(req, conf)) != ERROR_SUCCESS) { 185 + if ((ret = parse_ffmpeg(req, conf)) != ERROR_SUCCESS) {
170 srs_error("parse stream scope=%s transcode engines failed. " 186 srs_error("parse stream scope=%s transcode engines failed. "
171 "ret=%d", scope.c_str(), ret); 187 "ret=%d", scope.c_str(), ret);
172 return ret; 188 return ret;
@@ -176,7 +192,7 @@ int SrsEncoder::parse_scope_engines(SrsRequest* req) @@ -176,7 +192,7 @@ int SrsEncoder::parse_scope_engines(SrsRequest* req)
176 return ret; 192 return ret;
177 } 193 }
178 194
179 -int SrsEncoder::parse_transcode(SrsRequest* req, SrsConfDirective* conf) 195 +int SrsEncoder::parse_ffmpeg(SrsRequest* req, SrsConfDirective* conf)
180 { 196 {
181 int ret = ERROR_SUCCESS; 197 int ret = ERROR_SUCCESS;
182 198
@@ -216,17 +232,9 @@ int SrsEncoder::parse_transcode(SrsRequest* req, SrsConfDirective* conf) @@ -216,17 +232,9 @@ int SrsEncoder::parse_transcode(SrsRequest* req, SrsConfDirective* conf)
216 } 232 }
217 233
218 SrsFFMPEG* ffmpeg = new SrsFFMPEG(ffmpeg_bin); 234 SrsFFMPEG* ffmpeg = new SrsFFMPEG(ffmpeg_bin);
219 -  
220 - if ((ret = ffmpeg->initialize(req, engine)) != ERROR_SUCCESS) { 235 + if ((ret = initialize_ffmpeg(ffmpeg, req, engine)) != ERROR_SUCCESS) {
221 srs_freep(ffmpeg); 236 srs_freep(ffmpeg);
222 -  
223 - // if got a loop, donot transcode the whole stream.  
224 - if (ret == ERROR_ENCODER_LOOP) {  
225 - break;  
226 - }  
227 -  
228 - srs_error("invalid transcode engine: %s %s",  
229 - conf->arg0().c_str(), engine->arg0().c_str()); 237 + srs_error("invalid transcode engine: %s %s", conf->arg0().c_str(), engine->arg0().c_str());
230 return ret; 238 return ret;
231 } 239 }
232 240
@@ -236,6 +244,62 @@ int SrsEncoder::parse_transcode(SrsRequest* req, SrsConfDirective* conf) @@ -236,6 +244,62 @@ int SrsEncoder::parse_transcode(SrsRequest* req, SrsConfDirective* conf)
236 return ret; 244 return ret;
237 } 245 }
238 246
  247 +int SrsEncoder::initialize_ffmpeg(SrsFFMPEG* ffmpeg, SrsRequest* req, SrsConfDirective* engine)
  248 +{
  249 + int ret = ERROR_SUCCESS;
  250 +
  251 + std::string input;
  252 + // input stream, from local.
  253 + // ie. rtmp://127.0.0.1:1935/live/livestream
  254 + input = "rtmp://127.0.0.1:";
  255 + input += req->port;
  256 + input += "/";
  257 + input += req->app;
  258 + input += "?vhost=";
  259 + input += req->vhost;
  260 + input += "/";
  261 + input += req->stream;
  262 +
  263 + std::string output = _srs_config->get_engine_output(engine);
  264 + // output stream, to other/self server
  265 + // ie. rtmp://127.0.0.1:1935/live/livestream_sd
  266 + output = srs_string_replace(output, "[vhost]", req->vhost);
  267 + output = srs_string_replace(output, "[port]", req->port);
  268 + output = srs_string_replace(output, "[app]", req->app);
  269 + output = srs_string_replace(output, "[stream]", req->stream);
  270 + output = srs_string_replace(output, "[engine]", engine->arg0());
  271 +
  272 + std::string log_file;
  273 + // write ffmpeg info to log file.
  274 + log_file = _srs_config->get_ffmpeg_log_dir();
  275 + log_file += "/";
  276 + log_file += "encoder";
  277 + log_file += "-";
  278 + log_file += req->vhost;
  279 + log_file += "-";
  280 + log_file += req->app;
  281 + log_file += "-";
  282 + log_file += req->stream;
  283 + log_file += ".log";
  284 +
  285 + // important: loop check, donot transcode again.
  286 + std::vector<std::string>::iterator it;
  287 + it = std::find(_transcoded_url.begin(), _transcoded_url.end(), input);
  288 + if (it != _transcoded_url.end()) {
  289 + ret = ERROR_ENCODER_LOOP;
  290 + srs_info("detect a loop cycle, input=%s, output=%s, ignore it. ret=%d",
  291 + input.c_str(), output.c_str(), ret);
  292 + return ret;
  293 + }
  294 + _transcoded_url.push_back(output);
  295 +
  296 + if ((ret = ffmpeg->initialize(input, output, log_file, engine)) != ERROR_SUCCESS) {
  297 + return ret;
  298 + }
  299 +
  300 + return ret;
  301 +}
  302 +
239 void SrsEncoder::encoder() 303 void SrsEncoder::encoder()
240 { 304 {
241 // reportable 305 // reportable
@@ -66,7 +66,8 @@ private: @@ -66,7 +66,8 @@ private:
66 virtual void clear_engines(); 66 virtual void clear_engines();
67 virtual SrsFFMPEG* at(int index); 67 virtual SrsFFMPEG* at(int index);
68 virtual int parse_scope_engines(SrsRequest* req); 68 virtual int parse_scope_engines(SrsRequest* req);
69 - virtual int parse_transcode(SrsRequest* req, SrsConfDirective* conf); 69 + virtual int parse_ffmpeg(SrsRequest* req, SrsConfDirective* conf);
  70 + virtual int initialize_ffmpeg(SrsFFMPEG* ffmpeg, SrsRequest* req, SrsConfDirective* engine);
70 virtual void encoder(); 71 virtual void encoder();
71 }; 72 };
72 73
@@ -30,7 +30,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -30,7 +30,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 #include <signal.h> 30 #include <signal.h>
31 #include <sys/types.h> 31 #include <sys/types.h>
32 32
33 -#include <algorithm> 33 +using namespace std;
34 34
35 #include <srs_kernel_error.hpp> 35 #include <srs_kernel_error.hpp>
36 #include <srs_kernel_log.hpp> 36 #include <srs_kernel_log.hpp>
@@ -50,9 +50,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -50,9 +50,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
50 // for example, libaacplus, aac, fdkaac 50 // for example, libaacplus, aac, fdkaac
51 #define SRS_ENCODER_ACODEC "aac" 51 #define SRS_ENCODER_ACODEC "aac"
52 52
53 -// for encoder to detect the dead loop  
54 -static std::vector<std::string> _transcoded_url;  
55 -  
56 SrsFFMPEG::SrsFFMPEG(std::string ffmpeg_bin) 53 SrsFFMPEG::SrsFFMPEG(std::string ffmpeg_bin)
57 { 54 {
58 started = false; 55 started = false;
@@ -76,7 +73,12 @@ SrsFFMPEG::~SrsFFMPEG() @@ -76,7 +73,12 @@ SrsFFMPEG::~SrsFFMPEG()
76 stop(); 73 stop();
77 } 74 }
78 75
79 -int SrsFFMPEG::initialize(SrsRequest* req, SrsConfDirective* engine) 76 +string SrsFFMPEG::output()
  77 +{
  78 + return _output;
  79 +}
  80 +
  81 +int SrsFFMPEG::initialize(string in, string out, string log, SrsConfDirective* engine)
80 { 82 {
81 int ret = ERROR_SUCCESS; 83 int ret = ERROR_SUCCESS;
82 84
@@ -95,53 +97,14 @@ int SrsFFMPEG::initialize(SrsRequest* req, SrsConfDirective* engine) @@ -95,53 +97,14 @@ int SrsFFMPEG::initialize(SrsRequest* req, SrsConfDirective* engine)
95 asample_rate = _srs_config->get_engine_asample_rate(engine); 97 asample_rate = _srs_config->get_engine_asample_rate(engine);
96 achannels = _srs_config->get_engine_achannels(engine); 98 achannels = _srs_config->get_engine_achannels(engine);
97 _srs_config->get_engine_aparams(engine, aparams); 99 _srs_config->get_engine_aparams(engine, aparams);
98 - output = _srs_config->get_engine_output(engine);  
99 100
100 // ensure the size is even. 101 // ensure the size is even.
101 vwidth -= vwidth % 2; 102 vwidth -= vwidth % 2;
102 vheight -= vheight % 2; 103 vheight -= vheight % 2;
103 -  
104 - // input stream, from local.  
105 - // ie. rtmp://127.0.0.1:1935/live/livestream  
106 - input = "rtmp://127.0.0.1:";  
107 - input += req->port;  
108 - input += "/";  
109 - input += req->app;  
110 - input += "?vhost=";  
111 - input += req->vhost;  
112 - input += "/";  
113 - input += req->stream;  
114 104
115 - // output stream, to other/self server  
116 - // ie. rtmp://127.0.0.1:1935/live/livestream_sd  
117 - output = srs_string_replace(output, "[vhost]", req->vhost);  
118 - output = srs_string_replace(output, "[port]", req->port);  
119 - output = srs_string_replace(output, "[app]", req->app);  
120 - output = srs_string_replace(output, "[stream]", req->stream);  
121 - output = srs_string_replace(output, "[engine]", engine->arg0());  
122 -  
123 - // write ffmpeg info to log file.  
124 - log_file = _srs_config->get_ffmpeg_log_dir();  
125 - log_file += "/";  
126 - log_file += "encoder";  
127 - log_file += "-";  
128 - log_file += req->vhost;  
129 - log_file += "-";  
130 - log_file += req->app;  
131 - log_file += "-";  
132 - log_file += req->stream;  
133 - log_file += ".log";  
134 -  
135 - // important: loop check, donot transcode again.  
136 - std::vector<std::string>::iterator it;  
137 - it = std::find(_transcoded_url.begin(), _transcoded_url.end(), input);  
138 - if (it != _transcoded_url.end()) {  
139 - ret = ERROR_ENCODER_LOOP;  
140 - srs_info("detect a loop cycle, input=%s, output=%s, ignore it. ret=%d",  
141 - input.c_str(), output.c_str(), ret);  
142 - return ret;  
143 - }  
144 - _transcoded_url.push_back(output); 105 + input = in;
  106 + _output = out;
  107 + log_file = log;
145 108
146 if (vcodec == SRS_ENCODER_NO_VIDEO && acodec == SRS_ENCODER_NO_AUDIO) { 109 if (vcodec == SRS_ENCODER_NO_VIDEO && acodec == SRS_ENCODER_NO_AUDIO) {
147 ret = ERROR_ENCODER_VCODEC; 110 ret = ERROR_ENCODER_VCODEC;
@@ -219,7 +182,7 @@ int SrsFFMPEG::initialize(SrsRequest* req, SrsConfDirective* engine) @@ -219,7 +182,7 @@ int SrsFFMPEG::initialize(SrsRequest* req, SrsConfDirective* engine)
219 return ret; 182 return ret;
220 } 183 }
221 } 184 }
222 - if (output.empty()) { 185 + if (_output.empty()) {
223 ret = ERROR_ENCODER_OUTPUT; 186 ret = ERROR_ENCODER_OUTPUT;
224 srs_error("invalid empty output, ret=%d", ret); 187 srs_error("invalid empty output, ret=%d", ret);
225 return ret; 188 return ret;
@@ -352,7 +315,7 @@ int SrsFFMPEG::start() @@ -352,7 +315,7 @@ int SrsFFMPEG::start()
352 params.push_back("flv"); 315 params.push_back("flv");
353 316
354 params.push_back("-y"); 317 params.push_back("-y");
355 - params.push_back(output); 318 + params.push_back(_output);
356 319
357 if (true) { 320 if (true) {
358 int pparam_size = 8 * 1024; 321 int pparam_size = 8 * 1024;
@@ -491,12 +454,6 @@ void SrsFFMPEG::stop() @@ -491,12 +454,6 @@ void SrsFFMPEG::stop()
491 srs_trace("stop the encoder success. pid=%d", pid); 454 srs_trace("stop the encoder success. pid=%d", pid);
492 pid = -1; 455 pid = -1;
493 } 456 }
494 -  
495 - std::vector<std::string>::iterator it;  
496 - it = std::find(_transcoded_url.begin(), _transcoded_url.end(), output);  
497 - if (it != _transcoded_url.end()) {  
498 - _transcoded_url.erase(it);  
499 - }  
500 } 457 }
501 458
502 #endif 459 #endif
@@ -35,7 +35,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -35,7 +35,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 #include <vector> 35 #include <vector>
36 36
37 class SrsConfDirective; 37 class SrsConfDirective;
38 -class SrsRequest;  
39 class SrsPithyPrint; 38 class SrsPithyPrint;
40 39
41 /** 40 /**
@@ -52,28 +51,30 @@ private: @@ -52,28 +51,30 @@ private:
52 int log_fd; 51 int log_fd;
53 private: 52 private:
54 std::string ffmpeg; 53 std::string ffmpeg;
55 - std::vector<std::string> vfilter; 54 + std::vector<std::string> vfilter;
56 std::string vcodec; 55 std::string vcodec;
57 int vbitrate; 56 int vbitrate;
58 - double vfps; 57 + double vfps;
59 int vwidth; 58 int vwidth;
60 int vheight; 59 int vheight;
61 int vthreads; 60 int vthreads;
62 std::string vprofile; 61 std::string vprofile;
63 std::string vpreset; 62 std::string vpreset;
64 - std::vector<std::string> vparams; 63 + std::vector<std::string> vparams;
65 std::string acodec; 64 std::string acodec;
66 int abitrate; 65 int abitrate;
67 int asample_rate; 66 int asample_rate;
68 int achannels; 67 int achannels;
69 - std::vector<std::string> aparams;  
70 - std::string output; 68 + std::vector<std::string> aparams;
  69 + std::string _output;
71 std::string input; 70 std::string input;
72 public: 71 public:
73 SrsFFMPEG(std::string ffmpeg_bin); 72 SrsFFMPEG(std::string ffmpeg_bin);
74 virtual ~SrsFFMPEG(); 73 virtual ~SrsFFMPEG();
75 public: 74 public:
76 - virtual int initialize(SrsRequest* req, SrsConfDirective* engine); 75 + virtual std::string output();
  76 +public:
  77 + virtual int initialize(std::string in, std::string out, std::string log, SrsConfDirective* engine);
77 virtual int start(); 78 virtual int start();
78 virtual int cycle(); 79 virtual int cycle();
79 virtual void stop(); 80 virtual void stop();
@@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -31,7 +31,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 // current release version 31 // current release version
32 #define VERSION_MAJOR "0" 32 #define VERSION_MAJOR "0"
33 #define VERSION_MINOR "9" 33 #define VERSION_MINOR "9"
34 -#define VERSION_REVISION "51" 34 +#define VERSION_REVISION "52"
35 #define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION 35 #define RTMP_SIG_SRS_VERSION VERSION_MAJOR"."VERSION_MINOR"."VERSION_REVISION
36 // server info. 36 // server info.
37 #define RTMP_SIG_SRS_KEY "srs" 37 #define RTMP_SIG_SRS_KEY "srs"