payload_type.c 10.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
payload_type.c
Copyright (C) 2017 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include <string.h>
#include <ortp/payloadtype.h>
22

23 24
#include "linphone/payload_type.h"

25
#include "c-wrapper/c-wrapper.h"
26 27
#include "utils/payload-type-handler.h"

28 29 30
// TODO: From coreapi. Remove me later.
#include "private.h"

31 32 33 34 35 36 37
struct _LinphonePayloadType {
	belle_sip_object_t base;
	OrtpPayloadType *pt;
	LinphoneCore *lc;
};

BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(LinphonePayloadType);
Ghislain MARY's avatar
Ghislain MARY committed
38
BELLE_SIP_DECLARE_VPTR_NO_EXPORT(LinphonePayloadType);
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

LinphonePayloadType *linphone_payload_type_new(LinphoneCore *lc, OrtpPayloadType *ortp_pt) {
	LinphonePayloadType *pt;
	if (ortp_pt == NULL) return NULL;
	pt = belle_sip_object_new(LinphonePayloadType);
	pt->pt = ortp_pt;
	pt->lc = lc;
	return pt;
}

LinphonePayloadType *linphone_payload_type_ref(LinphonePayloadType *pt) {
	return (LinphonePayloadType *)belle_sip_object_ref(pt);
}

void linphone_payload_type_unref(LinphonePayloadType *pt) {
	belle_sip_object_unref(pt);
}

int linphone_payload_type_get_type(const LinphonePayloadType *pt) {
	return pt->pt->type;
}

static bool_t _payload_type_is_in_core(const OrtpPayloadType *pt, const LinphoneCore *lc) {
	return (bctbx_list_find(lc->codecs_conf.audio_codecs, pt) != NULL)
		|| (bctbx_list_find(lc->codecs_conf.video_codecs, pt) != NULL)
		|| (bctbx_list_find(lc->codecs_conf.text_codecs, pt) != NULL);
}

static char *_payload_type_get_description(const OrtpPayloadType *pt) {
	return bctbx_strdup_printf("%s/%d/%d", pt->mime_type, pt->clock_rate, pt->channels);
}

static int _linphone_core_enable_payload_type(LinphoneCore *lc, OrtpPayloadType *pt, bool_t enabled) {
	payload_type_set_enable(pt,enabled);
	_linphone_core_codec_config_write(lc);
	linphone_core_update_allocated_audio_bandwidth(lc);
	return 0;
}

78
LinphoneStatus linphone_core_enable_payload_type(LinphoneCore *lc, OrtpPayloadType *pt, bool_t enabled){
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 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 137 138
	if (!_payload_type_is_in_core(pt, lc)) {
		char *desc = _payload_type_get_description(pt);
		ms_error("cannot enable '%s' payload type: not in the core", desc);
		bctbx_free(desc);
		return -1;
	}
	return _linphone_core_enable_payload_type(lc, pt, enabled);
}

int linphone_payload_type_enable(LinphonePayloadType *pt, bool_t enabled) {
	if (pt->lc == NULL) {
		char *desc = linphone_payload_type_get_description(pt);
		ms_error("cannot enable '%s' payload type: no core associated", desc);
		bctbx_free(desc);
		return -1;
	}
	return _linphone_core_enable_payload_type(pt->lc, pt->pt, enabled);
}

bool_t linphone_core_payload_type_enabled(const LinphoneCore *lc, const OrtpPayloadType *pt){
	return payload_type_enabled(pt);
}

bool_t linphone_payload_type_enabled(const LinphonePayloadType *pt) {
	return payload_type_enabled(pt->pt);
}

char *linphone_payload_type_get_description(const LinphonePayloadType *pt) {
	return _payload_type_get_description(pt->pt);
}

static const char *_linphone_core_get_payload_type_codec_description(const LinphoneCore *lc, const OrtpPayloadType *pt) {
	if (ms_factory_codec_supported(lc->factory, pt->mime_type)){
		MSFilterDesc *desc=ms_factory_get_encoder(lc->factory, pt->mime_type);
		return desc->text;
	}
	return NULL;
}

const char *linphone_core_get_payload_type_description(LinphoneCore *lc, const OrtpPayloadType *pt){
	if (!_payload_type_is_in_core(pt, lc)) {
		char *desc = _payload_type_get_description(pt);
		ms_error("cannot get codec description for '%s' payload type: not in the core", desc);
		bctbx_free(desc);
		return NULL;
	}
	return _linphone_core_get_payload_type_codec_description(lc, pt);
}

const char *linphone_payload_type_get_encoder_description(const LinphonePayloadType *pt) {
	if (pt->lc == NULL) {
		char *desc = linphone_payload_type_get_description(pt);
		ms_error("cannot get codec description for '%s' payload type: no associated core", desc);
		bctbx_free(desc);
		return NULL;
	}
	return _linphone_core_get_payload_type_codec_description(pt->lc, pt->pt);
}

static int _linphone_core_get_payload_type_normal_bitrate(const LinphoneCore *lc, const OrtpPayloadType *pt) {
139
	int maxbw = LinphonePrivate::PayloadTypeHandler::getMinBandwidth(linphone_core_get_download_bandwidth(lc),
140 141
					linphone_core_get_upload_bandwidth(lc));
	if (pt->type==PAYLOAD_AUDIO_CONTINUOUS || pt->type==PAYLOAD_AUDIO_PACKETIZED){
142
		return LinphonePrivate::PayloadTypeHandler::getAudioPayloadTypeBandwidth(pt, maxbw);
143 144 145 146 147
	}else if (pt->type==PAYLOAD_VIDEO){
		int video_bw;
		if (maxbw<=0) {
			video_bw=1500; /*default bitrate for video stream when no bandwidth limit is set, around 1.5 Mbit/s*/
		}else{
148
			video_bw=LinphonePrivate::PayloadTypeHandler::getRemainingBandwidthForVideo(maxbw,lc->audio_bw);
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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 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 252 253 254 255 256 257 258 259 260 261 262
		}
		return video_bw;
	}
	return 0;
}

int linphone_core_get_payload_type_bitrate(LinphoneCore *lc, const OrtpPayloadType *pt){
	if (_payload_type_is_in_core(pt, lc)) {
		return _linphone_core_get_payload_type_normal_bitrate(lc, pt);
	} else {
		char *desc = _payload_type_get_description(pt);
		ms_error("cannot get normal bitrate of payload type '%s': not in the core", desc);
		bctbx_free(desc);
		return -1;
	}
}

int linphone_payload_type_get_normal_bitrate(const LinphonePayloadType *pt) {
	if (pt->lc == NULL) {
		char *desc = linphone_payload_type_get_description(pt);
		ms_error("cannot get normal bitrate of codec '%s': no associated core", desc);
		bctbx_free(desc);
		return -1;
	}
	return _linphone_core_get_payload_type_normal_bitrate(pt->lc, pt->pt);
}

static void _linphone_core_set_payload_type_normal_bitrate(LinphoneCore *lc, OrtpPayloadType *pt, int bitrate) {
	if (pt->type==PAYLOAD_VIDEO || pt->flags & PAYLOAD_TYPE_IS_VBR){
		pt->normal_bitrate=bitrate*1000;
		pt->flags|=PAYLOAD_TYPE_BITRATE_OVERRIDE;
		linphone_core_update_allocated_audio_bandwidth(lc);
	}else{
		char *desc = _payload_type_get_description(pt);
		ms_error("Cannot set an explicit bitrate for codec '%s', because it is not VBR.",desc);
		bctbx_free(desc);
	}
}

void linphone_core_set_payload_type_bitrate(LinphoneCore *lc, OrtpPayloadType *pt, int bitrate) {
	if (_payload_type_is_in_core(pt, lc)) {
		_linphone_core_set_payload_type_normal_bitrate(lc, pt, bitrate);
	} else {
		char *desc = _payload_type_get_description(pt);
		ms_error("cannot set normal bitrate of codec '%s': not in the core", desc);
		bctbx_free(desc);
	}
}

void linphone_payload_type_set_normal_bitrate(LinphonePayloadType *pt, int bitrate) {
	if (pt->lc == NULL) {
		ms_error("cannot set bitrate of codec %s/%d: no associated core", pt->pt->mime_type, pt->pt->clock_rate);
		return;
	}
	_linphone_core_set_payload_type_normal_bitrate(pt->lc, pt->pt, bitrate);
}

const char *linphone_payload_type_get_mime_type(const LinphonePayloadType *pt) {
	return pt->pt->mime_type;
}

int linphone_payload_type_get_channels(const LinphonePayloadType *pt) {
	return pt->pt->channels;
}

int linphone_core_get_payload_type_number(LinphoneCore *lc, const OrtpPayloadType *pt) {
	return payload_type_get_number(pt);
}

int linphone_payload_type_get_number(const LinphonePayloadType *pt) {
	return payload_type_get_number(pt->pt);
}

void linphone_core_set_payload_type_number(LinphoneCore *lc, OrtpPayloadType *pt, int number) {
	payload_type_set_number(pt, number);
}

void linphone_payload_type_set_number(LinphonePayloadType *pt, int number) {
	payload_type_set_number(pt->pt, number);
}

const char *linphone_payload_type_get_recv_fmtp(const LinphonePayloadType *pt) {
	return pt->pt->recv_fmtp;
}

void linphone_payload_type_set_recv_fmtp(LinphonePayloadType *pt, const char *recv_fmtp) {
	if (pt->pt->recv_fmtp != NULL) bctbx_free(pt->pt->recv_fmtp);
	if (recv_fmtp != NULL) pt->pt->recv_fmtp = bctbx_strdup(recv_fmtp);
	else recv_fmtp = NULL;
}

const char *linphone_payload_type_get_send_fmtp(const LinphonePayloadType *pt) {
	return pt->pt->send_fmtp;
}

void linphone_payload_type_set_send_fmtp(LinphonePayloadType *pt, const char *send_fmtp) {
	if (pt->pt->send_fmtp != NULL) bctbx_free(pt->pt->send_fmtp);
	if (send_fmtp != NULL) pt->pt->recv_fmtp = bctbx_strdup(send_fmtp);
	else send_fmtp = NULL;
}

int linphone_payload_type_get_clock_rate(const LinphonePayloadType *pt) {
	return pt->pt->clock_rate;
}

bool_t linphone_core_payload_type_is_vbr(const LinphoneCore *lc, const OrtpPayloadType *pt) {
	return payload_type_is_vbr(pt);
}

bool_t linphone_payload_type_is_vbr(const LinphonePayloadType *pt) {
	return payload_type_is_vbr(pt->pt);
}

bool_t _linphone_core_check_payload_type_usability(const LinphoneCore *lc, const OrtpPayloadType *pt) {
263
	int maxbw=LinphonePrivate::PayloadTypeHandler::getMinBandwidth(linphone_core_get_download_bandwidth(lc),
264
					linphone_core_get_upload_bandwidth(lc));
265
	return linphone_core_is_payload_type_usable_for_bandwidth(lc, pt, maxbw);
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
}

bool_t linphone_core_check_payload_type_usability(LinphoneCore *lc, const OrtpPayloadType *pt) {
	if (!_payload_type_is_in_core(pt, lc)) {
		char *desc = _payload_type_get_description(pt);
		ms_error("cannot check usability of '%s' payload type: not in the core", desc);
		bctbx_free(desc);
		return FALSE;
	}
	return _linphone_core_check_payload_type_usability(lc, pt);
}

bool_t linphone_payload_type_is_usable(const LinphonePayloadType *pt) {
	if (pt->lc == NULL) {
		char *desc = linphone_payload_type_get_description(pt);
		ms_error("cannot check usability of '%s' payload type: no associated core", desc);
		bctbx_free(desc);
		return FALSE;
	}
	return _linphone_core_check_payload_type_usability(pt->lc, pt->pt);
}

OrtpPayloadType *linphone_payload_type_get_ortp_pt(const LinphonePayloadType *pt) {
	return pt->pt;
}

BELLE_SIP_INSTANCIATE_VPTR(LinphonePayloadType, belle_sip_object_t,
	NULL, // uninit
	NULL, // clone
	NULL, // marshale
	TRUE // unown
);
298 299 300 301 302 303 304 305 306 307 308 309


void payload_type_set_enable(OrtpPayloadType *pt, bool_t value) {
	if (value)
		payload_type_set_flag(pt, PAYLOAD_TYPE_ENABLED);
	else
		payload_type_unset_flag(pt, PAYLOAD_TYPE_ENABLED);
}

bool_t payload_type_enabled(const OrtpPayloadType *pt) {
	return (pt->flags & PAYLOAD_TYPE_ENABLED);
}