天天看點

TS碼流解析-7-整合相關表,并且生成可播放檔案任務七步驟

任務七

任務描述:把前幾個階段解析出來的表整合起來,并且展示出來,輸入一個存在的pid,然後導出一個ts檔案,并且能夠在KMP上播放

步驟

這裡主要是根據輸入的PID,去尋找對應的PMT中的内容,并且根據對應的elementary的PID周遊碼流檔案,把相關的檔案另存,然後用播放器播放。既然表已經解析完了,那麼重點就是尋找跟另存的功能實作,另外怎麼顯示,顯示什麼,得看需要方怎麼要求了,下面是另存的代碼

/*****************************************************************************
*   Function Name: check_pid_callback
*   Description  : the pid check callback function
*   Parameters   : STREAM_MESSAGE  *stream_message                   
*                  unsigned int ts_package_pid  
*   Returns : static int
*             1 success match
*             0 match error
*****************************************************************************/
static int check_pid_callback(STREAM_MESSAGE  *stream_message, unsigned int ts_package_pid)
{
    while(stream_message != NULL)
    {
        if (stream_message->elementary_pid ==  ts_package_pid)
        {
            return 1;
        }
        stream_message = stream_message->next;
    }
    return 0;
}
/*****************************************************************************
*   Function Name: get_package_head
*   Description  : copy the package head to the package_head struct
*   Parameters   : unsigned char *package_buffer     
*                  TS_PACKAGE_HEAD *ts_package_head  
*   Returns : void
*****************************************************************************/
void get_package_head(unsigned char *package_buffer, TS_PACKAGE_HEAD *ts_package_head)
{
    if ((NULL==package_buffer) || (NULL==ts_package_head))
    {
        debug_log("function get_package_head parameter error!\n");
        return;
    }
    
    ts_package_head->sync_byte = package_buffer[0];
    ts_package_head->transport_error_indicator = package_buffer[1] >> 7;
    ts_package_head->payload_unit_start_indicator = (package_buffer[1] >> 6) & 0x1;
    ts_package_head->transport_priority = (package_buffer[1] >> 5) & 0x1 & 0x1;
    ts_package_head->pid = ((package_buffer[1] & 0x1f) << 8) | package_buffer[2];
    ts_package_head->transport_scrambling_control = package_buffer[3] >> 6;
    ts_package_head->adaptation_field_control = (package_buffer[3] >> 4) & 0x3;
    ts_package_head->continuity_counter = package_buffer[3] & 0xf;
}
/*****************************************************************************
*   Function Name: match_writing
*   Description  : check package pid == Getpid,if equal,write to file
*   Parameters   : FILE *file                
*                  FILE *same_pid_file          
*                  unsigned int package_length  
*                  CHECK_PID check_callback     
*                  STREAM_MESSAGE  *stream_message                   
*   Returns : static void
*****************************************************************************/
static void match_writing(FILE *file, FILE *same_pid_file, unsigned int package_length, CHECK_PID check_callback, STREAM_MESSAGE  *stream_message)
{
    unsigned char package_buffer[204] = {0};
    TS_PACKAGE_HEAD ts_package_head = {0};
    
    while (package_length == fread(package_buffer, 1, package_length, file))
    {
        get_package_head(package_buffer, &ts_package_head);

        if ((1==check_callback(stream_message, ts_package_head.pid)) && (SYNC_BYTE==ts_package_head.sync_byte))
        {
            if (fwrite(package_buffer, 1, package_length, same_pid_file) != package_length)
            {
                service_log("write package content to file error!!!\n");
                return;
            }
        } 
    }
    service_log("write same pid package content to file success!!!\n");
}

/*****************************************************************************
*   Function Name: write_to_file
*   Description  : from the first position to 0x47 and then write the same
                   pid to file
*   Parameters   : FILE *file              
*                  int first_sync_position  
*                  const int package_length   
*                  STREAM_MESSAGE  *stream_message                
*   Returns : void
*****************************************************************************/
void write_to_file(FILE *file, int first_sync_position, const int package_length, STREAM_MESSAGE  *stream_message)
{
    FILE *writed_file = NULL;

    if ((NULL==file) || (first_sync_position<0) || (package_length<=0) || (NULL==stream_message))
    {
        debug_log("function write_to_file parameter error\n");
        return;
    }

    if (fseek(file, first_sync_position - 1, SEEK_SET) != 0)
    {
        debug_log("Fseek to first position to 0x47 error!\n");
        return;
    }
    writed_file = fopen(SAVE_FILE_PATH, "wb");
    if (NULL == writed_file)
    {
        service_log("writed_file open error\n");
        return;
    }
    match_writing(file, writed_file, package_length, check_pid_callback, stream_message);
    fclose(writed_file);
}
           

以上是整個解碼流任務的過程。

寫在最後

匆匆忙忙兩個月的實習期就要到尾聲了,有做得好的地方,也有做得不足之處,但是卻是一直在成長,學習很多之前沒有接觸過的内容,編碼能力提高了,對碼流的認識更深了,至于能不能留在這裡,聽天命吧,我已經在我能力範圍内,盡量把任務做得足夠好了,不足之處是回報不足,帶我們的人比較忙,沒太多時間檢查我們的代碼。

全部的代碼可以在我的部落格裡面找到下載下傳連結,如果有什麼問題,歡迎前來指導!!!

繼續閱讀