rtpCodec.cxx
上传用户:sy_wanhua
上传日期:2013-07-25
资源大小:3048k
文件大小:15k
- /* ====================================================================
- * The Vovida Software License, Version 1.0
- *
- * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- *
- * 3. The names "VOCAL", "Vovida Open Communication Application Library",
- * and "Vovida Open Communication Application Library (VOCAL)" must
- * not be used to endorse or promote products derived from this
- * software without prior written permission. For written
- * permission, please contact vocal@vovida.org.
- *
- * 4. Products derived from this software may not be called "VOCAL", nor
- * may "VOCAL" appear in their name, without prior written
- * permission of Vovida Networks, Inc.
- *
- * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
- * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
- * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
- * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
- * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
- * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
- * DAMAGE.
- *
- * ====================================================================
- *
- * This software consists of voluntary contributions made by Vovida
- * Networks, Inc. and many individuals on behalf of Vovida Networks,
- * Inc. For more information on Vovida Networks, Inc., please see
- * <http://www.vovida.org/>.
- *
- */
- static const char* const rtpCodec_cxx_Version =
- "$Id: rtpCodec.cxx,v 1.15 2001/07/26 21:50:42 kle Exp $";
- #include "rtpCodec.h"
- #include "cpLog.h"
- #include "g711.h"
- #include "rtpTypes.h"
- #include "RtpPacket.hxx"
- const int numRtpCodecInfo = 10;
- RtpCodecInfo rtpCodecInfo[numRtpCodecInfo] =
- {
- // g711ulaw
- // 80 words, 160 bytes silence payload for 20ms packetization -
- { rtpPayloadPCMU, 160,
- { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }
- },
- // g711alaw
- // 80 words, 160 bytes silence payload for 20ms packetization -
- { rtpPayloadPCMA, 160,
- { 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
- 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
- 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
- 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
- 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
- 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
- 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
- 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55 }
- },
- // gsm TODO
- // find specs
- // gsmfr
- // 17 words, 34 bytes silence payload for 20ms packetization -
- { rtpPayloadGSM, 34,
- { 0xD9, 0xEA, 0xC4, 0x6C, 0xE1, 0x84, 0x00, 0x39, 0x5C, 0x91, 0xC9, 0x2B, 0xA6, 0xA0, 0x49, 0x24, 0x95, 0x37, 0x24, 0x76,
- 0xE0, 0x4B, 0x23, 0x8E, 0x56, 0xDB, 0x56, 0xA0, 0x48, 0xE5, 0x8E, 0x4B, 0x2C, 0x52 }
- },
- // gsmfr
- // 16 words, 32 bytes silence payload for 20ms packetization -
- { rtpPayloadGSMFR, 34,
- { 0xC4, 0x8F, 0x43, 0xF9, 0x0A, 0xEA, 0xD6, 0x00, 0x00, 0x00, 0x00, 0x01, 0xB5, 0x80, 0x00, 0x00, 0x00, 0x00, 0x55, 0xAC,
- 0x00, 0x00, 0x00, 0x00, 0x03, 0x6B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
- },
- // g723r53
- // 10 words, 20 bytes silence payload for 30 ms packetization -
- { rtpPayloadG723, 20,
- { 0x95, 0x0A, 0xCE, 0xDA, 0x3F, 0x3E, 0x84, 0xC3, 0xB3, 0x4C, 0xEB, 0x62, 0xA6, 0x2A, 0x0F, 0x4C, 0x72, 0xCE, 0xA9, 0xA1 }
- },
- // g723r63
- // 12 words, 24 bytes silence payload for 30 ms packetization -
- { rtpPayloadG723, 24,
- { 0xE8, 0x2E, 0xCE, 0xDE, 0xC8, 0xB8, 0x6A, 0x84, 0xB1, 0x61, 0xE2, 0x99, 0xBE, 0x0C, 0x69, 0x71, 0x0D, 0x99, 0x53, 0x72,
- 0xFF, 0xAD, 0xB6, 0xE8 }
- },
- // g732ar63 TODO
- // find specs
- // g723ar53 TODO
- // find specs
- // g728 TODO
- // find specs
- // g726r16
- // 20 words, 40 bytes silence payload for 20ms packetization -
- { rtpPayloadG726_16, 40,
- { 0x03, 0x33, 0xC3, 0x3C, 0x3C, 0xCF, 0x3C, 0x03, 0xCF, 0x00, 0xF0, 0xF3, 0x3F, 0xFF, 0xC0, 0xCF, 0xF0, 0xF3, 0x00, 0xFC,
- 0xFF, 0x0C, 0x3C, 0x00, 0x0C, 0xFF, 0xCC, 0x03, 0x0C, 0xC0, 0x0C, 0xF0, 0xC3, 0x30, 0x30, 0x00, 0xCC, 0xF0, 0xF3, 0x0C }
- },
- // g726r24
- // 30 words, 60 bytes silence payload for 20ms packetization -
- { rtpPayloadG726_24, 60,
- { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFB, 0xFF,
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xBF, 0xFF, 0xFD, 0xFF, 0xFF, 0xFD, 0xFF, 0xE7 }
- },
- // g726r32
- // 40 words, 80 bytes silence payload for 20ms packetization -
- { rtpPayloadG726_32, 80,
- { 0xFF, 0xF1, 0xEF, 0xEF, 0xFF, 0xF1, 0xEF, 0xEF, 0x1F, 0xFF, 0xFE, 0xEF, 0xFF, 0xFE, 0x1F, 0x1F, 0xFE, 0x11, 0xEF, 0x1E,
- 0x1E, 0xEF, 0xEF, 0xFF, 0xFE, 0xFE, 0xEE, 0xEF, 0xEE, 0x1F, 0xEF, 0xFE, 0xFE, 0xE1, 0xF1, 0xFE, 0xE1, 0xFF, 0xFF, 0xEF,
- 0xFE, 0xFF, 0xEF, 0xFE, 0xF1, 0xEF, 0xFE, 0xFF, 0xEF, 0xFE, 0xFF, 0x1E, 0xEF, 0xFF, 0x1F, 0xFC, 0xFF, 0xFF, 0x1F, 0xFF,
- 0xEF, 0xFF, 0xFF, 0x1F, 0xFF, 0x1E, 0x1E, 0xEE, 0xF1, 0x1F, 0xFF, 0xEE, 0x1F, 0xFE, 0xFE, 0xEE, 0x1F, 0xFF, 0xFE, 0xFF }
- },
- // linear16mono TODO
- // find specs
- // g729 all versions TODO
- // find specs
- // unknown
- // 240 words, 480 bytes silence payload -
- { rtpPayloadUndefined, 480,
- { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
- }
- };
- /* extra unused silence packets from ducati vsp ENG-86534
- probably number under another alias
- // g728r16
- // 20 words, 40 bytes silence payload for 20ms packetization -
- { 0x58C0, 0x324C, 0xAD01, 0xD83F, 0xC415, 0x3DF6, 0xC1CF, 0xA0C5, 0x4229, 0x8442,
- 0x76D5, 0x62CD, 0xCD1C, 0xC92D, 0x2D02, 0x30D8, 0x624B, 0x568C, 0x0A3C, 0x3CC2 }
- // g729r8
- // 10 words, 20 bytes silence payload for 20ms packetization -
- { 0x4057, 0x8C6E, 0x309A, 0x87E8, 0xCA05, 0xF8FE, 0xA91E, 0xCCE3, 0x56A1, 0xE590 }
- */
- RtpPacket*
- convertRtpPacketCodec (RtpPayloadType toType, RtpPacket* packet)
- {
- RtpPacket* p = new RtpPacket(packet, 1012);
- assert (p);
- int result_size = convertCodec (packet->getPayloadType(), toType,
- packet->getPayloadLoc(), p->getPayloadLoc(),
- packet->getPayloadUsage());
- p->setPayloadType(toType);
- p->setPayloadUsage(result_size);
- return p;
- }
- // return value is size of converted data stored at out_data
- int
- convertCodec(RtpPayloadType fromType, RtpPayloadType toType,
- char* in_data, char* out_data, int len)
- {
- int result_len = -1;
- unsigned char* charBuff = (unsigned char*)out_data;
- short* shortBuff = (short*)out_data;
- unsigned char* charPayload = (unsigned char*)in_data;
- short* shortPayload = (short*)in_data;
- switch (fromType)
- {
- case rtpPayloadPCMU :
- {
- switch (toType)
- {
- case rtpPayloadL16_mono :
- case rtpPayloadL16_8k_mono :
- for (int i = 0; i < len; i++)
- shortBuff[i] = ulaw2linear (charPayload[i]);
- result_len = len * 2;
- break;
- default :
- cpLog(LOG_DEBUG, "Unsupported converstion %d -> %d",
- fromType, toType);
- break;
- }
- }
- break;
- case rtpPayloadL16_mono :
- case rtpPayloadL16_8k_mono :
- {
- switch (toType)
- {
- case rtpPayloadPCMU :
- for (int i = 0; i < len / 2; i++)
- charBuff[i] = linear2ulaw (shortPayload[i]);
- result_len = len / 2;
- break;
- default :
- cpLog(LOG_DEBUG, "Unsupported conversion %d -> %d",
- fromType, toType);
- break;
- }
- }
- break;
- default :
- {
- cpLog(LOG_DEBUG, "Unsupported conversion %d -> %d",
- fromType, toType);
- break;
- }
- }
- return result_len;
- }
- void rtp_htonl (RtpPacket* p)
- {
- #if __BYTE_ORDER == __LITTLE_ENDIAN
- unsigned char tdata;
- if (p->getPayloadType() == rtpPayloadL16_mono)
- {
- unsigned char* data = (unsigned char*) p->getPayloadLoc();
- for (int i = 0; i < p->getPayloadUsage(); i = i + 2)
- {
- tdata = data[i];
- data[i] = data[i + 1];
- data[i + 1] = tdata;
- }
- }
- #endif
- }
- void rtp_ntohl (RtpPacket* p)
- {
- #if __BYTE_ORDER == __LITTLE_ENDIAN
- unsigned char tdata;
- if (p->getPayloadType() == rtpPayloadL16_mono)
- {
- unsigned char* data = (unsigned char*) p->getPayloadLoc();
- for (int i = 0; i < p->getPayloadUsage(); i = i + 2)
- {
- tdata = data[i];
- data[i] = data[i + 1];
- data[i + 1] = tdata;
- }
- }
- #endif
- }
- char* findSilenceCodec( RtpPayloadType type, int len )
- {
- for( int i = 0; i < numRtpCodecInfo; i++ )
- {
- if( rtpCodecInfo[i].payloadType == type && rtpCodecInfo[i].length == len )
- {
- cpLog( LOG_DEBUG_STACK, "Silence packet template found at index %d",
- i );
- return (char*)&rtpCodecInfo[i].silence;
- }
- }
- cpLog( LOG_DEBUG_STACK, "Silence unknown for type:%d, len%d", type, len );
- return 0;
- }
- void createSilenceCodec (char* buffer, RtpPayloadType type, int len)
- {
- switch (type)
- {
- case rtpPayloadPCMU:
- memset (buffer, 0xFF, len);
- break;
- case rtpPayloadL16_mono: // debug
- case rtpPayloadG729: // debug
- default:
- cpLog (LOG_ERR, "Can't make exact silence for type: &d", (int)type);
- memset (buffer, 0, len);
- break;
- }
- }
- // sets the exact networkFormat information, esp network_perSampleSize
- void setNetworkFormatCodec()
- {
- return ;
- }