home *** CD-ROM | disk | FTP | other *** search
/ ftp.muug.mb.ca / 2014.06.ftp.muug.mb.ca.tar / ftp.muug.mb.ca / pub / openh323.tar.gz / openh323.tar / openh323 / src / g726codec.cxx < prev    next >
C/C++ Source or Header  |  2002-11-09  |  7KB  |  245 lines

  1. /*
  2.  * g726codec.cxx
  3.  *
  4.  * H.323 protocol handler
  5.  *
  6.  * Open H323 Library
  7.  *
  8.  * Copyright (c) 1998-2000 Equivalence Pty. Ltd.
  9.  *
  10.  * The contents of this file are subject to the Mozilla Public License
  11.  * Version 1.0 (the "License"); you may not use this file except in
  12.  * compliance with the License. You may obtain a copy of the License at
  13.  * http://www.mozilla.org/MPL/
  14.  *
  15.  * Software distributed under the License is distributed on an "AS IS"
  16.  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  17.  * the License for the specific language governing rights and limitations
  18.  * under the License.
  19.  *
  20.  * The Original Code is Open H323 Library.
  21.  *
  22.  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
  23.  *
  24.  * Contributor(s): ______________________________________.
  25.  *
  26.  * $Log: g726codec.cxx,v $
  27.  * Revision 1.5  2002/11/09 07:07:10  robertj
  28.  * Made public the media format names.
  29.  * Other cosmetic changes.
  30.  *
  31.  * Revision 1.4  2002/09/03 07:28:42  robertj
  32.  * Cosmetic change to formatting.
  33.  *
  34.  */
  35.  
  36. #include <ptlib.h>
  37.  
  38. #ifdef __GNUC__
  39. #pragma implementation "g726codec.h"
  40. #endif
  41.  
  42. #include "g726codec.h"
  43.  
  44. #include "rtp.h"
  45.  
  46. extern "C" {
  47. #include "g726/g72x.h"
  48. };
  49.  
  50.  
  51. #define new PNEW
  52.  
  53.  
  54. OpalMediaFormat const OpalG726_40(OPAL_G726_40,
  55.                                   OpalMediaFormat::DefaultAudioSessionID,
  56.                                   RTP_DataFrame::G721,
  57.                                   TRUE,  // Needs jitter
  58.                                   40000, // bits/sec
  59.                                   5,  // 4 bytes per "frame"
  60.                                   8,  // 1 millisecond
  61.                                   OpalMediaFormat::AudioTimeUnits);
  62.  
  63. OpalMediaFormat const OpalG726_32(OPAL_G726_32,
  64.                                   OpalMediaFormat::DefaultAudioSessionID,
  65.                                   RTP_DataFrame::G721,
  66.                                   TRUE,  // Needs jitter
  67.                                   32000, // bits/sec
  68.                                   4,  // 4 bytes per "frame"
  69.                                   8,  // 1 millisecond
  70.                                   OpalMediaFormat::AudioTimeUnits);
  71.  
  72. OpalMediaFormat const OpalG726_24(OPAL_G726_24,
  73.                                   OpalMediaFormat::DefaultAudioSessionID,
  74.                                   RTP_DataFrame::G721,
  75.                                   TRUE,  // Needs jitter
  76.                                   24000, // bits/sec
  77.                                   3,  // 4 bytes per "frame"
  78.                                   8,  // 1 millisecond
  79.                                   OpalMediaFormat::AudioTimeUnits);
  80.  
  81. OpalMediaFormat const OpalG726_16(OPAL_G726_16,
  82.                                   OpalMediaFormat::DefaultAudioSessionID,
  83.                                   RTP_DataFrame::G721,
  84.                                   TRUE,  // Needs jitter
  85.                                   16000, // bits/sec
  86.                                   2,  // 4 bytes per "frame"
  87.                                   8,  // 1 millisecond
  88.                                   OpalMediaFormat::AudioTimeUnits);
  89.  
  90.  
  91. H323_REGISTER_CAPABILITY_FUNCTION(H323_G726_40_Capability, OPAL_G726_40"{sw}", ep)
  92. {
  93.   return new H323_G726_Capability(ep, H323_G726_Capability::e_40k);
  94. }
  95.  
  96. H323_REGISTER_CAPABILITY_FUNCTION(H323_G726_32_Capability, OPAL_G726_32"{sw}", ep)
  97. {
  98.   return new H323_G726_Capability(ep, H323_G726_Capability::e_32k);
  99. }
  100.  
  101. H323_REGISTER_CAPABILITY_FUNCTION(H323_G726_24_Capability, OPAL_G726_24"{sw}", ep)
  102. {
  103.   return new H323_G726_Capability(ep, H323_G726_Capability::e_24k);
  104. }
  105.  
  106. H323_REGISTER_CAPABILITY_FUNCTION(H323_G726_16_Capability, OPAL_G726_16"{sw}", ep)
  107. {
  108.   return new H323_G726_Capability(ep, H323_G726_Capability::e_16k);
  109. }
  110.  
  111.  
  112. struct G726_NonStandardInfo {
  113.   char name[10]; /// G.726-xxk
  114.   BYTE count;
  115. };
  116.  
  117.  
  118. static G726_NonStandardInfo const G726_NonStandard[H323_G726_Capability::NumSpeeds] = {
  119.   { OPAL_G726_40 },
  120.   { OPAL_G726_32 },
  121.   { OPAL_G726_24 },
  122.   { OPAL_G726_16 }
  123. };
  124.  
  125.  
  126. /////////////////////////////////////////////////////////////////////////////
  127.  
  128. H323_G726_Capability::H323_G726_Capability(H323EndPoint & endpoint, Speeds s)
  129.     : H323NonStandardAudioCapability(240, 10, endpoint,
  130.                                      (const BYTE *)&G726_NonStandard[s],
  131.                                      sizeof(G726_NonStandard),
  132.                                      0, sizeof(G726_NonStandard[s].name))
  133. {
  134.   speed = s;
  135. }
  136.  
  137.  
  138. PObject * H323_G726_Capability::Clone() const
  139. {
  140.   return new H323_G726_Capability(*this);
  141. }
  142.  
  143.  
  144. PString H323_G726_Capability::GetFormatName() const
  145. {
  146.   return PString(G726_NonStandard[speed].name) + "{sw}";
  147. }
  148.  
  149.  
  150. BOOL H323_G726_Capability::OnSendingPDU(H245_AudioCapability & pdu,
  151.                                         unsigned packetSize) const
  152. {
  153.   G726_NonStandardInfo * info = (G726_NonStandardInfo *)(const BYTE *)nonStandardData;
  154.   info->count = (BYTE)packetSize;
  155.   return H323NonStandardAudioCapability::OnSendingPDU(pdu, packetSize);
  156. }
  157.  
  158.  
  159. BOOL H323_G726_Capability::OnReceivedPDU(const H245_AudioCapability & pdu,
  160.                                          unsigned & packetSize)
  161. {
  162.   if (!H323NonStandardAudioCapability::OnReceivedPDU(pdu, packetSize))
  163.     return FALSE;
  164.  
  165.   const G726_NonStandardInfo * info = (const G726_NonStandardInfo *)(const BYTE *)nonStandardData;
  166.   packetSize = info->count;
  167.   return TRUE;
  168. }
  169.  
  170.  
  171. H323Codec * H323_G726_Capability::CreateCodec(H323Codec::Direction direction) const
  172. {
  173.   unsigned packetSize = 8*(direction == H323Codec::Encoder ? txFramesInPacket : rxFramesInPacket);
  174.  
  175.   return new H323_G726_Codec(speed, direction, packetSize);
  176. }
  177.  
  178.  
  179. /////////////////////////////////////////////////////////////////////////////
  180.  
  181. H323_G726_Codec::H323_G726_Codec(H323_G726_Capability::Speeds s,
  182.                                  Direction dir,
  183.                                  unsigned frameSize)
  184.   : H323StreamedAudioCodec(G726_NonStandard[s].name, dir, frameSize, 5-s)
  185. {
  186.   speed = s;
  187.  
  188.   g726 = new g726_state_s;
  189.   g726_init_state(g726);
  190.   
  191.   PTRACE(3, "Codec\t" <<  G726_NonStandard[speed].name << ' '
  192.          << (dir == Encoder ? "en" : "de") << "coder created for "
  193.          << frameSize << " samples");
  194. }
  195.  
  196.  
  197. H323_G726_Codec::~H323_G726_Codec()
  198. {
  199.   delete g726;
  200. }
  201.  
  202.  
  203. int H323_G726_Codec::Encode(short sample) const
  204. {
  205.   switch (speed) {
  206.     case H323_G726_Capability::e_40k :
  207.       return g726_40_encoder(sample, AUDIO_ENCODING_LINEAR, g726);
  208.     case H323_G726_Capability::e_32k :
  209.       return g726_32_encoder(sample, AUDIO_ENCODING_LINEAR, g726);
  210.     case H323_G726_Capability::e_24k :
  211.       return g726_24_encoder(sample, AUDIO_ENCODING_LINEAR, g726);
  212.     case H323_G726_Capability::e_16k :
  213.       return g726_16_encoder(sample, AUDIO_ENCODING_LINEAR, g726);
  214.     default :
  215.       PAssertAlways(PLogicError);
  216.   }
  217.  
  218.   return 0;
  219. }
  220.  
  221.  
  222. short H323_G726_Codec::Decode(int sample) const
  223. {
  224.   switch (speed) {
  225.     case H323_G726_Capability::e_40k :
  226.       return (short)g726_40_decoder(sample, AUDIO_ENCODING_LINEAR, g726);
  227.     case H323_G726_Capability::e_32k :
  228.       return (short)g726_32_decoder(sample, AUDIO_ENCODING_LINEAR, g726);
  229.     case H323_G726_Capability::e_24k :
  230.       return (short)g726_24_decoder(sample, AUDIO_ENCODING_LINEAR, g726);
  231.     case H323_G726_Capability::e_16k :
  232.       return (short)g726_16_decoder(sample, AUDIO_ENCODING_LINEAR, g726);
  233.     default :
  234.       PAssertAlways(PLogicError);
  235.   }
  236.  
  237.   return 0;
  238. }
  239.  
  240.  
  241. /////////////////////////////////////////////////////////////////////////////
  242.  
  243.  
  244.  
  245.