美文网首页视频开发直播iOS技术文章
LibRTMP源码分析 1:解析URL

LibRTMP源码分析 1:解析URL

作者: 熊皮皮 | 来源:发表于2016-03-26 18:29 被阅读1039次

本系列文档分析LibRTMP源码,所用源码位于GitHubLibrtmp for Android & iOS,应该不是最新的LibRTMP源码,对于学习是够用的。

小广告:欢迎加入iOS Android 直播、全景播放技术讨论群:459486016

文件:parseurl.c
函数调用关系:
RTMP_ParseURL
---- RTMP_ParsePlaypath

1、分析RTMP_ParseURL

函数定义:

int RTMP_ParseURL(const char *url,
                  int *protocol,
                  AVal *host,
                  unsigned int *port,
                  AVal *playpath,
                  AVal *app)

URL的组成格式为:[协议]://[主机名]:[端口]/文件路径?[键]:[值]
RTMP URL的格式为:

  • rtmp://host[:port]/app[/appinstance][/...]
  • application = app[/appinstance]

1.1、解析协议

1.1.1、源码

char *p, *end, *col, *ques, *slash;

RTMP_Log(RTMP_LOGDEBUG, "Parsing...");

*protocol = RTMP_PROTOCOL_RTMP;
*port = 0;
playpath->av_len = 0;
playpath->av_val = NULL;
app->av_len = 0;
app->av_val = NULL;

/* Old School Parsing */

/* look for usual :// pattern */
p = strstr(url, "://");
if (!p) {
    RTMP_Log(RTMP_LOGERROR, "RTMP URL: No :// in url!");
    return FALSE;
}
{
    int len = (int) (p - url);
    
    if (len == 4 && strncasecmp(url, "rtmp", 4) == 0)
        *protocol = RTMP_PROTOCOL_RTMP;
    else if (len == 5 && strncasecmp(url, "rtmpt", 5) == 0)
        *protocol = RTMP_PROTOCOL_RTMPT;
    else if (len == 5 && strncasecmp(url, "rtmps", 5) == 0)
        *protocol = RTMP_PROTOCOL_RTMPS;
    else if (len == 5 && strncasecmp(url, "rtmpe", 5) == 0)
        *protocol = RTMP_PROTOCOL_RTMPE;
    else if (len == 5 && strncasecmp(url, "rtmfp", 5) == 0)
        *protocol = RTMP_PROTOCOL_RTMFP;
    else if (len == 6 && strncasecmp(url, "rtmpte", 6) == 0)
        *protocol = RTMP_PROTOCOL_RTMPTE;
    else if (len == 6 && strncasecmp(url, "rtmpts", 6) == 0)
        *protocol = RTMP_PROTOCOL_RTMPTS;
    else {
        RTMP_Log(RTMP_LOGWARNING, "Unknown protocol!\n");
        goto parsehost;
    }
}

RTMP_Log(RTMP_LOGDEBUG, "Parsed protocol: %d", *protocol);

1.1.2、流程分析

  1. 找出://所在位置。找不到则报错:RTMP URL: No :// in url!。
  2. 若找到,则p为:在URL字符串中的位置。
  3. int len = (int) (p - url)得到://前面字符串的长度,即协议的长度。
  4. 判断协议。使用strncasecmp以忽略字符串大小写方式进行比较。
  5. 对于以://开头的URL,默认为RTMP协议。

1.1.3、验证

抽取如下代码并执行。

#define RTMP_FEATURE_HTTP   0x01
#define RTMP_FEATURE_ENC    0x02
#define RTMP_FEATURE_SSL    0x04
#define RTMP_FEATURE_MFP    0x08    /* not yet supported */
#define RTMP_FEATURE_WRITE  0x10    /* publish, not play */
#define RTMP_FEATURE_HTTP2  0x20    /* server-side rtmpt */

#define RTMP_PROTOCOL_UNDEFINED -1
#define RTMP_PROTOCOL_RTMP      0
#define RTMP_PROTOCOL_RTMPE     RTMP_FEATURE_ENC
#define RTMP_PROTOCOL_RTMPT     RTMP_FEATURE_HTTP
#define RTMP_PROTOCOL_RTMPS     RTMP_FEATURE_SSL
#define RTMP_PROTOCOL_RTMPTE    (RTMP_FEATURE_HTTP|RTMP_FEATURE_ENC)
#define RTMP_PROTOCOL_RTMPTS    (RTMP_FEATURE_HTTP|RTMP_FEATURE_SSL)
#define RTMP_PROTOCOL_RTMFP     RTMP_FEATURE_MFP

char *p;
int ptl, *protocol = &ptl;
char *url = "rtmp://live.hkstv.hk.lxdns.com/live/hks";
p = strstr(url, "://");
if (!p) {
    printf("RTMP URL: No :// in url!");
    return FALSE;
}
{
    int len = (int) (p - url);
    
    if (len == 4 && strncasecmp(url, "rtmp", 4) == 0)
        *protocol = RTMP_PROTOCOL_RTMP;
    else if (len == 5 && strncasecmp(url, "rtmpt", 5) == 0)
        *protocol = RTMP_PROTOCOL_RTMPT;
    else if (len == 5 && strncasecmp(url, "rtmps", 5) == 0)
        *protocol = RTMP_PROTOCOL_RTMPS;
    else if (len == 5 && strncasecmp(url, "rtmpe", 5) == 0)
        *protocol = RTMP_PROTOCOL_RTMPE;
    else if (len == 5 && strncasecmp(url, "rtmfp", 5) == 0)
        *protocol = RTMP_PROTOCOL_RTMFP;
    else if (len == 6 && strncasecmp(url, "rtmpte", 6) == 0)
        *protocol = RTMP_PROTOCOL_RTMPTE;
    else if (len == 6 && strncasecmp(url, "rtmpts", 6) == 0)
        *protocol = RTMP_PROTOCOL_RTMPTS;
    else {
        printf("Unknown protocol!\n");
    }
}

printf("Parsed protocol: %d", *protocol);

运行:

输入:url 输出:protocol
"rtmptslive.hkstv.hk.lxdns.com/live/hks" RTMP URL: No :// in url!
"rtmp://live.hkstv.hk.lxdns.com/live/hks" Parsed protocol: 0
"://live.hkstv.hk.lxdns.com/live/hks" Unknown protocol!
Parsed protocol: 0
"rtmpts://live.hkstv.hk.lxdns.com/live/hks" Parsed protocol: 5

协议解析结束,开始解析主机。

1.2、解析主机

1.2.1、源码

parsehost:
    /* let's get the hostname */
    p += 3;

    /* check for sudden death */
    if (*p == 0) {
        RTMP_Log(RTMP_LOGWARNING, "No hostname in URL!");
        return FALSE;
    }

    end = p + strlen(p);
    col = strchr(p, ':');
    ques = strchr(p, '?');
    slash = strchr(p, '/');

    {
        int hostlen;
        if (slash)
            hostlen = slash - p;
        else
            hostlen = end - p;
        if (col && col - p < hostlen)
            hostlen = col - p;

        if (hostlen < 256) {
            host->av_val = p;
            host->av_len = hostlen;
            RTMP_Log(RTMP_LOGDEBUG, "Parsed host    : %.*s", hostlen, host->av_val);
        } else {
            RTMP_Log(RTMP_LOGWARNING, "Hostname exceeds 255 characters!");
        }

        p += hostlen;
    }

1.2.2、流程分析

  1. 如果主机为空,则报错No hostname in URL!。
  2. end = p + strlen(p);由p后移三个位置,跳过://且位于主机字符串起始点。由strlen(p)计算出剩余字符串长度并加上起始位置,得到整个URL字符串的终点位置。
  3. 跟紧主机字符串结尾的/:字符不算在主机字符串长度内。如果只有路径,无端口,则主机字符串的长度 = 路径字符串的起始位置 - 主机字符串的起始位置。如果指定了端口,则主机字符串的长度 = 路径字符串的起始位置 - 主机字符串的起始位置。
  4. 由3得到的主机字符串若超过255个字符,则警告Hostname exceeds 255 characters!。

1.2.3、验证

抽取如下代码并执行。

    /* let's get the hostname */
    p += 3;
    
    /* check for sudden death */
    if (*p == 0) {
        printf("No hostname in URL!\n");
        return FALSE;
    }
    
    end = p + strlen(p);
    printf("url length = %lu, p = %p, p length = %lu, end = %p\n", strlen(url), p, strlen(p), end);
    col = strchr(p, ':');
    ques = strchr(p, '?');
    slash = strchr(p, '/');
    printf("port = %s\nques = %s\nslash = %s\n", col, ques, slash);

    {
        int hostlen;
        if (slash)
            hostlen = slash - p;
        else
            hostlen = end - p;
        if (col && col - p < hostlen)
            hostlen = col - p;
        
        if (hostlen < 256) {
            printf("Parsed host    : %.*s\n", hostlen, p);
        } else {
            printf("Hostname exceeds 255 characters!");
        }
        p += hostlen;
    }

运行:

输入:URL 输出:host
"rTmPe://live.hkstv.hk.lxdns.com/live/hks" live.hkstv.hk.lxdns.com
"rtmp://live.hkstv.hk.lxdns.com:2000/cardhouse/s3?e=1&starttime=000010" live.hkstv.hk.lxdns.com

其中,对于输入URL
"rtmp://live.hkstv.hk.lxdns.com:2000/cardhouse/s3?e=1&starttime=000010"
printf("url length = %lu, p = %p, p length = %lu, end = %p\n", strlen(url), p, strlen(p), end);
输出
url length = 68, p = 0x10d915e3a, p length = 61, end = 0x10d915e77
可验证end为URL字符串末尾。

port = :2000/cardhouse/s3?e=1&starttime=000010
ques = ?e=1&starttime=000010
slash = /cardhouse/s3?e=1&starttime=000010

strchr找出指定字符首次出现位置的指针。

主机解析结束,开始解析端口号、应用程序名或播放路径。

1.3、解析端口号

1、源码

/* get the port number if available */
if (*p == ':') {
    unsigned int p2;
    p++;
    p2 = atoi(p);
    if (p2 > 65535) {
        RTMP_Log(RTMP_LOGWARNING, "Invalid port number!");
    } else {
        *port = p2;
    }
}

if (!slash) {
    RTMP_Log(RTMP_LOGWARNING, "No application or playpath in URL!");
    return TRUE;
}
p = slash + 1;

2、分析

如果p指向:字符,atoi持续读取字符,直到非数字字符为止,并将已读取的数值当成十进制处理,如

int i;
char *buffer = "31273a";
i = atoi (buffer);
printf ("The value entered is %d. Its double is %d.\n",i,i*2);

输出:The value entered is 31273. Its double is 62546.

3、验证

unsigned int prt, *port = &prt;
/* get the port number if available */
if (*p == ':') {
    unsigned int p2;
    p++;
    p2 = atoi(p);
    if (p2 > 65535) {
        printf("Invalid port number!");
    } else {
        *port = p2;
        printf("port = %u", *port);
    }
} else {
    printf("port does not exists.\n");
}
输入:URL 输出:port
"rtmp://live.hkstv.hk.lxdns.com:2000/cardhouse/s3?e=1&starttime=000010" 2000
"rtmp://live.hkstv.hk.lxdns.com/cardhouse/s3?e=1&starttime=000010"

1.4、解析应用程序名或播放路径

相关文章

网友评论

本文标题:LibRTMP源码分析 1:解析URL

本文链接:https://www.haomeiwen.com/subject/tdmdlttx.html