sdptools.cpp
Upload User: dangjiwu
Upload Date: 2013-07-19
Package Size: 42019k
Code Size: 8k
Category:

Symbian

Development Platform:

Visual C++

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Source last modified: $Id: sdptools.cpp,v 1.6.2.1 2004/07/09 02:05:26 hubbe Exp $
  3.  * 
  4.  * Portions Copyright (c) 1995-2004 RealNetworks, Inc. All Rights Reserved.
  5.  * 
  6.  * The contents of this file, and the files included with this file,
  7.  * are subject to the current version of the RealNetworks Public
  8.  * Source License (the "RPSL") available at
  9.  * http://www.helixcommunity.org/content/rpsl unless you have licensed
  10.  * the file under the current version of the RealNetworks Community
  11.  * Source License (the "RCSL") available at
  12.  * http://www.helixcommunity.org/content/rcsl, in which case the RCSL
  13.  * will apply. You may also obtain the license terms directly from
  14.  * RealNetworks.  You may not use this file except in compliance with
  15.  * the RPSL or, if you have a valid RCSL with RealNetworks applicable
  16.  * to this file, the RCSL.  Please see the applicable RPSL or RCSL for
  17.  * the rights, obligations and limitations governing use of the
  18.  * contents of the file.
  19.  * 
  20.  * Alternatively, the contents of this file may be used under the
  21.  * terms of the GNU General Public License Version 2 or later (the
  22.  * "GPL") in which case the provisions of the GPL are applicable
  23.  * instead of those above. If you wish to allow use of your version of
  24.  * this file only under the terms of the GPL, and not to allow others
  25.  * to use your version of this file under the terms of either the RPSL
  26.  * or RCSL, indicate your decision by deleting the provisions above
  27.  * and replace them with the notice and other provisions required by
  28.  * the GPL. If you do not delete the provisions above, a recipient may
  29.  * use your version of this file under the terms of any one of the
  30.  * RPSL, the RCSL or the GPL.
  31.  * 
  32.  * This file is part of the Helix DNA Technology. RealNetworks is the
  33.  * developer of the Original Code and owns the copyrights in the
  34.  * portions it created.
  35.  * 
  36.  * This file, and the files included with this file, is distributed
  37.  * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY
  38.  * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS
  39.  * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES
  40.  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET
  41.  * ENJOYMENT OR NON-INFRINGEMENT.
  42.  * 
  43.  * Technology Compatibility Kit Test Suite(s) Location:
  44.  *    http://www.helixcommunity.org/content/tck
  45.  * 
  46.  * Contributor(s):
  47.  * 
  48.  * ***** END LICENSE BLOCK ***** */
  49. /****************************************************************************
  50.  *  Defines
  51.  */
  52. #define AUDIO_MIMETYPE_PREFIX     "audio/"
  53. #define AUDIO_MIMETYPE_PREFIX_SIZE  (sizeof(AUDIO_MIMETYPE_PREFIX) - 1)
  54. #define VIDEO_MIMETYPE_PREFIX     "video/"
  55. #define VIDEO_MIMETYPE_PREFIX_SIZE  (sizeof(VIDEO_MIMETYPE_PREFIX) - 1)
  56. #define APP_MIMETYPE_PREFIX     "application/"
  57. #define APP_MIMETYPE_PREFIX_SIZE    (sizeof(APP_MIMETYPE_PREFIX) - 1)
  58. /****************************************************************************
  59.  *  Includes
  60.  */
  61. //#include "hlxclib/stdlib.h"
  62. #include "sdptools.h"
  63. #include "hxstrutl.h"
  64. #include "hxassert.h"
  65. #include "hxstring.h"
  66. #include "hxinline.h"
  67. /****************************************************************************
  68.  *  Locals
  69.  */
  70. HX_INLINE HX_RESULT _HexCharPairToByte(UINT8* pByte, const char* pCharPair);
  71. HX_INLINE void _ByteToHexCharPair(const UINT8 pByte, char* pCharPair);
  72. /****************************************************************************
  73.  *  Tools
  74.  */
  75. HX_RESULT HexStringToBinary(UINT8* pOutBuffer, const char* pString)
  76. {
  77.     HX_RESULT retVal = HXR_OK;
  78.     HX_ASSERT(pOutBuffer);
  79.     HX_ASSERT(pString);
  80.     while (SUCCEEDED(retVal) && ((*pString) != ''))
  81.     {
  82. retVal = _HexCharPairToByte(pOutBuffer, pString);
  83. pOutBuffer++;
  84. pString += 2;
  85.     }
  86.     return retVal;
  87. }
  88. HX_RESULT HexCharPairToByte(UINT8* pByte, const char* pCharPair)
  89. {
  90.     return _HexCharPairToByte(pByte, pCharPair);
  91. }
  92. HX_INLINE HX_RESULT _HexCharPairToByte(UINT8* pByte, const char* pCharPair)
  93. {
  94.     UINT8 uIdx = 2;
  95.     UINT8 uByte = 0;
  96.     UINT8 uAddVal;
  97.     char cBits;
  98.     do
  99.     {
  100. uIdx--;
  101. cBits = *pCharPair;
  102. if ((cBits >= '0') && (cBits <= '9'))
  103. {
  104.     uAddVal = cBits - '0';
  105. }
  106. else if ((cBits >= 'a') && (cBits <= 'f'))
  107. {
  108.     uAddVal = cBits - 'a' + 10;
  109. }
  110. else if ((cBits >= 'A') && (cBits <= 'F'))
  111. {
  112.     uAddVal = cBits - 'A' + 10;
  113. }
  114. else
  115. {
  116.     return HXR_FAIL;
  117. }
  118. uByte += (uAddVal << (4 * uIdx));
  119. pCharPair++;
  120.     } while ((uIdx != 0) && (*pCharPair != ''));
  121.     *pByte = uByte;
  122.     return HXR_OK;
  123. }
  124. void BinaryToHexString(const UINT8* pBuffer, UINT32 ulSize, char* pString)
  125. {
  126.     HX_ASSERT(pBuffer);
  127.     HX_ASSERT(pString);
  128.     for(UINT32 i = 0; i < ulSize; i++)
  129.     {
  130. _ByteToHexCharPair(pBuffer[i], &pString[i*2]);
  131.     }
  132.     pString[ulSize*2] = '';
  133. }
  134. void ByteToHexCharPair(const UINT8 uByte, char* pCharPair)
  135. {
  136.     _ByteToHexCharPair(uByte, pCharPair);
  137. }
  138. HX_INLINE void _ByteToHexCharPair(const UINT8 uByte, char* pCharPair)
  139. {
  140.     HX_ASSERT(pCharPair);
  141.     char c = (char)((uByte >> 4) & 0x0f);
  142.     pCharPair[0] = c < 0xa ? c + '0' : c - 0xa + 'a';
  143.     c = (char)(uByte & 0x0f);
  144.     pCharPair[1] = c < 0xa ? c + '0' : c - 0xa + 'a';
  145. }
  146. RTSPMediaType SDPMapMimeToMediaType(const char* pMimeType)
  147. {
  148.     RTSPMediaType eMediaType = RTSPMEDIA_TYPE_UNKNOWN;
  149.     if (pMimeType)
  150.     {
  151. if (strncasecmp(AUDIO_MIMETYPE_PREFIX, 
  152. pMimeType, 
  153. AUDIO_MIMETYPE_PREFIX_SIZE) == 0)
  154. {
  155.     eMediaType = RTSPMEDIA_TYPE_AUDIO;
  156. }
  157. else if (strncasecmp(VIDEO_MIMETYPE_PREFIX, 
  158. pMimeType, 
  159. VIDEO_MIMETYPE_PREFIX_SIZE) == 0)
  160. {
  161.     eMediaType = RTSPMEDIA_TYPE_VIDEO;
  162. }
  163. else if (strncasecmp(APP_MIMETYPE_PREFIX, 
  164. pMimeType, 
  165. APP_MIMETYPE_PREFIX_SIZE) == 0)
  166. {
  167.     eMediaType = RTSPMEDIA_TYPE_APP;
  168. }
  169.     }
  170.     return eMediaType;
  171. }
  172. /*
  173.  * Copies SDPData from pSDP tp pNewSDP, removing any tokens matching pTokens
  174.  * pTokens is a NULL terminated list of strings
  175.  */
  176. UINT32 RemoveSDPTokens(const char* pTokens[], const UINT32 ulTokenLen[],
  177.                       const char* pSDP, UINT32 ulSDPLen, 
  178.                       char* pNewSDP, UINT32 ulNewSDPSize)
  179. {
  180.     UINT32 ulBytesCopied = 0;
  181.     UINT32 i = 0;
  182.     UINT32 ulEnd;
  183.     UINT32 ulCopySize;
  184.     char* pWriter = pNewSDP;
  185.     while(pSDP[i] != '' && i < ulSDPLen)
  186.     {
  187.         // ignore trailing white space
  188.         for( ; i < ulSDPLen && isspace(pSDP[i]); i++);
  189.         // Find the end of the line
  190.         for(ulEnd = i; ulEnd < ulSDPLen && pSDP[ulEnd] != ''; ulEnd++)
  191.         {
  192.             if(pSDP[ulEnd] == 'r' && ulEnd + 1 < ulSDPLen && 
  193.                 pSDP[ulEnd + 1] == 'n')
  194.             {
  195.                 ulEnd +=2;
  196.                 break;
  197.             }
  198.         }
  199.         // See if this one is to be removed
  200.         BOOL bRemove = FALSE;
  201.         for(int j=0; pTokens[j] && !bRemove; j++)
  202.         {
  203.             if(ulTokenLen[j] < ulSDPLen - i &&
  204.                 strncasecmp(&pSDP[i], pTokens[j], ulTokenLen[j]) == 0)
  205.             {
  206.                 bRemove = TRUE;
  207.             }
  208.         }
  209.         // copy it if it doesn't match a remove token
  210.         if(!bRemove)
  211.         {
  212.             HX_ASSERT(strncasecmp(&pSDP[i], "a=", 2) == 0);
  213.             ulCopySize = ulEnd - i;
  214.             if(ulCopySize < ulNewSDPSize)
  215.             {
  216.                 memcpy(pWriter, &pSDP[i], ulCopySize);
  217.                 ulNewSDPSize -= ulCopySize;
  218.                 pWriter += ulCopySize;
  219.             }
  220.         }
  221.         i = ulEnd;
  222.     }
  223.     return pWriter - pNewSDP;
  224. }