AmPlayoutBuffer.h

Go to the documentation of this file.
00001 /*
00002  * $Id: AmPlayoutBuffer.h 711 2008-02-10 18:52:44Z sayer $
00003  *
00004  * Copyright (C) 2005-2006 iptelorg GmbH
00005  *
00006  * This file is part of sems, a free SIP media server.
00007  *
00008  * sems is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version
00012  *
00013  * For a license to use the ser software under conditions
00014  * other than those described here, or to purchase support for this
00015  * software, please contact iptel.org by e-mail at the following addresses:
00016  *    info@iptel.org
00017  *
00018  * sems is distributed in the hope that it will be useful,
00019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021  * GNU General Public License for more details.
00022  *
00023  * You should have received a copy of the GNU General Public License 
00024  * along with this program; if not, write to the Free Software 
00025  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00026  */
00028 #ifndef _AmPlayoutBuffer_h_
00029 #define _AmPlayoutBuffer_h_
00030 
00031 #include "SampleArray.h"
00032 #include "AmStats.h"
00033 #include "LowcFE.h"
00034 #include "AmJitterBuffer.h"
00035 #include <set>
00036 using std::multiset;
00037 
00038 
00039 #define ORDER_STAT_WIN_SIZE  35
00040 #define ORDER_STAT_LOSS_RATE 0.1
00041 
00042 #define EXP_THRESHOLD 20
00043 #define SHR_THRESHOLD 180
00044 
00045 #define WSOLA_START_OFF  80
00046 #define WSOLA_SCALED_WIN 50
00047 
00048 // the maximum packet size that will be processed
00049 //   640 is 80ms @ 8khz
00050 #define MAX_PACKET_SAMPLES 640
00051 // search segments of size TEMPLATE_SEG samples 
00052 #define TEMPLATE_SEG   80
00053 
00054 // Maximum value: AUDIO_BUFFER_SIZE / 2
00055 // Note: plc result get stored in our back buffer
00056 #define PLC_MAX_SAMPLES (160*4) 
00057 
00058 class AmPLCBuffer;
00059 
00061 class AmPlayoutBuffer
00062 {
00063   // Playout buffer
00064   SampleArrayShort buffer;
00065 
00066  protected:
00067   u_int32_t r_ts,w_ts;
00068   AmPLCBuffer *m_plcbuffer;
00069 
00070   unsigned int last_ts;
00071   bool         last_ts_i;
00072 
00074   unsigned int   recv_offset;
00076   bool           recv_offset_i;
00077 
00078   void buffer_put(unsigned int ts, ShortSample* buf, unsigned int len);
00079   void buffer_get(unsigned int ts, ShortSample* buf, unsigned int len);
00080 
00081   virtual void write_buffer(u_int32_t ref_ts, u_int32_t ts, int16_t* buf, u_int32_t len);
00082   virtual void direct_write_buffer(unsigned int ts, ShortSample* buf, unsigned int len);
00083  public:
00084   AmPlayoutBuffer(AmPLCBuffer *plcbuffer);
00085   virtual ~AmPlayoutBuffer() {}
00086 
00087   virtual void write(u_int32_t ref_ts, u_int32_t ts, int16_t* buf, u_int32_t len, bool begin_talk);
00088   virtual u_int32_t read(u_int32_t ts, int16_t* buf, u_int32_t len);
00089 
00090   void clearLastTs() { last_ts_i = false; }
00091 };
00092 
00094 class AmAdaptivePlayout: public AmPlayoutBuffer
00095 {
00096   // Order statistics delay estimation
00097   multiset<int32_t> o_stat;
00098   int32_t n_stat[ORDER_STAT_WIN_SIZE];
00099   int     idx;
00100   double  loss_rate;
00101 
00102   // adaptive WSOLA
00103   u_int32_t wsola_off;
00104   int       shr_threshold;
00105   MeanArray short_scaled;
00106 
00107   // second stage PLC
00108   int       plc_cnt;
00109   LowcFE    fec;
00110 
00111   // buffers
00112   // strech buffer
00113   short p_buf[MAX_PACKET_SAMPLES*4];
00114   // merging buffer (merge segment from strech + original seg)
00115   short merge_buf[TEMPLATE_SEG];
00116 
00117   u_int32_t time_scale(u_int32_t ts, float factor, u_int32_t packet_len);
00118   u_int32_t next_delay(u_int32_t ref_ts, u_int32_t ts);
00119 
00120  public:
00121 
00122   AmAdaptivePlayout(AmPLCBuffer *);
00123 
00125   void direct_write_buffer(unsigned int ts, ShortSample* buf, unsigned int len);
00126 
00129   void write_buffer(u_int32_t ref_ts, u_int32_t ts, int16_t* buf, u_int32_t len);
00130 
00132   u_int32_t read(u_int32_t ts, int16_t* buf, u_int32_t len);
00133 
00134 };
00135 
00137 class AmJbPlayout : public AmPlayoutBuffer
00138 {
00139  private:
00140   AmJitterBuffer m_jb;
00141   unsigned int m_last_rtp_endts;
00142 
00143  protected:
00144   void direct_write_buffer(unsigned int ts, ShortSample* buf, unsigned int len);
00145   void prepare_buffer(unsigned int ts, unsigned int ms);
00146 
00147  public:
00148   AmJbPlayout(AmPLCBuffer *plcbuffer);
00149 
00150   u_int32_t read(u_int32_t ts, int16_t* buf, u_int32_t len);
00151   void write(u_int32_t ref_ts, u_int32_t rtp_ts, int16_t* buf, u_int32_t len, bool begin_talk);
00152 };
00153 
00154 
00155 #endif

Generated on Fri May 16 12:02:05 2008 for SEMS by  doxygen 1.5.1
Home |  Recent changes |  Search |  Glossary |  Sitemap |  Login