⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rtsppars.cpp

📁 著名的 helix realplayer 基于手机 symbian 系统的 播放器全套源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* ***** BEGIN LICENSE BLOCK ***** 
 * Version: RCSL 1.0/RPSL 1.0 
 *  
 * Portions Copyright (c) 1995-2002 RealNetworks, Inc. All Rights Reserved. 
 *      
 * The contents of this file, and the files included with this file, are 
 * subject to the current version of the RealNetworks Public Source License 
 * Version 1.0 (the "RPSL") available at 
 * http://www.helixcommunity.org/content/rpsl unless you have licensed 
 * the file under the RealNetworks Community Source License Version 1.0 
 * (the "RCSL") available at http://www.helixcommunity.org/content/rcsl, 
 * in which case the RCSL will apply. You may also obtain the license terms 
 * directly from RealNetworks.  You may not use this file except in 
 * compliance with the RPSL or, if you have a valid RCSL with RealNetworks 
 * applicable to this file, the RCSL.  Please see the applicable RPSL or 
 * RCSL for the rights, obligations and limitations governing use of the 
 * contents of the file.  
 *  
 * This file is part of the Helix DNA Technology. RealNetworks is the 
 * developer of the Original Code and owns the copyrights in the portions 
 * it created. 
 *  
 * This file, and the files included with this file, is distributed and made 
 * available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 
 * EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL SUCH WARRANTIES, 
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS 
 * FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 
 * 
 * Technology Compatibility Kit Test Suite(s) Location: 
 *    http://www.helixcommunity.org/content/tck 
 * 
 * Contributor(s): 
 *  
 * ***** END LICENSE BLOCK ***** */ 

//#include "hlxclib/stdlib.h"
//#include "hlxclib/stdio.h"

#include "hxcom.h"
#include "hxtypes.h"
#include "hxassert.h"
#include "debug.h"

#include "hxstrutl.h"
#include "hxstring.h"
#include "hxslist.h"
#include "mimehead.h"
#include "rtsppars.h"
#include "smpte.h"
#include "nptime.h"
#include "rtspmsg.h"
#include "chxpckts.h"
#include "hxauth.h"

#include "hxheap.h"
#ifdef _DEBUG
#undef HX_THIS_FILE		
static const char HX_THIS_FILE[] = __FILE__;
#endif


RTSPParser::RTSPParser()
{
}

RTSPParser::~RTSPParser()
{
    clearMessageLines();
}

/*
 * Extract major/minor version info
 */
int 
RTSPParser::parseProtocolVersion(const CHXString& prot,
    int& majorVersion, int& minorVersion)
{
    if(strncasecmp(prot, "RTSP/", 5) != 0)
    	return 0;

    int nVerOffset = prot.Find('.');
    if(nVerOffset > 5)
    {
	CHXString majVersion = prot.Mid(5, nVerOffset-5);
	majorVersion = (int)strtol(majVersion, 0, 10);
	CHXString minVersion = prot.Mid(nVerOffset+1);
	minorVersion = (int)strtol(minVersion, 0, 10);
	return 1;
    }
    return 0;
}

/*
 * Parse option in form 'value *[;name[=value]]'
 */
int 
RTSPParser::parseHeaderValue(const char* pValue, MIMEHeader* pHeader)
{
    if(strlen(pValue) == 0)
	return 0;

    MIMEInputStream input(pValue, strlen(pValue));
    MIMEScanner scanner(input);

    MIMEHeaderValue* pHeaderValue = 0;

    MIMEToken tok = scanner.nextToken(";=");
    while(tok.hasValue())
    {
	if(!pHeaderValue)
	{
	    CHXString attribute = tok.value();
	    pHeaderValue = new MIMEHeaderValue;
	    if(!pHeaderValue)
	    {
	        return 0;
	    }
	    if(tok.lastChar() == '=')
	    {
		tok = scanner.nextToken(";");
		CHXString value = tok.value();
		pHeaderValue->addParameter(attribute, value);
	    }
	    else
	    {
		pHeaderValue->addParameter(attribute);
	    }
	}
	else if(tok.lastChar() == '=')
	{
	    CHXString attribute = tok.value();
	    tok = scanner.nextToken(";");
	    CHXString value = tok.value();
	    pHeaderValue->addParameter(attribute, value);
	}
	else
	{
	    CHXString attribute = tok.value();
	    pHeaderValue->addParameter(attribute, "");
	}
	tok = scanner.nextToken("=;");
    }
    if(pHeaderValue)
	pHeader->addHeaderValue(pHeaderValue);
    return 0;
}

int
RTSPParser::parseRangeValue(const char* pValue, MIMEHeader* pHeader)
{
    MIMEInputStream input(pValue, strlen(pValue));
    MIMEScanner scanner(input);

    MIMEToken nextTok = scanner.nextToken("=");
    if(strcasecmp(nextTok.value(), "smpte") == 0)
    {
	UINT32 tBegin = RTSP_PLAY_RANGE_BLANK;
	UINT32 tEnd = RTSP_PLAY_RANGE_BLANK;
	nextTok = scanner.nextToken("-");
	if(nextTok.hasValue())
	{
	    SMPTETimeCode tCode1(nextTok.value());
	    tBegin = UINT32(tCode1);
	}
	if((nextTok.lastChar() != MIMEToken::T_EOL) ||
	   (nextTok.lastChar() != MIMEToken::T_EOF))
	{
	    nextTok = scanner.nextToken("\n");
	    if(nextTok.hasValue())
	    {
		SMPTETimeCode tCode2(nextTok.value());
		tEnd = UINT32(tCode2);
	    }
	}
	pHeader->addHeaderValue(new RTSPRange(tBegin, tEnd,
	    RTSPRange::TR_SMPTE));
    }
    else if(strcasecmp(nextTok.value(), "npt") == 0)
    {
	UINT32 tBegin = RTSP_PLAY_RANGE_BLANK;
	UINT32 tEnd = RTSP_PLAY_RANGE_BLANK;
	nextTok = scanner.nextToken("-");
	if(nextTok.hasValue())
	{
	    NPTime t1(nextTok.value());
	    tBegin = UINT32(t1);
	}
	if((nextTok.lastChar() != MIMEToken::T_EOL) ||
	   (nextTok.lastChar() != MIMEToken::T_EOF))
	{
	    nextTok = scanner.nextToken("\n");
	    if(nextTok.hasValue())
	    {
		NPTime t2(nextTok.value());
		tEnd = UINT32(t2);
	    }
	}
	pHeader->addHeaderValue(new RTSPRange(tBegin, tEnd,
	    RTSPRange::TR_NPT));
    }
    else if(strcasecmp(nextTok.value(), "clock") == 0)
    {
	//XXXBAB fill this in
    }
    return 0;
}

int
RTSPParser::parseAuthenticationValue(const char* pValue, MIMEHeader* pHeader)
{
    MIMEInputStream input(pValue, strlen(pValue));
    MIMEScanner scanner(input);

    MIMEToken nextTok = scanner.nextToken(" ");
    if(strcasecmp(nextTok.value(), "HXPrivate") == 0)
    {
	nextTok = scanner.nextToken("=");
	if(strcasecmp(nextTok.value(), "nonce") == 0)
	{
	    nextTok = scanner.nextToken();
	    pHeader->addHeaderValue(new RTSPAuthentication(nextTok.value(),
		RTSPAuthentication::AU_HX_PRIVATE));
	}
    }
    return 0;
}

int
RTSPParser::parsePEPInfoHeaderValues(const char* pValue, MIMEHeader* pHeader)
{
    MIMEInputStream input(pValue, strlen(pValue));
    MIMEScanner scanner(input);
    BOOL bStrengthMust = FALSE;

    MIMEToken nextTok = scanner.nextToken(" {}");
    while(nextTok.lastChar() != MIMEToken::T_EOF)
    {
	if(strcasecmp(nextTok.value(), "strength") == 0)
	{
	    nextTok = scanner.nextToken(" }");
	    if(strcasecmp(nextTok.value(), "must") == 0)
	    {
		bStrengthMust = TRUE;
		break;
	    }
	}
	nextTok = scanner.nextToken(" {}");
    }
    pHeader->addHeaderValue(new RTSPPEPInfo(bStrengthMust));
    return 0;
}

int
RTSPParser::parseRTPInfoHeaderValues(const char* pValue, MIMEHeader* pHeader)
{
    // There is a bug in RFC2326 related to the ambiguity of "url=" parameter in
    // RTP-Info. More details can be found at:
    // http://sourceforge.net/tracker/index.php?func=detail&group_id=23194&atid=377744&aid=448521
    //
    // For now, we only can do the best we can:
    // 1. find stream URL entry based on ",url="
    // 2. read "url", "seq" and "rtptime" within the stream URL entry
    if (!pValue || 0 == strlen(pValue))
    {
        return 0;
    }

    const char* pCurrentEntry = NULL;
    const char* pNextEntry = NULL;
    CHXString   value = pValue;
    CHXString   entry;

    pCurrentEntry = strstr(pValue, "url=");
    // RTP-Info starts with "url="
    HX_ASSERT(pCurrentEntry == pValue);

    while (pNextEntry = NextRTPInfoEntry(pCurrentEntry+4, "url=", ','))
    {
        // retreive stream URL entry
        entry = value.Mid(pCurrentEntry - pValue, pNextEntry - pCurrentEntry);

        // retrieve "url", "seq" and "rtptime"
        SetRTPInfoEntry(entry, pHeader);
        
        pCurrentEntry = pNextEntry;
    }

    entry = value.Mid(pCurrentEntry - pValue);
    SetRTPInfoEntry(entry, pHeader);

    return 0;
}

const char*
RTSPParser::NextRTPInfoEntry(const char* pValue, const char* pTarget, const char cDelimiter)
{
    const char* pEntry = NULL;

    while (pEntry = strstr(pValue, pTarget))
    {
        const char* pTemp = pEntry;

        // a valid entry should begin with:
        // ",url=" OR ", url="

        // rewind and skip all the spaces in between
        while (*(--pTemp) == ' ') {}

        // the first non-space character should be the delimiter
        if (*pTemp == cDelimiter)
        {
            break;
        }
        // not valid entry, keep looking 
        else
        {
            pValue = pEntry + strlen(pTarget);
        }
    }

    return pEntry;
}

int 
RTSPParser::ReadRTPInfoEntry(CHXString      in, 
                             INT32          i,
                             INT32          length,
                             CHXString&     out)
{
    UINT32 ulLength = 0;
    CHXString temp;

    if (length > 0)
    {
        temp = in.Mid(i, length);
    }
    else
    {
        temp = in.Mid(i);
    }

    temp.TrimLeft();
    temp.TrimRight();

    ulLength = temp.GetLength();
    // remove trailing ',' or ';' in case there is any
    if (temp[ulLength-1] == ',' || temp[ulLength-1] == ';')
    {
        out = temp.Mid(0, ulLength-1);
        out.TrimRight();
    }
    else
    {
        out = temp;
    }

    return 0;
}

int
RTSPParser::SetRTPInfoEntry(CHXString in, MIMEHeader* pHeader)
{
    INT32 lURL = -1, lSeq = -1, lRTPTime = -1;
    CHXString URLValue, SeqValue, RTPTimeValue;
    MIMEHeaderValue* pHeaderValue = new MIMEHeaderValue;

    const char* pIn = (const char*)in;    
    const char* pSeq = NextRTPInfoEntry(pIn, "seq=", ';');
    const char* pRTPTime = NextRTPInfoEntry(pIn, "rtptime=", ';');

    lURL = in.Find("url=");
    HX_ASSERT(lURL == 0);

    if (pSeq)
    {
        lSeq = pSeq - pIn;
    }

    if (pRTPTime)
    {
        lRTPTime = pRTPTime - pIn;
    }

    // both seq and rtptime are present
    if (lSeq > 0 && lRTPTime > 0)
    {
        // url;seq;rtptime
        if (lRTPTime > lSeq)
        {
            ReadRTPInfoEntry(in, lURL+4, lSeq-lURL-5, URLValue);
            ReadRTPInfoEntry(in, lSeq+4, lRTPTime-lSeq-5, SeqValue);
            ReadRTPInfoEntry(in, lRTPTime+8, -1, RTPTimeValue);
        }
        // url;rtptime;seq
        else
        {
            ReadRTPInfoEntry(in, lURL+4, lRTPTime-lURL-5, URLValue);
            ReadRTPInfoEntry(in, lRTPTime+8, lSeq-lRTPTime-9, RTPTimeValue);
            ReadRTPInfoEntry(in, lSeq+4, -1, SeqValue);
        }
    }
    // url;seq
    else if (lSeq > 0)
    {
        ReadRTPInfoEntry(in, lURL+4, lSeq-lURL-5, URLValue);
        ReadRTPInfoEntry(in, lSeq+4, -1, SeqValue);
    }
    // url;rtptime
    else if (lRTPTime > 0)
    {
        ReadRTPInfoEntry(in, lURL+4, lRTPTime-lURL-5, URLValue);
        ReadRTPInfoEntry(in, lRTPTime+8, -1, RTPTimeValue);
    }
    // invalid case, either seq or rtptime has to be present
    else
    {
        HX_ASSERT(FALSE);
    }

    if (!URLValue.IsEmpty())
    {
        pHeaderValue->addParameter("url", URLValue);
    }

    if (!SeqValue.IsEmpty())
    {
        pHeaderValue->addParameter("seq", SeqValue);
    }

    if (!RTPTimeValue.IsEmpty())
    {
        pHeaderValue->addParameter("rtptime", RTPTimeValue);
    }

    pHeader->addHeaderValue(pHeaderValue);

    return 0;
}

int
RTSPParser::parseBackChannelValue(const char* pValue, MIMEHeader* pHeader)
{
    MIMEInputStream input(pValue, strlen(pValue));
    MIMEScanner scanner(input);

    MIMEToken nextTok = scanner.nextToken();
    pHeader->addHeaderValue(new MIMEHeaderValue(nextTok.value()));
    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -