player.c 7.76 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

/*
linphone
Copyright (C) 2014 Belledonne Communications SARL

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 20
*/

21
#include "c-wrapper/c-wrapper.h"
22

23 24 25
// TODO: From coreapi. Remove me later.
#include "private.h"

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(LinphonePlayer);

BELLE_SIP_INSTANCIATE_VPTR(LinphonePlayer, belle_sip_object_t,
	_linphone_player_destroy, // destroy
	NULL, // clone
	NULL, // marshal
	FALSE
);

LinphonePlayer * linphone_player_new(void) {
	LinphonePlayer *player = belle_sip_object_new(LinphonePlayer);
	player->callbacks = linphone_player_cbs_new();
	return player;
}

LinphonePlayer * linphone_player_ref(LinphonePlayer *player) {
	belle_sip_object_ref(player);
	return player;
}

void linphone_player_unref(LinphonePlayer *player) {
	belle_sip_object_unref(player);
}

void *linphone_player_get_user_data(const LinphonePlayer *player) {
	return player->user_data;
}

void linphone_player_set_user_data(LinphonePlayer *player, void *ud) {
	player->user_data = ud;
}

LinphonePlayerCbs * linphone_player_get_callbacks(const LinphonePlayer *player) {
	return player->callbacks;
}

LinphoneStatus linphone_player_open(LinphonePlayer *obj, const char *filename){
63
	return obj->open(obj,filename);
64 65
}

66
LinphoneStatus linphone_player_start(LinphonePlayer *obj){
Simon Morlat's avatar
Simon Morlat committed
67
	return obj->start(obj);
68 69
}

70
LinphoneStatus linphone_player_pause(LinphonePlayer *obj){
Simon Morlat's avatar
Simon Morlat committed
71
	return obj->pause(obj);
72 73
}

74
LinphoneStatus linphone_player_seek(LinphonePlayer *obj, int time_ms){
Simon Morlat's avatar
Simon Morlat committed
75
	return obj->seek(obj,time_ms);
76 77
}

78 79 80 81 82 83 84 85 86 87
LinphonePlayerState linphone_player_get_state(LinphonePlayer *obj){
	switch (obj->get_state(obj)) {
		case MSPlayerClosed:
		default:
			return LinphonePlayerClosed;
		case MSPlayerPaused:
			return LinphonePlayerPaused;
		case MSPlayerPlaying:
			return LinphonePlayerPlaying;
	}
88 89
}

90 91 92 93
int linphone_player_get_duration(LinphonePlayer *obj) {
	return obj->get_duration(obj);
}

94
int linphone_player_get_current_position(LinphonePlayer *obj) {
95 96 97
	return obj->get_position(obj);
}

98
void linphone_player_close(LinphonePlayer *obj){
99
	obj->close(obj);
100 101
}

102
void linphone_player_destroy(LinphonePlayer *obj) {
103
	_linphone_player_destroy(obj);
104 105 106
}

void _linphone_player_destroy(LinphonePlayer *player) {
107
	if(player->destroy) player->destroy(player);
108
	linphone_player_cbs_unref(player->callbacks);
109 110
}

111 112 113 114 115 116 117 118

/*
 * Call player implementation below.
 */


static bool_t call_player_check_state(LinphonePlayer *player, bool_t check_player){
	LinphoneCall *call=(LinphoneCall*)player->impl;
119 120
	if (linphone_call_get_state(call)!=LinphoneCallStreamsRunning){
		ms_warning("Call [%p]: in-call player not usable in state [%s]",call,linphone_call_state_to_string(linphone_call_get_state(call)));
121 122
		return FALSE;
	}
123 124
	AudioStream *astream = reinterpret_cast<AudioStream *>(linphone_call_get_stream(call, LinphoneStreamTypeAudio));
	if (astream==NULL) {
125 126 127
		ms_error("call_player_check_state(): no audiostream.");
		return FALSE;
	}
128
	if (check_player && astream->av_player.player==NULL){
129 130 131 132 133 134 135 136
		ms_error("call_player_check_state(): no player.");
		return FALSE;
	}
	return TRUE;
}

static void on_eof(void *user_data, MSFilter *f, unsigned int event_id, void *arg){
	LinphonePlayer *player=(LinphonePlayer *)user_data;
137 138 139 140 141 142 143
	switch (event_id){
		case MS_PLAYER_EOF:
			LinphonePlayerCbs *cbs = linphone_player_get_callbacks(player);
			LinphonePlayerCbsEofReachedCb cb = linphone_player_cbs_get_eof_reached(cbs);
			if (cb) cb(player);
		break;
	}
144 145
}

146
static int call_player_open(LinphonePlayer* player, const char *filename){
147 148 149
	LinphoneCall *call=(LinphoneCall*)player->impl;
	MSFilter *filter;
	if (!call_player_check_state(player,FALSE)) return -1;
150 151
	AudioStream *astream = reinterpret_cast<AudioStream *>(linphone_call_get_stream(call, LinphoneStreamTypeAudio));
	filter=audio_stream_open_remote_play(astream,filename);
152 153 154 155 156 157 158 159
	if (!filter) return -1;
	ms_filter_add_notify_callback(filter,&on_eof,player,FALSE);
	return 0;
}

static int call_player_start(LinphonePlayer *player){
	LinphoneCall *call=(LinphoneCall*)player->impl;
	if (!call_player_check_state(player,TRUE)) return -1;
160 161
	AudioStream *astream = reinterpret_cast<AudioStream *>(linphone_call_get_stream(call, LinphoneStreamTypeAudio));
	return ms_filter_call_method_noarg(astream->av_player.player,MS_PLAYER_START);
162 163 164 165 166
}

static int call_player_pause(LinphonePlayer *player){
	LinphoneCall *call=(LinphoneCall*)player->impl;
	if (!call_player_check_state(player,TRUE)) return -1;
167 168
	AudioStream *astream = reinterpret_cast<AudioStream *>(linphone_call_get_stream(call, LinphoneStreamTypeAudio));
	return ms_filter_call_method_noarg(astream->av_player.player,MS_PLAYER_PAUSE);
169 170 171 172 173 174
}

static MSPlayerState call_player_get_state(LinphonePlayer *player){
	LinphoneCall *call=(LinphoneCall*)player->impl;
	MSPlayerState state=MSPlayerClosed;
	if (!call_player_check_state(player,TRUE)) return MSPlayerClosed;
175 176
	AudioStream *astream = reinterpret_cast<AudioStream *>(linphone_call_get_stream(call, LinphoneStreamTypeAudio));
	ms_filter_call_method(astream->av_player.player,MS_PLAYER_GET_STATE,&state);
177 178 179 180 181 182
	return state;
}

static int call_player_seek(LinphonePlayer *player, int time_ms){
	LinphoneCall *call=(LinphoneCall*)player->impl;
	if (!call_player_check_state(player,TRUE)) return -1;
183 184
	AudioStream *astream = reinterpret_cast<AudioStream *>(linphone_call_get_stream(call, LinphoneStreamTypeAudio));
	return ms_filter_call_method(astream->av_player.player,MS_PLAYER_SEEK_MS,&time_ms);
185 186 187 188 189
}

static void call_player_close(LinphonePlayer *player){
	LinphoneCall *call=(LinphoneCall*)player->impl;
	if (!call_player_check_state(player,TRUE)) return;
190 191
	AudioStream *astream = reinterpret_cast<AudioStream *>(linphone_call_get_stream(call, LinphoneStreamTypeAudio));
	audio_stream_close_remote_play(astream);
192

193 194 195
}

static void on_call_destroy(void *obj, belle_sip_object_t *call_being_destroyed){
Ghislain MARY's avatar
Ghislain MARY committed
196
	linphone_player_unref(reinterpret_cast<LinphonePlayer *>(obj));
197 198 199
}

LinphonePlayer *linphone_call_build_player(LinphoneCall *call){
200
	LinphonePlayer *obj = linphone_player_new();
201 202 203 204 205 206 207 208 209 210
	obj->open=call_player_open;
	obj->close=call_player_close;
	obj->start=call_player_start;
	obj->seek=call_player_seek;
	obj->pause=call_player_pause;
	obj->get_state=call_player_get_state;
	obj->impl=call;
	belle_sip_object_weak_ref(call,on_call_destroy,obj);
	return obj;
}
211

212 213 214
LinphoneCore *linphone_player_get_core(const LinphonePlayer *player){
	return linphone_call_get_core((LinphoneCall *)player->impl);
}
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251

BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(LinphonePlayerCbs);

BELLE_SIP_INSTANCIATE_VPTR(LinphonePlayerCbs, belle_sip_object_t,
	NULL, // destroy
	NULL, // clone
	NULL, // marshal
	FALSE);

LinphonePlayerCbs *linphone_player_cbs_new(void) {
	return belle_sip_object_new(LinphonePlayerCbs);
}

LinphonePlayerCbs * linphone_player_cbs_ref(LinphonePlayerCbs *cbs) {
	belle_sip_object_ref(cbs);
	return cbs;
}

void linphone_player_cbs_unref(LinphonePlayerCbs *cbs) {
	belle_sip_object_unref(cbs);
}

void *linphone_player_cbs_get_user_data(const LinphonePlayerCbs *cbs) {
	return cbs->user_data;
}

void linphone_player_cbs_set_user_data(LinphonePlayerCbs *cbs, void *ud) {
	cbs->user_data = ud;
}

LinphonePlayerCbsEofReachedCb linphone_player_cbs_get_eof_reached(const LinphonePlayerCbs *cbs) {
	return cbs->eof;
}

void linphone_player_cbs_set_eof_reached(LinphonePlayerCbs *cbs, LinphonePlayerCbsEofReachedCb cb) {
	cbs->eof = cb;
}