Blame view

trunk/src/app/srs_app_encoder.cpp 20.3 KB
winlin authored
1 2 3
/*
The MIT License (MIT)
4
Copyright (c) 2013-2014 winlin
winlin authored
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

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.
*/
24
#include <srs_app_encoder.hpp>
winlin authored
25 26 27 28 29 30 31 32 33 34

#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>

#include <algorithm>
35
#include <srs_kernel_error.hpp>
36
#include <srs_kernel_log.hpp>
37
#include <srs_app_config.hpp>
38
#include <srs_protocol_rtmp.hpp>
39
#include <srs_app_pithy_print.hpp>
40
#include <srs_protocol_rtmp_stack.hpp>
winlin authored
41 42 43

#ifdef SRS_FFMPEG
44
#define SRS_ENCODER_COPY    "copy"
45 46
#define SRS_ENCODER_NO_VIDEO    "vn"
#define SRS_ENCODER_NO_AUDIO    "an"
47 48
#define SRS_ENCODER_VCODEC     "libx264"
#define SRS_ENCODER_ACODEC     "libaacplus"
winlin authored
49 50 51 52 53 54

// for encoder to detect the dead loop
static std::vector<std::string> _transcoded_url;

SrsFFMPEG::SrsFFMPEG(std::string ffmpeg_bin)
{
55 56 57 58 59 60 61 62 63 64 65 66 67 68
    started            = false;
    pid                = -1;
    ffmpeg             = ffmpeg_bin;
    
    vbitrate         = 0;
    vfps             = 0;
    vwidth             = 0;
    vheight         = 0;
    vthreads         = 0;
    abitrate         = 0;
    asample_rate     = 0;
    achannels         = 0;
    
    log_fd = -1;
winlin authored
69 70 71 72
}

SrsFFMPEG::~SrsFFMPEG()
{
73
    stop();
winlin authored
74 75 76 77
}

int SrsFFMPEG::initialize(SrsRequest* req, SrsConfDirective* engine)
{
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
    int ret = ERROR_SUCCESS;
    
    _srs_config->get_engine_vfilter(engine, vfilter);
    vcodec             = _srs_config->get_engine_vcodec(engine);
    vbitrate         = _srs_config->get_engine_vbitrate(engine);
    vfps             = _srs_config->get_engine_vfps(engine);
    vwidth             = _srs_config->get_engine_vwidth(engine);
    vheight         = _srs_config->get_engine_vheight(engine);
    vthreads         = _srs_config->get_engine_vthreads(engine);
    vprofile         = _srs_config->get_engine_vprofile(engine);
    vpreset         = _srs_config->get_engine_vpreset(engine);
    _srs_config->get_engine_vparams(engine, vparams);
    acodec             = _srs_config->get_engine_acodec(engine);
    abitrate         = _srs_config->get_engine_abitrate(engine);
    asample_rate     = _srs_config->get_engine_asample_rate(engine);
    achannels         = _srs_config->get_engine_achannels(engine);
    _srs_config->get_engine_aparams(engine, aparams);
    output             = _srs_config->get_engine_output(engine);
    
    // ensure the size is even.
    vwidth -= vwidth % 2;
    vheight -= vheight % 2;

    // input stream, from local.
    // ie. rtmp://127.0.0.1:1935/live/livestream
    input = "rtmp://127.0.0.1:";
    input += req->port;
    input += "/";
    input += req->app;
    input += "?vhost=";
    input += req->vhost;
    input += "/";
    input += req->stream;
    
    // output stream, to other/self server
    // ie. rtmp://127.0.0.1:1935/live/livestream_sd
    output = srs_replace(output, "[vhost]", req->vhost);
    output = srs_replace(output, "[port]", req->port);
    output = srs_replace(output, "[app]", req->app);
    output = srs_replace(output, "[stream]", req->stream);
    output = srs_replace(output, "[engine]", engine->arg0());
    
    // write ffmpeg info to log file.
121
    log_file = _srs_config->get_ffmpeg_log_dir();
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    log_file += "/";
    log_file += "encoder";
    log_file += "-";
    log_file += req->vhost;
    log_file += "-";
    log_file += req->app;
    log_file += "-";
    log_file += req->stream;
    log_file += ".log";

    // important: loop check, donot transcode again.
    std::vector<std::string>::iterator it;
    it = std::find(_transcoded_url.begin(), _transcoded_url.end(), input);
    if (it != _transcoded_url.end()) {
        ret = ERROR_ENCODER_LOOP;
        srs_info("detect a loop cycle, input=%s, output=%s, ignore it. ret=%d",
            input.c_str(), output.c_str(), ret);
        return ret;
    }
    _transcoded_url.push_back(output);
    
143 144 145 146 147 148 149
    if (vcodec == SRS_ENCODER_NO_VIDEO && acodec == SRS_ENCODER_NO_AUDIO) {
        ret = ERROR_ENCODER_VCODEC;
        srs_warn("video and audio disabled. ret=%d", ret);
        return ret;
    }
    
    if (vcodec != SRS_ENCODER_COPY && vcodec != SRS_ENCODER_NO_VIDEO) {
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
        if (vcodec != SRS_ENCODER_VCODEC) {
            ret = ERROR_ENCODER_VCODEC;
            srs_error("invalid vcodec, must be %s, actual %s, ret=%d",
                SRS_ENCODER_VCODEC, vcodec.c_str(), ret);
            return ret;
        }
        if (vbitrate <= 0) {
            ret = ERROR_ENCODER_VBITRATE;
            srs_error("invalid vbitrate: %d, ret=%d", vbitrate, ret);
            return ret;
        }
        if (vfps <= 0) {
            ret = ERROR_ENCODER_VFPS;
            srs_error("invalid vfps: %.2f, ret=%d", vfps, ret);
            return ret;
        }
        if (vwidth <= 0) {
            ret = ERROR_ENCODER_VWIDTH;
            srs_error("invalid vwidth: %d, ret=%d", vwidth, ret);
            return ret;
        }
        if (vheight <= 0) {
            ret = ERROR_ENCODER_VHEIGHT;
            srs_error("invalid vheight: %d, ret=%d", vheight, ret);
            return ret;
        }
        if (vthreads < 0) {
            ret = ERROR_ENCODER_VTHREADS;
            srs_error("invalid vthreads: %d, ret=%d", vthreads, ret);
            return ret;
        }
        if (vprofile.empty()) {
            ret = ERROR_ENCODER_VPROFILE;
            srs_error("invalid vprofile: %s, ret=%d", vprofile.c_str(), ret);
            return ret;
        }
        if (vpreset.empty()) {
            ret = ERROR_ENCODER_VPRESET;
            srs_error("invalid vpreset: %s, ret=%d", vpreset.c_str(), ret);
            return ret;
        }
    }
    
193
    if (acodec != SRS_ENCODER_COPY && acodec != SRS_ENCODER_NO_AUDIO) {
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
        if (acodec != SRS_ENCODER_ACODEC) {
            ret = ERROR_ENCODER_ACODEC;
            srs_error("invalid acodec, must be %s, actual %s, ret=%d",
                SRS_ENCODER_ACODEC, acodec.c_str(), ret);
            return ret;
        }
        if (abitrate <= 0) {
            ret = ERROR_ENCODER_ABITRATE;
            srs_error("invalid abitrate: %d, ret=%d", 
                abitrate, ret);
            return ret;
        }
        if (asample_rate <= 0) {
            ret = ERROR_ENCODER_ASAMPLE_RATE;
            srs_error("invalid sample rate: %d, ret=%d", 
                asample_rate, ret);
            return ret;
        }
        if (achannels != 1 && achannels != 2) {
            ret = ERROR_ENCODER_ACHANNELS;
            srs_error("invalid achannels, must be 1 or 2, actual %d, ret=%d", 
                achannels, ret);
            return ret;
        }
    }
    if (output.empty()) {
        ret = ERROR_ENCODER_OUTPUT;
        srs_error("invalid empty output, ret=%d", ret);
        return ret;
    }
    
    return ret;
winlin authored
226 227 228 229
}

int SrsFFMPEG::start()
{
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
    int ret = ERROR_SUCCESS;
    
    if (started) {
        return ret;
    }
    
    // prepare exec params
    char tmp[256];
    std::vector<std::string> params;
    
    // argv[0], set to ffmpeg bin.
    // The  execv()  and  execvp() functions ....
    // The first argument, by convention, should point to 
    // the filename associated  with  the file being executed.
    params.push_back(ffmpeg);
    
    // input.
    params.push_back("-f");
    params.push_back("flv");
    
    params.push_back("-i");
    params.push_back(input);
    
    // build the filter
    if (!vfilter.empty()) {
        std::vector<std::string>::iterator it;
        for (it = vfilter.begin(); it != vfilter.end(); ++it) {
            std::string p = *it;
            if (!p.empty()) {
                params.push_back(p);
            }
        }
    }
    
    // video specified.
265 266 267 268 269 270
    if (vcodec != SRS_ENCODER_NO_VIDEO) {
        params.push_back("-vcodec");
        params.push_back(vcodec);
    } else {
        params.push_back("-vn");
    }
271 272
    
    // the codec params is disabled when copy
273
    if (vcodec != SRS_ENCODER_COPY && vcodec != SRS_ENCODER_NO_VIDEO) {
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
        params.push_back("-b:v");
        snprintf(tmp, sizeof(tmp), "%d", vbitrate * 1000);
        params.push_back(tmp);
        
        params.push_back("-r");
        snprintf(tmp, sizeof(tmp), "%.2f", vfps);
        params.push_back(tmp);
        
        params.push_back("-s");
        snprintf(tmp, sizeof(tmp), "%dx%d", vwidth, vheight);
        params.push_back(tmp);
        
        // TODO: add aspect if needed.
        params.push_back("-aspect");
        snprintf(tmp, sizeof(tmp), "%d:%d", vwidth, vheight);
        params.push_back(tmp);
        
        params.push_back("-threads");
        snprintf(tmp, sizeof(tmp), "%d", vthreads);
        params.push_back(tmp);
        
        params.push_back("-profile:v");
        params.push_back(vprofile);
        
        params.push_back("-preset");
        params.push_back(vpreset);
        
        // vparams
        if (!vparams.empty()) {
            std::vector<std::string>::iterator it;
            for (it = vparams.begin(); it != vparams.end(); ++it) {
                std::string p = *it;
                if (!p.empty()) {
                    params.push_back(p);
                }
            }
        }
    }
    
    // audio specified.
314 315 316 317 318 319
    if (acodec != SRS_ENCODER_NO_AUDIO) {
        params.push_back("-acodec");
        params.push_back(acodec);
    } else {
        params.push_back("-an");
    }
320 321
    
    // the codec params is disabled when copy
322
    if (acodec != SRS_ENCODER_COPY && acodec != SRS_ENCODER_NO_AUDIO) {
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
        params.push_back("-b:a");
        snprintf(tmp, sizeof(tmp), "%d", abitrate * 1000);
        params.push_back(tmp);
        
        params.push_back("-ar");
        snprintf(tmp, sizeof(tmp), "%d", asample_rate);
        params.push_back(tmp);
        
        params.push_back("-ac");
        snprintf(tmp, sizeof(tmp), "%d", achannels);
        params.push_back(tmp);
        
        // aparams
        if (!aparams.empty()) {
            std::vector<std::string>::iterator it;
            for (it = aparams.begin(); it != aparams.end(); ++it) {
                std::string p = *it;
                if (!p.empty()) {
                    params.push_back(p);
                }
            }
        }
    }

    // output
    params.push_back("-f");
    params.push_back("flv");
    
    params.push_back("-y");
    params.push_back(output);

    if (true) {
        int pparam_size = 8 * 1024;
        char* pparam = new char[pparam_size];
        char* p = pparam;
        char* last = pparam + pparam_size;
        for (int i = 0; i < (int)params.size(); i++) {
            std::string ffp = params[i];
            snprintf(p, last - p, "%s ", ffp.c_str());
            p += ffp.length() + 1;
        }
        srs_trace("start transcoder, log: %s, params: %s", 
            log_file.c_str(), pparam);
        srs_freepa(pparam);
    }
    
    // TODO: fork or vfork?
    if ((pid = fork()) < 0) {
        ret = ERROR_ENCODER_FORK;
        srs_error("vfork process failed. ret=%d", ret);
        return ret;
    }
    
    // child process: ffmpeg encoder engine.
    if (pid == 0) {
        // redirect logs to file.
        int flags = O_CREAT|O_WRONLY|O_APPEND;
        mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH;
        if ((log_fd = ::open(log_file.c_str(), flags, mode)) < 0) {
            ret = ERROR_ENCODER_OPEN;
            srs_error("open encoder file %s failed. ret=%d", log_file.c_str(), ret);
            return ret;
        }
        if (dup2(log_fd, STDOUT_FILENO) < 0) {
            ret = ERROR_ENCODER_DUP2;
            srs_error("dup2 encoder file failed. ret=%d", ret);
            return ret;
        }
        if (dup2(log_fd, STDERR_FILENO) < 0) {
            ret = ERROR_ENCODER_DUP2;
            srs_error("dup2 encoder file failed. ret=%d", ret);
            return ret;
        }
        // close other fds
        // TODO: do in right way.
        for (int i = 3; i < 1024; i++) {
            ::close(i);
        }
        
        // memory leak in child process, it's ok.
        char** charpv_params = new char*[params.size() + 1];
        for (int i = 0; i < (int)params.size(); i++) {
            std::string p = params[i];
            charpv_params[i] = (char*)p.c_str();
        }
        // EOF: NULL
        charpv_params[params.size()] = NULL;
        
        // TODO: execv or execvp
        ret = execv(ffmpeg.c_str(), charpv_params);
        if (ret < 0) {
            fprintf(stderr, "fork ffmpeg failed, errno=%d(%s)", 
                errno, strerror(errno));
        }
        exit(ret);
    }

    // parent.
    if (pid > 0) {
        started = true;
        srs_trace("vfored ffmpeg encoder engine, pid=%d", pid);
        return ret;
    }
    
    return ret;
winlin authored
428 429 430 431
}

int SrsFFMPEG::cycle()
{
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
    int ret = ERROR_SUCCESS;
    
    if (!started) {
        return ret;
    }
    
    int status = 0;
    pid_t p = waitpid(pid, &status, WNOHANG);
    
    if (p < 0) {
        ret = ERROR_SYSTEM_WAITPID;
        srs_error("transcode waitpid failed, pid=%d, ret=%d", pid, ret);
        return ret;
    }
    
    if (p == 0) {
        srs_info("transcode process pid=%d is running.", pid);
        return ret;
    }
    
    srs_trace("transcode process pid=%d terminate, restart it.", pid);
    started = false;
    
    return ret;
winlin authored
456 457 458 459
}

void SrsFFMPEG::stop()
{
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
    if (log_fd > 0) {
        ::close(log_fd);
        log_fd = -1;
    }
    
    if (!started) {
        return;
    }
    
    // kill the ffmpeg,
    // when rewind, upstream will stop publish(unpublish),
    // unpublish event will stop all ffmpeg encoders,
    // then publish will start all ffmpeg encoders.
    if (pid > 0) {
        if (kill(pid, SIGKILL) < 0) {
            srs_warn("kill the encoder failed, ignored. pid=%d", pid);
        }
        
        // wait for the ffmpeg to quit.
        // ffmpeg will gracefully quit if signal is:
        //         1) SIGHUP     2) SIGINT     3) SIGQUIT
        // other signals, directly exit(123), for example:
        //        9) SIGKILL    15) SIGTERM
        int status = 0;
        if (waitpid(pid, &status, 0) < 0) {
            srs_warn("wait the encoder quit failed, ignored. pid=%d", pid);
        }
        
        srs_trace("stop the encoder success. pid=%d", pid);
        pid = -1;
    }
    
    std::vector<std::string>::iterator it;
    it = std::find(_transcoded_url.begin(), _transcoded_url.end(), output);
    if (it != _transcoded_url.end()) {
        _transcoded_url.erase(it);
    }
winlin authored
497 498 499 500
}

SrsEncoder::SrsEncoder()
{
501 502
    pthread = new SrsThread(this, SRS_ENCODER_SLEEP_US);
    pithy_print = new SrsPithyPrint(SRS_STAGE_ENCODER);
winlin authored
503 504 505 506
}

SrsEncoder::~SrsEncoder()
{
507 508 509
    on_unpublish();
    
    srs_freep(pthread);
winlin authored
510 511 512 513
}

int SrsEncoder::on_publish(SrsRequest* req)
{
514 515 516 517 518 519 520 521 522 523 524 525 526 527
    int ret = ERROR_SUCCESS;

    ret = parse_scope_engines(req);
    
    // ignore the loop encoder
    if (ret == ERROR_ENCODER_LOOP) {
        clear_engines();
        ret = ERROR_SUCCESS;
    }
    
    // return for error or no engine.
    if (ret != ERROR_SUCCESS || ffmpegs.empty()) {
        return ret;
    }
winlin authored
528 529
    
    // start thread to run all encoding engines.
530
    if ((ret = pthread->start()) != ERROR_SUCCESS) {
winlin authored
531 532 533 534
        srs_error("st_thread_create failed. ret=%d", ret);
        return ret;
    }
    
535
    return ret;
winlin authored
536 537 538 539
}

void SrsEncoder::on_unpublish()
{
540 541
    pthread->stop();
    clear_engines();
542 543 544 545
}

int SrsEncoder::cycle()
{
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
    int ret = ERROR_SUCCESS;
    
    std::vector<SrsFFMPEG*>::iterator it;
    for (it = ffmpegs.begin(); it != ffmpegs.end(); ++it) {
        SrsFFMPEG* ffmpeg = *it;
        
        // start all ffmpegs.
        if ((ret = ffmpeg->start()) != ERROR_SUCCESS) {
            srs_error("ffmpeg start failed. ret=%d", ret);
            return ret;
        }

        // check ffmpeg status.
        if ((ret = ffmpeg->cycle()) != ERROR_SUCCESS) {
            srs_error("ffmpeg cycle failed. ret=%d", ret);
            return ret;
        }
    }

    // pithy print
    encoder();
    pithy_print->elapse(SRS_ENCODER_SLEEP_US / 1000);
    
    return ret;
570 571 572 573
}

void SrsEncoder::on_leave_loop()
{
574 575
    // kill ffmpeg when finished and it alive
    std::vector<SrsFFMPEG*>::iterator it;
576
577 578 579 580
    for (it = ffmpegs.begin(); it != ffmpegs.end(); ++it) {
        SrsFFMPEG* ffmpeg = *it;
        ffmpeg->stop();
    }
winlin authored
581 582 583 584
}

void SrsEncoder::clear_engines()
{
585 586 587 588 589 590 591 592
    std::vector<SrsFFMPEG*>::iterator it;
    
    for (it = ffmpegs.begin(); it != ffmpegs.end(); ++it) {
        SrsFFMPEG* ffmpeg = *it;
        srs_freep(ffmpeg);
    }

    ffmpegs.clear();
winlin authored
593 594 595 596
}

SrsFFMPEG* SrsEncoder::at(int index)
{
597
    return ffmpegs[index];
winlin authored
598 599
}
600 601
int SrsEncoder::parse_scope_engines(SrsRequest* req)
{
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
    int ret = ERROR_SUCCESS;
    
    // parse all transcode engines.
    SrsConfDirective* conf = NULL;
    
    // parse vhost scope engines
    std::string scope = "";
    if ((conf = _srs_config->get_transcode(req->vhost, scope)) != NULL) {
        if ((ret = parse_transcode(req, conf)) != ERROR_SUCCESS) {
            srs_error("parse vhost scope=%s transcode engines failed. "
                "ret=%d", scope.c_str(), ret);
            return ret;
        }
    }
    // parse app scope engines
    scope = req->app;
    if ((conf = _srs_config->get_transcode(req->vhost, scope)) != NULL) {
        if ((ret = parse_transcode(req, conf)) != ERROR_SUCCESS) {
            srs_error("parse app scope=%s transcode engines failed. "
                "ret=%d", scope.c_str(), ret);
            return ret;
        }
    }
    // parse stream scope engines
    scope += "/";
    scope += req->stream;
    if ((conf = _srs_config->get_transcode(req->vhost, scope)) != NULL) {
        if ((ret = parse_transcode(req, conf)) != ERROR_SUCCESS) {
            srs_error("parse stream scope=%s transcode engines failed. "
                "ret=%d", scope.c_str(), ret);
            return ret;
        }
    }
    
    return ret;
637 638
}
winlin authored
639 640
int SrsEncoder::parse_transcode(SrsRequest* req, SrsConfDirective* conf)
{
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
    int ret = ERROR_SUCCESS;
    
    srs_assert(conf);
    
    // enabled
    if (!_srs_config->get_transcode_enabled(conf)) {
        srs_trace("ignore the disabled transcode: %s", 
            conf->arg0().c_str());
        return ret;
    }
    
    // ffmpeg
    std::string ffmpeg_bin = _srs_config->get_transcode_ffmpeg(conf);
    if (ffmpeg_bin.empty()) {
        srs_trace("ignore the empty ffmpeg transcode: %s", 
            conf->arg0().c_str());
        return ret;
    }
    
    // get all engines.
    std::vector<SrsConfDirective*> engines;
    _srs_config->get_transcode_engines(conf, engines);
    if (engines.empty()) {
        srs_trace("ignore the empty transcode engine: %s", 
            conf->arg0().c_str());
        return ret;
    }
    
    // create engine
    for (int i = 0; i < (int)engines.size(); i++) {
        SrsConfDirective* engine = engines[i];
        if (!_srs_config->get_engine_enabled(engine)) {
            srs_trace("ignore the diabled transcode engine: %s %s", 
                conf->arg0().c_str(), engine->arg0().c_str());
            continue;
        }
        
        SrsFFMPEG* ffmpeg = new SrsFFMPEG(ffmpeg_bin);
        
        if ((ret = ffmpeg->initialize(req, engine)) != ERROR_SUCCESS) {
            srs_freep(ffmpeg);
            
            // if got a loop, donot transcode the whole stream.
            if (ret == ERROR_ENCODER_LOOP) {
                break;
            }
            
            srs_error("invalid transcode engine: %s %s", 
                conf->arg0().c_str(), engine->arg0().c_str());
            return ret;
        }

        ffmpegs.push_back(ffmpeg);
    }
    
    return ret;
winlin authored
697 698
}
699
void SrsEncoder::encoder()
winlin authored
700
{
701 702 703 704 705
    // reportable
    if (pithy_print->can_print()) {
        // TODO: FIXME: show more info.
        srs_trace("-> time=%"PRId64", encoders=%d", pithy_print->get_age(), (int)ffmpegs.size());
    }
winlin authored
706 707 708 709
}

#endif