rtmp数据封装四-音频(AAC&G711)

前面文章《rtmp数据封装一-块(chunk)》中讲到rtmp传输媒体数据时是把一整帧数据拆分成chunk的形式发送的。其实一帧媒体数据在拆分成chunk之前还需要进行必要的头部信息封装,本文介绍rtmp协议中AAC/G.711音频数据的头部信息封装。

第一个byte包含音频的编码参数:

  • 1-4bit: audioCodeId
  • 5-6bit: 采样率 00 5.5KHZ, 01 11KHZ, 10 22KHZ, 11 44KHZ
  • 7 bit: 采样长度 0 8bit, 1 16bit
  • 8 bit: 立体声 0 单声道, 1 双声道

AAC rtmp头部信息封装

第一帧 AAC sequence header

第一帧共4个byte:

1) 第1个byte : audioCodeId=10,如果是44KHZ、16bit、双声道,第一个byte是0xAF。如果实际采样率不是5.5KHZ、11KHZ、22KHZ、44KHZ,就选一个接近的。

2) 第2个byte : 0x00 表示是sequence header

3) 第3-4个byte : 0x14,0x10

其他帧 AAC raw data

1) 第1个byte : audioCodeId=10,如果是44KHZ、16bit、双声道,第一个byte是0xAF。如果实际采样率不是5.5KHZ、11KHZ、22KHZ、44KHZ,就选一个接近的。

2) 第2个byte : 0x01 表示是raw data

3) 第3byte开始 : 去掉前7个byte的AAC头之后的AAC数据。

  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
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); ``` 注意,以上代码仅为示例,实际情况下需要根据具体的视频编码器和音频编码器来设置编码参数。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值