rtspbase.cpp

来自「symbian 下的helix player源代码」· C++ 代码 · 共 674 行 · 第 1/2 页

CPP
674
字号
/* ***** BEGIN LICENSE BLOCK *****
 * Source last modified: $Id: rtspbase.cpp,v 1.11.2.1 2004/07/09 02:04:52 hubbe Exp $
 * 
 * Portions Copyright (c) 1995-2004 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 (the "RPSL") available at
 * http://www.helixcommunity.org/content/rpsl unless you have licensed
 * the file under the current version of the RealNetworks Community
 * Source License (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.
 * 
 * Alternatively, the contents of this file may be used under the
 * terms of the GNU General Public License Version 2 or later (the
 * "GPL") in which case the provisions of the GPL are applicable
 * instead of those above. If you wish to allow use of your version of
 * this file only under the terms of the GPL, and not to allow others
 * to use your version of this file under the terms of either the RPSL
 * or RCSL, indicate your decision by deleting the provisions above
 * and replace them with the notice and other provisions required by
 * the GPL. If you do not delete the provisions above, a recipient may
 * use your version of this file under the terms of any one of the
 * RPSL, the RCSL or the GPL.
 * 
 * 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 "hxcom.h"
#include "hxtypes.h"
#include "hxstring.h"
#include "hxslist.h"
#include "hxengin.h"
#include "ihxpckts.h"
#include "chxpckts.h"
#include "safestring.h"
//#include "rtmlpars.h"
#include "mimehead.h"
#include "mimescan.h"
#include "timerep.h"
#include "rtspmsg.h"
#include "rtsppars.h"
#include "rtspmdsc.h"
#include "basepkt.h"
#include "servrsnd.h"
#include "rtspbase.h"
#include "dbcs.h" // for HXIsEqual

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

// XXXHP: NEED TO MODIFY THE SAME STRUCTURE AT DLLMAIN.CPP!!!
struct RTSPError
{
    const char* pErrNo;
    const char* pErrMsg;
};

//
// XXXBAB - turn this into a dictionary
//
// XXXHP: NEED TO REGENERATE ON WIN16 EVERYTIME THIS ARRAY
//	  IS MODIFIED!!!
#ifdef _WIN16
extern RTSPError* RTSPErrorTable;
#else
static const RTSPError RTSPErrorTable[] =
{
    { "100", "Continue" },

    { "200", "OK" },
    { "201", "Created" },
    { "250", "Low On Storage Space" },

    { "300", "Multiple Choices" },
    { "301", "Moved Permanently" },
    { "302", "Moved Temporarily" },
    { "303", "See Other" },
    { "304", "Not Modified" },
    { "305", "Use Proxy" },

    { "400", "Bad Request" },
    { "401", "Unauthorized" },
    { "402", "Payment Required" },
    { "403", "Forbidden" },
    { "404", "Not Found" },
    { "405", "Method Not Allowed" },
    { "406", "Not Acceptable" },
    { "407", "Proxy Authentication Required" },
    { "408", "Request Time-out" },
    { "409", "Conflict" },
    { "410", "Gone" },
    { "411", "Length Required" },
    { "412", "Precondition Failed" },
    { "413", "Request Entity Too Large" },
    { "414", "Request-URI Too Large" },
    { "415", "Unsupported Media Type" },
    { "451", "Parameter Not Understood" },
    { "452", "Conference Not Found" },
    { "453", "Not Enough Bandwidth" },
    { "454", "Session Not Found" },
    { "455", "Method Not Valid In This State" },
    { "456", "Header Field Not Valid For Resource" },
    { "457", "Invalid Range" },
    { "458", "Parameter Is Read-Only" },
    { "459", "Aggregate Operation Not Allowed" },
    { "460", "Only Aggregate Operation Allowed" },
    { "461", "Unsupported Transport" },
    { "462", "Destination Unreachable" },
    { "500", "Internal Server Error" },
    { "501", "Not Implemented" },
    { "502", "Bad Gateway" },
    { "503", "Service Unavailable" },
    { "504", "Gateway Time-out" },
    { "505", "RTSP Version not supported" },
    { "551", "Option not supported" }
};
#endif

const RTSPRequireOptions RTSPBaseProtocol::RTSPRequireOptionsTable
    [RTSPBaseProtocol::m_NumRTSPRequireOptions] =
    {
        {"com.real.retain-entity-for-setup", "DESCRIBE", 8},
        {"com.real.load-test-password-enabled", "OPTIONS", 7}
    };

const RTSPAcceptEncodingOptions RTSPBaseProtocol::RTSPAcceptEncodingOptionsTable
    [RTSPBaseProtocol::m_NumRTSPAcceptEncodingOptions] =
    {
        {"mei", "DESCRIBE", 8},
        {"mei", "SETUP", 5}
    };

/*
 * RTSPBaseProtocol methods
 */

RTSPBaseProtocol::RTSPBaseProtocol():
    m_pContext(NULL),
    m_pCommonClassFactory(NULL),
    m_pSocket(0),
    m_pFastSocket(0),
    m_uControlBytesSent(0),
    m_pControlBuffer(0)
{
}

RTSPBaseProtocol::~RTSPBaseProtocol()
{
    clearMessages();

    HX_RELEASE(m_pControlBuffer);
    HX_RELEASE(m_pCommonClassFactory);
    HX_RELEASE(m_pContext);
}

HX_RESULT
RTSPBaseProtocol::enqueueMessage(RTSPMessage* pMsg)
{
    m_msgQueue.AddTail(pMsg);
    return HXR_OK;
}

RTSPMessage*
RTSPBaseProtocol::dequeueMessage(UINT32 seqNo)
{
    LISTPOSITION pos = m_msgQueue.GetHeadPosition();
    while(pos)
    {
        RTSPMessage* pMsg = (RTSPMessage*)m_msgQueue.GetAt(pos);
        if(pMsg->seqNo() == seqNo)
        {
            (void)m_msgQueue.RemoveAt(pos);
            return pMsg;
        }
        (void)m_msgQueue.GetNext(pos);
    }
    return 0;
}

void
RTSPBaseProtocol::clearMessages()
{
    LISTPOSITION pos = m_msgQueue.GetHeadPosition();
    while(pos)
    {
        RTSPMessage* pMsg = (RTSPMessage*)m_msgQueue.GetNext(pos);
        delete pMsg;
    }
}

const char*
RTSPBaseProtocol::getErrorText(const char* pErrNo)
{
    int tabSize = sizeof(RTSPErrorTable) / sizeof(RTSPErrorTable[0]);
    for (int i = 0; i < tabSize; ++i)
    {
        if (strcmp(pErrNo, RTSPErrorTable[i].pErrNo) == 0)
        {
            return RTSPErrorTable[i].pErrMsg;
        }
    }
    return "";
}

HX_RESULT
RTSPBaseProtocol::sendRequest(RTSPRequestMessage* pMsg,
    const char* pContent, const char* pMimeType, UINT32 seqNo)
{
    if(pContent)
    {
        char tmpBuf[32];
        pMsg->addHeader("Content-type", pMimeType);
        SafeSprintf(tmpBuf, 32,"%d", strlen(pContent)); 
        pMsg->addHeader("Content-length", tmpBuf);
        pMsg->setContent(pContent);
    }
    return sendRequest(pMsg, seqNo);
}

HX_RESULT
RTSPBaseProtocol::sendRequest(RTSPRequestMessage* pMsg, UINT32 seqNo)
{
    // set sequence number
    char seqBuf[32]; 
    SafeSprintf(seqBuf, 32, "%ld", seqNo); 
    pMsg->addHeader("CSeq", seqBuf, TRUE);	// add to head of list
    pMsg->setSeqNo(seqNo);
    enqueueMessage(pMsg);
    CHXString msgStr = pMsg->asString();

    CHXBuffer* pBuffer = new CHXBuffer;
    pBuffer->AddRef();
    pBuffer->Set((BYTE*)(const char*)msgStr, msgStr.GetLength());
    HX_RESULT rc = sendControlMessage(pBuffer);
    handleSendEvent(pMsg);
    pBuffer->Release();
    return HXR_OK;
}

void
RTSPBaseProtocol::handleDebug(IHXBuffer* pMsgBuf, BOOL bInbound)
{
    // Only implemented by subclasses in server modules
}

void
RTSPBaseProtocol::handleTiming(IHXBuffer* pMsgBuf, BOOL bInbound)
{
    // Only implemented by subclasses in server modules
}

void 
RTSPBaseProtocol::handleSendEvent(RTSPRequestMessage* pMsg)
{
    // Only implemented by subclasses in server modules
}

RTSPResponseMessage*
RTSPBaseProtocol::makeResponseMessage(UINT32 seqNo, const char* pErrNo)
{
    RTSPResponseMessage* pMsg = new RTSPResponseMessage;

    // set sequence number
    char seqBuf[32]; 
    SafeSprintf(seqBuf,32,"%ld", seqNo);
    pMsg->addHeader("CSeq", seqBuf, TRUE);	// add to head of list
    pMsg->setSeqNo(seqNo);

    pMsg->setErrorCode(pErrNo);
    pMsg->setErrorMsg(getErrorText(pErrNo));
    UTCTimeRep utcNow;
    pMsg->addHeader("Date", utcNow.asRFC1123String());
    return pMsg;
}

HX_RESULT
RTSPBaseProtocol::sendResponse(UINT32 seqNo, const char* pErrNo)
{
    RTSPResponseMessage* pMsg = makeResponseMessage(seqNo, pErrNo);
    HX_RESULT err = sendResponse(pMsg);
    delete pMsg;
    return err;
}

HX_RESULT
RTSPBaseProtocol::sendResponse(
    RTSPResponseMessage* pMsg, const char* pContent, const char* pMimeType)
{
    if (pContent != NULL)
    {
        char tmpBuf[32]; 
        pMsg->addHeader("Content-type", pMimeType);
        SafeSprintf(tmpBuf,32, "%d", strlen(pContent));  // only for strings 
        pMsg->addHeader("Content-length", tmpBuf);
        pMsg->setContent(pContent);
    }

    CHXString msgStr = pMsg->asString();

    CHXBuffer* pBuffer = new CHXBuffer;
    pBuffer->AddRef();
    pBuffer->Set((BYTE*)(const char*)msgStr, msgStr.GetLength());
    sendControlMessage(pBuffer);
    handleSendEvent(pMsg);
    pBuffer->Release();
    return HXR_OK;
}

void 
RTSPBaseProtocol::handleSendEvent(RTSPResponseMessage* pMsg)
{
    // Only implmented by subclasses in server modules

⌨️ 快捷键说明

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