1. 程式人生 > >LIVE555學習4:live555MediaServer講解——如何根據指定檔案字尾建立ServerMediaSession

LIVE555學習4:live555MediaServer講解——如何根據指定檔案字尾建立ServerMediaSession

在前面《LIVE555學習1:Linux下live555的編譯及測試》中,當我們執行起可執行程式live555MediaServer,在VLC中輸入rtsp://192.168.1.100/Titanic.ts, 便可以播放ts流。當我們輸入其他格式,如H264、H265、aac等,均可以輸出對應流。但是在主函式中,只建立了一個RTSPServer例項,並沒有建立這些對應不同格式檔案的ServerMediaSession和ServerMediaSubSession.

上一篇中也大致介紹了RTSP服務執行的整個流程,但是沒有對程式碼進行詳細的講解,所以並不清楚ServerMediaSession是如何建立的,什麼時候建立的,如何根據不同的檔案格式進行建立不同的物件。。。。

在RTSP會話過程中,再DESCRIBE和SETUP中,會來確定ServerMediaSession是否存在。具體實現在DynamicRTSPServer.cpp中,在函式lookupServerMediaSession中,會進行以下檢查:

  • ①檢查ServerMediaSession是否存在
  ServerMediaSession* sms = RTSPServer::lookupServerMediaSession(streamName);
  Boolean smsExists = sms != NULL;
  • ②判斷檔案是否存在,如果檔案沒了,
    ServerMediaSession有,則刪除ServerMediaSession
  if (!fileExists) {
    if (smsExists) {
      // "sms" was created for a file that no longer exists. Remove it:
      removeServerMediaSession(sms);
      sms = NULL;
    }
  • ③最終根據判斷結果來決定是否建立
ServerMediaSession
    if (sms == NULL) {
      sms = createNewSMS(envir(), streamName, fid);
      addServerMediaSession(sms);
    }

若是需要建立ServerMediaSession,則會呼叫createNewSMS,在此函式中,也做了幾件事:

  • ①確定檔案字尾
  char const* extension = strrchr(fileName, '.');
  if (extension == NULL) return NULL;
  • ②根據檔案字尾來建立對應的ServerMediaSession和ServerMediaSubSession
    例如H264:
  }else if (strcmp(extension, ".264") == 0) {
    // Assumed to be a H.264 Video Elementary Stream file:
    NEW_SMS("H.264 Video");
    OutPacketBuffer::maxSize = 100000; // allow for some possibly large H.264 frames
    sms->addSubsession(H264VideoFileServerMediaSubsession::createNew(env, fileName, reuseSource));
}

例如H265:

  } else if (strcmp(extension, ".265") == 0) {
    // Assumed to be a H.265 Video Elementary Stream file:
    NEW_SMS("H.265 Video");
    OutPacketBuffer::maxSize = 100000; // allow for some possibly large H.265 frames
    sms->addSubsession(H265VideoFileServerMediaSubsession::createNew(env, fileName, reuseSource));
}

結束。根據以上的分析可以知道,在程式執行時候,會首先建立一個RTSPServer例項,然後會根據RTSP會話傳遞過來的檔案格式來建立對應的ServerMediaSession和ServerMediaSubSession。