ASessionDescription::getFormatType

  函數簡介: strrchr
  函數原型:char *strrchr(const char *str, char c);
  所屬庫: string.h
  函數功能:查找一個字符c在另一個字符串str中末次出現的位置(也就是從str的右側開始查找字符c首次出現的位置),並返回這個位置的地址。如果未能找到指定字符,那麼函數將返回NULL。使用這個地址返回從最後一個字符c到str末尾的字符串。

  函數簡介:strtoul (將字符串轉換成無符號長整型數)
  函數原型:unsigned long strtoul(const char *nptr,char **endptr,int base);
  所屬庫:stdlib.h
  函數功能:strtoul()會將參數nptr字符串根據參數base來轉換成無符號的長整型數。參數base範圍從2至36,或0。參數base代表採用的進制方式,如base值爲10則採用10進制,若base值爲16則採用16進制數等。當base值爲0時會根據情況選擇用哪種進制:如果第一個字符是’0’,就判斷第二字符如果是‘x’則用16進制,否則用8進制;第一個字符不是‘0’,則用10進制。一開始strtoul()會掃描參數nptr字符串,跳過前面的空格字符串,直到遇上數字或正負符號纔開始做轉換,再遇到非數字或字符串結束時結束轉換,並將結果返回。若參數endptr不爲NULL,則會將遇到不合條件而終止的nptr中的字符指針由endptr返回。

void ASessionDescription::getFormatType(
        size_t index, unsigned long *PT,
        AString *desc, AString *params) const {
    //index得到對應的format
    //format是容器mFormats裏位置爲index的元素的值
    AString format;
    getFormat(index, &format);

    //format形如:m=video 0 RTP/AVP 31
    //找到字符串format.c_str()最後一個空格符' '的位置
    const char *lastSpacePos = strrchr(format.c_str(), ' ');
    CHECK(lastSpacePos != NULL);

    //將空格符' '字符表示的整數轉換成長整型
    char *end;
    unsigned long x = strtoul(lastSpacePos + 1, &end, 10);
    CHECK_GT(end, lastSpacePos + 1);
    CHECK_EQ(*end, '\0');

    //將轉換成的長整型賦值給*PT
    *PT = x;

    //組合得到鍵值key "a=rtpmap:%lu"
    char key[32];
    snprintf(key, sizeof(key), "a=rtpmap:%lu", x);
    //由參數index和鍵值key "a=rtpmap:%lu"得到對應該鍵值key的value並由參數desc傳回
    if (findAttribute(index, key, desc)) {
        //如果成功由參數index和鍵值key "a=rtpmap:%lu"得到對應該鍵值key的value
        //組合鍵值key "a=fmtp:%lu"
        snprintf(key, sizeof(key), "a=fmtp:%lu", x);
        //由參數index和鍵值key得到對應該鍵值key "a=fmtp:%lu"的value並由參數params傳回
        if (!findAttribute(index, key, params)) {
            //如果未能成功由參數index和鍵值key "a=fmtp:%lu"得到對應該鍵值key的value強調內容
            //清空params
            params->clear();
        }
    } else {
        //如果未能成功由參數index和鍵值key "a=rtpmap:%lu"得到對應該鍵值key的value將desc和params都清空
        desc->clear();
        params->clear();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章