winlin

add research empty ffmpeg

@@ -18,7 +18,7 @@ vhost __defaultVhost__ { @@ -18,7 +18,7 @@ vhost __defaultVhost__ {
18 #forward 127.0.0.1:1936; 18 #forward 127.0.0.1:1936;
19 transcode { 19 transcode {
20 enabled on; 20 enabled on;
21 - ffmpeg ./objs/ffmpeg/bin/ffmpeg; 21 + ffmpeg /home/winlin/srs/objs/ffmpeg/bin/ffmpeg;
22 engine fd{ 22 engine fd{
23 enabled on; 23 enabled on;
24 vcodec libx264; 24 vcodec libx264;
@@ -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_core_encoder.hpp> 24 #include <srs_core_encoder.hpp>
25 25
  26 +#include <stdlib.h>
  27 +#include <unistd.h>
  28 +
26 #include <srs_core_error.hpp> 29 #include <srs_core_error.hpp>
27 #include <srs_core_log.hpp> 30 #include <srs_core_log.hpp>
28 #include <srs_core_config.hpp> 31 #include <srs_core_config.hpp>
@@ -35,6 +38,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -35,6 +38,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 SrsFFMPEG::SrsFFMPEG(std::string ffmpeg_bin) 38 SrsFFMPEG::SrsFFMPEG(std::string ffmpeg_bin)
36 { 39 {
37 started = false; 40 started = false;
  41 + pid = -1;
38 ffmpeg = ffmpeg_bin; 42 ffmpeg = ffmpeg_bin;
39 43
40 vbitrate = 0; 44 vbitrate = 0;
@@ -76,6 +80,14 @@ int SrsFFMPEG::initialize(std::string vhost, std::string port, std::string app, @@ -76,6 +80,14 @@ int SrsFFMPEG::initialize(std::string vhost, std::string port, std::string app,
76 vwidth -= vwidth % 2; 80 vwidth -= vwidth % 2;
77 vheight -= vheight % 2; 81 vheight -= vheight % 2;
78 82
  83 + // input stream, from local.
  84 + input = "rtmp://127.0.0.1:";
  85 + input += port;
  86 + input += "/";
  87 + input += app;
  88 + input += "/";
  89 + input += stream;
  90 +
79 if (vhost == RTMP_VHOST_DEFAULT) { 91 if (vhost == RTMP_VHOST_DEFAULT) {
80 output = srs_replace(output, "[vhost]", "127.0.0.1"); 92 output = srs_replace(output, "[vhost]", "127.0.0.1");
81 } else { 93 } else {
@@ -167,6 +179,59 @@ int SrsFFMPEG::start() @@ -167,6 +179,59 @@ int SrsFFMPEG::start()
167 return ret; 179 return ret;
168 } 180 }
169 181
  182 + // prepare execl params
  183 + char vsize[22];
  184 + snprintf(vsize, sizeof(vsize), "%dx%d", vwidth, vheight);
  185 + char vaspect[22];
  186 + snprintf(vaspect, sizeof(vaspect), "%d:%d", vwidth, vheight);
  187 +
  188 + // we use vfork, for we use fored process
  189 + // to start ffmpeg, that is, exec after vfork.
  190 + if ((pid = fork()) < 0) {
  191 + ret = ERROR_ENCODER_FORK;
  192 + srs_error("vfork process failed. ret=%d", ret);
  193 + return ret;
  194 + }
  195 +
  196 + // child process: ffmpeg encoder engine.
  197 + if (pid == 0) {
  198 + // must exec immediately, or may introduce bug.
  199 + ret = execl(ffmpeg.c_str(),
  200 + "-i", input.c_str(),
  201 + // video specified.
  202 + "-vcodec", vcodec.c_str(),
  203 + "-b:v", vbitrate * 1000,
  204 + "-r", vfps,
  205 + "-size", vsize,
  206 + "-aspect", vaspect, // TODO: add aspect if needed.
  207 + "-threads", vthreads,
  208 + "-profile", vprofile.c_str(),
  209 + "-preset", vpreset.c_str(),
  210 + vparams.c_str(),
  211 + // audio specified.
  212 + "-acodec", acodec.c_str(),
  213 + "-b:a", abitrate * 1000,
  214 + "-ar", asample_rate,
  215 + "-ac", achannels,
  216 + aparams.c_str(),
  217 + "-f", "flv",
  218 + "-y", output.c_str(),
  219 + NULL
  220 + );
  221 + if (ret < 0) {
  222 + fprintf(stderr, "fork ffmpeg failed, errno=%d(%s)",
  223 + errno, strerror(errno));
  224 + }
  225 + exit(ret);
  226 + }
  227 +
  228 + // parent.
  229 + if (pid > 0) {
  230 + started = true;
  231 + srs_trace("vfored ffmpeg encoder engine, pid=%d", pid);
  232 + return ret;
  233 + }
  234 +
170 return ret; 235 return ret;
171 } 236 }
172 237
@@ -44,6 +44,7 @@ class SrsFFMPEG @@ -44,6 +44,7 @@ class SrsFFMPEG
44 { 44 {
45 private: 45 private:
46 bool started; 46 bool started;
  47 + pid_t pid;
47 private: 48 private:
48 std::string ffmpeg; 49 std::string ffmpeg;
49 std::string vcodec; 50 std::string vcodec;
@@ -61,6 +62,7 @@ private: @@ -61,6 +62,7 @@ private:
61 int achannels; 62 int achannels;
62 std::string aparams; 63 std::string aparams;
63 std::string output; 64 std::string output;
  65 + std::string input;
64 public: 66 public:
65 SrsFFMPEG(std::string ffmpeg_bin); 67 SrsFFMPEG(std::string ffmpeg_bin);
66 virtual ~SrsFFMPEG(); 68 virtual ~SrsFFMPEG();
@@ -135,5 +135,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. @@ -135,5 +135,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
135 #define ERROR_ENCODER_VWIDTH 710 135 #define ERROR_ENCODER_VWIDTH 710
136 #define ERROR_ENCODER_VFPS 711 136 #define ERROR_ENCODER_VFPS 711
137 #define ERROR_ENCODER_VBITRATE 712 137 #define ERROR_ENCODER_VBITRATE 712
  138 +#define ERROR_ENCODER_FORK 713
138 139
139 #endif 140 #endif