2014-02-10 06:06:44 +08:00
|
|
|
/** toxav.c
|
2015-02-15 06:37:52 +08:00
|
|
|
*
|
2015-02-18 06:34:40 +08:00
|
|
|
* Copyright (C) 2013-2015 Tox project All Rights Reserved.
|
2014-02-10 06:06:44 +08:00
|
|
|
*
|
|
|
|
* This file is part of Tox.
|
|
|
|
*
|
|
|
|
* Tox 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 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tox 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 Tox. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif /* HAVE_CONFIG_H */
|
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
#include "msi.h" /* Includes codec.h, rtp.h and toxav.h */
|
2014-02-10 06:06:44 +08:00
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
#include "../toxcore/Messenger.h"
|
2014-04-28 01:21:26 +08:00
|
|
|
#include "../toxcore/logger.h"
|
2014-11-18 07:46:46 +08:00
|
|
|
#include "../toxcore/util.h"
|
2014-04-28 01:21:26 +08:00
|
|
|
|
2014-05-11 00:00:49 +08:00
|
|
|
#include <assert.h>
|
2014-02-10 06:06:44 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-02-16 03:44:33 +08:00
|
|
|
|
2014-06-25 06:07:31 +08:00
|
|
|
#define MAX_ENCODE_TIME_US ((1000 / 24) * 1000)
|
2014-02-10 06:06:44 +08:00
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
enum {
|
|
|
|
audio_index,
|
|
|
|
video_index,
|
2014-07-27 06:26:58 +08:00
|
|
|
};
|
|
|
|
|
2015-02-20 07:23:38 +08:00
|
|
|
typedef struct ToxAVCall_s
|
2015-02-15 06:37:52 +08:00
|
|
|
{
|
2015-02-21 08:07:22 +08:00
|
|
|
ToxAV* av;
|
2015-01-18 01:22:20 +08:00
|
|
|
pthread_mutex_t mutex_control[1];
|
2014-12-17 05:10:48 +08:00
|
|
|
pthread_mutex_t mutex_encoding_audio[1];
|
|
|
|
pthread_mutex_t mutex_encoding_video[1];
|
|
|
|
pthread_mutex_t mutex_do[1];
|
2015-02-20 07:23:38 +08:00
|
|
|
RTPSession *rtps[2]; /* Audio is first and video is second */
|
2014-11-29 20:42:19 +08:00
|
|
|
CSSession *cs;
|
2015-02-15 06:37:52 +08:00
|
|
|
bool active;
|
2015-02-19 06:23:46 +08:00
|
|
|
MSICall* msi_call;
|
2015-02-20 07:23:38 +08:00
|
|
|
uint32_t friend_id;
|
2015-02-15 06:37:52 +08:00
|
|
|
|
2015-02-20 07:23:38 +08:00
|
|
|
uint32_t s_audio_b; /* Sending audio bitrate */
|
|
|
|
uint32_t s_video_b; /* Sending video bitrate */
|
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
uint8_t last_capabilities;
|
|
|
|
|
2015-02-20 07:23:38 +08:00
|
|
|
struct ToxAVCall_s *prev;
|
|
|
|
struct ToxAVCall_s *next;
|
|
|
|
} ToxAVCall;
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
struct toxAV
|
2014-07-27 01:29:49 +08:00
|
|
|
{
|
2015-02-15 06:37:52 +08:00
|
|
|
Messenger* m;
|
|
|
|
MSISession* msi;
|
|
|
|
|
|
|
|
/* Two-way storage: first is array of calls and second is list of calls with head and tail */
|
2015-02-20 07:23:38 +08:00
|
|
|
ToxAVCall** calls;
|
2015-02-15 06:37:52 +08:00
|
|
|
uint32_t calls_tail;
|
|
|
|
uint32_t calls_head;
|
|
|
|
|
|
|
|
PAIR(toxav_call_cb *, void*) ccb; /* Call callback */
|
|
|
|
PAIR(toxav_call_state_cb *, void *) scb; /* Call state callback */
|
|
|
|
PAIR(toxav_receive_audio_frame_cb *, void *) acb; /* Audio frame receive callback */
|
|
|
|
PAIR(toxav_receive_video_frame_cb *, void *) vcb; /* Video frame receive callback */
|
|
|
|
|
|
|
|
/** Decode time measures */
|
|
|
|
int32_t dmssc; /** Measure count */
|
|
|
|
int32_t dmsst; /** Last cycle total */
|
|
|
|
int32_t dmssa; /** Average decoding time in ms */
|
|
|
|
|
|
|
|
uint32_t interval; /** Calculated interval */
|
|
|
|
};
|
2014-07-27 01:29:49 +08:00
|
|
|
|
2014-02-10 06:06:44 +08:00
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
int callback_invite(void* toxav_inst, MSICall* call);
|
|
|
|
int callback_start(void* toxav_inst, MSICall* call);
|
|
|
|
int callback_end(void* toxav_inst, MSICall* call);
|
|
|
|
int callback_error(void* toxav_inst, MSICall* call);
|
|
|
|
int callback_capabilites(void* toxav_inst, MSICall* call);
|
2015-02-15 06:37:52 +08:00
|
|
|
|
2015-02-19 06:23:46 +08:00
|
|
|
TOXAV_CALL_STATE capabilities_to_state(uint8_t capabilities);
|
2015-02-21 08:07:22 +08:00
|
|
|
ToxAVCall* call_new(ToxAV* av, uint32_t friend_number, TOXAV_ERR_CALL* error);
|
|
|
|
ToxAVCall* call_get(ToxAV* av, uint32_t friend_number);
|
|
|
|
void call_remove(ToxAVCall* call);
|
|
|
|
bool audio_bitrate_invalid(uint32_t bitrate);
|
|
|
|
bool video_bitrate_invalid(uint32_t bitrate);
|
|
|
|
bool call_prepare_transmission(ToxAVCall* call);
|
|
|
|
void call_kill_transmission(ToxAVCall* call);
|
2015-02-15 06:37:52 +08:00
|
|
|
|
2014-04-28 01:21:26 +08:00
|
|
|
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
ToxAV* toxav_new(Tox* tox, TOXAV_ERR_NEW* error)
|
|
|
|
{
|
|
|
|
TOXAV_ERR_NEW rc = TOXAV_ERR_NEW_OK;
|
|
|
|
ToxAV *av = NULL;
|
|
|
|
|
|
|
|
if (tox == NULL) {
|
|
|
|
rc = TOXAV_ERR_NEW_NULL;
|
|
|
|
goto FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (((Messenger*)tox)->msi_packet) {
|
|
|
|
rc = TOXAV_ERR_NEW_MULTIPLE;
|
|
|
|
goto FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
av = calloc ( sizeof(ToxAV), 1);
|
|
|
|
|
2014-05-24 22:02:01 +08:00
|
|
|
if (av == NULL) {
|
|
|
|
LOGGER_WARNING("Allocation failed!");
|
2015-02-15 06:37:52 +08:00
|
|
|
rc = TOXAV_ERR_NEW_MALLOC;
|
|
|
|
goto FAILURE;
|
2014-05-24 22:02:01 +08:00
|
|
|
}
|
2015-02-15 06:37:52 +08:00
|
|
|
|
|
|
|
av->m = (Messenger *)tox;
|
2015-02-19 06:23:46 +08:00
|
|
|
av->msi = msi_new(av->m);
|
2015-02-15 06:37:52 +08:00
|
|
|
|
|
|
|
if (av->msi == NULL) {
|
|
|
|
rc = TOXAV_ERR_NEW_MALLOC;
|
|
|
|
goto FAILURE;
|
2014-11-29 02:10:27 +08:00
|
|
|
}
|
2015-02-15 06:37:52 +08:00
|
|
|
|
|
|
|
av->interval = 200;
|
2015-02-19 06:23:46 +08:00
|
|
|
av->msi->av = av;
|
2015-02-15 06:37:52 +08:00
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
msi_register_callback(av->msi, callback_invite, msi_OnInvite);
|
|
|
|
msi_register_callback(av->msi, callback_start, msi_OnStart);
|
|
|
|
msi_register_callback(av->msi, callback_end, msi_OnEnd);
|
|
|
|
msi_register_callback(av->msi, callback_error, msi_OnError);
|
|
|
|
msi_register_callback(av->msi, callback_error, msi_OnPeerTimeout);
|
|
|
|
msi_register_callback(av->msi, callback_capabilites, msi_OnCapabilities);
|
2015-02-15 06:37:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
*error = rc;
|
|
|
|
|
2014-02-10 06:06:44 +08:00
|
|
|
return av;
|
2015-02-15 06:37:52 +08:00
|
|
|
|
|
|
|
FAILURE:
|
|
|
|
if (error)
|
|
|
|
*error = rc;
|
|
|
|
|
|
|
|
free(av);
|
|
|
|
|
|
|
|
return NULL;
|
2014-02-10 06:06:44 +08:00
|
|
|
}
|
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
void toxav_kill(ToxAV* av)
|
2014-02-17 09:01:30 +08:00
|
|
|
{
|
2015-02-15 06:37:52 +08:00
|
|
|
if (av == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
msi_kill(av->msi);
|
2015-02-21 08:07:22 +08:00
|
|
|
|
|
|
|
/* Msi kill will hang up all calls so just clean these calls */
|
|
|
|
if (av->calls) {
|
|
|
|
ToxAVCall* it = call_get(av, av->calls_head);
|
|
|
|
for (; it; it = it->next) {
|
|
|
|
call_kill_transmission(it);
|
|
|
|
call_remove(it); /* This will eventually free av->calls */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-10 06:06:44 +08:00
|
|
|
free(av);
|
|
|
|
}
|
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
Tox* toxav_get_tox(ToxAV* av)
|
2014-02-10 06:06:44 +08:00
|
|
|
{
|
2015-02-15 06:37:52 +08:00
|
|
|
return (Tox*) av->m;
|
|
|
|
}
|
2014-12-04 02:52:18 +08:00
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
uint32_t toxav_iteration_interval(const ToxAV* av)
|
|
|
|
{
|
|
|
|
return av->calls ? av->interval : 200;
|
2014-02-10 06:06:44 +08:00
|
|
|
}
|
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
void toxav_iteration(ToxAV* av)
|
2014-02-10 06:06:44 +08:00
|
|
|
{
|
2015-02-15 06:37:52 +08:00
|
|
|
if (av->calls == NULL)
|
|
|
|
return;
|
2015-02-02 06:43:54 +08:00
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
uint64_t start = current_time_monotonic();
|
|
|
|
uint32_t rc = 200 + av->dmssa; /* If no call is active interval is 200 */
|
2015-02-02 06:43:54 +08:00
|
|
|
|
2015-02-20 07:23:38 +08:00
|
|
|
ToxAVCall* i = av->calls[av->calls_head];
|
2015-02-15 06:37:52 +08:00
|
|
|
for (; i; i = i->next) {
|
|
|
|
if (i->active) {
|
|
|
|
cs_do(i->cs);
|
|
|
|
rc = MIN(i->cs->last_packet_frame_duration, rc);
|
2014-12-17 05:10:48 +08:00
|
|
|
}
|
2014-11-29 02:10:27 +08:00
|
|
|
}
|
2015-02-02 06:43:54 +08:00
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
av->interval = rc < av->dmssa ? 0 : rc - av->dmssa;
|
|
|
|
av->dmsst += current_time_monotonic() - start;
|
2015-02-02 06:43:54 +08:00
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
if (++av->dmssc == 3) {
|
|
|
|
av->dmssa = av->dmsst / 3 + 2 /* NOTE Magic Offset for precission */;
|
|
|
|
av->dmssc = 0;
|
|
|
|
av->dmsst = 0;
|
2014-11-18 07:46:46 +08:00
|
|
|
}
|
2014-02-10 06:06:44 +08:00
|
|
|
}
|
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
bool toxav_call(ToxAV* av, uint32_t friend_number, uint32_t audio_bit_rate, uint32_t video_bit_rate, TOXAV_ERR_CALL* error)
|
2014-07-08 04:10:10 +08:00
|
|
|
{
|
2015-02-21 08:07:22 +08:00
|
|
|
ToxAVCall* call = call_new(av, friend_number, error);
|
2015-02-20 07:23:38 +08:00
|
|
|
if (call == NULL)
|
2015-02-15 06:37:52 +08:00
|
|
|
return false;
|
|
|
|
|
2015-02-20 07:23:38 +08:00
|
|
|
call->s_audio_b = audio_bit_rate;
|
|
|
|
call->s_video_b = video_bit_rate;
|
2015-02-15 06:37:52 +08:00
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
call->last_capabilities = msi_CapRAudio | msi_CapRVideo;
|
2015-02-15 06:37:52 +08:00
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
call->last_capabilities |= audio_bit_rate > 0 ? msi_CapSAudio : 0;
|
|
|
|
call->last_capabilities |= video_bit_rate > 0 ? msi_CapSVideo : 0;
|
2015-02-20 07:23:38 +08:00
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
if (msi_invite(av->msi, &call->msi_call, friend_number, call->last_capabilities) != 0) {
|
|
|
|
call_remove(call);
|
2015-02-15 06:37:52 +08:00
|
|
|
if (error)
|
2015-02-20 07:23:38 +08:00
|
|
|
*error = TOXAV_ERR_CALL_MALLOC;
|
2015-02-15 06:37:52 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-23 06:41:40 +08:00
|
|
|
call->msi_call->av_call = call;
|
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
return true;
|
2014-11-18 07:46:46 +08:00
|
|
|
}
|
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
void toxav_callback_call(ToxAV* av, toxav_call_cb* function, void* user_data)
|
2014-02-10 06:06:44 +08:00
|
|
|
{
|
2015-02-15 06:37:52 +08:00
|
|
|
av->ccb.first = function;
|
|
|
|
av->ccb.second = user_data;
|
2014-02-10 06:06:44 +08:00
|
|
|
}
|
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
bool toxav_answer(ToxAV* av, uint32_t friend_number, uint32_t audio_bit_rate, uint32_t video_bit_rate, TOXAV_ERR_ANSWER* error)
|
2014-02-10 06:06:44 +08:00
|
|
|
{
|
2015-02-15 06:37:52 +08:00
|
|
|
TOXAV_ERR_ANSWER rc = TOXAV_ERR_ANSWER_OK;
|
|
|
|
if (m_friend_exists(av->m, friend_number) == 0) {
|
|
|
|
rc = TOXAV_ERR_ANSWER_FRIEND_NOT_FOUND;
|
|
|
|
goto END;
|
|
|
|
}
|
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
if ((audio_bit_rate && audio_bitrate_invalid(audio_bit_rate))
|
|
|
|
||(video_bit_rate && video_bitrate_invalid(video_bit_rate))
|
2015-02-15 06:37:52 +08:00
|
|
|
) {
|
|
|
|
rc = TOXAV_ERR_CALL_INVALID_BIT_RATE;
|
|
|
|
goto END;
|
|
|
|
}
|
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
ToxAVCall* call = call_get(av, friend_number);
|
2015-02-20 07:23:38 +08:00
|
|
|
if (call == NULL) {
|
2015-02-15 06:37:52 +08:00
|
|
|
rc = TOXAV_ERR_ANSWER_FRIEND_NOT_CALLING;
|
|
|
|
goto END;
|
|
|
|
}
|
|
|
|
|
2015-02-20 07:23:38 +08:00
|
|
|
call->s_audio_b = audio_bit_rate;
|
|
|
|
call->s_video_b = video_bit_rate;
|
2015-02-15 06:37:52 +08:00
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
call->last_capabilities = msi_CapRAudio | msi_CapRVideo;
|
2015-02-20 07:23:38 +08:00
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
call->last_capabilities |= audio_bit_rate > 0 ? msi_CapSAudio : 0;
|
|
|
|
call->last_capabilities |= video_bit_rate > 0 ? msi_CapSVideo : 0;
|
2015-02-20 07:23:38 +08:00
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
if (msi_answer(call->msi_call, call->last_capabilities) != 0)
|
2015-02-20 07:23:38 +08:00
|
|
|
rc = TOXAV_ERR_ANSWER_FRIEND_NOT_CALLING; /* the only reason for msi_answer to fail */
|
2015-02-15 06:37:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
END:
|
|
|
|
if (error)
|
|
|
|
*error = rc;
|
|
|
|
|
|
|
|
return rc == TOXAV_ERR_ANSWER_OK;
|
2014-02-10 06:06:44 +08:00
|
|
|
}
|
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
void toxav_callback_call_state(ToxAV* av, toxav_call_state_cb* function, void* user_data)
|
2014-02-10 06:06:44 +08:00
|
|
|
{
|
2015-02-15 06:37:52 +08:00
|
|
|
av->scb.first = function;
|
|
|
|
av->scb.second = user_data;
|
2014-02-10 06:06:44 +08:00
|
|
|
}
|
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
bool toxav_call_control(ToxAV* av, uint32_t friend_number, TOXAV_CALL_CONTROL control, TOXAV_ERR_CALL_CONTROL* error)
|
2014-02-10 06:06:44 +08:00
|
|
|
{
|
2015-02-15 06:37:52 +08:00
|
|
|
TOXAV_ERR_CALL_CONTROL rc = TOXAV_ERR_CALL_CONTROL_OK;
|
|
|
|
|
|
|
|
if (m_friend_exists(av->m, friend_number) == 0) {
|
|
|
|
rc = TOXAV_ERR_CALL_CONTROL_FRIEND_NOT_FOUND;
|
|
|
|
goto END;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
ToxAVCall* call = call_get(av, friend_number);
|
2015-02-15 06:37:52 +08:00
|
|
|
if (call == NULL) {
|
|
|
|
rc = TOXAV_ERR_CALL_CONTROL_FRIEND_NOT_IN_CALL;
|
|
|
|
goto END;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO rest of these */
|
|
|
|
switch (control)
|
|
|
|
{
|
|
|
|
case TOXAV_CALL_CONTROL_RESUME: {
|
2015-02-23 06:41:40 +08:00
|
|
|
if (!call->active) {
|
|
|
|
rc = TOXAV_ERR_CALL_CONTROL_FRIEND_NOT_IN_CALL;
|
|
|
|
goto END;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Only act if paused and had media transfer active before */
|
|
|
|
if (call->msi_call->self_capabilities == 0 &&
|
2015-02-21 08:07:22 +08:00
|
|
|
call->last_capabilities ) {
|
|
|
|
|
2015-02-23 06:41:40 +08:00
|
|
|
if (msi_change_capabilities(call->msi_call,
|
|
|
|
call->last_capabilities) == -1) {
|
|
|
|
/* The only reason for this function to fail is invalid state
|
|
|
|
* ( not active ) */
|
|
|
|
rc = TOXAV_ERR_CALL_CONTROL_FRIEND_NOT_IN_CALL;
|
|
|
|
goto END;
|
|
|
|
}
|
2015-02-21 08:07:22 +08:00
|
|
|
|
|
|
|
rtp_start_receiving(call->rtps[audio_index]);
|
|
|
|
rtp_start_receiving(call->rtps[video_index]);
|
|
|
|
}
|
2015-02-15 06:37:52 +08:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case TOXAV_CALL_CONTROL_PAUSE: {
|
2015-02-23 06:41:40 +08:00
|
|
|
if (!call->active) {
|
|
|
|
rc = TOXAV_ERR_CALL_CONTROL_FRIEND_NOT_IN_CALL;
|
|
|
|
goto END;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Only act if not already paused */
|
2015-02-21 08:07:22 +08:00
|
|
|
if (call->msi_call->self_capabilities) {
|
|
|
|
call->last_capabilities = call->msi_call->self_capabilities;
|
|
|
|
|
2015-02-23 06:41:40 +08:00
|
|
|
if (msi_change_capabilities(call->msi_call, 0) == -1 ) {
|
|
|
|
/* The only reason for this function to fail is invalid state
|
|
|
|
* ( not active ) */
|
|
|
|
rc = TOXAV_ERR_CALL_CONTROL_FRIEND_NOT_IN_CALL;
|
|
|
|
goto END;
|
|
|
|
}
|
2015-02-21 08:07:22 +08:00
|
|
|
|
|
|
|
rtp_stop_receiving(call->rtps[audio_index]);
|
|
|
|
rtp_stop_receiving(call->rtps[video_index]);
|
|
|
|
}
|
2015-02-15 06:37:52 +08:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case TOXAV_CALL_CONTROL_CANCEL: {
|
2015-02-21 08:07:22 +08:00
|
|
|
/* Hang up */
|
|
|
|
msi_hangup(call->msi_call);
|
2015-02-20 07:23:38 +08:00
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
/* No mather the case, terminate the call */
|
2015-02-23 06:41:40 +08:00
|
|
|
call_kill_transmission(call);
|
2015-02-21 08:07:22 +08:00
|
|
|
call_remove(call);
|
2015-02-15 06:37:52 +08:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case TOXAV_CALL_CONTROL_MUTE_AUDIO: {
|
2015-02-23 06:41:40 +08:00
|
|
|
if (!call->active) {
|
|
|
|
rc = TOXAV_ERR_CALL_CONTROL_FRIEND_NOT_IN_CALL;
|
|
|
|
goto END;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (call->msi_call->self_capabilities & msi_CapRAudio) {
|
|
|
|
if (msi_change_capabilities(call->msi_call, call->
|
|
|
|
msi_call->self_capabilities ^ msi_CapRAudio) == -1) {
|
|
|
|
/* The only reason for this function to fail is invalid state
|
|
|
|
* ( not active ) */
|
|
|
|
rc = TOXAV_ERR_CALL_CONTROL_FRIEND_NOT_IN_CALL;
|
|
|
|
goto END;
|
|
|
|
}
|
2015-02-21 08:07:22 +08:00
|
|
|
|
|
|
|
rtp_stop_receiving(call->rtps[audio_index]);
|
|
|
|
}
|
2015-02-15 06:37:52 +08:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case TOXAV_CALL_CONTROL_MUTE_VIDEO: {
|
2015-02-23 06:41:40 +08:00
|
|
|
if (!call->active) {
|
|
|
|
rc = TOXAV_ERR_CALL_CONTROL_FRIEND_NOT_IN_CALL;
|
|
|
|
goto END;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (call->msi_call->self_capabilities & msi_CapRVideo) {
|
|
|
|
if (msi_change_capabilities(call->msi_call, call->
|
|
|
|
msi_call->self_capabilities ^ msi_CapRVideo) == -1) {
|
|
|
|
/* The only reason for this function to fail is invalid state
|
|
|
|
* ( not active ) */
|
|
|
|
rc = TOXAV_ERR_CALL_CONTROL_FRIEND_NOT_IN_CALL;
|
|
|
|
goto END;
|
|
|
|
}
|
|
|
|
|
|
|
|
rtp_stop_receiving(call->rtps[video_index]);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case TOXAV_CALL_CONTROL_UNMUTE_AUDIO: {
|
|
|
|
if (!call->active) {
|
|
|
|
rc = TOXAV_ERR_CALL_CONTROL_FRIEND_NOT_IN_CALL;
|
|
|
|
goto END;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (call->msi_call->self_capabilities & ~msi_CapRAudio) {
|
|
|
|
if (msi_change_capabilities(call->msi_call, call->
|
|
|
|
msi_call->self_capabilities | msi_CapRAudio) == -1) {
|
|
|
|
/* The only reason for this function to fail is invalid state
|
|
|
|
* ( not active ) */
|
|
|
|
rc = TOXAV_ERR_CALL_CONTROL_FRIEND_NOT_IN_CALL;
|
|
|
|
goto END;
|
|
|
|
}
|
|
|
|
|
|
|
|
rtp_start_receiving(call->rtps[audio_index]);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case TOXAV_CALL_CONTROL_UNMUTE_VIDEO: {
|
|
|
|
if (!call->active) {
|
|
|
|
rc = TOXAV_ERR_CALL_CONTROL_FRIEND_NOT_IN_CALL;
|
|
|
|
goto END;
|
|
|
|
}
|
2015-02-15 06:37:52 +08:00
|
|
|
|
2015-02-23 06:41:40 +08:00
|
|
|
if (call->msi_call->self_capabilities & ~msi_CapRVideo) {
|
|
|
|
if (msi_change_capabilities(call->msi_call, call->
|
|
|
|
msi_call->self_capabilities | msi_CapRVideo) == -1) {
|
|
|
|
/* The only reason for this function to fail is invalid state
|
|
|
|
* ( not active ) */
|
|
|
|
rc = TOXAV_ERR_CALL_CONTROL_FRIEND_NOT_IN_CALL;
|
|
|
|
goto END;
|
|
|
|
}
|
|
|
|
|
|
|
|
rtp_start_receiving(call->rtps[video_index]);
|
|
|
|
}
|
2015-02-15 06:37:52 +08:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
|
|
|
|
END:
|
|
|
|
if (error)
|
|
|
|
*error = rc;
|
|
|
|
|
|
|
|
return rc == TOXAV_ERR_CALL_CONTROL_OK;
|
2014-02-10 06:06:44 +08:00
|
|
|
}
|
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
bool toxav_set_audio_bit_rate(ToxAV* av, uint32_t friend_number, uint32_t audio_bit_rate, TOXAV_ERR_BIT_RATE* error)
|
2014-02-10 06:06:44 +08:00
|
|
|
{
|
2015-02-15 06:37:52 +08:00
|
|
|
/* TODO */
|
2014-02-10 06:06:44 +08:00
|
|
|
}
|
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
bool toxav_set_video_bit_rate(ToxAV* av, uint32_t friend_number, uint32_t video_bit_rate, TOXAV_ERR_BIT_RATE* error)
|
2014-07-21 07:10:57 +08:00
|
|
|
{
|
2015-02-15 06:37:52 +08:00
|
|
|
/* TODO */
|
2014-07-21 07:10:57 +08:00
|
|
|
}
|
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
void toxav_callback_request_video_frame(ToxAV* av, toxav_request_video_frame_cb* function, void* user_data)
|
2014-02-10 06:06:44 +08:00
|
|
|
{
|
2015-02-15 06:37:52 +08:00
|
|
|
/* TODO */
|
2014-02-10 06:06:44 +08:00
|
|
|
}
|
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
bool toxav_send_video_frame(ToxAV* av, uint32_t friend_number, uint16_t width, uint16_t height, const uint8_t* y, const uint8_t* u, const uint8_t* v, TOXAV_ERR_SEND_FRAME* error)
|
2014-02-10 06:06:44 +08:00
|
|
|
{
|
2015-02-15 06:37:52 +08:00
|
|
|
TOXAV_ERR_SEND_FRAME rc = TOXAV_ERR_SEND_FRAME_OK;
|
2015-02-20 07:23:38 +08:00
|
|
|
ToxAVCall* call;
|
2015-02-15 06:37:52 +08:00
|
|
|
|
|
|
|
if (m_friend_exists(av->m, friend_number) == 0) {
|
|
|
|
rc = TOXAV_ERR_SEND_FRAME_FRIEND_NOT_FOUND;
|
|
|
|
goto END;
|
2014-02-10 06:06:44 +08:00
|
|
|
}
|
2015-02-15 06:37:52 +08:00
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
call = call_get(av, friend_number);
|
2015-02-15 06:37:52 +08:00
|
|
|
if (call == NULL) {
|
|
|
|
rc = TOXAV_ERR_SEND_FRAME_FRIEND_NOT_IN_CALL;
|
|
|
|
goto END;
|
2014-12-04 03:41:01 +08:00
|
|
|
}
|
2015-02-15 06:37:52 +08:00
|
|
|
|
2015-02-20 07:23:38 +08:00
|
|
|
if (call->msi_call->state != msi_CallActive) {
|
2015-02-15 06:37:52 +08:00
|
|
|
/* TODO */
|
|
|
|
rc = TOXAV_ERR_SEND_FRAME_NOT_REQUESTED;
|
|
|
|
goto END;
|
2014-11-18 07:46:46 +08:00
|
|
|
}
|
2015-02-15 06:37:52 +08:00
|
|
|
|
|
|
|
if ( y == NULL || u == NULL || v == NULL ) {
|
|
|
|
rc = TOXAV_ERR_SEND_FRAME_NULL;
|
|
|
|
goto END;
|
2014-02-10 06:06:44 +08:00
|
|
|
}
|
2015-02-15 06:37:52 +08:00
|
|
|
|
|
|
|
if ( cs_set_sending_video_resolution(call->cs, width, height) != 0 ) {
|
|
|
|
rc = TOXAV_ERR_SEND_FRAME_INVALID;
|
|
|
|
goto END;
|
2014-07-03 22:58:00 +08:00
|
|
|
}
|
2015-02-15 06:37:52 +08:00
|
|
|
|
|
|
|
{ /* Encode */
|
|
|
|
vpx_image_t img;
|
|
|
|
img.w = img.h = img.d_w = img.d_h = 0;
|
|
|
|
vpx_img_alloc(&img, VPX_IMG_FMT_VPXI420, width, height, 1);
|
|
|
|
|
|
|
|
/* I420 "It comprises an NxM Y plane followed by (N/2)x(M/2) V and U planes."
|
|
|
|
* http://fourcc.org/yuv.php#IYUV
|
|
|
|
*/
|
|
|
|
memcpy(img.planes[VPX_PLANE_Y], y, width * height);
|
|
|
|
memcpy(img.planes[VPX_PLANE_U], u, (width/2) * (height/2));
|
|
|
|
memcpy(img.planes[VPX_PLANE_V], v, (width/2) * (height/2));
|
|
|
|
|
|
|
|
int vrc = vpx_codec_encode(call->cs->v_encoder, &img,
|
|
|
|
call->cs->frame_counter, 1, 0, MAX_ENCODE_TIME_US);
|
|
|
|
|
|
|
|
vpx_img_free(&img); /* FIXME don't free? */
|
|
|
|
if ( vrc != VPX_CODEC_OK) {
|
|
|
|
LOGGER_ERROR("Could not encode video frame: %s\n", vpx_codec_err_to_string(vrc));
|
|
|
|
rc = TOXAV_ERR_SEND_FRAME_INVALID;
|
|
|
|
goto END;
|
|
|
|
}
|
2014-07-03 22:58:00 +08:00
|
|
|
}
|
2015-02-15 06:37:52 +08:00
|
|
|
|
|
|
|
++call->cs->frame_counter;
|
|
|
|
|
|
|
|
{ /* Split and send */
|
|
|
|
vpx_codec_iter_t iter = NULL;
|
|
|
|
const vpx_codec_cx_pkt_t *pkt;
|
|
|
|
|
|
|
|
cs_init_video_splitter_cycle(call->cs);
|
|
|
|
|
|
|
|
while ( (pkt = vpx_codec_get_cx_data(call->cs->v_encoder, &iter)) ) {
|
|
|
|
if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
|
|
|
|
int parts = cs_update_video_splitter_cycle(call->cs, pkt->data.frame.buf,
|
|
|
|
pkt->data.frame.sz);
|
|
|
|
|
|
|
|
if (parts < 0) /* Should never happen though */
|
|
|
|
continue;
|
|
|
|
|
|
|
|
uint16_t part_size;
|
|
|
|
const uint8_t *iter;
|
|
|
|
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < parts; i++) {
|
|
|
|
iter = cs_iterate_split_video_frame(call->cs, &part_size);
|
|
|
|
|
|
|
|
if (rtp_send_msg(call->rtps[video_index], iter, part_size) < 0)
|
|
|
|
goto END;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-07-06 01:27:31 +08:00
|
|
|
}
|
2015-02-15 06:37:52 +08:00
|
|
|
|
|
|
|
END:
|
|
|
|
if (error)
|
|
|
|
*error = rc;
|
|
|
|
|
|
|
|
return rc == TOXAV_ERR_SEND_FRAME_OK;
|
2014-02-10 06:06:44 +08:00
|
|
|
}
|
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
void toxav_callback_request_audio_frame(ToxAV* av, toxav_request_audio_frame_cb* function, void* user_data)
|
2014-02-10 06:06:44 +08:00
|
|
|
{
|
2015-02-15 06:37:52 +08:00
|
|
|
/* TODO */
|
2014-02-16 03:44:33 +08:00
|
|
|
}
|
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
bool toxav_send_audio_frame(ToxAV* av, uint32_t friend_number, const int16_t* pcm, size_t sample_count, uint8_t channels, uint32_t sampling_rate, TOXAV_ERR_SEND_FRAME* error)
|
2014-02-10 06:06:44 +08:00
|
|
|
{
|
2015-02-15 06:37:52 +08:00
|
|
|
TOXAV_ERR_SEND_FRAME rc = TOXAV_ERR_SEND_FRAME_OK;
|
2015-02-20 07:23:38 +08:00
|
|
|
ToxAVCall* call;
|
2015-02-15 06:37:52 +08:00
|
|
|
|
|
|
|
if (m_friend_exists(av->m, friend_number) == 0) {
|
|
|
|
rc = TOXAV_ERR_SEND_FRAME_FRIEND_NOT_FOUND;
|
|
|
|
goto END;
|
2014-07-03 22:58:00 +08:00
|
|
|
}
|
2015-02-15 06:37:52 +08:00
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
call = call_get(av, friend_number);
|
2015-02-15 06:37:52 +08:00
|
|
|
if (call == NULL) {
|
|
|
|
rc = TOXAV_ERR_SEND_FRAME_FRIEND_NOT_IN_CALL;
|
|
|
|
goto END;
|
2014-07-06 01:27:31 +08:00
|
|
|
}
|
2015-02-15 06:37:52 +08:00
|
|
|
|
2015-02-20 07:23:38 +08:00
|
|
|
if (call->msi_call->state != msi_CallActive) {
|
2015-02-15 06:37:52 +08:00
|
|
|
/* TODO */
|
|
|
|
rc = TOXAV_ERR_SEND_FRAME_NOT_REQUESTED;
|
|
|
|
goto END;
|
2014-07-06 05:31:06 +08:00
|
|
|
}
|
2015-02-15 06:37:52 +08:00
|
|
|
|
|
|
|
if ( pcm == NULL ) {
|
|
|
|
rc = TOXAV_ERR_SEND_FRAME_NULL;
|
|
|
|
goto END;
|
2014-02-16 03:44:33 +08:00
|
|
|
}
|
2015-02-15 06:37:52 +08:00
|
|
|
|
|
|
|
if ( channels != 1 || channels != 2 ) {
|
|
|
|
rc = TOXAV_ERR_SEND_FRAME_INVALID;
|
|
|
|
goto END;
|
|
|
|
}
|
|
|
|
|
|
|
|
{ /* Encode and send */
|
|
|
|
/* TODO redundant? */
|
|
|
|
cs_set_sending_audio_channels(call->cs, channels);
|
|
|
|
cs_set_sending_audio_sampling_rate(call->cs, sampling_rate);
|
|
|
|
|
|
|
|
uint8_t dest[sample_count * channels * 2 /* sizeof(uint16_t) */];
|
|
|
|
int vrc = opus_encode(call->cs->audio_encoder, pcm, sample_count, dest, sizeof (dest));
|
|
|
|
|
|
|
|
if (vrc < 0) {
|
|
|
|
LOGGER_WARNING("Failed to encode frame");
|
|
|
|
rc = TOXAV_ERR_SEND_FRAME_INVALID;
|
|
|
|
goto END;
|
2014-02-16 03:44:33 +08:00
|
|
|
}
|
2015-02-15 06:37:52 +08:00
|
|
|
|
|
|
|
vrc = rtp_send_msg(call->rtps[audio_index], dest, vrc);
|
|
|
|
/* TODO check for error? */
|
2014-02-16 03:44:33 +08:00
|
|
|
}
|
2015-02-15 06:37:52 +08:00
|
|
|
|
|
|
|
END:
|
|
|
|
if (error)
|
|
|
|
*error = rc;
|
|
|
|
|
|
|
|
return rc == TOXAV_ERR_SEND_FRAME_OK;
|
2014-02-16 03:44:33 +08:00
|
|
|
}
|
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
void toxav_callback_receive_video_frame(ToxAV* av, toxav_receive_video_frame_cb* function, void* user_data)
|
2014-02-16 03:44:33 +08:00
|
|
|
{
|
2015-02-15 06:37:52 +08:00
|
|
|
av->vcb.first = function;
|
|
|
|
av->vcb.second = user_data;
|
2014-05-03 07:46:03 +08:00
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
void toxav_callback_receive_audio_frame(ToxAV* av, toxav_receive_audio_frame_cb* function, void* user_data)
|
2014-02-16 03:44:33 +08:00
|
|
|
{
|
2015-02-15 06:37:52 +08:00
|
|
|
av->acb.first = function;
|
|
|
|
av->acb.second = user_data;
|
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-05 21:11:25 +08:00
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
/*******************************************************************************
|
|
|
|
*
|
|
|
|
* :: Internal
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
2015-02-21 08:07:22 +08:00
|
|
|
int callback_invite(void* toxav_inst, MSICall* call)
|
2015-02-15 06:37:52 +08:00
|
|
|
{
|
|
|
|
ToxAV* toxav = toxav_inst;
|
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
ToxAVCall* av_call = call_new(toxav, call->friend_id, NULL);
|
2015-02-19 06:23:46 +08:00
|
|
|
if (av_call == NULL) {
|
2015-02-21 08:07:22 +08:00
|
|
|
LOGGER_WARNING("Failed to initialize call...");
|
|
|
|
return -1;
|
2014-05-11 00:00:49 +08:00
|
|
|
}
|
2015-02-15 06:37:52 +08:00
|
|
|
|
2015-02-19 06:23:46 +08:00
|
|
|
call->av_call = av_call;
|
|
|
|
av_call->msi_call = call;
|
2015-02-15 06:37:52 +08:00
|
|
|
|
|
|
|
if (toxav->ccb.first)
|
2015-02-19 06:23:46 +08:00
|
|
|
toxav->ccb.first(toxav, call->friend_id, call->peer_capabilities & msi_CapSAudio,
|
|
|
|
call->peer_capabilities & msi_CapSVideo, toxav->ccb.second);
|
2015-02-21 08:07:22 +08:00
|
|
|
|
|
|
|
return 0;
|
2014-02-10 06:06:44 +08:00
|
|
|
}
|
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
int callback_start(void* toxav_inst, MSICall* call)
|
2015-02-15 06:37:52 +08:00
|
|
|
{
|
|
|
|
ToxAV* toxav = toxav_inst;
|
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
ToxAVCall* av_call = call_get(toxav, call->friend_id);
|
2015-02-15 06:37:52 +08:00
|
|
|
|
2015-02-23 06:41:40 +08:00
|
|
|
if (av_call == NULL) {
|
|
|
|
/* Should this ever happen? */
|
2015-02-21 08:07:22 +08:00
|
|
|
return -1;
|
2014-11-18 07:46:46 +08:00
|
|
|
}
|
2015-02-15 06:37:52 +08:00
|
|
|
|
2015-02-23 06:41:40 +08:00
|
|
|
if (!call_prepare_transmission(av_call)) {
|
|
|
|
callback_error(toxav_inst, call);
|
|
|
|
call_remove(av_call);
|
|
|
|
return -1;
|
|
|
|
}
|
2015-02-15 06:37:52 +08:00
|
|
|
|
|
|
|
if (toxav->scb.first)
|
2015-02-23 06:41:40 +08:00
|
|
|
toxav->scb.first(toxav, call->friend_id,
|
|
|
|
capabilities_to_state(call->peer_capabilities), toxav->scb.second);
|
2015-02-21 08:07:22 +08:00
|
|
|
|
|
|
|
return 0;
|
2014-03-07 10:13:04 +08:00
|
|
|
}
|
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
int callback_end(void* toxav_inst, MSICall* call)
|
2014-07-05 01:28:47 +08:00
|
|
|
{
|
2015-02-15 06:37:52 +08:00
|
|
|
ToxAV* toxav = toxav_inst;
|
|
|
|
|
|
|
|
if (toxav->scb.first)
|
2015-02-19 06:23:46 +08:00
|
|
|
toxav->scb.first(toxav, call->friend_id, TOXAV_CALL_STATE_END, toxav->scb.second);
|
2015-02-21 08:07:22 +08:00
|
|
|
|
2015-02-23 06:41:40 +08:00
|
|
|
call_kill_transmission(call->av_call);
|
|
|
|
call_remove(call->av_call);
|
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
return 0;
|
2014-07-05 00:16:53 +08:00
|
|
|
}
|
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
int callback_error(void* toxav_inst, MSICall* call)
|
2014-03-07 10:13:04 +08:00
|
|
|
{
|
2015-02-15 06:37:52 +08:00
|
|
|
ToxAV* toxav = toxav_inst;
|
2015-02-23 06:41:40 +08:00
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
if (toxav->scb.first)
|
2015-02-19 06:23:46 +08:00
|
|
|
toxav->scb.first(toxav, call->friend_id, TOXAV_CALL_STATE_ERROR, toxav->scb.second);
|
2015-02-21 08:07:22 +08:00
|
|
|
|
|
|
|
return 0;
|
2014-03-07 10:13:04 +08:00
|
|
|
}
|
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
int callback_capabilites(void* toxav_inst, MSICall* call)
|
2014-05-17 01:56:40 +08:00
|
|
|
{
|
2015-02-15 06:37:52 +08:00
|
|
|
ToxAV* toxav = toxav_inst;
|
2015-02-21 08:07:22 +08:00
|
|
|
if (toxav->scb.first)
|
|
|
|
toxav->scb.first(toxav, call->friend_id,
|
|
|
|
capabilities_to_state(call->peer_capabilities), toxav->scb.second);
|
|
|
|
|
|
|
|
return 0;
|
2014-06-21 07:58:55 +08:00
|
|
|
}
|
|
|
|
|
2015-02-19 06:23:46 +08:00
|
|
|
TOXAV_CALL_STATE capabilities_to_state(uint8_t capabilities)
|
2014-07-08 04:10:10 +08:00
|
|
|
{
|
2015-02-23 06:41:40 +08:00
|
|
|
if (capabilities == 0)
|
|
|
|
return TOXAV_CALL_STATE_PAUSED;
|
|
|
|
else if ((capabilities & msi_CapSAudio) && (capabilities & msi_CapSVideo))
|
2015-02-19 06:23:46 +08:00
|
|
|
return TOXAV_CALL_STATE_SENDING_AV;
|
|
|
|
else if (capabilities & msi_CapSAudio)
|
|
|
|
return TOXAV_CALL_STATE_SENDING_A;
|
|
|
|
else if (capabilities & msi_CapSVideo)
|
|
|
|
return TOXAV_CALL_STATE_SENDING_V;
|
2015-02-23 06:41:40 +08:00
|
|
|
|
|
|
|
return TOXAV_CALL_STATE_NOT_SENDING;
|
2015-02-15 06:37:52 +08:00
|
|
|
}
|
2014-12-04 03:41:01 +08:00
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
bool audio_bitrate_invalid(uint32_t bitrate)
|
2015-02-15 06:37:52 +08:00
|
|
|
{
|
2015-02-21 08:07:22 +08:00
|
|
|
/* Opus RFC 6716 section-2.1.1 dictates the following:
|
|
|
|
* Opus supports all bitrates from 6 kbit/s to 510 kbit/s.
|
|
|
|
*/
|
|
|
|
return bitrate < 6 || bitrate > 510;
|
2015-02-15 06:37:52 +08:00
|
|
|
}
|
2014-12-04 03:41:01 +08:00
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
bool video_bitrate_invalid(uint32_t bitrate)
|
2015-02-15 06:37:52 +08:00
|
|
|
{
|
2015-02-21 08:07:22 +08:00
|
|
|
/* TODO: If anyone knows the answer to this one please fill it up */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ToxAVCall* call_new(ToxAV* av, uint32_t friend_number, TOXAV_ERR_CALL* error)
|
|
|
|
{
|
|
|
|
TOXAV_ERR_CALL rc = TOXAV_ERR_CALL_OK;
|
|
|
|
ToxAVCall* call = NULL;
|
2015-02-15 06:37:52 +08:00
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
if (m_friend_exists(av->m, friend_number) == 0) {
|
|
|
|
rc = TOXAV_ERR_CALL_FRIEND_NOT_FOUND;
|
|
|
|
goto END;
|
|
|
|
}
|
2015-02-15 06:37:52 +08:00
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
if (m_get_friend_connectionstatus(av->m, friend_number) != 1) {
|
|
|
|
rc = TOXAV_ERR_CALL_FRIEND_NOT_CONNECTED;
|
|
|
|
goto END;
|
|
|
|
}
|
2015-02-15 06:37:52 +08:00
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
if (call_get(av, friend_number) != NULL) {
|
|
|
|
rc = TOXAV_ERR_CALL_FRIEND_ALREADY_IN_CALL;
|
|
|
|
goto END;
|
2014-12-04 03:41:01 +08:00
|
|
|
}
|
2015-02-15 06:37:52 +08:00
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
|
|
|
|
call = calloc(sizeof(ToxAVCall), 1);
|
|
|
|
|
|
|
|
if (call == NULL) {
|
|
|
|
rc = TOXAV_ERR_CALL_MALLOC;
|
|
|
|
goto END;
|
|
|
|
}
|
|
|
|
|
|
|
|
call->av = av;
|
|
|
|
call->friend_id = friend_number;
|
|
|
|
|
|
|
|
if (create_recursive_mutex(call->mutex_control) != 0) {
|
|
|
|
free(call);
|
|
|
|
call = NULL;
|
|
|
|
rc = TOXAV_ERR_CALL_MALLOC;
|
|
|
|
goto END;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (create_recursive_mutex(call->mutex_do) != 0) {
|
|
|
|
pthread_mutex_destroy(call->mutex_control);
|
|
|
|
free(call);
|
|
|
|
call = NULL;
|
|
|
|
rc = TOXAV_ERR_CALL_MALLOC;
|
|
|
|
goto END;
|
2015-02-15 06:37:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (av->calls == NULL) { /* Creating */
|
2015-02-20 07:23:38 +08:00
|
|
|
av->calls = calloc (sizeof(ToxAVCall*), friend_number + 1);
|
2015-02-15 06:37:52 +08:00
|
|
|
|
|
|
|
if (av->calls == NULL) {
|
2015-02-21 08:07:22 +08:00
|
|
|
pthread_mutex_destroy(call->mutex_control);
|
|
|
|
pthread_mutex_destroy(call->mutex_do);
|
|
|
|
free(call);
|
|
|
|
call = NULL;
|
|
|
|
rc = TOXAV_ERR_CALL_MALLOC;
|
|
|
|
goto END;
|
2015-02-15 06:37:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
av->calls_tail = av->calls_head = friend_number;
|
|
|
|
|
|
|
|
} else if (av->calls_tail < friend_number) { /* Appending */
|
2015-02-20 07:23:38 +08:00
|
|
|
void* tmp = realloc(av->calls, sizeof(ToxAVCall*) * friend_number + 1);
|
2015-02-15 06:37:52 +08:00
|
|
|
|
|
|
|
if (tmp == NULL) {
|
2015-02-21 08:07:22 +08:00
|
|
|
pthread_mutex_destroy(call->mutex_control);
|
|
|
|
pthread_mutex_destroy(call->mutex_do);
|
|
|
|
free(call);
|
|
|
|
call = NULL;
|
|
|
|
rc = TOXAV_ERR_CALL_MALLOC;
|
|
|
|
goto END;
|
2015-02-15 06:37:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
av->calls = tmp;
|
|
|
|
|
|
|
|
/* Set fields in between to null */
|
|
|
|
int32_t i = av->calls_tail;
|
|
|
|
for (; i < friend_number; i ++)
|
|
|
|
av->calls[i] = NULL;
|
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
call->prev = av->calls[av->calls_tail];
|
|
|
|
av->calls[av->calls_tail]->next = call;
|
2015-02-15 06:37:52 +08:00
|
|
|
|
|
|
|
av->calls_tail = friend_number;
|
|
|
|
|
|
|
|
} else if (av->calls_head > friend_number) { /* Inserting at front */
|
2015-02-21 08:07:22 +08:00
|
|
|
call->next = av->calls[av->calls_head];
|
|
|
|
av->calls[av->calls_head]->prev = call;
|
2015-02-15 06:37:52 +08:00
|
|
|
av->calls_head = friend_number;
|
|
|
|
}
|
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
av->calls[friend_number] = call;
|
2015-02-15 06:37:52 +08:00
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
END:
|
2015-02-15 06:37:52 +08:00
|
|
|
if (error)
|
|
|
|
*error = rc;
|
|
|
|
|
|
|
|
return call;
|
2014-11-11 06:59:14 +08:00
|
|
|
}
|
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
ToxAVCall* call_get(ToxAV* av, uint32_t friend_number)
|
2015-02-19 06:23:46 +08:00
|
|
|
{
|
2015-02-21 08:07:22 +08:00
|
|
|
if (av->calls == NULL || av->calls_tail < friend_number)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return av->calls[friend_number];
|
2015-02-19 06:23:46 +08:00
|
|
|
}
|
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
bool call_prepare_transmission(ToxAVCall* call)
|
2015-02-15 06:37:52 +08:00
|
|
|
{
|
2015-02-21 08:07:22 +08:00
|
|
|
if (call == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ToxAV* av = call->av;
|
|
|
|
|
2015-02-20 07:23:38 +08:00
|
|
|
if (!av->acb.first && !av->vcb.first)
|
|
|
|
/* It makes no sense to have CSession without callbacks */
|
|
|
|
return false;
|
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
pthread_mutex_lock(call->mutex_control);
|
|
|
|
|
|
|
|
if (call->active) {
|
|
|
|
pthread_mutex_unlock(call->mutex_control);
|
|
|
|
LOGGER_WARNING("Call already active!\n");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pthread_mutex_init(call->mutex_encoding_audio, NULL) != 0)
|
|
|
|
goto MUTEX_INIT_ERROR;
|
|
|
|
|
|
|
|
if (pthread_mutex_init(call->mutex_encoding_video, NULL) != 0) {
|
|
|
|
pthread_mutex_destroy(call->mutex_encoding_audio);
|
|
|
|
goto MUTEX_INIT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pthread_mutex_init(call->mutex_do, NULL) != 0) {
|
|
|
|
pthread_mutex_destroy(call->mutex_encoding_audio);
|
|
|
|
pthread_mutex_destroy(call->mutex_encoding_video);
|
|
|
|
goto MUTEX_INIT_ERROR;
|
|
|
|
}
|
|
|
|
|
2015-02-20 07:23:38 +08:00
|
|
|
call->cs = cs_new(call->msi_call->peer_vfpsz);
|
2015-02-15 06:37:52 +08:00
|
|
|
|
|
|
|
if ( !call->cs ) {
|
|
|
|
LOGGER_ERROR("Error while starting Codec State!\n");
|
|
|
|
goto FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
call->cs->agent = av;
|
2015-02-20 07:23:38 +08:00
|
|
|
call->cs->friend_id = call->friend_id;
|
2015-02-15 06:37:52 +08:00
|
|
|
|
|
|
|
memcpy(&call->cs->acb, &av->acb, sizeof(av->acb));
|
|
|
|
memcpy(&call->cs->vcb, &av->vcb, sizeof(av->vcb));
|
|
|
|
|
2015-02-23 06:41:40 +08:00
|
|
|
{ /* Prepare audio */
|
2015-02-20 07:23:38 +08:00
|
|
|
call->rtps[audio_index] = rtp_new(rtp_TypeAudio, av->m, call->friend_id);
|
2015-02-15 06:37:52 +08:00
|
|
|
|
|
|
|
if ( !call->rtps[audio_index] ) {
|
|
|
|
LOGGER_ERROR("Error while starting audio RTP session!\n");
|
|
|
|
goto FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
call->rtps[audio_index]->cs = call->cs;
|
|
|
|
|
2015-02-23 06:41:40 +08:00
|
|
|
/* Only enable sending if bitrate is defined */
|
|
|
|
if (call->s_audio_b > 0 && cs_enable_audio_sending(call->cs, call->s_audio_b, 2) != 0) {
|
2015-02-20 07:23:38 +08:00
|
|
|
LOGGER_WARNING("Failed to enable audio sending!");
|
|
|
|
goto FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-02-23 06:41:40 +08:00
|
|
|
if (cs_enable_audio_receiving(call->cs) != 0) {
|
|
|
|
LOGGER_WARNING("Failed to enable audio receiving!");
|
|
|
|
goto FAILURE;
|
2015-02-20 07:23:38 +08:00
|
|
|
}
|
2015-02-23 06:41:40 +08:00
|
|
|
|
|
|
|
rtp_start_receiving(call->rtps[audio_index]);
|
2015-02-15 06:37:52 +08:00
|
|
|
}
|
|
|
|
|
2015-02-23 06:41:40 +08:00
|
|
|
{ /* Prepare video */
|
2015-02-20 07:23:38 +08:00
|
|
|
call->rtps[video_index] = rtp_new(rtp_TypeVideo, av->m, call->friend_id);
|
2015-02-15 06:37:52 +08:00
|
|
|
|
|
|
|
if ( !call->rtps[video_index] ) {
|
|
|
|
LOGGER_ERROR("Error while starting video RTP session!\n");
|
|
|
|
goto FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
call->rtps[video_index]->cs = call->cs;
|
|
|
|
|
2015-02-23 06:41:40 +08:00
|
|
|
/* Only enable sending if bitrate is defined */
|
|
|
|
if (call->s_video_b > 0 && cs_enable_video_sending(call->cs, call->s_video_b) != 0) {
|
2015-02-20 07:23:38 +08:00
|
|
|
LOGGER_WARNING("Failed to enable video sending!");
|
|
|
|
goto FAILURE;
|
|
|
|
}
|
|
|
|
|
2015-02-23 06:41:40 +08:00
|
|
|
|
|
|
|
if (cs_enable_video_receiving(call->cs) != 0) {
|
|
|
|
LOGGER_WARNING("Failed to enable video receiving!");
|
|
|
|
goto FAILURE;
|
2015-02-20 07:23:38 +08:00
|
|
|
}
|
2015-02-23 06:41:40 +08:00
|
|
|
|
|
|
|
rtp_start_receiving(call->rtps[audio_index]);
|
2015-02-15 06:37:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
call->active = 1;
|
|
|
|
pthread_mutex_unlock(call->mutex_control);
|
|
|
|
return true;
|
|
|
|
|
2015-02-23 06:41:40 +08:00
|
|
|
FAILURE:
|
2015-02-15 06:37:52 +08:00
|
|
|
rtp_kill(call->rtps[audio_index]);
|
|
|
|
call->rtps[audio_index] = NULL;
|
|
|
|
rtp_kill(call->rtps[video_index]);
|
|
|
|
call->rtps[video_index] = NULL;
|
|
|
|
cs_kill(call->cs);
|
|
|
|
call->cs = NULL;
|
|
|
|
call->active = 0;
|
|
|
|
pthread_mutex_destroy(call->mutex_encoding_audio);
|
|
|
|
pthread_mutex_destroy(call->mutex_encoding_video);
|
|
|
|
pthread_mutex_destroy(call->mutex_do);
|
|
|
|
|
|
|
|
pthread_mutex_unlock(call->mutex_control);
|
|
|
|
return false;
|
|
|
|
|
2015-02-23 06:41:40 +08:00
|
|
|
MUTEX_INIT_ERROR:
|
2015-02-15 06:37:52 +08:00
|
|
|
pthread_mutex_unlock(call->mutex_control);
|
|
|
|
LOGGER_ERROR("Mutex initialization failed!\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
void call_kill_transmission(ToxAVCall* call)
|
2015-02-15 06:37:52 +08:00
|
|
|
{
|
2015-02-21 08:07:22 +08:00
|
|
|
if (call == NULL || call->active == 0)
|
2015-02-15 06:37:52 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
pthread_mutex_lock(call->mutex_control);
|
|
|
|
|
|
|
|
call->active = 0;
|
|
|
|
|
|
|
|
pthread_mutex_lock(call->mutex_encoding_audio);
|
|
|
|
pthread_mutex_unlock(call->mutex_encoding_audio);
|
|
|
|
pthread_mutex_lock(call->mutex_encoding_video);
|
|
|
|
pthread_mutex_unlock(call->mutex_encoding_video);
|
|
|
|
pthread_mutex_lock(call->mutex_do);
|
|
|
|
pthread_mutex_unlock(call->mutex_do);
|
|
|
|
|
|
|
|
rtp_kill(call->rtps[audio_index]);
|
|
|
|
call->rtps[audio_index] = NULL;
|
|
|
|
rtp_kill(call->rtps[video_index]);
|
|
|
|
call->rtps[video_index] = NULL;
|
|
|
|
cs_kill(call->cs);
|
|
|
|
call->cs = NULL;
|
|
|
|
|
|
|
|
pthread_mutex_destroy(call->mutex_encoding_audio);
|
|
|
|
pthread_mutex_destroy(call->mutex_encoding_video);
|
|
|
|
pthread_mutex_destroy(call->mutex_do);
|
|
|
|
|
|
|
|
pthread_mutex_unlock(call->mutex_control);
|
2015-02-21 08:07:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void call_remove(ToxAVCall* call)
|
|
|
|
{
|
|
|
|
if (call == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
uint32_t friend_id = call->friend_id;
|
|
|
|
ToxAV* av = call->av;
|
|
|
|
|
|
|
|
ToxAVCall* prev = call->prev;
|
|
|
|
ToxAVCall* next = call->next;
|
|
|
|
|
|
|
|
pthread_mutex_destroy(call->mutex_control);
|
|
|
|
pthread_mutex_destroy(call->mutex_do);
|
|
|
|
|
|
|
|
free(call);
|
|
|
|
|
|
|
|
if (prev)
|
|
|
|
prev->next = next;
|
|
|
|
else if (next)
|
|
|
|
av->calls_head = next->friend_id;
|
|
|
|
else goto CLEAR;
|
|
|
|
|
|
|
|
if (next)
|
|
|
|
next->prev = prev;
|
|
|
|
else if (prev)
|
|
|
|
av->calls_tail = prev->friend_id;
|
|
|
|
else goto CLEAR;
|
|
|
|
|
|
|
|
av->calls[friend_id] = NULL;
|
|
|
|
return;
|
|
|
|
|
|
|
|
CLEAR:
|
|
|
|
av->calls_head = av->calls_tail = 0;
|
|
|
|
free(av->calls);
|
|
|
|
av->calls = NULL;
|
2015-02-19 06:23:46 +08:00
|
|
|
}
|