音视频同步

 

同步线程:

static MS_BOOL _SyncStcMonTaskStart(void)
{
    SyncStcTaskData_t *pstSync = &_stSyncStcTask;

    if(pstSync->s32TaskId >= 0)
    {
        MS_MODULE_WARN( "%s(%d) already started\n", __FUNCTION__, __LINE__);
        return TRUE;
    }

    pstSync->bTerminate = FALSE;
    pstSync->s32TaskExitSem = MsOS_CreateSemaphore(0, E_MSOS_FIFO, "SyncTaskExitSem");
    if(pstSync->s32TaskExitSem < 0)
    {
        MS_MODULE_ERROR( "%s(%d) failed to create semaphore for exiting task!!!\n", __FUNCTION__, __LINE__);
        goto FAILED_START_TASK;
    }

    pstSync->pStack = malloc(SYNC_STC_STACK_SIZE);
    if(pstSync->pStack == NULL)
    {
        MS_MODULE_ERROR( "%s(%d) failed to malloc stack(%d) for task!!!\n", __FUNCTION__, __LINE__, SYNC_STC_STACK_SIZE);
        goto FAILED_START_TASK;
    }

    MApi_AUDIO_SetCommAudioInfo(Audio_Comm_infoType_ADEC1_33bit_PTS, 1, 0);  // decoder mute

    pstSync->u8SyncState = 0;
    pstSync->s32TaskId = MsOS_CreateTask((TaskEntry) _SyncStcMonTask,
                                         (MS_U32)pstSync,
                                         E_TASK_PRI_HIGH,
                                         TRUE,
                                         pstSync->pStack,
                                         SYNC_STC_STACK_SIZE,
                                         "SyncStcMonTask");
    if(pstSync->s32TaskId < 0)
    {
        MS_MODULE_ERROR( "%s(%d) failed to create task!!!\n", __FUNCTION__, __LINE__);
        goto FAILED_START_TASK;
    }

    return TRUE;

FAILED_START_TASK:


    _SyncStcMonTaskStop();

    return FALSE;
}
同步实现:

static void _SyncStcMonTask(MS_U32 argc, void *argv)

{
    SyncStcTaskData_t *pstSync = (SyncStcTaskData_t*)&_stSyncStcTask;
    if(pstSync == NULL)
    {
        MS_MODULE_ERROR( "%s(%d) Invalid argc parameters!!!\n", __FUNCTION__, __LINE__);
        return;
    }

    MS_U32 u32PrvAPTS = 0xFFFFFFFF;
    MS_U32 u32PTS;
    MS_U32 u32STC;
    MS_U32 u32STC32;
    MS_U32 u32VPTS;
    while(!pstSync->bTerminate)
    {
        switch(pstSync->u8SyncState)
        {
            case 0:
                MS_MODULE_INFO( "Audio Sync State.\n");
                u32VPTS=MApi_VDEC_GetPTS();
                if(u32VPTS != U32_MAX)
                {
                    MS_MODULE_INFO( "sync VPTS to STC [%ld] \n", u32VPTS);
                    MApi_DMX_Stc_Set(0, (u32VPTS)*90);
                    MApi_AUDIO_SetAC3PInfo(Audio_AC3P_infoType_syncSTC_in_mmTs, 1, 0);
                    MsOS_DelayTask(30);
                    MApi_AUDIO_SetAC3PInfo(Audio_AC3P_infoType_syncSTC_in_mmTs, 0, 0);
                    pstSync->u8SyncState = 1;
                }

                break;
            case 1:
#if DIRECT_AUDIO_MASTER ==1
                MS_MODULE_INFO( "Enter Monitor State.\n");
                MApi_AUDIO_SetCommAudioInfo(Audio_Comm_infoType_ADEC1_33bit_PTS, 0, 0);  // decoder mute
                pstSync->u8SyncState = 2;
                break;
#else
                //u32PTS = DApi_AUDIO_GetAudioInfo(E_AUDIO_INFO_TYPE_CURRENT_PTS);
                u32PTS = MApi_AUDIO_GetCommAudioInfo(Audio_Comm_infoType_33Bit_PTS);
                u32PTS = u32PTS/90; // ms

                if(MApi_DMX_Stc_Get(&u32STC32, &u32STC) != DMX_FILTER_STATUS_OK)
                    break;

                u32STC = u32STC/90; // ms
                if(u32STC > u32PTS)
                {
                    if((u32STC - u32PTS) < RESET_STC_THRESHOLD)
                    {
                        //MS_MODULE_WARN( "0: Sync handling!!! APTS=%ld, STC=%ld, STC-PTS=%ld, Use APTS to set STC\n", u32PTS, u32STC, u32STC-u32PTS);
                        pstSync->u8SyncState = 2;
                        //DApi_AUDIO_SetSTCbyAPTS(u32PTS);
                    }
                }
                else
                {
                    if((u32PTS - u32STC) < RESET_STC_THRESHOLD)
                    {
                        //MS_MODULE_WARN( "1: Sync handling!!! APTS=%ld, STC=%ld, PTS-STC=%ld, Use APTS to set STC\n", u32PTS, u32STC, u32PTS-u32STC);
                        pstSync->u8SyncState = 2;
                        //DApi_AUDIO_SetSTCbyAPTS(u32PTS);
                    }
                }
                break;
#endif
            case 2:
                // monitor av sync
               // u32PTS = DApi_AUDIO_GetAudioInfo(E_AUDIO_INFO_TYPE_CURRENT_PTS);
                u32PTS = MApi_AUDIO_GetCommAudioInfo(Audio_Comm_infoType_33Bit_PTS);
                u32PTS = u32PTS/90; // ms

                if(u32PTS == u32PrvAPTS)
                {
                    break;
                }
                u32PrvAPTS = u32PTS;

                if(MApi_DMX_Stc_Get(&u32STC32, &u32STC) != DMX_FILTER_STATUS_OK)
                {
                    break;
                }
                u32STC = u32STC/90; // ms
                if(u32STC > u32PTS)
                {
                    if((u32STC - u32PTS) > RESET_STC_THRESHOLD)
                    {
                        MS_MODULE_WARN( "0: Sync handling!!! APTS=%ld, STC=%ld, STC-PTS=%ld, Use APTS to set STC\n", u32PTS, u32STC, u32STC-u32PTS);
                        //DApi_AUDIO_SetSTCbyAPTS(u32PTS);
                  MApi_DMX_Stc_Set(0, (u32PTS * 90));

                    }
                }
                else
                {
                    if((u32PTS - u32STC) > RESET_STC_THRESHOLD)
                    {
                        MS_MODULE_WARN( "1: Sync handling!!! APTS=%ld, STC=%ld, PTS-STC=%ld, Use APTS to set STC\n", u32PTS, u32STC, u32PTS-u32STC);
                        //DApi_AUDIO_SetSTCbyAPTS(u32PTS);
            MApi_DMX_Stc_Set(0, (u32PTS * 90));
                    }
                }
                break;
            default:
                break;
        }
        MsOS_DelayTask(100);
    }

// Task Exit!

    if(pstSync->s32TaskExitSem >= 0)
    {
        MsOS_ReleaseSemaphore(pstSync->s32TaskExitSem);
    }

    MS_MODULE_DEBUG( "%s(%d) exit!\n", __FUNCTION__, __LINE__);


}

你可能感兴趣的:(一种视频播放思想)