QByteArray::data()返回值解析,结构体中char*赋值崩溃

这是一个在使用QByteArray时遇到的bug分析记录

情景
  • 在使用QTcpSocket编程时,使用了自定义的结构体进行数据通信,结构体如下,其中,char* load是消息体的地址。在实际使用过程中,使用readALL读取数据流后,进行分包和组包。在使用char* QByteArray::data 接口对load进行赋值,程序会崩溃。
typedef struct
{
   
    unsigned int send_id;       // 发送方的ID(网络的IP、SRIO的ID)
    unsigned int recv_id;       // 接收方的ID(网络的IP、SRIO的ID)
    unsigned int msg_type : 8;  // 消息类型
    unsigned int len : 24;      // 消息长度,字节数
    unsigned int conn_type: 8;  // 连接类型,TCP、UDP等
    unsigned int pkg_num : 8;   // 总的包数(用于组包传输)
    unsigned int pkg_idx : 8;   // 子包索引(用于组包传输),从1计数
    unsigned 
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用FFmpeg新版本接口播放QByteArray数据,可以按照以下步骤: 1. 将QByteArray数据存储到缓冲区。 ``` QByteArray data = ...; unsigned char* buffer = new unsigned char[data.size()]; memcpy(buffer, data.data(), data.size()); ``` 2. 初始化FFmpeg相关的结构体和函数。 ``` AVFormatContext* format_ctx = avformat_alloc_context(); AVCodecContext* codec_ctx = avcodec_alloc_context3(NULL); AVCodec* codec = NULL; av_register_all(); avformat_network_init(); ``` 3. 打开缓冲区的数据。 ``` AVIOContext* io_ctx = NULL; io_ctx = avio_alloc_context(buffer, data.size(), 0, NULL, &read_packet, NULL, NULL); format_ctx->pb = io_ctx; if (avformat_open_input(&format_ctx, "", NULL, NULL) < 0) { qDebug() << "Could not open input file"; return; } if (avformat_find_stream_info(format_ctx, NULL) < 0) { qDebug() << "Could not find stream information"; return; } //找到视频流和音频流 int video_stream_index = -1; int audio_stream_index = -1; for (unsigned int i = 0; i < format_ctx->nb_streams; i++) { if (format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) { video_stream_index = i; } if (format_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { audio_stream_index = i; } } ``` 4. 打开视频流和音频流。 ``` if (audio_stream_index != -1) { codec_ctx = avcodec_alloc_context3(NULL); avcodec_parameters_to_context(codec_ctx, format_ctx->streams[audio_stream_index]->codecpar); codec = avcodec_find_decoder(codec_ctx->codec_id); if (codec == NULL) { qDebug() << "Unsupported codec!"; return; } if (avcodec_open2(codec_ctx, codec, NULL) < 0) { qDebug() << "Could not open codec!"; return; } } if (video_stream_index != -1) { codec_ctx = avcodec_alloc_context3(NULL); avcodec_parameters_to_context(codec_ctx, format_ctx->streams[video_stream_index]->codecpar); codec = avcodec_find_decoder(codec_ctx->codec_id); if (codec == NULL) { qDebug() << "Unsupported codec!"; return; } if (avcodec_open2(codec_ctx, codec, NULL) < 0) { qDebug() << "Could not open codec!"; return; } } ``` 5. 读取数据并进行解码。 ``` AVPacket packet; av_init_packet(&packet); while (av_read_frame(format_ctx, &packet) >= 0) { if (packet.stream_index == audio_stream_index || packet.stream_index == video_stream_index) { if (avcodec_send_packet(codec_ctx, &packet) < 0) { qDebug() << "Error sending packet for decoding"; break; } AVFrame* frame = av_frame_alloc(); while (avcodec_receive_frame(codec_ctx, frame) >= 0) { //处理音频帧或视频帧 } av_frame_free(&frame); } av_packet_unref(&packet); } ``` 6. 释放资源。 ``` avformat_close_input(&format_ctx); avformat_free_context(format_ctx); avcodec_free_context(&codec_ctx); avio_context_free(&io_ctx); delete[] buffer; ``` 希望对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值