player.c 5.96 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

/*
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
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "private.h"

/**
 * Open a new source on this player.
 * @param obj the player
 * @param filename file to open.
 * @param cb a callback used to notify end of play.
 * @param user_data a user-data provided in the callback to help the application to retrieve its context.
 * @return 0 if successful, -1 otherwise
**/
31
int linphone_player_open(LinphonePlayer *obj, const char *filename, LinphonePlayerEofCallback cb, void *user_data){
32 33
	obj->user_data=user_data;
	obj->cb=cb;
34
	return obj->open(obj,filename);
35 36 37 38 39 40 41 42
}

/**
 * Start a play operation. The player must have been open previously with linphone_player_open().
 * @param obj the player.
 * @return 0 if successful, -1 otherwise
**/
int linphone_player_start(LinphonePlayer *obj){
43
	return obj->start(obj);
44 45 46 47 48 49 50 51
}

/**
 * Suspend a play operation. The player must have been started previously with linphone_player_start().
 * @param obj the player.
 * @return 0 if successful, -1 otherwise
**/
int linphone_player_pause(LinphonePlayer *obj){
52
	return obj->pause(obj);
53 54 55 56 57 58 59 60 61
}

/**
 * Seek at a given position given in milliseconds. The player must be in the paused state.
 * @param obj the player.
 * @param time_ms the position to seek to.
 * @return 0 if successful, -1 otherwise
**/
int linphone_player_seek(LinphonePlayer *obj, int time_ms){
62
	return obj->seek(obj,time_ms);
63 64 65 66 67 68 69 70
}

/**
 * Get the state of play operation.
 * @param obj the player.
 * @return the state of the player within MSPlayerClosed, MSPlayerStarted, MSPlayerPaused.
**/
MSPlayerState linphone_player_get_state(LinphonePlayer *obj){
71
	return obj->get_state(obj);
72 73
}

74 75 76 77 78 79 80 81 82 83 84 85 86 87
/**
 * Get the duration of the media
 * @param obj the player
 * @return  The duration in milliseconds
 */
int linphone_player_get_duration(LinphonePlayer *obj) {
	return obj->get_duration(obj);
}

/**
 * Get the position of the playback
 * @param obj the player
 * @return Position of the playback in milliseconds
 */
88
int linphone_player_get_current_position(LinphonePlayer *obj) {
89 90 91
	return obj->get_position(obj);
}

92 93 94 95 96
/**
 * Close the player.
 * @param obj the player.
**/
void linphone_player_close(LinphonePlayer *obj){
97
	obj->close(obj);
98 99
}

100 101 102 103 104 105 106 107 108 109 110 111
/**
 * @brief Destroy a player
 * @param obj The player
 */
void linphone_player_destroy(LinphonePlayer *obj) {
	if(obj->destroy) obj->destroy(obj);
}

void _linphone_player_destroy(LinphonePlayer *player) {
	ms_free(player);
}

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

/*
 * Call player implementation below.
 */


static bool_t call_player_check_state(LinphonePlayer *player, bool_t check_player){
	LinphoneCall *call=(LinphoneCall*)player->impl;
	if (call->state!=LinphoneCallStreamsRunning){
		ms_warning("Call [%p]: in-call player not usable in state [%s]",call,linphone_call_state_to_string(call->state));
		return FALSE;
	}
	if (call->audiostream==NULL) {
		ms_error("call_player_check_state(): no audiostream.");
		return FALSE;
	}
	if (check_player && call->audiostream->av_player.player==NULL){
		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
	if (player->cb) player->cb(player,player->user_data);
138 139
}

140
static int call_player_open(LinphonePlayer* player, const char *filename){
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
	LinphoneCall *call=(LinphoneCall*)player->impl;
	MSFilter *filter;
	if (!call_player_check_state(player,FALSE)) return -1;
	filter=audio_stream_open_remote_play(call->audiostream,filename);
	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;
	return ms_filter_call_method_noarg(call->audiostream->av_player.player,MS_PLAYER_START);
}

static int call_player_pause(LinphonePlayer *player){
	LinphoneCall *call=(LinphoneCall*)player->impl;
	if (!call_player_check_state(player,TRUE)) return -1;
	return ms_filter_call_method_noarg(call->audiostream->av_player.player,MS_PLAYER_PAUSE);
}

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;
	ms_filter_call_method(call->audiostream->av_player.player,MS_PLAYER_GET_STATE,&state);
	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;
	return ms_filter_call_method(call->audiostream->av_player.player,MS_PLAYER_SEEK_MS,&time_ms);
}

static void call_player_close(LinphonePlayer *player){
	LinphoneCall *call=(LinphoneCall*)player->impl;
	if (!call_player_check_state(player,TRUE)) return;
179
	audio_stream_close_remote_play(call->audiostream);
180 181 182 183
	
}

static void on_call_destroy(void *obj, belle_sip_object_t *call_being_destroyed){
184
	_linphone_player_destroy(obj);
185 186 187 188 189 190 191 192 193 194 195 196 197 198
}

LinphonePlayer *linphone_call_build_player(LinphoneCall *call){
	LinphonePlayer *obj=ms_new0(LinphonePlayer,1);
	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;
}