简体   繁体   English

Live555会截断FFMpeg的编码数据

[英]Live555 truncates encoded data of FFMpeg

I am trying to stream H264 based data using Live555 over RTSP. 我正在尝试通过RTSP使用Live555流式传输基于H264的数据。

I am capturing data using V4L2, and then encodes it using FFMPEG and then passing data to Live555's DeviceSource file, in that I using H264VideoStreamFramer class, 我使用V4L2捕获数据,然后使用FFMPEG对其进行编码,然后将数据传递到Live555的DeviceSource文件中,因为我使用的是H264VideoStreamFramer类,

Below is my codec settings to configure AVCodecContext of encoder, 以下是我的编解码器设置,用于配置编码器的AVCodecContext,

codec = avcodec_find_encoder_by_name(CODEC_NAME);
if (!codec) {
    cerr << "Codec " << codec_name << " not found\n";
    exit(1);
}

c = avcodec_alloc_context3(codec);
if (!c) {
    cerr << "Could not allocate video codec context\n";
    exit(1);
}

pkt = av_packet_alloc();
if (!pkt)
    exit(1);

/* put sample parameters */
c->bit_rate = 400000;
/* resolution must be a multiple of two */
c->width = PIC_HEIGHT;
c->height = PIC_WIDTH;
/* frames per second */
c->time_base = (AVRational){1, FPS};
c->framerate = (AVRational){FPS, 1};
c->gop_size = 10;
c->max_b_frames = 1;
c->pix_fmt = AV_PIX_FMT_YUV420P;
c->rtp_payload_size = 30000;
if (codec->id == AV_CODEC_ID_H264)
    av_opt_set(c->priv_data, "preset", "fast", 0);
av_opt_set_int(c->priv_data, "slice-max-size", 30000, 0);
/* open it */
ret = avcodec_open2(c, codec, NULL);
if (ret < 0) {
    cerr << "Could not open codec\n";
    exit(1);
}

And I am getting encoded data using avcodec_receive_packet() function. 我正在使用avcodec_receive_packet()函数获取编码数据。 which will return AVPacket. 它将返回AVPacket。

And I am passing AVPacket's data into DeviceSource file below is code snippet of my Live555 code: 我将AVPacket的数据传递到DeviceSource文件中,这是我的Live555代码的代码片段:

void DeviceSource::deliverFrame() {
    if (!isCurrentlyAwaitingData()) return; // we're not ready for the data yet

    u_int8_t* newFrameDataStart = (u_int8_t*) pkt->data;
    unsigned newFrameSize = pkt->size; //%%% TO BE WRITTEN %%%
    // Deliver the data here:
    if (newFrameSize > fMaxSize) { // Condition becomes true many times
        fFrameSize = fMaxSize;
        fNumTruncatedBytes = newFrameSize - fMaxSize;
    } else {
        fFrameSize = newFrameSize;
    }
    gettimeofday(&fPresentationTime, NULL); // If you have a more accurate time - e.g., from an encoder - then use that instead.
    // If the device is *not* a 'live source' (e.g., it comes instead from a file or buffer), then set "fDurationInMicroseconds" here.
    memmove(fTo, newFrameDataStart, fFrameSize);
}

But here, sometimes my packet's size is getting more than fMaxSize value and as per LIVE555 logic it will truncate frame data, so that sometimes I am getting bad frames on my VLC, 但是在这里,有时我的数据包的大小超过了fMaxSize的值,并且按照LIVE555逻辑,它将截断帧数据,因此有时我的VLC上出现错误的帧,

From Live555 forum, I get to know that encoder should not send packet whose size is more than fMaxSize value, so my question is: 从Live555论坛,我知道编码器不应发送大小大于fMaxSize值的数据包,所以我的问题是:

How to restrict encoder to limit size of packet? 如何限制编码器限制数据包的大小?

Thanks in Advance, 提前致谢,

Harshil 哈希尔

You can increase the maximum allowed sample size by changing "maxSize" in the OutPacketBuffer class in MediaSink.cpp. 您可以通过更改MediaSink.cpp的OutPacketBuffer类中的“ maxSize”来增加允许的最大样本大小。 This worked for me. 这对我有用。 There are cases we may require high-quality video to be streamed, I don't think we will always be able to restrict the encoder to not to produce samples of size more than a particular value which would result in video quality issues. 在某些情况下,我们可能需要流式传输高质量的视频,但我认为我们不会总是能够限制编码器不生成大小超过特定值的样本,而这会导致视频质量问题。 In fact, the samples are fragmented by the UDP sink live555 to match the default MTU (1500), so increasing the max sample size limit has no side effects. 实际上,样本由UDP接收器live555分段以匹配默认的MTU(1500),因此增加最大样本大小限制没有副作用。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM