[Libav-user] Streaming RTSP

nacho collado del ser n.collado.ser at gmail.com
Tue Dec 5 20:53:32 EET 2017


Hi, Im trying to make a RTSP server, but I dont know how could I set the
url from my server or when.

By the moment I have:

#include "SenderRtsp.h"
#include <sys/time.h>

namespace payloader {

DEFINE_LOGGER(SenderRtsp, "SenderRtsp");

SenderRtsp::SenderRtsp(const std::string& url) : input_url_(url)  {
     ELOG_DEBUG("Creating source reader to %s", input_url_.c_str());
    // url = url.c_str();
    //outContext = NULL;
}

SenderRtsp::~SenderRtsp() {
    // deliver_thread_.join();
  //  avformat_close_input(&av_context_);

}


int SenderRtsp::init(){

    printf("starting sender...\n");

    //const char *url = "rtsp://rtsp://138.4.13.92:8554/ej";
   // url = "rtsp://test:password@192.168.33.19:1935/ffmpeg/0";


    av_log_set_level(AV_LOG_DEBUG);
    av_log_set_level(AV_LOG_TRACE);

    av_register_all();
    avformat_network_init();

    static const char* output_formats[] = { NULL, "mp3", "ogg", "avi",
"rtsp" };


    ret = avformat_alloc_output_context2(&outContext, NULL ,
output_formats[4], input_url_.c_str()); //Asignamos contexto de SALIDA
    if (ret < 0) {
        av_log(NULL, AV_LOG_FATAL, "Could not allocate an output context
for '%s'.\n",  input_url_.c_str());

    }else{
         ELOG_DEBUG("Allocated an output context for %s .\n",
input_url_.c_str());
       //  goto end;
    }




    av_dump_format(outContext, 0, input_url_.c_str(), 1);

    if (!outContext->oformat) {
        av_log(NULL, AV_LOG_FATAL, "Could not create the output format for
'%s'.\n",  input_url_.c_str());

    }else{
         ELOG_DEBUG("Created an output context.\n");
       // goto end;
    }

        // open output file to write to it.
    // ret = avio_open2(&outContext->pb, url, AVIO_FLAG_WRITE, NULL, NULL);
    // if (ret < 0) {
    //     av_log(NULL, AV_LOG_FATAL, "Could not open output file to write
to it.\n");

    // }else{
    //      ELOG_DEBUG("Allocated an output context.\n");
    // }



 //   av_dump_format(outContext, 0, url, 1);
    ELOG_DEBUG("Conecting to RTSP server for '%s'.\n", input_url_.c_str());
    ret = avformat_write_header(outContext, NULL);
    if (ret != 0) {
        av_log(NULL, AV_LOG_ERROR, "Failed to connect to RTSP server for
%s.\n",input_url_.c_str());
        //goto end;
    }else{
         ELOG_DEBUG("Conected to RTSP server for '%s'.\n",
input_url_.c_str());
    }

  //  sendFrame(outContext,video_st);

    return 1;
}
void SenderRtsp::receiveRtpPacket(unsigned char* inBuff, int buffSize){
return;
}


void SenderRtsp::sendPacket(AVPacket& pkt){

    printf("Press any key to start streaming...\n");
    getchar();

   // ELOG_DEBUG("Trying to send packet of %d bytes", pkt.size);

    auto startSend = std::chrono::system_clock::now();

    while (pkt.size != NULL ) {
        frameCount++;
        auto startFrame = std::chrono::system_clock::now();


        pkt.stream_index = 0;
        ret = av_write_frame(outContext, &pkt);// Manda el frame
         if (ret < 0) {
                av_log(NULL, AV_LOG_ERROR, "Error while writing video
frame.\n");
         }

        auto streamDuration =
std::chrono::duration_cast<chrono::milliseconds>(std::chrono::system_clock::now()
- startSend).count();

       // printf("Elapsed time %ldms, video stream pts %ld.\n",
streamDuration, video_st->pts.val);

        if (streamDuration / 1000.0 > STREAM_DURATION) {
            break;
        }
        else {
            auto frameDuration =
std::chrono::duration_cast<chrono::milliseconds>(std::chrono::system_clock::now()
- startFrame).count();

std::this_thread::sleep_for(std::chrono::milliseconds((long)(1000.0 /
STREAM_FRAME_RATE - frameDuration)));
        }
    }

    // if (video_st) {
    //     avcodec_close(video_st->codec);
    //     av_free(src_picture.data[0]);
    //     av_free(dst_picture.data[0]);
    //     av_frame_free(&frame);
    // }

    avformat_free_context(outContext);

end:
    printf("finished.\n");

    getchar();
}


}   // Namespace payloader
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20171205/76fdf9be/attachment.html>


More information about the Libav-user mailing list