简体   繁体   English

FFmpeg:解码从 UDP 套接字接收的 AVPackets

[英]FFmpeg: Decoding AVPackets received from UDP socket

I am trying to stream video frames encoded with FFmpeg from an Unity game to a client UI via UDP.我正在尝试将使用 FFmpeg 编码的 stream 视频帧从 Unity 游戏通过 UDP 从 Unity 游戏发送到客户端 UI。 To be specific, I am sending AVPackets (which contain the compressed frames, as far as I understand) from the server, which are then, on the client side, extracted as follows:具体来说,我从服务器发送 AVPackets(据我所知,其中包含压缩帧),然后在客户端提取如下:

inline UDPpacket* SDLGameClient::receiveData()
{
    if(SDLNet_UDP_Recv(socket, packet))
        return packet;
    else
        return NULL;
}

int main()
{
    ...
    // Initialize UDP
    ...
    UDPpacket *udpPacket;

    int i = 0;

    for(;;)
    {
        udpPacket = client->receiveData();

        if(udpPacket != nullptr)
        {
            i++;
            std::cout << "Packet " << i << " received!" << std::endl;

            AVPacket packet;
            av_init_packet(&packet);

            packet.data = new uint8_t[udpPacket->len];
            memcpy(packet.data, udpPacket->data, udpPacket->len);
            packet.size = udpPacket->len;

            ...

To realize networking, I use the SDL_net library.为了实现联网,我使用了 SDL_net 库。 Fragmenting, sending and receiving the packets seem to work without any problem.分段、发送和接收数据包似乎没有任何问题。 My question is, how do I decode the incoming AVPackets and stream the frames recorded in Unity to the client?我的问题是,如何将传入的 AVPackets 和 stream 将 Unity 中记录的帧解码到客户端? I am aware that I need to use avcodec_send_packet and avcodec_receive_frame for decoding (since avcodec_decode_video2 , that is used in many example decoding code, is deprecated), but when I do something like this:我知道我需要使用avcodec_send_packetavcodec_receive_frame进行解码(因为不推荐使用在许多示例解码代码中使用的avcodec_decode_video2 ),但是当我执行以下操作时:

int ret = avcodec_send_packet(codecContext, &packet);
if(ret < 0 || ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
    std::cout << "avcodec_send_packet: " << ret << std::endl;
else
{
    while(ret >= 0)
    {
        ret = avcodec_receive_frame(codecContext, frame);
        if(ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
            std::cout << "avcodec_receive_frame: " << ret << std::endl;

        std::cout << "Frame: " << codecContext->frame_number << std::endl;
    }
}

av_packet_unref(packet);

ret always returns a negative value (-22), so perhaps something is wrong with the AVPackets, or I'm sending the frames way too fast, I really have no clue:/ ret 总是返回一个负值(-22),所以可能是 AVPackets 出了点问题,或者我发送帧的速度太快了,我真的不知道:/

Thanks in advance for any help!提前感谢您的帮助!

-22 means -EINVAL , Invalid argument . -22表示-EINVAL无效参数 You can normally check the ffmpeg error log messages for more details. 通常,您可以检查ffmpeg错误日志消息以获取更多详细信息。

My guess is that your packets are still fragmented, but avcodec_send_packet() expects AVPacket to always contain a full frame. 我的猜测是您的数据包仍然是零散的,但是avcodec_send_packet()希望AVPacket始终包含完整的帧。 Depending on the codec involved you should consider using an AVParser to recover the framing or frame the packets before UDP fragmentation, so you can recover the framing by yourself. 根据所涉及的编解码器,您应该考虑使用AVParser恢复帧或在UDP分段之前对数据包进行帧构造,以便您可以自己恢复帧。

Firstly sorry, i cannot add comment on this because of reputation.首先抱歉,由于声誉问题,我无法对此发表评论。 I am tyring to do exactly the same.我很想做同样的事情。 Also i am getting exact same error.我也得到完全相同的错误。 However, i didnt get how to add av_parser_parse2() function.但是,我不知道如何添加 av_parser_parse2() function。 It also requires AVCodecparseContext which is also need to be send separately from sender.它还需要 AVCodecparseContext ,它也需要与发送者分开发送。 Would you be able to share the part you have added av_parser_parse2() function?您能否分享您添加的部分 av_parser_parse2() function?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用UDP协议从Windows套接字发送到Qt套接字的网络数据包的结构编码和解码 - Structure encoding and decoding on the network packet sent from the Windows Socket to Qt Socket using the UDP Protocol 如何使用FFMPEG从存储为视频AVPackets的NAL单元播放H.264流 - How to use FFMPEG to play H.264 stream from NAL units that are stored as video AVPackets FFMPEG(Libav)-使用av_parser_parse2()从网络包(TCP)创建AV包 Function - FFMPEG(Libav)-Creating AVpackets from Network Packets(TCP) Using av_parser_parse2() Function 在Wireshark中看到的数据报,没有被Qt UDP Socket接收 - Datagrams seen in Wireshark, not received by Qt UDP Socket UDP 广播数据包被 tcpdump 看到但没有被 linux 套接字接收 - UDP Broadcast packets is seen by tcpdump but not received by linux socket Rust 不接收来自 C++ 的 UDP 消息 - UDP messages from C++ are not received by Rust 从UDP流读取时ffmpeg错误 - Error in ffmpeg when reading from UDP stream 如何强制 udp 服务器接收到的 udp 客户端套接字绑定到特定的客户端端口 - How do I force a udp client socket received by a udp server to bind to a specific client port LibUV从现有套接字创建UDP流 - LibUV create UDP stream from existing socket FFMPEG音频解码:从短到浮点样本缓冲区的高效转换 - FFMPEG audio decoding: efficient conversion from short to float sample buffer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM