[英]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_packet
和avcodec_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.