使用rtmp协议推送H264裸码流

本文主要是基于srs开放实现的rtmp协议源码进行推送H264裸码流。

1、建立rtmp连接(初始化阶段)

const char* rtmp_url = "rtmp://172.19.33.115:1935/live/livestream";
srs_rtmp_t rtmp;
{
    //目前一路 进行url创建和握手
	rtmp = srs_rtmp_create(rtmp_url);

	if (srs_rtmp_handshake(rtmp) != 0) {
        MessageBox("simple handshake failed.");
        //goto rtmp_destroy;
    }
    srs_human_trace("simple handshake success");
    
    if (srs_rtmp_connect_app(rtmp) != 0) {
        MessageBox("connect vhost/app failed.");
        //goto rtmp_destroy;
    }
    srs_human_trace("connect vhost/app success");
    
    if (srs_rtmp_publish_stream(rtmp) != 0) {
        MessageBox("publish stream failed.");
        //goto rtmp_destroy;
    }
    srs_human_trace("publish stream success");
}

2、发送裸流码

{
    static int pts = 0;
	static int dts = 0;
	
	if (pRawFrameInfo->uiFr
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RTMP(Real-Time Messaging Protocol)是一种用于音频、视频和数据传输的通信协议,通常用于媒体传输。 在RTMP推送H264和AAC数据之前,需要先准备好H264和AAC数据。以下是RTMP推送H264和AAC数据的步骤: 1. 建立RTMP连接 使用RTMP协议的应用程序需要首先建立到媒体服务器的连接。可以使用第三方库如librtmp来实现。连接的过程中需要设置一些连接参数,如服务器地址、端口号、应用程序名称、名称等。 2. 发送RTMP元数据 在发送音视频数据前,需要先发送一些元数据,用于描述音视频数据的格式及相关信息。RTMP协议中用AMF0或AMF3格式来传递元数据信息。例如,可以发送视频的宽度、高度、帧率、编码格式等信息。 3. 发送H264视频数据 发送H264视频数据时,需要将视频数据封装为FLV格式。FLV格式是一种常用的媒体视频格式,由头部、tag和数据三部分组成。其中,tag包含了视频的时间戳和数据大小等信息。每个tag的前4个字节表示tag类型,其中8表示视频tag类型。 4. 发送AAC音频数据 发送AAC音频数据也需要将音频数据封装为FLV格式。每个tag的前4个字节表示tag类型,其中10表示音频tag类型。音频tag中需要包含音频的时间戳和数据大小等信息。 以下是一个推送H264和AAC数据的示例代码: ``` AVFormatContext *fmt_ctx; AVCodecContext *video_enc_ctx, *audio_enc_ctx; AVOutputFormat *ofmt; AVStream *video_stream, *audio_stream; AVPacket video_pkt, audio_pkt; int video_frame_count = 0, audio_frame_count = 0; // 初始化FFmpeg av_register_all(); avformat_network_init(); // 打开输出 avformat_alloc_output_context2(&fmt_ctx, NULL, "flv", "rtmp://server/live/stream"); ofmt = fmt_ctx->oformat; if (avio_open(&fmt_ctx->pb, fmt_ctx->filename, AVIO_FLAG_WRITE) < 0) { fprintf(stderr, "Failed to open output file\n"); exit(1); } // 添加视频和音频 video_stream = avformat_new_stream(fmt_ctx, NULL); audio_stream = avformat_new_stream(fmt_ctx, NULL); video_enc_ctx = video_stream->codec; audio_enc_ctx = audio_stream->codec; // 设置视频编码器 video_enc_ctx->codec_id = AV_CODEC_ID_H264; video_enc_ctx->bit_rate = 400000; video_enc_ctx->width = 640; video_enc_ctx->height = 480; video_enc_ctx->time_base = (AVRational){1, 25}; video_enc_ctx->gop_size = 10; video_stream->time_base = video_enc_ctx->time_base; // 设置音频编码器 audio_enc_ctx->codec_id = AV_CODEC_ID_AAC; audio_enc_ctx->bit_rate = 64000; audio_enc_ctx->sample_rate = 44100; audio_enc_ctx->channels = 2; audio_stream->time_base = (AVRational){1, audio_enc_ctx->sample_rate}; // 写入文件头 avformat_write_header(fmt_ctx, NULL); // 发送视频元数据 AVDictionary *video_options = NULL; av_dict_set(&video_options, "profile", "main", 0); av_dict_set(&video_options, "preset", "medium", 0); av_dict_set(&video_options, "tune", "zerolatency", 0); av_dict_set(&video_options, "crf", "23", 0); av_dict_set(&video_options, "level", "4.0", 0); av_dict_set(&video_options, "refs", "4", 0); av_dict_set(&video_options, "bufsize", "2M", 0); av_dict_set(&video_options, "maxrate", "400k", 0); av_dict_set(&video_options, "zerolatency", "1", 0); av_dict_set(&video_options, "threads", "4", 0); av_dict_set(&video_options, "bframes", "0", 0); av_dict_set(&video_options, "slices", "8", 0); avformat_write_header(fmt_ctx, &video_options); // 发送音频元数据 AVDictionary *audio_options = NULL; av_dict_set(&audio_options, "profile", "aac_low", 0); av_dict_set(&audio_options, "strict", "-2", 0); avformat_write_header(fmt_ctx, &audio_options); // 发送视频数据 while (1) { // 生成视频帧 AVFrame *video_frame = av_frame_alloc(); video_frame->width = video_enc_ctx->width; video_frame->height = video_enc_ctx->height; video_frame->format = AV_PIX_FMT_YUV420P; av_image_alloc(video_frame->data, video_frame->linesize, video_frame->width, video_frame->height, video_frame->format, 32); // 编码视频帧 avcodec_send_frame(video_enc_ctx, video_frame); while (avcodec_receive_packet(video_enc_ctx, &video_pkt) == 0) { // 将视频数据封装为FLV格式 video_pkt.pts = av_rescale_q(video_pkt.pts, video_enc_ctx->time_base, video_stream->time_base); video_pkt.dts = av_rescale_q(video_pkt.dts, video_enc_ctx->time_base, video_stream->time_base); video_pkt.duration = av_rescale_q(video_pkt.duration, video_enc_ctx->time_base, video_stream->time_base); video_pkt.stream_index = video_stream->index; av_interleaved_write_frame(fmt_ctx, &video_pkt); av_packet_unref(&video_pkt); } // 发送音频数据 AVFrame *audio_frame = av_frame_alloc(); audio_frame->nb_samples = audio_enc_ctx->frame_size; audio_frame->format = audio_enc_ctx->sample_fmt; audio_frame->channel_layout = audio_enc_ctx->channel_layout; av_frame_get_buffer(audio_frame, 0); // 填充音频数据 ... // 编码音频帧 avcodec_send_frame(audio_enc_ctx, audio_frame); while (avcodec_receive_packet(audio_enc_ctx, &audio_pkt) == 0) { // 将音频数据封装为FLV格式 audio_pkt.pts = av_rescale_q(audio_pkt.pts, audio_enc_ctx->time_base, audio_stream->time_base); audio_pkt.dts = av_rescale_q(audio_pkt.dts, audio_enc_ctx->time_base, audio_stream->time_base); audio_pkt.duration = av_rescale_q(audio_pkt.duration, audio_enc_ctx->time_base, audio_stream->time_base); audio_pkt.stream_index = audio_stream->index; av_interleaved_write_frame(fmt_ctx, &audio_pkt); av_packet_unref(&audio_pkt); } // 释放帧内存 av_frame_free(&video_frame); av_frame_free(&audio_frame); } // 写入文件尾 av_write_trailer(fmt_ctx); // 关闭输出 avio_close(fmt_ctx->pb); // 释放FFmpeg资源 avformat_free_context(fmt_ctx); ``` 注意,以上代码仅为示例,实际情况下需要根据具体的视频编码器和音频编码器来设置编码参数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值