Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ngovanquang opened this issue Jun 16, 2023 · 5 comments
Open

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

ngovanquang opened this issue Jun 16, 2023 · 5 comments

Comments

@ngovanquang
Copy link

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
Copy link
Owner

ireader commented Jun 16, 2023

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

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

@ngovanquang
Copy link
Author

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

@ireader
Copy link
Owner

ireader commented Jun 16, 2023

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
Copy link
Author

ngovanquang commented Jun 16, 2023

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
Copy link
Owner

ireader commented Jun 16, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants