Sending raw h264 video and aac audio frames to an RTMP server using ffmpeg
<p>I am  receiving raw h264 and aac audio frames from an even driven source. I am trying to send these frames to an rtmp server. 
I started working from the ffmpeg example muxing.c which successfully sends a custom stream to the rtmp server. I figure I just need to replace their frame data with my own.I found this suggestion online. I have tried <a href="https://stackoverflow.com/questions/38098140/how-to-pack-raw-h264-stream-to-flv-container-and-send-over-rtmp-using-ffmpeg-no">How to pack raw h264 stream to flv container and send over rtmp using ffmpeg (not command)</a> 
and
<a href="https://stackoverflow.com/questions/15143918/how-to-publish-selfmade-stream-with-ffmpeg-and-c-to-rtmp-server">How to publish selfmade stream with ffmpeg and c++ to rtmp server?</a>
and a few other suggestions but none have worked for me. 
I have tried to directly memcpy my byte buffer but my code keeps failing
at ret = avcodec_encode_video2(c, &amp;pkt, frame, &amp;got_packet).
Specifically, I get an invalid access error.
For a little more context, anytime I receive a frame (which is event driven), void RTMPWriter::WriteVideoFrame(...) is called. Assume the constructor has already been called before the first frame is received. 
I am not that familiar with ffmpeg and there could be several things wrong with the code.  Any input will be really appreciated. </p>

<pre><code>    #define STREAM_FRAME_RATE 25 /* 25 images/s */
    #define STREAM_PIX_FMT    AV_PIX_FMT_YUV420P /* default pix_fmt */
    #define SCALE_FLAGS SWS_BICUBIC
    RTMPWriter::RTMPWriter()
      : seenKeyFrame(false),
        video_st({ 0 }), 
        audio_st({ 0 }),
        have_video(0), 
        have_audio(0)
    {

        const char *filename;
        AVCodec *audio_codec = NULL, *video_codec = NULL;
        int ret;

        int encode_video = 0, encode_audio = 0;
        AVDictionary *opt = NULL;
        int i;

        /* Initialize libavcodec, and register all codecs and formats. */
        av_register_all();

        avformat_network_init();

       String^ StreamURL = "StreamURL";
       String^ out_uri = safe_cast&lt;Platform::String^&gt;(ApplicationData::Current-&gt;LocalSettings-&gt;Values-&gt;Lookup(StreamURL));
       std::wstring out_uriW(out_uri-&gt;Begin());
       std::string out_uriA(out_uriW.begin(), out_uriW.end());
       filename = out_uriA.c_str();  

       /* allocate the output media context */
       avformat_alloc_output_context2(&amp;oc, NULL, "flv", filename);
       if (!oc)
       {
           OutputDebugString(L"Could not deduce output format from file extension: using MPEG.\n");
           avformat_alloc_output_context2(&amp;oc, NULL, "mpeg", filename);
       }
       if (!oc)
       {
           OutputDebugString(L"Could not allocate  using MPEG.\n");
       }


       fmt = oc-&gt;oformat;

       /* Add the audio and video streams using the default format codecs
       * and initialize the codecs. */
       if (fmt-&gt;video_codec != AV_CODEC_ID_NONE) {
           add_stream(&amp;video_st, oc, &amp;video_codec, fmt-&gt;video_codec);
           have_video = 1;
           encode_video = 1;
       }
       if (fmt-&gt;audio_codec != AV_CODEC_ID_NONE) {
           add_stream(&amp;audio_st, oc, &amp;audio_codec, fmt-&gt;audio_codec);
           have_audio = 1;
           encode_audio = 1;
       }

       /* Now that all the parameters are set, we can open the audio and
        * video codecs and allocate the necessary encode buffers. */
       if (have_video)
       {
           open_video(oc, video_codec, &amp;video_st, opt);
       }

       if (have_audio)
       {
           open_audio(oc, audio_codec, &amp;audio_st, opt);
       }

       av_dump_format(oc, 0, filename, 1);

       /* open the output file, if needed */
       if (!(fmt-&gt;flags &amp; AVFMT_NOFILE))
       {
           ret = avio_open(&amp;oc-&gt;pb, filename, AVIO_FLAG_WRITE);
           if (ret &lt; 0)
           {
               OutputDebugString(L"Could not open ");
               OutputDebugString(out_uri-&gt;Data());
           }
       }

       /* Write the stream header, if any. */
       ret = avformat_write_header(oc, &amp;opt);
       if (ret &lt; 0)
       {
           OutputDebugString(L"Error occurred when writing stream header \n");
       }

    }

    void RTMPWriter::WriteVideoFrame(
        boolean isKeyFrame,
        boolean hasDiscontinuity,
        UINT64 frameId,
        UINT32 videoBufferLength,
        BYTE *videoBytes)
    {

        int ret;
        AVCodecContext *c;
        AVFrame* frame;
        int got_packet = 0;
        AVPacket pkt = { 0 };

        c = video_st.enc;

        frame = get_video_frame(videoBufferLength, videoBytes);

        /* encode the image */
        ret = avcodec_encode_video2(c, &amp;pkt, frame, &amp;got_packet);
        if (ret &lt; 0) {
             OutputDebugString(L"Error encoding video frame: \n")
        }

        if (got_packet) 
        {
            ret = write_frame(oc, &amp;c-&gt;time_base, video_st.st, &amp;pkt);
        }
        else {
            ret = 0;
        }

        if (ret &lt; 0) {
             OutputDebugString(L"Error while writing video frame: %s\n");
        }
    }

    AVFrame * RTMPWriter::get_video_frame(
       UINT32 videoBufferLength,
       BYTE *videoBytes)
    {
        AVCodecContext *c = video_st.enc;

        if (c-&gt;pix_fmt != AV_PIX_FMT_YUV420P) {
            /* as we only generate a YUV420P picture, we must convert it
            * to the codec pixel format if needed */
            if (!video_st.sws_ctx) {
                video_st.sws_ctx = sws_getContext(c-&gt;width, c-&gt;height,
                    AV_PIX_FMT_YUV420P,
                    c-&gt;width, c-&gt;height,
                    c-&gt;pix_fmt,
                    SCALE_FLAGS, NULL, NULL, NULL);
                if (!video_st.sws_ctx) {
                    fprintf(stderr,
                        "Could not initialize the conversion context\n");
                        exit(1);
                }
            }
            fill_yuv_image(video_st.tmp_frame, video_st.next_pts, c-&gt;width, c-&gt;height, videoBufferLength, videoBytes);
            sws_scale(video_st.sws_ctx,
            (const uint8_t * const *)video_st.tmp_frame-&gt;data, video_st.tmp_frame-&gt;linesize,
            0, c-&gt;height, video_st.frame-&gt;data, video_st.frame-&gt;linesize);
        }
        else {
            fill_yuv_image(video_st.frame, video_st.next_pts, c-&gt;width, c-&gt;height, videoBufferLength, videoBytes);
        }

        video_st.frame-&gt;pts = video_st.next_pts++;

        return video_st.frame;
    }

    /* Prepare a dummy image. */
    void  RTMPWriter::fill_yuv_image(
         AVFrame *pict, 
         int frame_index,
         int width, 
         int height, 
         UINT32 videoBufferLength,
         BYTE *videoBytes)
    {
        //int x, y, i, ret;

        /* when we pass a frame to the encoder, it may keep a reference to it
        * internally;
        * make sure we do not overwrite it here
        */
        ret = av_frame_make_writable(pict);
        if (ret &lt; 0) 
        {
             OutputDebugString(L"Unable to make piture writable");
        }

        memcpy(pict-&gt;data, videoBytes, videoBufferLength);

        //i = frame_index;

        ///* Y */
        //for (y = 0; y &lt; height; y++)
        //  for (x = 0; x &lt; width; x++)
        //      pict-&gt;data[0][y * pict-&gt;linesize[0] + x] = x + y + i * 3;

        ///* Cb and Cr */
        //for (y = 0; y &lt; height / 2; y++) {
        //  for (x = 0; x &lt; width / 2; x++) {
        //      pict-&gt;data[1][y * pict-&gt;linesize[1] + x] = 128 + y + i * 2;
        //      pict-&gt;data[2][y * pict-&gt;linesize[2] + x] = 64 + x + i * 5;
        //  }
        //}
    }

    void RTMPWriter::WriteAudioFrame()
    {

    }

    /* Add an output stream. */
    void  RTMPWriter::add_stream(
        OutputStream *ost, 
        AVFormatContext *oc,
        AVCodec **codec,
        enum AVCodecID codec_id)
   {
    AVCodecContext *c;
    int i;

    /* find the encoder */
    *codec = avcodec_find_encoder(codec_id);
    if (!(*codec)) {
        OutputDebugString(L"Could not find encoder for '%s'\n");
        //avcodec_get_name(codec_id));
        exit(1);
    }

    ost-&gt;st = avformat_new_stream(oc, NULL);
    if (!ost-&gt;st) {
        OutputDebugString(L"Could not allocate stream\n");
        exit(1);
    }
    ost-&gt;st-&gt;id = oc-&gt;nb_streams - 1;
    c = avcodec_alloc_context3(*codec);
    if (!c) {
        OutputDebugString(L"Could not alloc an encoding context\n");
        exit(1);
    }
    ost-&gt;enc = c;

    switch ((*codec)-&gt;type) {
    case AVMEDIA_TYPE_AUDIO:
        c-&gt;sample_fmt = (*codec)-&gt;sample_fmts ?
            (*codec)-&gt;sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
        c-&gt;bit_rate = 64000;
        c-&gt;sample_rate = 44100;
        if ((*codec)-&gt;supported_samplerates) {
            c-&gt;sample_rate = (*codec)-&gt;supported_samplerates[0];
            for (i = 0; (*codec)-&gt;supported_samplerates[i]; i++) {
                if ((*codec)-&gt;supported_samplerates[i] == 44100)
                    c-&gt;sample_rate = 44100;
            }
        }
        c-&gt;channels = av_get_channel_layout_nb_channels(c-&gt;channel_layout);
        c-&gt;channel_layout = AV_CH_LAYOUT_STEREO;
        if ((*codec)-&gt;channel_layouts) {
            c-&gt;channel_layout = (*codec)-&gt;channel_layouts[0];
            for (i = 0; (*codec)-&gt;channel_layouts[i]; i++) {
                if ((*codec)-&gt;channel_layouts[i] == AV_CH_LAYOUT_STEREO)
                    c-&gt;channel_layout = AV_CH_LAYOUT_STEREO;
            }
        }
        c-&gt;channels = av_get_channel_layout_nb_channels(c-&gt;channel_layout);
        ost-&gt;st-&gt;time_base = /*(AVRational)*/{ 1, c-&gt;sample_rate };
        break;

    case AVMEDIA_TYPE_VIDEO:
        c-&gt;codec_id = codec_id;

        c-&gt;bit_rate = 400000;
        /* Resolution must be a multiple of two. */
        c-&gt;width = 352;
        c-&gt;height = 288;
        /* timebase: This is the fundamental unit of time (in seconds) in terms
        * of which frame timestamps are represented. For fixed-fps content,
        * timebase should be 1/framerate and timestamp increments should be
        * identical to 1. */
        ost-&gt;st-&gt;time_base = /*(AVRational)*/{ 1, STREAM_FRAME_RATE };
        c-&gt;time_base = ost-&gt;st-&gt;time_base;

        c-&gt;gop_size = 12; /* emit one intra frame every twelve frames at most */
        c-&gt;pix_fmt = STREAM_PIX_FMT;
            if (c-&gt;codec_id == AV_CODEC_ID_MPEG2VIDEO) {
                /* just for testing, we also add B-frames */
                c-&gt;max_b_frames = 2;
            }
            if (c-&gt;codec_id == AV_CODEC_ID_MPEG1VIDEO) {
                /* Needed to avoid using macroblocks in which some coeffs overflow.
                * This does not happen with normal video, it just happens here as
                * the motion of the chroma plane does not match the luma plane. */
                c-&gt;mb_decision = 2;
            }
            break;

        default:
            break;
        }

         /* Some formats want stream headers to be separate. */
        if (oc-&gt;oformat-&gt;flags &amp; AVFMT_GLOBALHEADER)
            c-&gt;flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
    }

AVFrame * RTMPWriter::alloc_audio_frame(
    enum AVSampleFormat sample_fmt,
    uint64_t channel_layout,
    int sample_rate, int nb_samples)
{
    AVFrame *frame = av_frame_alloc();
    int ret;

    if (!frame) {
        OutputDebugString(L"Error allocating an audio frame\n");
        exit(1);
    }

    frame-&gt;format = sample_fmt;
    frame-&gt;channel_layout = channel_layout;
    frame-&gt;sample_rate = sample_rate;
    frame-&gt;nb_samples = nb_samples;

    if (nb_samples) {
        ret = av_frame_get_buffer(frame, 0);
        if (ret &lt; 0) {
            OutputDebugString(L"Error allocating an audio buffer\n");
            exit(1);
        }
    }

        return frame;
    }




void  RTMPWriter::open_audio(
    AVFormatContext *oc, 
    AVCodec *codec, 
    OutputStream *ost, 
    AVDictionary *opt_arg)
{
    AVCodecContext *c;
    int nb_samples;
    int ret;
    AVDictionary *opt = NULL;

    c = ost-&gt;enc;

    /* open it */
    av_dict_copy(&amp;opt, opt_arg, 0);
    ret = avcodec_open2(c, codec, &amp;opt);
    av_dict_free(&amp;opt);
    if (ret &lt; 0) {
        OutputDebugString(L"Could not open audio codec: %s\n");// , av_err2str(ret));
        exit(1);
    }

    /* init signal generator */
    ost-&gt;t = 0;
    ost-&gt;tincr = 2 * M_PI * 110.0 / c-&gt;sample_rate;
    /* increment frequency by 110 Hz per second */
    ost-&gt;tincr2 = 2 * M_PI * 110.0 / c-&gt;sample_rate / c-&gt;sample_rate;

    if (c-&gt;codec-&gt;capabilities &amp; AV_CODEC_CAP_VARIABLE_FRAME_SIZE)
        nb_samples = 10000;
    else
        nb_samples = c-&gt;frame_size;

    ost-&gt;frame = alloc_audio_frame(c-&gt;sample_fmt, c-&gt;channel_layout,
        c-&gt;sample_rate, nb_samples);
    ost-&gt;tmp_frame = alloc_audio_frame(AV_SAMPLE_FMT_S16, c-&gt;channel_layout,
        c-&gt;sample_rate, nb_samples);

    /* copy the stream parameters to the muxer */
    ret = avcodec_parameters_from_context(ost-&gt;st-&gt;codecpar, c);
    if (ret &lt; 0) {
        OutputDebugString(L"Could not copy the stream parameters\n");
        exit(1);
    }

    /* create resampler context */
    ost-&gt;swr_ctx = swr_alloc();
    if (!ost-&gt;swr_ctx) {
        OutputDebugString(L"Could not allocate resampler context\n");
        exit(1);
    }

    /* set options */
    av_opt_set_int(ost-&gt;swr_ctx, "in_channel_count", c-&gt;channels, 0);
    av_opt_set_int(ost-&gt;swr_ctx, "in_sample_rate", c-&gt;sample_rate, 0);
    av_opt_set_sample_fmt(ost-&gt;swr_ctx, "in_sample_fmt", AV_SAMPLE_FMT_S16, 0);
    av_opt_set_int(ost-&gt;swr_ctx, "out_channel_count", c-&gt;channels, 0);
    av_opt_set_int(ost-&gt;swr_ctx, "out_sample_rate", c-&gt;sample_rate, 0);
    av_opt_set_sample_fmt(ost-&gt;swr_ctx, "out_sample_fmt", c-&gt;sample_fmt, 0);

    /* initialize the resampling context */
    if ((ret = swr_init(ost-&gt;swr_ctx)) &lt; 0) {
        OutputDebugString(L"Failed to initialize the resampling context\n");
        exit(1);
    }
}

int RTMPWriter::write_frame(
    AVFormatContext *fmt_ctx, 
    const AVRational *time_base, 
    AVStream *st, 
    AVPacket *pkt)
{
    /* rescale output packet timestamp values from codec to stream timebase */
    av_packet_rescale_ts(pkt, *time_base, st-&gt;time_base);
    pkt-&gt;stream_index = st-&gt;index;

    /* Write the compressed frame to the media file. */
    //log_packet(fmt_ctx, pkt);
    OutputDebugString(L"Actually sending video frame: %s\n");
    return av_interleaved_write_frame(fmt_ctx, pkt);
}


AVFrame  *RTMPWriter::alloc_picture(
    enum AVPixelFormat pix_fmt, 
    int width, 
    int height)
{
    AVFrame *picture;
    int ret;

    picture = av_frame_alloc();
    if (!picture)
        return NULL;

    picture-&gt;format = pix_fmt;
    picture-&gt;width = width;
    picture-&gt;height = height;

    /* allocate the buffers for the frame data */
    ret = av_frame_get_buffer(picture, 32);
    if (ret &lt; 0) {
        fprintf(stderr, "Could not allocate frame data.\n");
        exit(1);
    }

    return picture;
}

void RTMPWriter::open_video(
    AVFormatContext *oc, 
    AVCodec *codec, 
    OutputStream *ost, 
    AVDictionary *opt_arg)
{
    int ret;
    AVCodecContext *c = ost-&gt;enc;
    AVDictionary *opt = NULL;

    av_dict_copy(&amp;opt, opt_arg, 0);

    /* open the codec */
    ret = avcodec_open2(c, codec, &amp;opt);
    av_dict_free(&amp;opt);
    if (ret &lt; 0) {
        OutputDebugString(L"Could not open video codec: %s\n");// , av_err2str(ret));
        exit(1);
    }

    /* allocate and init a re-usable frame */
    ost-&gt;frame = alloc_picture(c-&gt;pix_fmt, c-&gt;width, c-&gt;height);
    if (!ost-&gt;frame) {
        OutputDebugString(L"Could not allocate video frame\n");
        exit(1);
    }

    /* If the output format is not YUV420P, then a temporary YUV420P
    * picture is needed too. It is then converted to the required
    * output format. */
    ost-&gt;tmp_frame = NULL;
    if (c-&gt;pix_fmt != AV_PIX_FMT_YUV420P) {
        ost-&gt;tmp_frame = alloc_picture(AV_PIX_FMT_YUV420P, c-&gt;width, c-&gt;height);
        if (!ost-&gt;tmp_frame) {
            OutputDebugString(L"Could not allocate temporary picture\n");
            exit(1);
        }
    }

    /* copy the stream parameters to the muxer */
    ret = avcodec_parameters_from_context(ost-&gt;st-&gt;codecpar, c);
    if (ret &lt; 0) {
        OutputDebugString(L"Could not copy the stream parameters\n");
        exit(1);
    }
}

void RTMPWriter::close_stream(AVFormatContext *oc, OutputStream *ost)
{
    avcodec_free_context(&amp;ost-&gt;enc);
    av_frame_free(&amp;ost-&gt;frame);
    av_frame_free(&amp;ost-&gt;tmp_frame);
    sws_freeContext(ost-&gt;sws_ctx);
    swr_free(&amp;ost-&gt;swr_ctx);
}

RTMPWriter::~RTMPWriter()
{
    av_write_trailer(oc);
    /* Close each codec. */
    if (have_video)
        close_stream(oc, &amp;video_st);
    if (have_audio)
        close_stream(oc, &amp;audio_st);

    if (!(fmt-&gt;flags &amp; AVFMT_NOFILE))
        /* Close the output file. */
        avio_closep(&amp;oc-&gt;pb);

    /* free the stream */
    avformat_free_context(oc);
}
</code></pre>