1 /*
2  * Copyright (C) 2008 IPTEGO GmbH
3  *
4  * This file is part of SEMS, a free SIP media server.
5  *
6  * SEMS is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version. This program is released under
10  * the GPL with the additional exemption that compiling, linking,
11  * and/or using OpenSSL is allowed.
12  *
13  * For a license to use the sems software under conditions
14  * other than those described here, or to purchase support for this
15  * software, please contact iptel.org by e-mail at the following addresses:
16  *    info@iptel.org
17  *
18  * SEMS is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26  */
27 
28 #ifndef _AmAudioMixer_h_
29 #define _AmAudioMixer_h_
AmAudioMixer(int external_sample_rate)30 
31 #include "AmMultiPartyMixer.h"
32 #include "AmThread.h"
33 
34 #include <map>
35 #include <set>
36 
37 class AmAudioMixerConnector;
38 
39 /**
40  * \brief \ref AmAudio to mix input
41  *
42  * AmAudio that mixes some sources' audio and writes into a set of sinks.
43  *
44  * Can probably do lots of things together with AmAudioQueue and/or AmAudioMixIn.
45  *
46  * Attention: Sources (in fact AmAudioMixerConnector) are owned by the AmAudioMixer,
addSource(int external_sample_rate)47  *            i.e. deleted on releaseSink/destructor.
48  *            Sinks are not owned by the AmAudioMixer.
49  */
50 class AmAudioMixer
51 {
52   AmMultiPartyMixer mixer;
53 
54   AmMutex srcsink_mut;
55   std::map<AmAudioMixerConnector*, unsigned int> sources;
56 
57   unsigned int sink_channel;
58   AmAudioMixerConnector* sink_connector;
59   std::set<AmAudio*> sinks;
60 
61  public:
62   AmAudioMixer(int external_sample_rate);
63   ~AmAudioMixer();
64 
65   AmAudio* addSource(int external_sample_rate);
66   void releaseSource(AmAudio* s);
67 
68   void addSink(AmAudio* s);
69   void releaseSink(AmAudio* s);
70 };
71 
72 class AmAudioMixerConnector
73 : public AmAudio {
addSink(AmAudio * s)74   AmMultiPartyMixer& mixer;
75   unsigned int channel;
76   AmMutex* audio_mut;
77   std::set<AmAudio*>* sinks;
78   AmAudio* mix_channel;
79 
releaseSink(AmAudio * s)80  protected:
81   int get(unsigned long long system_ts, unsigned char* buffer,
82 	  int output_sample_rate, unsigned int nb_samples);
83   int put(unsigned long long system_ts, unsigned char* buffer,
84 	  int input_sample_rate, unsigned int size);
85 
get(unsigned long long system_ts,unsigned char * buffer,int output_sample_rate,unsigned int nb_samples)86   // dummies for AmAudio's pure virtual methods
87   int read(unsigned int user_ts, unsigned int size){ return -1; }
88   int write(unsigned int user_ts, unsigned int size){ return -1; }
89 
90  public:
91  AmAudioMixerConnector(AmMultiPartyMixer& mixer, unsigned int channel,
92 		       AmAudio* mix_channel,
93 		       AmMutex* audio_mut = NULL, std::set<AmAudio*>* sinks = NULL)
94    : mixer(mixer), channel(channel), audio_mut(audio_mut),
95     sinks(sinks), mix_channel(mix_channel) { }
96   ~AmAudioMixerConnector() { }
97 
98 };
99 
100 #endif
101