ireader / media-server

RTSP/RTP/RTMP/FLV/HLS/MPEG-TS/MPEG-PS/MPEG-DASH/MP4/fMP4/MKV/WebM
MIT License
3.06k stars 1.07k forks source link

How can I sync video and audio on mpegts in libmpeg #290

Open ngovanquang opened 1 year ago

ngovanquang commented 1 year ago

Hi author, I am having problem with mpegts, i have passed video and audio but i don't know what parameter in your library to sync audio and video. Here is my implementation:

void pack_mpegts(const void *buffer, int size, int media_type, int frame_number) { static int64_t a_pts = 0; static int64_t a_dts = 0; static int64_t v_pts = 0; static int64_t v_dts = 0; static int cnt_a = 0; static int cnt_v = 0;

if (ts != NULL)
{
    if (media_type == AAC_FRAME && aac_stream != 0)
    {
        if (cnt_a == 0)
        {
            cnt_a++;
            clock_gettime(CLOCK_MONOTONIC, &currentTime);
            long milliseconds = currentTime.tv_sec * 1000 + currentTime.tv_nsec / 1000000;
            a_dts = milliseconds * 90;
        }
        else
        {
            a_dts += (1024 * 1000 * 90) / 16000;
        }
        // a_dts = frame_number * 1024 * 90000 / 16000;
        a_pts = a_dts;
        int ret = mpeg_ts_write(ts, aac_stream, 0, a_pts, a_dts, buffer, size);
        if (ret != 0)
        {
            printf("AAC ERROR\n");
        }
    }
    else if (media_type == H264_FRAME && h264_stream != 0)
    {
        if (cnt_v == 0)
        {
            cnt_v++;
            clock_gettime(CLOCK_MONOTONIC, &currentTime);
            long milliseconds = currentTime.tv_sec * 1000 + currentTime.tv_nsec / 1000000;
            v_dts = milliseconds * 90;
        }
        else
        {
            v_dts += 1000 / 30 * 90;
        }

        // v_dts = frame_number * 3000;
        v_pts = v_dts;
        int ret = mpeg_ts_write(ts, h264_stream, MPEG_FLAG_IDR_FRAME, v_pts, v_dts, buffer, size);
        if (ret != 0)
        {
            printf("H264 ERROR\n");
        }
    }
}

}

ireader commented 1 year ago

In MPEG-TS audio/video have same sample rate (90MHz).

libmpeg only generate ts stream, don't include a/v sync function.

ngovanquang commented 1 year ago

Thanks for your reply! So, can you suggest me some documentation about a/v sync function? I'm a newbie to mpegts. Thanks!

ireader commented 1 year ago

Just only keep your input a/v frame pts/dts sequential。

From your code:

v_dts += 1000 / 30 * 90;

1000/30 introducing cumulative error.

ngovanquang commented 1 year ago

Thanks for your help, I still have a problem when I stream mpegts audio and video over my local network and play with vlc, I get the error as shown below. But when i only stream video, the error not occur image

ireader commented 1 year ago

try to print out audio/video dts before call mpeg_ts_write?