前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ffmpeg android 推送RTMP

ffmpeg android 推送RTMP

作者头像
曾大稳
发布2018-09-11 10:33:57
9790
发布2018-09-11 10:33:57
举报
文章被收录于专栏:曾大稳的博客
  1. Nginx 搭建RTMP服务器
  2. android端代码以及步骤
代码语言:javascript
复制

static int publish_file_stream(const char *input, const char *output) {
//注意这里要写=NULL  不然会崩溃
    AVFormatContext *pInAvFormatContext = NULL;
    AVFormatContext *pOutAvFormatContext = NULL;
    AVOutputFormat *pAvOutputFormat = NULL;
    int videoIndex = 0;

    //1. 注册
    av_register_all();
    LOGE("%s", output);

    //2. 获取输入的文件信息

    //打开文件
    if (avformat_open_input(&pInAvFormatContext, input, NULL, NULL) != 0) {
        LOGE("打开文件失败!");
        return -1;
    }

    //获取流信息
    if (avformat_find_stream_info(pInAvFormatContext, NULL) < 0) {
        LOGE("获取文件流失败!");
        return -1;
    }
    //找到video的对应位置
    int i = 0;
    for (; i < pInAvFormatContext->nb_streams; i++) {
        if (pInAvFormatContext->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
            videoIndex = i;
            break;
        }
    }

    //打印信息
//    av_dump_format(pAvFormatContext,0,input,0);

    //3. 输出设置
    //初始化输出AVFormatContext获取 AVOutputFormat进行设置
    avformat_alloc_output_context2(&pOutAvFormatContext, NULL, "flv", output);
    if (!pOutAvFormatContext) {
        LOGE("初始化输出AVFormatContext失败");
        return -1;
    }

    pAvOutputFormat = pOutAvFormatContext->oformat;
    i = 0;
    for (; i < pInAvFormatContext->nb_streams; i++) {
        AVStream *in_stream = pInAvFormatContext->streams[i];
        AVStream *out_stream = avformat_new_stream(pOutAvFormatContext, in_stream->codec->codec);
        if (!out_stream) {
            LOGE("初始化out_stream失败");
            return -1;
        }
        //复制AVCodecContext的设置(Copy the settings of AVCodecContext)
        if (avcodec_copy_context(out_stream->codec, in_stream->codec) != 0) {
            LOGE("copy AVCodecContext设置失败");
            return -1;
        }

        out_stream->codec->codec_tag = 0;
        if (pOutAvFormatContext->oformat->flags & AVFMT_GLOBALHEADER)
            out_stream->codec->flags |= CODEC_FLAG_GLOBAL_HEADER;
    }

    //打印
//        av_dump_format(pOutAvFormatContext,0,output,0);
    //打开输出文件/url
    if (!(pAvOutputFormat->flags & AVFMT_NOFILE)) {
        int ret = avio_open(&pOutAvFormatContext->pb, output, AVIO_FLAG_WRITE);
        if (ret < 0) {
            LOGE("打开输出文件或者url失败  %d", ret);
            return -1;
        }
    }

    //4.写入数据
    //写文件头
    if (avformat_write_header(pOutAvFormatContext, NULL) != 0) {
        LOGE("写入头数据失败");
        return -1;
    }
    int64_t start_time = av_gettime();
    AVPacket pkt;
    int frameIndex = 0;

    //写入数据源
    while (av_read_frame(pInAvFormatContext, &pkt) >= 0) {
        AVStream *in_stream, *out_stream;
        if (pkt.pts == AV_NOPTS_VALUE) {
            //Write PTS
            AVRational time_base1 = pInAvFormatContext->streams[videoIndex]->time_base;
            //Duration between 2 frames (us)
            int64_t calc_duration = (double) AV_TIME_BASE /
                                    av_q2d(pInAvFormatContext->streams[videoIndex]->r_frame_rate);
            //Parameters
            pkt.pts = (double) (frameIndex * calc_duration) /
                      (double) (av_q2d(time_base1) * AV_TIME_BASE);
            pkt.dts = pkt.pts;
            pkt.duration = (double) calc_duration / (double) (av_q2d(time_base1) * AV_TIME_BASE);
        }

        //Important:Delay
        if (pkt.stream_index == videoIndex) {
            AVRational time_base = pInAvFormatContext->streams[videoIndex]->time_base;
            AVRational time_base_q = {1, AV_TIME_BASE};
            int64_t pts_time = av_rescale_q(pkt.dts, time_base, time_base_q);
            int64_t now_time = av_gettime() - start_time;
            if (pts_time > now_time)
                av_usleep(pts_time - now_time);

        }

        in_stream = pInAvFormatContext->streams[pkt.stream_index];
        out_stream = pOutAvFormatContext->streams[pkt.stream_index];

        /* copy packet */
        //转换PTS/DTS(Convert PTS/DTS)
        pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream->time_base, out_stream->time_base,
                                   (AVRounding) (AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
        pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream->time_base, out_stream->time_base,
                                   (AVRounding) (AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
        pkt.duration = av_rescale_q(pkt.duration, in_stream->time_base, out_stream->time_base);
        pkt.pos = -1;
        //Print to Screen
        if (pkt.stream_index == videoIndex) {
            LOGE("Send %8d video frames to output URL\n", frameIndex);
            frameIndex++;
        }

        if (av_interleaved_write_frame(pOutAvFormatContext, &pkt) < 0) {
            LOGE("Error muxing packet\n");
            break;
        }

        av_packet_unref(&pkt);
    }


    //写文件尾(Write file trailer)
    av_write_trailer(pOutAvFormatContext);
    avformat_close_input(&pInAvFormatContext);
    /* close output */
    if (pOutAvFormatContext && !(pAvOutputFormat->flags & AVFMT_NOFILE))
        avio_close(pOutAvFormatContext->pb);
    avformat_free_context(pOutAvFormatContext);
    return 0;
}

JNIEXPORT jint JNICALL
Java_zzw_com_ffmpegdemo_VideoUtils_publish_1file_1stream(JNIEnv *env, jclass type, jstring input_,
                                                         jstring output_) {
    const char *input = env->GetStringUTFChars(input_, 0);
    const char *output = env->GetStringUTFChars(output_, 0);
    //input:  /storage/emulated/0/aaaaa/dst.mp4 
    //output:  rtmp://192.168.18.231:8082/live/room
    int ret = publish_file_stream(input, output);
    env->ReleaseStringUTFChars(input_, input);
    env->ReleaseStringUTFChars(output_, output);
    return ret;
}

参考连接:

https://blog.csdn.net/leixiaohua1020/article/details/39803457

-------------The End-------------

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-05-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档