1 /*******************************************************************************
2 
3     KHOMP generic endpoint/channel library.
4     Copyright (C) 2007-2010 Khomp Ind. & Com.
5 
6   The contents of this file are subject to the Mozilla Public License
7   Version 1.1 (the "License"); you may not use this file except in compliance
8   with the License. You may obtain a copy of the License at
9   http://www.mozilla.org/MPL/
10 
11   Software distributed under the License is distributed on an "AS IS" basis,
12   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
13   the specific language governing rights and limitations under the License.
14 
15   Alternatively, the contents of this file may be used under the terms of the
16   "GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
17   case the provisions of "LGPL License" are applicable instead of those above.
18 
19   If you wish to allow use of your version of this file only under the terms of
20   the LGPL License and not to allow others to use your version of this file
21   under the MPL, indicate your decision by deleting the provisions above and
22   replace them with the notice and other provisions required by the LGPL
23   License. If you do not delete the provisions above, a recipient may use your
24   version of this file under either the MPL or the LGPL License.
25 
26   The LGPL header follows below:
27 
28     This library is free software; you can redistribute it and/or
29     modify it under the terms of the GNU Lesser General Public
30     License as published by the Free Software Foundation; either
31     version 2.1 of the License, or (at your option) any later version.
32 
33     This library is distributed in the hope that it will be useful,
34     but WITHOUT ANY WARRANTY; without even the implied warranty of
35     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
36     Lesser General Public License for more details.
37 
38     You should have received a copy of the GNU Lesser General Public License
39     along with this library; if not, write to the Free Software Foundation,
40     Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
41 
42 *******************************************************************************/
43 
44 #ifndef _FRAME_HPP_
45 #define _FRAME_HPP_
46 
47 #include <stdlib.h>
48 #include <sys/mman.h>
49 
50 #include <ringbuffer.hpp>
51 
52 #include "globals.h"
53 
54 struct FrameStorage
55 {
56     static const unsigned int frame_count = 24;//6;
57     static const unsigned int audio_count = 24;//4;
58 
59     FrameStorage(switch_codec_t * codec, int packet_size);
60     virtual ~FrameStorage();
61 
next_frameFrameStorage62     inline switch_frame_t * next_frame(void)
63     {
64         return &(_frames[next_index()]);
65     }
66 
next_indexFrameStorage67     inline unsigned int next_index()
68     {
69         unsigned int tmp = _index;
70 
71         if (++_index >= frame_count)
72             _index = 0;
73 
74         return tmp;
75     }
76 
cng_frameFrameStorage77     inline switch_frame_t * cng_frame(void)
78     {
79         return &_cng_frame;
80     }
81 
audio_bufferFrameStorage82     char * audio_buffer()
83     {
84         return _buffer;
85     };
86 
87  private:
88     switch_frame_t   _cng_frame;
89 
90     switch_frame_t * _frames;
91     char           * _buffer;
92 
93     unsigned int     _index;
94 };
95 
96 /* Internal frame array structure. */
97 template < int S >
98 struct FrameManager: protected FrameStorage
99 {
100     typedef const char Packet[ S ];
101 
102     typedef Ringbuffer < Packet >  AudioBuffer;
103 
FrameManagerFrameManager104     FrameManager(switch_codec_t * codec)
105     : FrameStorage(codec, S),
106       _audio(audio_count, (Packet*)audio_buffer())
107     {};
108 
109 //    ~FrameManager();
110 
111     // may throw Ringbuffer::BufferEmpty
pickFrameManager112     switch_frame_t * pick(void)
113     {
114         try
115         {
116             /* try to consume from buffer.. */
117             Packet & a = _audio.consumer_start();
118 
119             switch_frame * f = next_frame();
120 
121             /* adjust pointer */
122             f->data = (char *)(&a);
123 
124             /* advance now */
125             _audio.consumer_commit();
126 
127             return f;
128         }
129         catch (...) // AudioBuffer::BufferEmpty & e)
130         {
131             return NULL;
132         }
133     }
134 
135     // may throw Ringbuffer::BufferFull
giveFrameManager136     bool give(const char * buf, unsigned int size)
137     {
138         return _audio.provider_partial(buf, size);
139     }
140 
cngFrameManager141     switch_frame_t * cng(void)
142     {
143         return cng_frame();
144     }
145 
clearFrameManager146     void clear()
147     {
148         _audio.clear();
149     }
150 
151  protected:
152     AudioBuffer      _audio;
153 
154     unsigned int     _index;
155 };
156 
157 typedef FrameManager < Globals::switch_packet_size > FrameSwitchManager;
158 typedef FrameManager < Globals::boards_packet_size > FrameBoardsManager;
159 
160 #endif /* _FRAME_HPP_ */
161