3 Copyright (C) 2010 Simon MORLAT (simon.morlat@linphone.org)
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 /* Linphone Sound Daemon: is a lightweight utility to play sounds to speaker during a conversation.
21 This is useful for embedded platforms, where sound apis are not performant enough to allow
22 simultaneous sound access.
25 #include "linphonecore_utils.h"
26 #include "mediastreamer2/mssndcard.h"
27 #include "mediastreamer2/msvolume.h"
30 #define MAX_BRANCHES 10
34 struct _LinphoneSoundDaemon *lsd;
37 MSFilter *chanadapter;
39 LsdEndOfPlayCallback eop_cb;
44 struct _LinphoneSoundDaemon {
49 LsdPlayer branches[MAX_BRANCHES];
53 LsdPlayer *linphone_sound_daemon_get_player(LinphoneSoundDaemon *obj){
55 for(i=0;i<MAX_BRANCHES;++i){
56 LsdPlayer *b=&obj->branches[i];
57 MSFilter *p=b->player;
59 ms_filter_call_method(p,MS_PLAYER_GET_STATE,&state);
60 if (state==MSPlayerClosed){
64 ms_warning("No more free players !");
68 void linphone_sound_daemon_release_player(LinphoneSoundDaemon *obj, LsdPlayer * player){
71 ms_filter_call_method(player->player,MS_PLAYER_GET_STATE,&state);
72 if (state!=MSPlayerClosed){
73 ms_filter_call_method(player->player,MS_PLAYER_CLOSE,&state);
75 ms_filter_call_method(player->volumectl,MS_VOLUME_SET_GAIN,&gain);
78 int lsd_player_stop(LsdPlayer *p){
79 ms_filter_call_method_noarg(p->player,MS_PLAYER_PAUSE);
83 MSFilter *linphone_sound_daemon_get_proxy(LinphoneSoundDaemon *obj);
84 void linphone_sound_daemon_destroy(LinphoneSoundDaemon *obj);
86 static void lsd_player_init(LsdPlayer *p, MSConnectionPoint mixer, MSFilterId playerid, LinphoneSoundDaemon *lsd){
88 p->player=ms_filter_new(playerid);
89 p->rateconv=ms_filter_new(MS_RESAMPLE_ID);
91 p->volumectl=ms_filter_new(MS_VOLUME_ID);
93 ms_connection_helper_start(&h);
94 ms_connection_helper_link(&h,p->player,-1,0);
95 ms_connection_helper_link(&h,p->rateconv,0,0);
96 ms_connection_helper_link(&h,p->chanadapter,0,0);
97 ms_connection_helper_link(&h,p->volumectl,0,0);
98 ms_connection_helper_link(&h,mixer.filter,mixer.pin,-1);
102 static void lsd_player_uninit(LsdPlayer *p, MSConnectionPoint mixer){
103 MSConnectionHelper h;
105 ms_connection_helper_start(&h);
106 ms_connection_helper_unlink (&h,p->player,-1,0);
107 ms_connection_helper_unlink(&h,p->rateconv,0,0);
108 ms_connection_helper_unlink(&h,p->chanadapter,0,0);
109 ms_connection_helper_unlink(&h,p->volumectl,0,0);
110 ms_connection_helper_unlink(&h,mixer.filter,mixer.pin,-1);
112 ms_filter_destroy(p->player);
113 ms_filter_destroy(p->rateconv);
114 ms_filter_destroy(p->chanadapter);
115 ms_filter_destroy(p->volumectl);
118 void lsd_player_set_callback(LsdPlayer *p, LsdEndOfPlayCallback cb){
122 void lsd_player_set_user_pointer(LsdPlayer *p, void *up){
126 void *lsd_player_get_user_pointer(LsdPlayer *p){
130 static void lsd_player_on_eop(void * userdata, unsigned int id, void *arg){
133 int lsd_player_play(LsdPlayer *b, const char *filename ){
136 LinphoneSoundDaemon *lsd=b->lsd;
138 ms_filter_call_method(b->player,MS_PLAYER_GET_STATE,&state);
139 if (state!=MSPlayerClosed){
140 ms_filter_call_method_noarg(b->player,MS_PLAYER_CLOSE);
143 if (ms_filter_call_method(b->player,MS_PLAYER_OPEN,(void*)filename)!=0){
146 ms_filter_call_method(b->player,MS_FILTER_GET_SAMPLE_RATE,&rate);
147 ms_filter_call_method(b->player,MS_FILTER_GET_NCHANNELS,&chans);
148 ms_filter_set_notify_callback (b->player,lsd_player_on_eop,b);
150 ms_filter_call_method(b->rateconv,MS_FILTER_SET_SAMPLE_RATE,&rate);
151 ms_filter_call_method(b->rateconv,MS_FILTER_SET_NCHANNELS,&chans);
152 ms_filter_call_method(b->rateconv,MS_FILTER_SET_OUTPUT_SAMPLE_RATE,&lsd->out_rate);
154 ms_filter_call_method(b->chanadapter,MS_FILTER_SET_NCHANNELS,&chans);
155 ms_filter_call_method(b->chanadapter,MS_FILTER_SET_OUTPUT_NCHANNELS,&lsd->out_nchans);
159 int lsd_player_stop(LsdPlayer *p);
160 void lsd_player_enable_loop(LsdPlayer *p, bool_t loopmode);
162 LinphoneSoundDaemon * linphone_sound_daemon_new(const char *cardname){
164 MSConnectionPoint mp;
165 LinphoneSoundDaemon *lsd;
166 MSSndCard *card=ms_snd_card_manager_get_card(
167 ms_snd_card_manager_get(),
170 card=ms_snd_card_manager_get_default_playback_card (
171 ms_snd_card_manager_get());
173 ms_error("linphone_sound_daemon_new(): No playback soundcard available");
178 lsd=ms_new0(LinphoneSoundDaemon,1);
179 lsd->soundout=ms_snd_card_create_writer(card);
182 ms_filter_call_method(lsd->soundout,MS_FILTER_SET_SAMPLE_RATE,&lsd->out_rate);
183 ms_filter_call_method(lsd->soundout,MS_FILTER_SET_NCHANNELS,&lsd->out_nchans);
185 mp.filter=lsd->filter;
188 lsd_player_init(&lsd->branches[0],mp,MS_ITC_SINK_ID,lsd);
189 for(i=1;i<MAX_BRANCHES;++i){
191 lsd_player_init(&lsd->branches[i],mp,MS_FILE_PLAYER_ID);