2020-03-12 09:10:33 +08:00
|
|
|
/* SPDX-License-Identifier: GPL-3.0-or-later
|
2018-08-26 07:36:42 +08:00
|
|
|
* Copyright © 2016-2018 The TokTok team.
|
2017-01-14 23:46:31 +08:00
|
|
|
* Copyright © 2013-2015 Tox project.
|
2014-02-10 06:06:44 +08:00
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif /* HAVE_CONFIG_H */
|
|
|
|
|
2016-09-11 22:47:51 +08:00
|
|
|
#include "toxav.h"
|
|
|
|
|
2015-04-22 08:09:37 +08:00
|
|
|
#include "msi.h"
|
|
|
|
#include "rtp.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"
|
2018-07-08 16:43:42 +08:00
|
|
|
#include "../toxcore/mono_time.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>
|
2017-01-04 00:26:15 +08:00
|
|
|
#include <errno.h>
|
Split bit_rate_set(), one for audio, one for video.
Fixes #572.
As discussed in the issue, there's a risk that toxcore may not hold the
maximum bitrates libvpx supports, if toxcore insists on using integer
type. I initially proposed to have another flag in set(), so that we can
use unsigned type instead. iphydf came up with a better solution, that is
splitting the original functions, one for audio, one for video. Now, we
could safely replace int32_t with uint32_t.
Also: clean video_bit_rate_invalid()
Though this is not a part of issue #572, as it's used in the
toxav_bit_rate_set(), i cleaned the code. As mannol said, there should be
a check. Uint32_t is large enough to hold the maximum bitrates libvpx
supports, but user may pass a value larger than uint while smaller than
uint32_t. Thanks to the reminding from nurupo, it's no longer a stub
function.
Bitrate error enums are shared for both audio and video
https://github.com/TokTok/c-toxcore/pull/578#issuecomment-360095609, just
as iphydf said.
2018-01-14 21:46:06 +08:00
|
|
|
#include <limits.h>
|
2014-02-10 06:06:44 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2014-02-16 03:44:33 +08:00
|
|
|
|
2018-07-20 06:00:21 +08:00
|
|
|
// TODO(zoff99): don't hardcode this, let the application choose it
|
2018-01-20 05:59:42 +08:00
|
|
|
// VPX Info: Time to spend encoding, in microseconds (it's a *soft* deadline)
|
2021-12-04 00:20:10 +08:00
|
|
|
#define WANTED_MAX_ENCODER_FPS 40
|
2018-01-20 05:59:42 +08:00
|
|
|
#define MAX_ENCODE_TIME_US (1000000 / WANTED_MAX_ENCODER_FPS) // to allow x fps
|
|
|
|
|
|
|
|
#define VIDEO_SEND_X_KEYFRAMES_FIRST 7 // force the first n frames to be keyframes!
|
|
|
|
|
|
|
|
/*
|
2020-05-17 06:04:10 +08:00
|
|
|
* VPX_DL_REALTIME (1) deadline parameter analogous to VPx REALTIME mode.
|
|
|
|
* VPX_DL_GOOD_QUALITY (1000000) deadline parameter analogous to VPx GOOD QUALITY mode.
|
|
|
|
* VPX_DL_BEST_QUALITY (0) deadline parameter analogous to VPx BEST QUALITY mode.
|
|
|
|
*/
|
2014-07-27 06:26:58 +08:00
|
|
|
|
2015-03-02 01:45:04 +08:00
|
|
|
typedef struct ToxAVCall_s {
|
2015-10-11 05:54:23 +08:00
|
|
|
ToxAV *av;
|
|
|
|
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_t mutex_audio[1];
|
2018-08-12 18:55:20 +08:00
|
|
|
RTPSession *audio_rtp;
|
|
|
|
ACSession *audio;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_t mutex_video[1];
|
2018-08-12 18:55:20 +08:00
|
|
|
RTPSession *video_rtp;
|
|
|
|
VCSession *video;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-11-11 17:33:03 +08:00
|
|
|
BWController *bwc;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
bool active;
|
2015-10-11 05:54:23 +08:00
|
|
|
MSICall *msi_call;
|
2015-04-29 07:01:25 +08:00
|
|
|
uint32_t friend_number;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-29 07:01:25 +08:00
|
|
|
uint32_t audio_bit_rate; /* Sending audio bit rate */
|
|
|
|
uint32_t video_bit_rate; /* Sending video bit rate */
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-26 06:31:03 +08:00
|
|
|
/** Required for monitoring changes in states */
|
2015-04-22 08:09:37 +08:00
|
|
|
uint8_t previous_self_capabilities;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2020-03-28 18:58:00 +08:00
|
|
|
pthread_mutex_t toxav_call_mutex[1];
|
2015-10-11 05:54:23 +08:00
|
|
|
|
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-10-11 06:01:44 +08:00
|
|
|
struct ToxAV {
|
2018-08-11 22:11:55 +08:00
|
|
|
Tox *tox;
|
2015-10-11 05:54:23 +08:00
|
|
|
Messenger *m;
|
|
|
|
MSISession *msi;
|
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
/* Two-way storage: first is array of calls and second is list of calls with head and tail */
|
2015-10-11 05:54:23 +08:00
|
|
|
ToxAVCall **calls;
|
2015-02-15 06:37:52 +08:00
|
|
|
uint32_t calls_tail;
|
|
|
|
uint32_t calls_head;
|
2015-03-02 01:45:04 +08:00
|
|
|
pthread_mutex_t mutex[1];
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-12 18:55:20 +08:00
|
|
|
/* Call callback */
|
|
|
|
toxav_call_cb *ccb;
|
|
|
|
void *ccb_user_data;
|
|
|
|
/* Call state callback */
|
|
|
|
toxav_call_state_cb *scb;
|
|
|
|
void *scb_user_data;
|
|
|
|
/* Audio frame receive callback */
|
|
|
|
toxav_audio_receive_frame_cb *acb;
|
|
|
|
void *acb_user_data;
|
|
|
|
/* Video frame receive callback */
|
|
|
|
toxav_video_receive_frame_cb *vcb;
|
|
|
|
void *vcb_user_data;
|
|
|
|
/* Bit rate control callback */
|
|
|
|
toxav_audio_bit_rate_cb *abcb;
|
|
|
|
void *abcb_user_data;
|
|
|
|
/* Bit rate control callback */
|
|
|
|
toxav_video_bit_rate_cb *vbcb;
|
|
|
|
void *vbcb_user_data;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
/** Decode time measures */
|
|
|
|
int32_t dmssc; /** Measure count */
|
|
|
|
int32_t dmsst; /** Last cycle total */
|
|
|
|
int32_t dmssa; /** Average decoding time in ms */
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
uint32_t interval; /** Calculated interval */
|
2020-04-07 21:56:29 +08:00
|
|
|
Mono_Time *toxav_mono_time; /** ToxAV's own mono_time instance */
|
2015-02-15 06:37:52 +08:00
|
|
|
};
|
2014-07-27 01:29:49 +08:00
|
|
|
|
2020-03-18 08:42:23 +08:00
|
|
|
static void callback_bwc(BWController *bwc, uint32_t friend_number, float loss, void *user_data);
|
|
|
|
|
|
|
|
static int callback_invite(void *toxav_inst, MSICall *call);
|
|
|
|
static int callback_start(void *toxav_inst, MSICall *call);
|
|
|
|
static int callback_end(void *toxav_inst, MSICall *call);
|
|
|
|
static int callback_error(void *toxav_inst, MSICall *call);
|
|
|
|
static int callback_capabilites(void *toxav_inst, MSICall *call);
|
|
|
|
|
|
|
|
static bool audio_bit_rate_invalid(uint32_t bit_rate);
|
|
|
|
static bool video_bit_rate_invalid(uint32_t bit_rate);
|
|
|
|
static bool invoke_call_state_callback(ToxAV *av, uint32_t friend_number, uint32_t state);
|
|
|
|
static ToxAVCall *call_new(ToxAV *av, uint32_t friend_number, Toxav_Err_Call *error);
|
|
|
|
static ToxAVCall *call_get(ToxAV *av, uint32_t friend_number);
|
|
|
|
static ToxAVCall *call_remove(ToxAVCall *call);
|
|
|
|
static bool call_prepare_transmission(ToxAVCall *call);
|
|
|
|
static void call_kill_transmission(ToxAVCall *call);
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2018-08-13 06:01:42 +08:00
|
|
|
ToxAV *toxav_new(Tox *tox, Toxav_Err_New *error)
|
2015-02-15 06:37:52 +08:00
|
|
|
{
|
2018-08-13 06:01:42 +08:00
|
|
|
Toxav_Err_New rc = TOXAV_ERR_NEW_OK;
|
2018-01-29 05:30:39 +08:00
|
|
|
ToxAV *av = nullptr;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
if (tox == nullptr) {
|
2015-02-15 06:37:52 +08:00
|
|
|
rc = TOXAV_ERR_NEW_NULL;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-02-15 06:37:52 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-07-25 14:54:04 +08:00
|
|
|
// TODO(iphydf): Don't rely on toxcore internals.
|
|
|
|
Messenger *m;
|
2020-03-14 11:49:28 +08:00
|
|
|
//!TOKSTYLE-
|
2018-07-25 14:54:04 +08:00
|
|
|
m = *(Messenger **)tox;
|
2020-03-14 11:49:28 +08:00
|
|
|
//!TOKSTYLE+
|
2018-07-25 14:54:04 +08:00
|
|
|
|
2016-08-19 20:07:45 +08:00
|
|
|
if (m->msi_packet) {
|
2015-02-15 06:37:52 +08:00
|
|
|
rc = TOXAV_ERR_NEW_MULTIPLE;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-02-15 06:37:52 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2016-09-18 08:31:55 +08:00
|
|
|
av = (ToxAV *)calloc(sizeof(ToxAV), 1);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
if (av == nullptr) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_WARNING(m->log, "Allocation failed!");
|
2015-02-15 06:37:52 +08:00
|
|
|
rc = TOXAV_ERR_NEW_MALLOC;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2014-05-24 22:02:01 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-08 07:00:19 +08:00
|
|
|
if (create_recursive_mutex(av->mutex) != 0) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_WARNING(m->log, "Mutex creation failed!");
|
2015-03-02 01:45:04 +08:00
|
|
|
rc = TOXAV_ERR_NEW_MALLOC;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-03-02 01:45:04 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-11 22:11:55 +08:00
|
|
|
av->tox = tox;
|
2016-08-19 20:07:45 +08:00
|
|
|
av->m = m;
|
2020-04-07 21:56:29 +08:00
|
|
|
av->toxav_mono_time = mono_time_new();
|
2015-02-19 06:23:46 +08:00
|
|
|
av->msi = msi_new(av->m);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
if (av->msi == nullptr) {
|
2015-03-02 01:45:04 +08:00
|
|
|
pthread_mutex_destroy(av->mutex);
|
2015-02-15 06:37:52 +08:00
|
|
|
rc = TOXAV_ERR_NEW_MALLOC;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2014-11-29 02:10:27 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +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-10-11 05:54:23 +08:00
|
|
|
|
2018-08-12 20:53:12 +08:00
|
|
|
msi_register_callback(av->msi, callback_invite, MSI_ON_INVITE);
|
|
|
|
msi_register_callback(av->msi, callback_start, MSI_ON_START);
|
|
|
|
msi_register_callback(av->msi, callback_end, MSI_ON_END);
|
|
|
|
msi_register_callback(av->msi, callback_error, MSI_ON_ERROR);
|
|
|
|
msi_register_callback(av->msi, callback_error, MSI_ON_PEERTIMEOUT);
|
|
|
|
msi_register_callback(av->msi, callback_capabilites, MSI_ON_CAPABILITIES);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-13 06:01:42 +08:00
|
|
|
RETURN:
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (error) {
|
2015-02-15 06:37:52 +08:00
|
|
|
*error = rc;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-29 07:01:25 +08:00
|
|
|
if (rc != TOXAV_ERR_NEW_OK) {
|
|
|
|
free(av);
|
2018-01-29 05:30:39 +08:00
|
|
|
av = nullptr;
|
2015-04-29 07:01:25 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-29 07:01:25 +08:00
|
|
|
return av;
|
2014-02-10 06:06:44 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
void toxav_kill(ToxAV *av)
|
2014-02-17 09:01:30 +08:00
|
|
|
{
|
2018-01-29 05:30:39 +08:00
|
|
|
if (av == nullptr) {
|
2015-02-15 06:37:52 +08:00
|
|
|
return;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_lock(av->mutex);
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
/* To avoid possible deadlocks */
|
2016-11-15 15:53:47 +08:00
|
|
|
while (av->msi && msi_kill(av->msi, av->m->log) != 0) {
|
2015-10-11 05:54:23 +08:00
|
|
|
pthread_mutex_unlock(av->mutex);
|
|
|
|
pthread_mutex_lock(av->mutex);
|
|
|
|
}
|
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
/* Msi kill will hang up all calls so just clean these calls */
|
|
|
|
if (av->calls) {
|
2015-10-11 05:54:23 +08:00
|
|
|
ToxAVCall *it = call_get(av, av->calls_head);
|
|
|
|
|
2015-05-02 04:15:12 +08:00
|
|
|
while (it) {
|
2015-02-21 08:07:22 +08:00
|
|
|
call_kill_transmission(it);
|
2018-01-29 05:30:39 +08:00
|
|
|
it->msi_call = nullptr; /* msi_kill() frees the call's msi_call handle; which causes #278 */
|
2015-05-02 04:15:12 +08:00
|
|
|
it = call_remove(it); /* This will eventually free av->calls */
|
2015-02-21 08:07:22 +08:00
|
|
|
}
|
|
|
|
}
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2020-04-07 21:56:29 +08:00
|
|
|
mono_time_free(av->toxav_mono_time);
|
|
|
|
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_unlock(av->mutex);
|
2015-03-02 01:45:04 +08:00
|
|
|
pthread_mutex_destroy(av->mutex);
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2014-02-10 06:06:44 +08:00
|
|
|
free(av);
|
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
Tox *toxav_get_tox(const ToxAV *av)
|
2014-02-10 06:06:44 +08:00
|
|
|
{
|
2018-08-11 22:11:55 +08:00
|
|
|
return av->tox;
|
2015-02-15 06:37:52 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
uint32_t toxav_iteration_interval(const ToxAV *av)
|
2015-02-15 06:37:52 +08:00
|
|
|
{
|
2015-03-02 01:45:04 +08:00
|
|
|
/* If no call is active interval is 200 */
|
2015-02-15 06:37:52 +08:00
|
|
|
return av->calls ? av->interval : 200;
|
2014-02-10 06:06:44 +08:00
|
|
|
}
|
2016-09-20 04:49:04 +08:00
|
|
|
void toxav_iterate(ToxAV *av)
|
2014-02-10 06:06:44 +08:00
|
|
|
{
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_lock(av->mutex);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
if (av->calls == nullptr) {
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_unlock(av->mutex);
|
2015-02-15 06:37:52 +08:00
|
|
|
return;
|
2015-04-07 07:24:36 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2020-04-07 21:56:29 +08:00
|
|
|
uint64_t start = current_time_monotonic(av->toxav_mono_time);
|
2015-04-30 06:40:30 +08:00
|
|
|
int32_t rc = 500;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2021-12-09 21:38:28 +08:00
|
|
|
for (ToxAVCall *i = av->calls[av->calls_head]; i; i = i->next) {
|
2015-02-15 06:37:52 +08:00
|
|
|
if (i->active) {
|
2020-03-28 18:58:00 +08:00
|
|
|
pthread_mutex_lock(i->toxav_call_mutex);
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_unlock(av->mutex);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-12 18:55:20 +08:00
|
|
|
ac_iterate(i->audio);
|
|
|
|
vc_iterate(i->video);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-12 20:53:12 +08:00
|
|
|
if (i->msi_call->self_capabilities & MSI_CAP_R_AUDIO &&
|
|
|
|
i->msi_call->peer_capabilities & MSI_CAP_S_AUDIO) {
|
2018-08-12 18:55:20 +08:00
|
|
|
rc = min_s32(i->audio->lp_frame_duration, rc);
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-12 20:53:12 +08:00
|
|
|
if (i->msi_call->self_capabilities & MSI_CAP_R_VIDEO &&
|
|
|
|
i->msi_call->peer_capabilities & MSI_CAP_S_VIDEO) {
|
2019-08-30 17:44:21 +08:00
|
|
|
pthread_mutex_lock(i->video->queue_mutex);
|
2018-08-13 06:01:42 +08:00
|
|
|
rc = min_u32(i->video->lcfd, rc);
|
2019-08-30 17:44:21 +08:00
|
|
|
pthread_mutex_unlock(i->video->queue_mutex);
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-29 07:01:25 +08:00
|
|
|
uint32_t fid = i->friend_number;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2020-03-28 18:58:00 +08:00
|
|
|
pthread_mutex_unlock(i->toxav_call_mutex);
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_lock(av->mutex);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-08 07:00:19 +08:00
|
|
|
/* In case this call is popped from container stop iteration */
|
2016-09-01 02:12:19 +08:00
|
|
|
if (call_get(av, fid) != i) {
|
2015-04-08 07:00:19 +08:00
|
|
|
break;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-04-07 07:24:36 +08:00
|
|
|
}
|
2014-11-29 02:10:27 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-17 21:31:14 +08:00
|
|
|
av->interval = rc < av->dmssa ? 0 : (rc - av->dmssa);
|
2020-04-07 21:56:29 +08:00
|
|
|
av->dmsst += current_time_monotonic(av->toxav_mono_time) - start;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
if (++av->dmssc == 3) {
|
2020-03-14 11:37:28 +08:00
|
|
|
av->dmssa = av->dmsst / 3 + 5; /* NOTE Magic Offset 5 for precision */
|
2015-02-15 06:37:52 +08:00
|
|
|
av->dmssc = 0;
|
|
|
|
av->dmsst = 0;
|
2014-11-18 07:46:46 +08:00
|
|
|
}
|
2019-08-30 17:29:50 +08:00
|
|
|
|
|
|
|
pthread_mutex_unlock(av->mutex);
|
2014-02-10 06:06:44 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
bool toxav_call(ToxAV *av, uint32_t friend_number, uint32_t audio_bit_rate, uint32_t video_bit_rate,
|
2018-08-13 06:01:42 +08:00
|
|
|
Toxav_Err_Call *error)
|
2014-07-08 04:10:10 +08:00
|
|
|
{
|
2018-08-13 06:01:42 +08:00
|
|
|
Toxav_Err_Call rc = TOXAV_ERR_CALL_OK;
|
2016-09-18 08:31:55 +08:00
|
|
|
ToxAVCall *call;
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_lock(av->mutex);
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
if ((audio_bit_rate && audio_bit_rate_invalid(audio_bit_rate))
|
2015-11-04 02:42:05 +08:00
|
|
|
|| (video_bit_rate && video_bit_rate_invalid(video_bit_rate))) {
|
2015-10-11 05:54:23 +08:00
|
|
|
rc = TOXAV_ERR_CALL_INVALID_BIT_RATE;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-10-11 05:54:23 +08:00
|
|
|
}
|
|
|
|
|
2016-09-18 08:31:55 +08:00
|
|
|
call = call_new(av, friend_number, &rc);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
if (call == nullptr) {
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-03-29 08:10:34 +08:00
|
|
|
call->audio_bit_rate = audio_bit_rate;
|
|
|
|
call->video_bit_rate = video_bit_rate;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-12 20:53:12 +08:00
|
|
|
call->previous_self_capabilities = MSI_CAP_R_AUDIO | MSI_CAP_R_VIDEO;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-12 20:53:12 +08:00
|
|
|
call->previous_self_capabilities |= audio_bit_rate > 0 ? MSI_CAP_S_AUDIO : 0;
|
|
|
|
call->previous_self_capabilities |= video_bit_rate > 0 ? MSI_CAP_S_VIDEO : 0;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-22 08:09:37 +08:00
|
|
|
if (msi_invite(av->msi, &call->msi_call, friend_number, call->previous_self_capabilities) != 0) {
|
2015-02-21 08:07:22 +08:00
|
|
|
call_remove(call);
|
2015-10-11 05:54:23 +08:00
|
|
|
rc = TOXAV_ERR_CALL_SYNC;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-02-15 06:37:52 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-02-23 06:41:40 +08:00
|
|
|
call->msi_call->av_call = call;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-13 06:01:42 +08:00
|
|
|
RETURN:
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_unlock(av->mutex);
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (error) {
|
2015-10-11 05:54:23 +08:00
|
|
|
*error = rc;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
|
|
|
return rc == TOXAV_ERR_CALL_OK;
|
2014-11-18 07:46:46 +08:00
|
|
|
}
|
2016-09-20 04:49:04 +08:00
|
|
|
void toxav_callback_call(ToxAV *av, toxav_call_cb *callback, void *user_data)
|
2014-02-10 06:06:44 +08:00
|
|
|
{
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_lock(av->mutex);
|
2018-08-12 18:55:20 +08:00
|
|
|
av->ccb = callback;
|
|
|
|
av->ccb_user_data = user_data;
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_unlock(av->mutex);
|
2015-04-29 07:01:25 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
bool toxav_answer(ToxAV *av, uint32_t friend_number, uint32_t audio_bit_rate, uint32_t video_bit_rate,
|
2018-08-13 06:01:42 +08:00
|
|
|
Toxav_Err_Answer *error)
|
2014-02-10 06:06:44 +08:00
|
|
|
{
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_lock(av->mutex);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-13 06:01:42 +08:00
|
|
|
Toxav_Err_Answer rc = TOXAV_ERR_ANSWER_OK;
|
2016-09-18 08:31:55 +08:00
|
|
|
ToxAVCall *call;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
if (m_friend_exists(av->m, friend_number) == 0) {
|
|
|
|
rc = TOXAV_ERR_ANSWER_FRIEND_NOT_FOUND;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-02-15 06:37:52 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-29 07:01:25 +08:00
|
|
|
if ((audio_bit_rate && audio_bit_rate_invalid(audio_bit_rate))
|
2015-11-04 02:42:05 +08:00
|
|
|
|| (video_bit_rate && video_bit_rate_invalid(video_bit_rate))
|
2015-10-11 05:54:23 +08:00
|
|
|
) {
|
2015-05-02 04:15:12 +08:00
|
|
|
rc = TOXAV_ERR_ANSWER_INVALID_BIT_RATE;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-02-15 06:37:52 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2016-09-18 08:31:55 +08:00
|
|
|
call = call_get(av, friend_number);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
if (call == nullptr) {
|
2015-02-15 06:37:52 +08:00
|
|
|
rc = TOXAV_ERR_ANSWER_FRIEND_NOT_CALLING;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-02-15 06:37:52 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-03-23 06:50:43 +08:00
|
|
|
if (!call_prepare_transmission(call)) {
|
2015-10-11 05:54:23 +08:00
|
|
|
rc = TOXAV_ERR_ANSWER_CODEC_INITIALIZATION;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-10-11 05:54:23 +08:00
|
|
|
}
|
|
|
|
|
2015-03-29 08:10:34 +08:00
|
|
|
call->audio_bit_rate = audio_bit_rate;
|
|
|
|
call->video_bit_rate = video_bit_rate;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-12 20:53:12 +08:00
|
|
|
call->previous_self_capabilities = MSI_CAP_R_AUDIO | MSI_CAP_R_VIDEO;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-12 20:53:12 +08:00
|
|
|
call->previous_self_capabilities |= audio_bit_rate > 0 ? MSI_CAP_S_AUDIO : 0;
|
|
|
|
call->previous_self_capabilities |= video_bit_rate > 0 ? MSI_CAP_S_VIDEO : 0;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (msi_answer(call->msi_call, call->previous_self_capabilities) != 0) {
|
2015-10-11 05:54:23 +08:00
|
|
|
rc = TOXAV_ERR_ANSWER_SYNC;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-13 06:01:42 +08:00
|
|
|
RETURN:
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_unlock(av->mutex);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (error) {
|
2015-02-15 06:37:52 +08:00
|
|
|
*error = rc;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
return rc == TOXAV_ERR_ANSWER_OK;
|
2014-02-10 06:06:44 +08:00
|
|
|
}
|
2016-09-20 04:49:04 +08:00
|
|
|
void toxav_callback_call_state(ToxAV *av, toxav_call_state_cb *callback, void *user_data)
|
2014-02-10 06:06:44 +08:00
|
|
|
{
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_lock(av->mutex);
|
2018-08-12 18:55:20 +08:00
|
|
|
av->scb = callback;
|
|
|
|
av->scb_user_data = user_data;
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_unlock(av->mutex);
|
2014-02-10 06:06:44 +08:00
|
|
|
}
|
2018-08-13 06:01:42 +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-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_lock(av->mutex);
|
2018-08-13 06:01:42 +08:00
|
|
|
Toxav_Err_Call_Control rc = TOXAV_ERR_CALL_CONTROL_OK;
|
2016-09-18 08:31:55 +08:00
|
|
|
ToxAVCall *call;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
if (m_friend_exists(av->m, friend_number) == 0) {
|
|
|
|
rc = TOXAV_ERR_CALL_CONTROL_FRIEND_NOT_FOUND;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-02-15 06:37:52 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2016-09-18 08:31:55 +08:00
|
|
|
call = call_get(av, friend_number);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
if (call == nullptr || (!call->active && control != TOXAV_CALL_CONTROL_CANCEL)) {
|
2015-02-15 06:37:52 +08:00
|
|
|
rc = TOXAV_ERR_CALL_CONTROL_FRIEND_NOT_IN_CALL;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-02-15 06:37:52 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-03-02 01:45:04 +08:00
|
|
|
switch (control) {
|
2015-02-15 06:37:52 +08:00
|
|
|
case TOXAV_CALL_CONTROL_RESUME: {
|
2015-02-23 06:41:40 +08:00
|
|
|
/* Only act if paused and had media transfer active before */
|
2015-10-11 05:54:23 +08:00
|
|
|
if (call->msi_call->self_capabilities == 0 &&
|
|
|
|
call->previous_self_capabilities) {
|
|
|
|
|
|
|
|
if (msi_change_capabilities(call->msi_call,
|
|
|
|
call->previous_self_capabilities) == -1) {
|
|
|
|
rc = TOXAV_ERR_CALL_CONTROL_SYNC;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-02-23 06:41:40 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-12 18:55:20 +08:00
|
|
|
rtp_allow_receiving(call->audio_rtp);
|
|
|
|
rtp_allow_receiving(call->video_rtp);
|
2015-05-02 04:15:12 +08:00
|
|
|
} else {
|
2015-05-23 05:22:31 +08:00
|
|
|
rc = TOXAV_ERR_CALL_CONTROL_INVALID_TRANSITION;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-02-21 08:07:22 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
case TOXAV_CALL_CONTROL_PAUSE: {
|
2015-02-23 06:41:40 +08:00
|
|
|
/* Only act if not already paused */
|
2015-02-21 08:07:22 +08:00
|
|
|
if (call->msi_call->self_capabilities) {
|
2015-04-22 08:09:37 +08:00
|
|
|
call->previous_self_capabilities = call->msi_call->self_capabilities;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
|
|
|
if (msi_change_capabilities(call->msi_call, 0) == -1) {
|
|
|
|
rc = TOXAV_ERR_CALL_CONTROL_SYNC;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-02-23 06:41:40 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-12 18:55:20 +08:00
|
|
|
rtp_stop_receiving(call->audio_rtp);
|
|
|
|
rtp_stop_receiving(call->video_rtp);
|
2015-05-02 04:15:12 +08:00
|
|
|
} else {
|
2015-05-23 05:22:31 +08:00
|
|
|
rc = TOXAV_ERR_CALL_CONTROL_INVALID_TRANSITION;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-02-21 08:07:22 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
case TOXAV_CALL_CONTROL_CANCEL: {
|
2015-02-21 08:07:22 +08:00
|
|
|
/* Hang up */
|
2020-03-28 18:58:00 +08:00
|
|
|
pthread_mutex_lock(call->toxav_call_mutex);
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
if (msi_hangup(call->msi_call) != 0) {
|
|
|
|
rc = TOXAV_ERR_CALL_CONTROL_SYNC;
|
2020-03-28 18:58:00 +08:00
|
|
|
pthread_mutex_unlock(call->toxav_call_mutex);
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-10-11 05:54:23 +08:00
|
|
|
}
|
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
call->msi_call = nullptr;
|
2020-03-28 18:58:00 +08:00
|
|
|
pthread_mutex_unlock(call->toxav_call_mutex);
|
2015-10-24 02:42:48 +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-10-11 05:54:23 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-05-23 05:22:31 +08:00
|
|
|
case TOXAV_CALL_CONTROL_MUTE_AUDIO: {
|
2018-08-12 20:53:12 +08:00
|
|
|
if (call->msi_call->self_capabilities & MSI_CAP_R_AUDIO) {
|
2015-02-23 06:41:40 +08:00
|
|
|
if (msi_change_capabilities(call->msi_call, call->
|
2018-08-12 20:53:12 +08:00
|
|
|
msi_call->self_capabilities ^ MSI_CAP_R_AUDIO) == -1) {
|
2015-10-11 05:54:23 +08:00
|
|
|
rc = TOXAV_ERR_CALL_CONTROL_SYNC;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-02-23 06:41:40 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-12 18:55:20 +08:00
|
|
|
rtp_stop_receiving(call->audio_rtp);
|
2015-03-02 01:45:04 +08:00
|
|
|
} else {
|
2015-05-23 05:22:31 +08:00
|
|
|
rc = TOXAV_ERR_CALL_CONTROL_INVALID_TRANSITION;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-05-23 05:22:31 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-05-23 05:22:31 +08:00
|
|
|
case TOXAV_CALL_CONTROL_UNMUTE_AUDIO: {
|
2018-08-12 20:53:12 +08:00
|
|
|
if (call->msi_call->self_capabilities ^ MSI_CAP_R_AUDIO) {
|
2015-02-23 06:41:40 +08:00
|
|
|
if (msi_change_capabilities(call->msi_call, call->
|
2018-08-12 20:53:12 +08:00
|
|
|
msi_call->self_capabilities | MSI_CAP_R_AUDIO) == -1) {
|
2015-10-11 05:54:23 +08:00
|
|
|
rc = TOXAV_ERR_CALL_CONTROL_SYNC;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-02-23 06:41:40 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-12 18:55:20 +08:00
|
|
|
rtp_allow_receiving(call->audio_rtp);
|
2015-05-23 05:22:31 +08:00
|
|
|
} else {
|
|
|
|
rc = TOXAV_ERR_CALL_CONTROL_INVALID_TRANSITION;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-02-23 06:41:40 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-05-23 05:22:31 +08:00
|
|
|
case TOXAV_CALL_CONTROL_HIDE_VIDEO: {
|
2018-08-12 20:53:12 +08:00
|
|
|
if (call->msi_call->self_capabilities & MSI_CAP_R_VIDEO) {
|
2015-02-23 06:41:40 +08:00
|
|
|
if (msi_change_capabilities(call->msi_call, call->
|
2018-08-12 20:53:12 +08:00
|
|
|
msi_call->self_capabilities ^ MSI_CAP_R_VIDEO) == -1) {
|
2015-10-11 05:54:23 +08:00
|
|
|
rc = TOXAV_ERR_CALL_CONTROL_SYNC;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-02-23 06:41:40 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-12 18:55:20 +08:00
|
|
|
rtp_stop_receiving(call->video_rtp);
|
2015-03-02 01:45:04 +08:00
|
|
|
} else {
|
2015-05-23 05:22:31 +08:00
|
|
|
rc = TOXAV_ERR_CALL_CONTROL_INVALID_TRANSITION;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-05-23 05:22:31 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2015-05-23 05:22:31 +08:00
|
|
|
case TOXAV_CALL_CONTROL_SHOW_VIDEO: {
|
2018-08-12 20:53:12 +08:00
|
|
|
if (call->msi_call->self_capabilities ^ MSI_CAP_R_VIDEO) {
|
2015-02-23 06:41:40 +08:00
|
|
|
if (msi_change_capabilities(call->msi_call, call->
|
2018-08-12 20:53:12 +08:00
|
|
|
msi_call->self_capabilities | MSI_CAP_R_VIDEO) == -1) {
|
2015-10-11 05:54:23 +08:00
|
|
|
rc = TOXAV_ERR_CALL_CONTROL_SYNC;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-02-23 06:41:40 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-12 18:55:20 +08:00
|
|
|
rtp_allow_receiving(call->video_rtp);
|
2015-05-23 05:22:31 +08:00
|
|
|
} else {
|
|
|
|
rc = TOXAV_ERR_CALL_CONTROL_INVALID_TRANSITION;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-02-23 06:41:40 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
}
|
|
|
|
break;
|
2015-02-15 06:37:52 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-13 06:01:42 +08:00
|
|
|
RETURN:
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_unlock(av->mutex);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (error) {
|
2015-02-15 06:37:52 +08:00
|
|
|
*error = rc;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
return rc == TOXAV_ERR_CALL_CONTROL_OK;
|
2014-02-10 06:06:44 +08:00
|
|
|
}
|
2018-01-28 19:14:04 +08:00
|
|
|
bool toxav_audio_set_bit_rate(ToxAV *av, uint32_t friend_number, uint32_t audio_bit_rate,
|
2018-08-13 06:01:42 +08:00
|
|
|
Toxav_Err_Bit_Rate_Set *error)
|
2015-04-27 06:15:57 +08:00
|
|
|
{
|
2018-08-13 06:01:42 +08:00
|
|
|
Toxav_Err_Bit_Rate_Set rc = TOXAV_ERR_BIT_RATE_SET_OK;
|
2015-10-11 05:54:23 +08:00
|
|
|
ToxAVCall *call;
|
|
|
|
|
2015-03-29 08:10:34 +08:00
|
|
|
if (m_friend_exists(av->m, friend_number) == 0) {
|
2015-10-11 05:54:23 +08:00
|
|
|
rc = TOXAV_ERR_BIT_RATE_SET_FRIEND_NOT_FOUND;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-03-29 08:10:34 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
|
|
|
if (audio_bit_rate > 0 && audio_bit_rate_invalid(audio_bit_rate)) {
|
Split bit_rate_set(), one for audio, one for video.
Fixes #572.
As discussed in the issue, there's a risk that toxcore may not hold the
maximum bitrates libvpx supports, if toxcore insists on using integer
type. I initially proposed to have another flag in set(), so that we can
use unsigned type instead. iphydf came up with a better solution, that is
splitting the original functions, one for audio, one for video. Now, we
could safely replace int32_t with uint32_t.
Also: clean video_bit_rate_invalid()
Though this is not a part of issue #572, as it's used in the
toxav_bit_rate_set(), i cleaned the code. As mannol said, there should be
a check. Uint32_t is large enough to hold the maximum bitrates libvpx
supports, but user may pass a value larger than uint while smaller than
uint32_t. Thanks to the reminding from nurupo, it's no longer a stub
function.
Bitrate error enums are shared for both audio and video
https://github.com/TokTok/c-toxcore/pull/578#issuecomment-360095609, just
as iphydf said.
2018-01-14 21:46:06 +08:00
|
|
|
rc = TOXAV_ERR_BIT_RATE_SET_INVALID_BIT_RATE;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-10-11 05:54:23 +08:00
|
|
|
}
|
|
|
|
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_lock(av->mutex);
|
2015-03-29 08:10:34 +08:00
|
|
|
call = call_get(av, friend_number);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-12 20:53:12 +08:00
|
|
|
if (call == nullptr || !call->active || call->msi_call->state != MSI_CALL_ACTIVE) {
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_unlock(av->mutex);
|
2015-10-11 05:54:23 +08:00
|
|
|
rc = TOXAV_ERR_BIT_RATE_SET_FRIEND_NOT_IN_CALL;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-06-27 07:55:28 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
Split bit_rate_set(), one for audio, one for video.
Fixes #572.
As discussed in the issue, there's a risk that toxcore may not hold the
maximum bitrates libvpx supports, if toxcore insists on using integer
type. I initially proposed to have another flag in set(), so that we can
use unsigned type instead. iphydf came up with a better solution, that is
splitting the original functions, one for audio, one for video. Now, we
could safely replace int32_t with uint32_t.
Also: clean video_bit_rate_invalid()
Though this is not a part of issue #572, as it's used in the
toxav_bit_rate_set(), i cleaned the code. As mannol said, there should be
a check. Uint32_t is large enough to hold the maximum bitrates libvpx
supports, but user may pass a value larger than uint while smaller than
uint32_t. Thanks to the reminding from nurupo, it's no longer a stub
function.
Bitrate error enums are shared for both audio and video
https://github.com/TokTok/c-toxcore/pull/578#issuecomment-360095609, just
as iphydf said.
2018-01-14 21:46:06 +08:00
|
|
|
LOGGER_DEBUG(av->m->log, "Setting new audio bitrate to: %d", audio_bit_rate);
|
|
|
|
|
|
|
|
if (call->audio_bit_rate == audio_bit_rate) {
|
|
|
|
LOGGER_DEBUG(av->m->log, "Audio bitrate already set to: %d", audio_bit_rate);
|
|
|
|
} else if (audio_bit_rate == 0) {
|
|
|
|
LOGGER_DEBUG(av->m->log, "Turned off audio sending");
|
|
|
|
|
|
|
|
if (msi_change_capabilities(call->msi_call, call->msi_call->
|
2018-08-12 20:53:12 +08:00
|
|
|
self_capabilities ^ MSI_CAP_S_AUDIO) != 0) {
|
Split bit_rate_set(), one for audio, one for video.
Fixes #572.
As discussed in the issue, there's a risk that toxcore may not hold the
maximum bitrates libvpx supports, if toxcore insists on using integer
type. I initially proposed to have another flag in set(), so that we can
use unsigned type instead. iphydf came up with a better solution, that is
splitting the original functions, one for audio, one for video. Now, we
could safely replace int32_t with uint32_t.
Also: clean video_bit_rate_invalid()
Though this is not a part of issue #572, as it's used in the
toxav_bit_rate_set(), i cleaned the code. As mannol said, there should be
a check. Uint32_t is large enough to hold the maximum bitrates libvpx
supports, but user may pass a value larger than uint while smaller than
uint32_t. Thanks to the reminding from nurupo, it's no longer a stub
function.
Bitrate error enums are shared for both audio and video
https://github.com/TokTok/c-toxcore/pull/578#issuecomment-360095609, just
as iphydf said.
2018-01-14 21:46:06 +08:00
|
|
|
pthread_mutex_unlock(av->mutex);
|
|
|
|
rc = TOXAV_ERR_BIT_RATE_SET_SYNC;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
Split bit_rate_set(), one for audio, one for video.
Fixes #572.
As discussed in the issue, there's a risk that toxcore may not hold the
maximum bitrates libvpx supports, if toxcore insists on using integer
type. I initially proposed to have another flag in set(), so that we can
use unsigned type instead. iphydf came up with a better solution, that is
splitting the original functions, one for audio, one for video. Now, we
could safely replace int32_t with uint32_t.
Also: clean video_bit_rate_invalid()
Though this is not a part of issue #572, as it's used in the
toxav_bit_rate_set(), i cleaned the code. As mannol said, there should be
a check. Uint32_t is large enough to hold the maximum bitrates libvpx
supports, but user may pass a value larger than uint while smaller than
uint32_t. Thanks to the reminding from nurupo, it's no longer a stub
function.
Bitrate error enums are shared for both audio and video
https://github.com/TokTok/c-toxcore/pull/578#issuecomment-360095609, just
as iphydf said.
2018-01-14 21:46:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Audio sending is turned off; notify peer */
|
|
|
|
call->audio_bit_rate = 0;
|
|
|
|
} else {
|
2020-03-28 18:58:00 +08:00
|
|
|
pthread_mutex_lock(call->toxav_call_mutex);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
Split bit_rate_set(), one for audio, one for video.
Fixes #572.
As discussed in the issue, there's a risk that toxcore may not hold the
maximum bitrates libvpx supports, if toxcore insists on using integer
type. I initially proposed to have another flag in set(), so that we can
use unsigned type instead. iphydf came up with a better solution, that is
splitting the original functions, one for audio, one for video. Now, we
could safely replace int32_t with uint32_t.
Also: clean video_bit_rate_invalid()
Though this is not a part of issue #572, as it's used in the
toxav_bit_rate_set(), i cleaned the code. As mannol said, there should be
a check. Uint32_t is large enough to hold the maximum bitrates libvpx
supports, but user may pass a value larger than uint while smaller than
uint32_t. Thanks to the reminding from nurupo, it's no longer a stub
function.
Bitrate error enums are shared for both audio and video
https://github.com/TokTok/c-toxcore/pull/578#issuecomment-360095609, just
as iphydf said.
2018-01-14 21:46:06 +08:00
|
|
|
if (call->audio_bit_rate == 0) {
|
|
|
|
LOGGER_DEBUG(av->m->log, "Turned on audio sending");
|
2015-11-04 02:42:05 +08:00
|
|
|
|
Split bit_rate_set(), one for audio, one for video.
Fixes #572.
As discussed in the issue, there's a risk that toxcore may not hold the
maximum bitrates libvpx supports, if toxcore insists on using integer
type. I initially proposed to have another flag in set(), so that we can
use unsigned type instead. iphydf came up with a better solution, that is
splitting the original functions, one for audio, one for video. Now, we
could safely replace int32_t with uint32_t.
Also: clean video_bit_rate_invalid()
Though this is not a part of issue #572, as it's used in the
toxav_bit_rate_set(), i cleaned the code. As mannol said, there should be
a check. Uint32_t is large enough to hold the maximum bitrates libvpx
supports, but user may pass a value larger than uint while smaller than
uint32_t. Thanks to the reminding from nurupo, it's no longer a stub
function.
Bitrate error enums are shared for both audio and video
https://github.com/TokTok/c-toxcore/pull/578#issuecomment-360095609, just
as iphydf said.
2018-01-14 21:46:06 +08:00
|
|
|
/* The audio has been turned off before this */
|
|
|
|
if (msi_change_capabilities(call->msi_call, call->
|
2018-08-12 20:53:12 +08:00
|
|
|
msi_call->self_capabilities | MSI_CAP_S_AUDIO) != 0) {
|
2020-03-28 18:58:00 +08:00
|
|
|
pthread_mutex_unlock(call->toxav_call_mutex);
|
2015-10-11 05:54:23 +08:00
|
|
|
pthread_mutex_unlock(av->mutex);
|
|
|
|
rc = TOXAV_ERR_BIT_RATE_SET_SYNC;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-10-11 05:54:23 +08:00
|
|
|
}
|
|
|
|
} else {
|
Split bit_rate_set(), one for audio, one for video.
Fixes #572.
As discussed in the issue, there's a risk that toxcore may not hold the
maximum bitrates libvpx supports, if toxcore insists on using integer
type. I initially proposed to have another flag in set(), so that we can
use unsigned type instead. iphydf came up with a better solution, that is
splitting the original functions, one for audio, one for video. Now, we
could safely replace int32_t with uint32_t.
Also: clean video_bit_rate_invalid()
Though this is not a part of issue #572, as it's used in the
toxav_bit_rate_set(), i cleaned the code. As mannol said, there should be
a check. Uint32_t is large enough to hold the maximum bitrates libvpx
supports, but user may pass a value larger than uint while smaller than
uint32_t. Thanks to the reminding from nurupo, it's no longer a stub
function.
Bitrate error enums are shared for both audio and video
https://github.com/TokTok/c-toxcore/pull/578#issuecomment-360095609, just
as iphydf said.
2018-01-14 21:46:06 +08:00
|
|
|
LOGGER_DEBUG(av->m->log, "Set new audio bit rate %d", audio_bit_rate);
|
|
|
|
}
|
2015-11-04 02:42:05 +08:00
|
|
|
|
Split bit_rate_set(), one for audio, one for video.
Fixes #572.
As discussed in the issue, there's a risk that toxcore may not hold the
maximum bitrates libvpx supports, if toxcore insists on using integer
type. I initially proposed to have another flag in set(), so that we can
use unsigned type instead. iphydf came up with a better solution, that is
splitting the original functions, one for audio, one for video. Now, we
could safely replace int32_t with uint32_t.
Also: clean video_bit_rate_invalid()
Though this is not a part of issue #572, as it's used in the
toxav_bit_rate_set(), i cleaned the code. As mannol said, there should be
a check. Uint32_t is large enough to hold the maximum bitrates libvpx
supports, but user may pass a value larger than uint while smaller than
uint32_t. Thanks to the reminding from nurupo, it's no longer a stub
function.
Bitrate error enums are shared for both audio and video
https://github.com/TokTok/c-toxcore/pull/578#issuecomment-360095609, just
as iphydf said.
2018-01-14 21:46:06 +08:00
|
|
|
call->audio_bit_rate = audio_bit_rate;
|
2020-03-28 18:58:00 +08:00
|
|
|
pthread_mutex_unlock(call->toxav_call_mutex);
|
Split bit_rate_set(), one for audio, one for video.
Fixes #572.
As discussed in the issue, there's a risk that toxcore may not hold the
maximum bitrates libvpx supports, if toxcore insists on using integer
type. I initially proposed to have another flag in set(), so that we can
use unsigned type instead. iphydf came up with a better solution, that is
splitting the original functions, one for audio, one for video. Now, we
could safely replace int32_t with uint32_t.
Also: clean video_bit_rate_invalid()
Though this is not a part of issue #572, as it's used in the
toxav_bit_rate_set(), i cleaned the code. As mannol said, there should be
a check. Uint32_t is large enough to hold the maximum bitrates libvpx
supports, but user may pass a value larger than uint while smaller than
uint32_t. Thanks to the reminding from nurupo, it's no longer a stub
function.
Bitrate error enums are shared for both audio and video
https://github.com/TokTok/c-toxcore/pull/578#issuecomment-360095609, just
as iphydf said.
2018-01-14 21:46:06 +08:00
|
|
|
}
|
2015-11-04 02:42:05 +08:00
|
|
|
|
Split bit_rate_set(), one for audio, one for video.
Fixes #572.
As discussed in the issue, there's a risk that toxcore may not hold the
maximum bitrates libvpx supports, if toxcore insists on using integer
type. I initially proposed to have another flag in set(), so that we can
use unsigned type instead. iphydf came up with a better solution, that is
splitting the original functions, one for audio, one for video. Now, we
could safely replace int32_t with uint32_t.
Also: clean video_bit_rate_invalid()
Though this is not a part of issue #572, as it's used in the
toxav_bit_rate_set(), i cleaned the code. As mannol said, there should be
a check. Uint32_t is large enough to hold the maximum bitrates libvpx
supports, but user may pass a value larger than uint while smaller than
uint32_t. Thanks to the reminding from nurupo, it's no longer a stub
function.
Bitrate error enums are shared for both audio and video
https://github.com/TokTok/c-toxcore/pull/578#issuecomment-360095609, just
as iphydf said.
2018-01-14 21:46:06 +08:00
|
|
|
pthread_mutex_unlock(av->mutex);
|
2018-08-13 06:01:42 +08:00
|
|
|
RETURN:
|
2015-11-04 02:42:05 +08:00
|
|
|
|
Split bit_rate_set(), one for audio, one for video.
Fixes #572.
As discussed in the issue, there's a risk that toxcore may not hold the
maximum bitrates libvpx supports, if toxcore insists on using integer
type. I initially proposed to have another flag in set(), so that we can
use unsigned type instead. iphydf came up with a better solution, that is
splitting the original functions, one for audio, one for video. Now, we
could safely replace int32_t with uint32_t.
Also: clean video_bit_rate_invalid()
Though this is not a part of issue #572, as it's used in the
toxav_bit_rate_set(), i cleaned the code. As mannol said, there should be
a check. Uint32_t is large enough to hold the maximum bitrates libvpx
supports, but user may pass a value larger than uint while smaller than
uint32_t. Thanks to the reminding from nurupo, it's no longer a stub
function.
Bitrate error enums are shared for both audio and video
https://github.com/TokTok/c-toxcore/pull/578#issuecomment-360095609, just
as iphydf said.
2018-01-14 21:46:06 +08:00
|
|
|
if (error) {
|
|
|
|
*error = rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc == TOXAV_ERR_BIT_RATE_SET_OK;
|
|
|
|
}
|
2018-01-28 19:14:04 +08:00
|
|
|
bool toxav_video_set_bit_rate(ToxAV *av, uint32_t friend_number, uint32_t video_bit_rate,
|
2018-08-13 06:01:42 +08:00
|
|
|
Toxav_Err_Bit_Rate_Set *error)
|
Split bit_rate_set(), one for audio, one for video.
Fixes #572.
As discussed in the issue, there's a risk that toxcore may not hold the
maximum bitrates libvpx supports, if toxcore insists on using integer
type. I initially proposed to have another flag in set(), so that we can
use unsigned type instead. iphydf came up with a better solution, that is
splitting the original functions, one for audio, one for video. Now, we
could safely replace int32_t with uint32_t.
Also: clean video_bit_rate_invalid()
Though this is not a part of issue #572, as it's used in the
toxav_bit_rate_set(), i cleaned the code. As mannol said, there should be
a check. Uint32_t is large enough to hold the maximum bitrates libvpx
supports, but user may pass a value larger than uint while smaller than
uint32_t. Thanks to the reminding from nurupo, it's no longer a stub
function.
Bitrate error enums are shared for both audio and video
https://github.com/TokTok/c-toxcore/pull/578#issuecomment-360095609, just
as iphydf said.
2018-01-14 21:46:06 +08:00
|
|
|
{
|
2018-08-13 06:01:42 +08:00
|
|
|
Toxav_Err_Bit_Rate_Set rc = TOXAV_ERR_BIT_RATE_SET_OK;
|
Split bit_rate_set(), one for audio, one for video.
Fixes #572.
As discussed in the issue, there's a risk that toxcore may not hold the
maximum bitrates libvpx supports, if toxcore insists on using integer
type. I initially proposed to have another flag in set(), so that we can
use unsigned type instead. iphydf came up with a better solution, that is
splitting the original functions, one for audio, one for video. Now, we
could safely replace int32_t with uint32_t.
Also: clean video_bit_rate_invalid()
Though this is not a part of issue #572, as it's used in the
toxav_bit_rate_set(), i cleaned the code. As mannol said, there should be
a check. Uint32_t is large enough to hold the maximum bitrates libvpx
supports, but user may pass a value larger than uint while smaller than
uint32_t. Thanks to the reminding from nurupo, it's no longer a stub
function.
Bitrate error enums are shared for both audio and video
https://github.com/TokTok/c-toxcore/pull/578#issuecomment-360095609, just
as iphydf said.
2018-01-14 21:46:06 +08:00
|
|
|
ToxAVCall *call;
|
|
|
|
|
|
|
|
if (m_friend_exists(av->m, friend_number) == 0) {
|
|
|
|
rc = TOXAV_ERR_BIT_RATE_SET_FRIEND_NOT_FOUND;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
Split bit_rate_set(), one for audio, one for video.
Fixes #572.
As discussed in the issue, there's a risk that toxcore may not hold the
maximum bitrates libvpx supports, if toxcore insists on using integer
type. I initially proposed to have another flag in set(), so that we can
use unsigned type instead. iphydf came up with a better solution, that is
splitting the original functions, one for audio, one for video. Now, we
could safely replace int32_t with uint32_t.
Also: clean video_bit_rate_invalid()
Though this is not a part of issue #572, as it's used in the
toxav_bit_rate_set(), i cleaned the code. As mannol said, there should be
a check. Uint32_t is large enough to hold the maximum bitrates libvpx
supports, but user may pass a value larger than uint while smaller than
uint32_t. Thanks to the reminding from nurupo, it's no longer a stub
function.
Bitrate error enums are shared for both audio and video
https://github.com/TokTok/c-toxcore/pull/578#issuecomment-360095609, just
as iphydf said.
2018-01-14 21:46:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (video_bit_rate > 0 && video_bit_rate_invalid(video_bit_rate)) {
|
|
|
|
rc = TOXAV_ERR_BIT_RATE_SET_INVALID_BIT_RATE;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
Split bit_rate_set(), one for audio, one for video.
Fixes #572.
As discussed in the issue, there's a risk that toxcore may not hold the
maximum bitrates libvpx supports, if toxcore insists on using integer
type. I initially proposed to have another flag in set(), so that we can
use unsigned type instead. iphydf came up with a better solution, that is
splitting the original functions, one for audio, one for video. Now, we
could safely replace int32_t with uint32_t.
Also: clean video_bit_rate_invalid()
Though this is not a part of issue #572, as it's used in the
toxav_bit_rate_set(), i cleaned the code. As mannol said, there should be
a check. Uint32_t is large enough to hold the maximum bitrates libvpx
supports, but user may pass a value larger than uint while smaller than
uint32_t. Thanks to the reminding from nurupo, it's no longer a stub
function.
Bitrate error enums are shared for both audio and video
https://github.com/TokTok/c-toxcore/pull/578#issuecomment-360095609, just
as iphydf said.
2018-01-14 21:46:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_lock(av->mutex);
|
|
|
|
call = call_get(av, friend_number);
|
|
|
|
|
2018-08-12 20:53:12 +08:00
|
|
|
if (call == nullptr || !call->active || call->msi_call->state != MSI_CALL_ACTIVE) {
|
Split bit_rate_set(), one for audio, one for video.
Fixes #572.
As discussed in the issue, there's a risk that toxcore may not hold the
maximum bitrates libvpx supports, if toxcore insists on using integer
type. I initially proposed to have another flag in set(), so that we can
use unsigned type instead. iphydf came up with a better solution, that is
splitting the original functions, one for audio, one for video. Now, we
could safely replace int32_t with uint32_t.
Also: clean video_bit_rate_invalid()
Though this is not a part of issue #572, as it's used in the
toxav_bit_rate_set(), i cleaned the code. As mannol said, there should be
a check. Uint32_t is large enough to hold the maximum bitrates libvpx
supports, but user may pass a value larger than uint while smaller than
uint32_t. Thanks to the reminding from nurupo, it's no longer a stub
function.
Bitrate error enums are shared for both audio and video
https://github.com/TokTok/c-toxcore/pull/578#issuecomment-360095609, just
as iphydf said.
2018-01-14 21:46:06 +08:00
|
|
|
pthread_mutex_unlock(av->mutex);
|
|
|
|
rc = TOXAV_ERR_BIT_RATE_SET_FRIEND_NOT_IN_CALL;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-10-11 05:54:23 +08:00
|
|
|
}
|
|
|
|
|
Split bit_rate_set(), one for audio, one for video.
Fixes #572.
As discussed in the issue, there's a risk that toxcore may not hold the
maximum bitrates libvpx supports, if toxcore insists on using integer
type. I initially proposed to have another flag in set(), so that we can
use unsigned type instead. iphydf came up with a better solution, that is
splitting the original functions, one for audio, one for video. Now, we
could safely replace int32_t with uint32_t.
Also: clean video_bit_rate_invalid()
Though this is not a part of issue #572, as it's used in the
toxav_bit_rate_set(), i cleaned the code. As mannol said, there should be
a check. Uint32_t is large enough to hold the maximum bitrates libvpx
supports, but user may pass a value larger than uint while smaller than
uint32_t. Thanks to the reminding from nurupo, it's no longer a stub
function.
Bitrate error enums are shared for both audio and video
https://github.com/TokTok/c-toxcore/pull/578#issuecomment-360095609, just
as iphydf said.
2018-01-14 21:46:06 +08:00
|
|
|
LOGGER_DEBUG(av->m->log, "Setting new video bitrate to: %d", video_bit_rate);
|
|
|
|
|
|
|
|
if (call->video_bit_rate == video_bit_rate) {
|
|
|
|
LOGGER_DEBUG(av->m->log, "Video bitrate already set to: %d", video_bit_rate);
|
|
|
|
} else if (video_bit_rate == 0) {
|
|
|
|
LOGGER_DEBUG(av->m->log, "Turned off video sending");
|
|
|
|
|
|
|
|
/* Video sending is turned off; notify peer */
|
|
|
|
if (msi_change_capabilities(call->msi_call, call->msi_call->
|
2018-08-12 20:53:12 +08:00
|
|
|
self_capabilities ^ MSI_CAP_S_VIDEO) != 0) {
|
Split bit_rate_set(), one for audio, one for video.
Fixes #572.
As discussed in the issue, there's a risk that toxcore may not hold the
maximum bitrates libvpx supports, if toxcore insists on using integer
type. I initially proposed to have another flag in set(), so that we can
use unsigned type instead. iphydf came up with a better solution, that is
splitting the original functions, one for audio, one for video. Now, we
could safely replace int32_t with uint32_t.
Also: clean video_bit_rate_invalid()
Though this is not a part of issue #572, as it's used in the
toxav_bit_rate_set(), i cleaned the code. As mannol said, there should be
a check. Uint32_t is large enough to hold the maximum bitrates libvpx
supports, but user may pass a value larger than uint while smaller than
uint32_t. Thanks to the reminding from nurupo, it's no longer a stub
function.
Bitrate error enums are shared for both audio and video
https://github.com/TokTok/c-toxcore/pull/578#issuecomment-360095609, just
as iphydf said.
2018-01-14 21:46:06 +08:00
|
|
|
pthread_mutex_unlock(av->mutex);
|
|
|
|
rc = TOXAV_ERR_BIT_RATE_SET_SYNC;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
Split bit_rate_set(), one for audio, one for video.
Fixes #572.
As discussed in the issue, there's a risk that toxcore may not hold the
maximum bitrates libvpx supports, if toxcore insists on using integer
type. I initially proposed to have another flag in set(), so that we can
use unsigned type instead. iphydf came up with a better solution, that is
splitting the original functions, one for audio, one for video. Now, we
could safely replace int32_t with uint32_t.
Also: clean video_bit_rate_invalid()
Though this is not a part of issue #572, as it's used in the
toxav_bit_rate_set(), i cleaned the code. As mannol said, there should be
a check. Uint32_t is large enough to hold the maximum bitrates libvpx
supports, but user may pass a value larger than uint while smaller than
uint32_t. Thanks to the reminding from nurupo, it's no longer a stub
function.
Bitrate error enums are shared for both audio and video
https://github.com/TokTok/c-toxcore/pull/578#issuecomment-360095609, just
as iphydf said.
2018-01-14 21:46:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
call->video_bit_rate = 0;
|
|
|
|
} else {
|
2020-03-28 18:58:00 +08:00
|
|
|
pthread_mutex_lock(call->toxav_call_mutex);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
Split bit_rate_set(), one for audio, one for video.
Fixes #572.
As discussed in the issue, there's a risk that toxcore may not hold the
maximum bitrates libvpx supports, if toxcore insists on using integer
type. I initially proposed to have another flag in set(), so that we can
use unsigned type instead. iphydf came up with a better solution, that is
splitting the original functions, one for audio, one for video. Now, we
could safely replace int32_t with uint32_t.
Also: clean video_bit_rate_invalid()
Though this is not a part of issue #572, as it's used in the
toxav_bit_rate_set(), i cleaned the code. As mannol said, there should be
a check. Uint32_t is large enough to hold the maximum bitrates libvpx
supports, but user may pass a value larger than uint while smaller than
uint32_t. Thanks to the reminding from nurupo, it's no longer a stub
function.
Bitrate error enums are shared for both audio and video
https://github.com/TokTok/c-toxcore/pull/578#issuecomment-360095609, just
as iphydf said.
2018-01-14 21:46:06 +08:00
|
|
|
if (call->video_bit_rate == 0) {
|
|
|
|
LOGGER_DEBUG(av->m->log, "Turned on video sending");
|
2015-11-04 02:42:05 +08:00
|
|
|
|
Split bit_rate_set(), one for audio, one for video.
Fixes #572.
As discussed in the issue, there's a risk that toxcore may not hold the
maximum bitrates libvpx supports, if toxcore insists on using integer
type. I initially proposed to have another flag in set(), so that we can
use unsigned type instead. iphydf came up with a better solution, that is
splitting the original functions, one for audio, one for video. Now, we
could safely replace int32_t with uint32_t.
Also: clean video_bit_rate_invalid()
Though this is not a part of issue #572, as it's used in the
toxav_bit_rate_set(), i cleaned the code. As mannol said, there should be
a check. Uint32_t is large enough to hold the maximum bitrates libvpx
supports, but user may pass a value larger than uint while smaller than
uint32_t. Thanks to the reminding from nurupo, it's no longer a stub
function.
Bitrate error enums are shared for both audio and video
https://github.com/TokTok/c-toxcore/pull/578#issuecomment-360095609, just
as iphydf said.
2018-01-14 21:46:06 +08:00
|
|
|
/* The video has been turned off before this */
|
|
|
|
if (msi_change_capabilities(call->msi_call, call->
|
2018-08-12 20:53:12 +08:00
|
|
|
msi_call->self_capabilities | MSI_CAP_S_VIDEO) != 0) {
|
2020-03-28 18:58:00 +08:00
|
|
|
pthread_mutex_unlock(call->toxav_call_mutex);
|
2015-10-11 05:54:23 +08:00
|
|
|
pthread_mutex_unlock(av->mutex);
|
|
|
|
rc = TOXAV_ERR_BIT_RATE_SET_SYNC;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-10-11 05:54:23 +08:00
|
|
|
}
|
|
|
|
} else {
|
Split bit_rate_set(), one for audio, one for video.
Fixes #572.
As discussed in the issue, there's a risk that toxcore may not hold the
maximum bitrates libvpx supports, if toxcore insists on using integer
type. I initially proposed to have another flag in set(), so that we can
use unsigned type instead. iphydf came up with a better solution, that is
splitting the original functions, one for audio, one for video. Now, we
could safely replace int32_t with uint32_t.
Also: clean video_bit_rate_invalid()
Though this is not a part of issue #572, as it's used in the
toxav_bit_rate_set(), i cleaned the code. As mannol said, there should be
a check. Uint32_t is large enough to hold the maximum bitrates libvpx
supports, but user may pass a value larger than uint while smaller than
uint32_t. Thanks to the reminding from nurupo, it's no longer a stub
function.
Bitrate error enums are shared for both audio and video
https://github.com/TokTok/c-toxcore/pull/578#issuecomment-360095609, just
as iphydf said.
2018-01-14 21:46:06 +08:00
|
|
|
LOGGER_DEBUG(av->m->log, "Set new video bit rate %d", video_bit_rate);
|
2015-06-27 07:55:28 +08:00
|
|
|
}
|
Split bit_rate_set(), one for audio, one for video.
Fixes #572.
As discussed in the issue, there's a risk that toxcore may not hold the
maximum bitrates libvpx supports, if toxcore insists on using integer
type. I initially proposed to have another flag in set(), so that we can
use unsigned type instead. iphydf came up with a better solution, that is
splitting the original functions, one for audio, one for video. Now, we
could safely replace int32_t with uint32_t.
Also: clean video_bit_rate_invalid()
Though this is not a part of issue #572, as it's used in the
toxav_bit_rate_set(), i cleaned the code. As mannol said, there should be
a check. Uint32_t is large enough to hold the maximum bitrates libvpx
supports, but user may pass a value larger than uint while smaller than
uint32_t. Thanks to the reminding from nurupo, it's no longer a stub
function.
Bitrate error enums are shared for both audio and video
https://github.com/TokTok/c-toxcore/pull/578#issuecomment-360095609, just
as iphydf said.
2018-01-14 21:46:06 +08:00
|
|
|
|
|
|
|
call->video_bit_rate = video_bit_rate;
|
2020-03-28 18:58:00 +08:00
|
|
|
pthread_mutex_unlock(call->toxav_call_mutex);
|
2015-04-27 06:15:57 +08:00
|
|
|
}
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_unlock(av->mutex);
|
2018-08-13 06:01:42 +08:00
|
|
|
RETURN:
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (error) {
|
2015-03-29 08:10:34 +08:00
|
|
|
*error = rc;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
|
|
|
return rc == TOXAV_ERR_BIT_RATE_SET_OK;
|
2014-02-10 06:06:44 +08:00
|
|
|
}
|
2018-01-28 19:14:04 +08:00
|
|
|
void toxav_callback_audio_bit_rate(ToxAV *av, toxav_audio_bit_rate_cb *callback, void *user_data)
|
2015-04-27 06:15:57 +08:00
|
|
|
{
|
|
|
|
pthread_mutex_lock(av->mutex);
|
2018-08-12 18:55:20 +08:00
|
|
|
av->abcb = callback;
|
|
|
|
av->abcb_user_data = user_data;
|
2018-01-28 19:14:04 +08:00
|
|
|
pthread_mutex_unlock(av->mutex);
|
|
|
|
}
|
|
|
|
void toxav_callback_video_bit_rate(ToxAV *av, toxav_video_bit_rate_cb *callback, void *user_data)
|
|
|
|
{
|
|
|
|
pthread_mutex_lock(av->mutex);
|
2018-08-12 18:55:20 +08:00
|
|
|
av->vbcb = callback;
|
|
|
|
av->vbcb_user_data = user_data;
|
2015-04-27 06:15:57 +08:00
|
|
|
pthread_mutex_unlock(av->mutex);
|
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
bool toxav_audio_send_frame(ToxAV *av, uint32_t friend_number, const int16_t *pcm, size_t sample_count,
|
2018-08-13 06:01:42 +08:00
|
|
|
uint8_t channels, uint32_t sampling_rate, Toxav_Err_Send_Frame *error)
|
2014-02-10 06:06:44 +08:00
|
|
|
{
|
2018-08-13 06:01:42 +08:00
|
|
|
Toxav_Err_Send_Frame rc = TOXAV_ERR_SEND_FRAME_OK;
|
2015-10-11 05:54:23 +08:00
|
|
|
ToxAVCall *call;
|
|
|
|
|
2015-05-23 05:22:31 +08:00
|
|
|
if (m_friend_exists(av->m, friend_number) == 0) {
|
|
|
|
rc = TOXAV_ERR_SEND_FRAME_FRIEND_NOT_FOUND;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-05-23 05:22:31 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-10-24 07:56:49 +08:00
|
|
|
if (pthread_mutex_trylock(av->mutex) != 0) {
|
|
|
|
rc = TOXAV_ERR_SEND_FRAME_SYNC;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-10-24 07:56:49 +08:00
|
|
|
}
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2015-05-23 05:22:31 +08:00
|
|
|
call = call_get(av, friend_number);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-12 20:53:12 +08:00
|
|
|
if (call == nullptr || !call->active || call->msi_call->state != MSI_CALL_ACTIVE) {
|
2015-05-23 05:22:31 +08:00
|
|
|
pthread_mutex_unlock(av->mutex);
|
|
|
|
rc = TOXAV_ERR_SEND_FRAME_FRIEND_NOT_IN_CALL;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-05-23 05:22:31 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-06-30 07:41:38 +08:00
|
|
|
if (call->audio_bit_rate == 0 ||
|
2018-08-12 20:53:12 +08:00
|
|
|
!(call->msi_call->self_capabilities & MSI_CAP_S_AUDIO) ||
|
|
|
|
!(call->msi_call->peer_capabilities & MSI_CAP_R_AUDIO)) {
|
2015-06-27 07:55:28 +08:00
|
|
|
pthread_mutex_unlock(av->mutex);
|
2015-06-30 07:41:38 +08:00
|
|
|
rc = TOXAV_ERR_SEND_FRAME_PAYLOAD_TYPE_DISABLED;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-06-27 07:55:28 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-05-23 05:22:31 +08:00
|
|
|
pthread_mutex_lock(call->mutex_audio);
|
|
|
|
pthread_mutex_unlock(av->mutex);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
if (pcm == nullptr) {
|
2015-05-23 05:22:31 +08:00
|
|
|
pthread_mutex_unlock(call->mutex_audio);
|
|
|
|
rc = TOXAV_ERR_SEND_FRAME_NULL;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-05-23 05:22:31 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
|
|
|
if (channels > 2) {
|
2015-05-23 05:22:31 +08:00
|
|
|
pthread_mutex_unlock(call->mutex_audio);
|
|
|
|
rc = TOXAV_ERR_SEND_FRAME_INVALID;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-05-23 05:22:31 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2020-05-03 08:09:06 +08:00
|
|
|
{ /* Encode and send */
|
2018-08-12 18:55:20 +08:00
|
|
|
if (ac_reconfigure_encoder(call->audio, call->audio_bit_rate * 1000, sampling_rate, channels) != 0) {
|
2015-05-23 05:22:31 +08:00
|
|
|
pthread_mutex_unlock(call->mutex_audio);
|
|
|
|
rc = TOXAV_ERR_SEND_FRAME_INVALID;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2014-06-24 02:43:39 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2017-01-21 05:16:55 +08:00
|
|
|
VLA(uint8_t, dest, sample_count + sizeof(sampling_rate)); /* This is more than enough always */
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2017-01-07 16:28:53 +08:00
|
|
|
sampling_rate = net_htonl(sampling_rate);
|
2015-05-23 05:22:31 +08:00
|
|
|
memcpy(dest, &sampling_rate, sizeof(sampling_rate));
|
2018-08-12 18:55:20 +08:00
|
|
|
int vrc = opus_encode(call->audio->encoder, pcm, sample_count,
|
2017-01-21 05:16:55 +08:00
|
|
|
dest + sizeof(sampling_rate), SIZEOF_VLA(dest) - sizeof(sampling_rate));
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-05-23 05:22:31 +08:00
|
|
|
if (vrc < 0) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_WARNING(av->m->log, "Failed to encode frame %s", opus_strerror(vrc));
|
2015-05-23 05:22:31 +08:00
|
|
|
pthread_mutex_unlock(call->mutex_audio);
|
|
|
|
rc = TOXAV_ERR_SEND_FRAME_INVALID;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-05-23 05:22:31 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-12 18:55:20 +08:00
|
|
|
if (rtp_send_data(call->audio_rtp, dest, vrc + sizeof(sampling_rate), false, av->m->log) != 0) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_WARNING(av->m->log, "Failed to send audio packet");
|
2015-05-23 05:22:31 +08:00
|
|
|
rc = TOXAV_ERR_SEND_FRAME_RTP_FAILED;
|
|
|
|
}
|
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-05-23 05:22:31 +08:00
|
|
|
pthread_mutex_unlock(call->mutex_audio);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-13 06:01:42 +08:00
|
|
|
RETURN:
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (error) {
|
2015-05-23 05:22:31 +08:00
|
|
|
*error = rc;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-05-23 05:22:31 +08:00
|
|
|
return rc == TOXAV_ERR_SEND_FRAME_OK;
|
2014-07-21 07:10:57 +08:00
|
|
|
}
|
2018-01-20 05:59:42 +08:00
|
|
|
|
2018-08-13 06:01:42 +08:00
|
|
|
static Toxav_Err_Send_Frame send_frames(const Logger *log, ToxAVCall *call)
|
|
|
|
{
|
|
|
|
vpx_codec_iter_t iter = nullptr;
|
|
|
|
|
|
|
|
for (const vpx_codec_cx_pkt_t *pkt = vpx_codec_get_cx_data(call->video->encoder, &iter);
|
|
|
|
pkt != nullptr;
|
|
|
|
pkt = vpx_codec_get_cx_data(call->video->encoder, &iter)) {
|
|
|
|
if (pkt->kind != VPX_CODEC_CX_FRAME_PKT) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool is_keyframe = (pkt->data.frame.flags & VPX_FRAME_IS_KEY) != 0;
|
|
|
|
|
|
|
|
// https://www.webmproject.org/docs/webm-sdk/structvpx__codec__cx__pkt.html
|
|
|
|
// pkt->data.frame.sz -> size_t
|
|
|
|
const uint32_t frame_length_in_bytes = pkt->data.frame.sz;
|
|
|
|
|
|
|
|
const int res = rtp_send_data(
|
|
|
|
call->video_rtp,
|
|
|
|
(const uint8_t *)pkt->data.frame.buf,
|
|
|
|
frame_length_in_bytes,
|
|
|
|
is_keyframe,
|
|
|
|
log);
|
|
|
|
|
|
|
|
LOGGER_DEBUG(log, "+ _sending_FRAME_TYPE_==%s bytes=%d frame_len=%d", is_keyframe ? "K" : ".",
|
|
|
|
(int)pkt->data.frame.sz, (int)frame_length_in_bytes);
|
|
|
|
const uint8_t *const buf = (const uint8_t *)pkt->data.frame.buf;
|
|
|
|
LOGGER_DEBUG(log, "+ _sending_FRAME_ b0=%d b1=%d", buf[0], buf[1]);
|
|
|
|
|
|
|
|
if (res < 0) {
|
|
|
|
LOGGER_WARNING(log, "Could not send video frame: %s", strerror(errno));
|
|
|
|
return TOXAV_ERR_SEND_FRAME_RTP_FAILED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return TOXAV_ERR_SEND_FRAME_OK;
|
|
|
|
}
|
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
bool toxav_video_send_frame(ToxAV *av, uint32_t friend_number, uint16_t width, uint16_t height, const uint8_t *y,
|
2018-08-13 06:01:42 +08:00
|
|
|
const uint8_t *u, const uint8_t *v, Toxav_Err_Send_Frame *error)
|
2014-02-10 06:06:44 +08:00
|
|
|
{
|
2018-08-13 06:01:42 +08:00
|
|
|
Toxav_Err_Send_Frame rc = TOXAV_ERR_SEND_FRAME_OK;
|
2015-10-11 05:54:23 +08:00
|
|
|
ToxAVCall *call;
|
|
|
|
|
2018-01-20 05:59:42 +08:00
|
|
|
int vpx_encode_flags = 0;
|
|
|
|
|
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;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2014-02-10 06:06:44 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-10-24 07:56:49 +08:00
|
|
|
if (pthread_mutex_trylock(av->mutex) != 0) {
|
|
|
|
rc = TOXAV_ERR_SEND_FRAME_SYNC;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-10-24 07:56:49 +08:00
|
|
|
}
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
call = call_get(av, friend_number);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-12 20:53:12 +08:00
|
|
|
if (call == nullptr || !call->active || call->msi_call->state != MSI_CALL_ACTIVE) {
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_unlock(av->mutex);
|
2015-02-15 06:37:52 +08:00
|
|
|
rc = TOXAV_ERR_SEND_FRAME_FRIEND_NOT_IN_CALL;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2014-12-04 03:41:01 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-06-30 07:41:38 +08:00
|
|
|
if (call->video_bit_rate == 0 ||
|
2018-08-12 20:53:12 +08:00
|
|
|
!(call->msi_call->self_capabilities & MSI_CAP_S_VIDEO) ||
|
|
|
|
!(call->msi_call->peer_capabilities & MSI_CAP_R_VIDEO)) {
|
2015-06-27 07:55:28 +08:00
|
|
|
pthread_mutex_unlock(av->mutex);
|
2015-06-30 07:41:38 +08:00
|
|
|
rc = TOXAV_ERR_SEND_FRAME_PAYLOAD_TYPE_DISABLED;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-06-27 07:55:28 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_lock(call->mutex_video);
|
|
|
|
pthread_mutex_unlock(av->mutex);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
if (y == nullptr || u == nullptr || v == nullptr) {
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_unlock(call->mutex_video);
|
2015-02-15 06:37:52 +08:00
|
|
|
rc = TOXAV_ERR_SEND_FRAME_NULL;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2014-02-10 06:06:44 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-12 18:55:20 +08:00
|
|
|
if (vc_reconfigure_encoder(call->video, call->video_bit_rate * 1000, width, height, -1) != 0) {
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_unlock(call->mutex_video);
|
2015-02-15 06:37:52 +08:00
|
|
|
rc = TOXAV_ERR_SEND_FRAME_INVALID;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2014-07-03 22:58:00 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-12 18:55:20 +08:00
|
|
|
if (call->video_rtp->ssrc < VIDEO_SEND_X_KEYFRAMES_FIRST) {
|
2018-01-20 05:59:42 +08:00
|
|
|
// Key frame flag for first frames
|
|
|
|
vpx_encode_flags = VPX_EFLAG_FORCE_KF;
|
2018-08-12 18:55:20 +08:00
|
|
|
LOGGER_INFO(av->m->log, "I_FRAME_FLAG:%d only-i-frame mode", call->video_rtp->ssrc);
|
2018-01-20 05:59:42 +08:00
|
|
|
|
2018-08-13 06:01:42 +08:00
|
|
|
++call->video_rtp->ssrc;
|
2018-08-12 18:55:20 +08:00
|
|
|
} else if (call->video_rtp->ssrc == VIDEO_SEND_X_KEYFRAMES_FIRST) {
|
2018-01-20 05:59:42 +08:00
|
|
|
// normal keyframe placement
|
|
|
|
vpx_encode_flags = 0;
|
2018-08-12 18:55:20 +08:00
|
|
|
LOGGER_INFO(av->m->log, "I_FRAME_FLAG:%d normal mode", call->video_rtp->ssrc);
|
2018-01-20 05:59:42 +08:00
|
|
|
|
2018-08-13 06:01:42 +08:00
|
|
|
++call->video_rtp->ssrc;
|
2018-01-20 05:59:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// we start with I-frames (full frames) and then switch to normal mode later
|
|
|
|
|
2020-05-03 08:09:06 +08:00
|
|
|
{ /* Encode */
|
2015-02-15 06:37:52 +08:00
|
|
|
vpx_image_t img;
|
2018-08-13 06:01:42 +08:00
|
|
|
img.w = 0;
|
|
|
|
img.h = 0;
|
|
|
|
img.d_w = 0;
|
|
|
|
img.d_h = 0;
|
2015-10-11 05:54:23 +08:00
|
|
|
vpx_img_alloc(&img, VPX_IMG_FMT_I420, width, height, 0);
|
|
|
|
|
|
|
|
/* I420 "It comprises an NxM Y plane followed by (N/2)x(M/2) V and U planes."
|
2015-02-15 06:37:52 +08:00
|
|
|
* http://fourcc.org/yuv.php#IYUV
|
|
|
|
*/
|
|
|
|
memcpy(img.planes[VPX_PLANE_Y], y, width * height);
|
2015-10-11 05:54:23 +08:00
|
|
|
memcpy(img.planes[VPX_PLANE_U], u, (width / 2) * (height / 2));
|
|
|
|
memcpy(img.planes[VPX_PLANE_V], v, (width / 2) * (height / 2));
|
|
|
|
|
2018-08-12 18:55:20 +08:00
|
|
|
vpx_codec_err_t vrc = vpx_codec_encode(call->video->encoder, &img,
|
|
|
|
call->video->frame_counter, 1, vpx_encode_flags, MAX_ENCODE_TIME_US);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-03-29 08:10:34 +08:00
|
|
|
vpx_img_free(&img);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
|
|
|
if (vrc != VPX_CODEC_OK) {
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_unlock(call->mutex_video);
|
2020-04-08 03:17:58 +08:00
|
|
|
LOGGER_ERROR(av->m->log, "Could not encode video frame: %s", vpx_codec_err_to_string(vrc));
|
2015-02-15 06:37:52 +08:00
|
|
|
rc = TOXAV_ERR_SEND_FRAME_INVALID;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-02-15 06:37:52 +08:00
|
|
|
}
|
2014-07-03 22:58:00 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-12 18:55:20 +08:00
|
|
|
++call->video->frame_counter;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-13 06:01:42 +08:00
|
|
|
rc = send_frames(av->m->log, call);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_unlock(call->mutex_video);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-13 06:01:42 +08:00
|
|
|
RETURN:
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (error) {
|
2015-02-15 06:37:52 +08:00
|
|
|
*error = rc;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
return rc == TOXAV_ERR_SEND_FRAME_OK;
|
2014-02-10 06:06:44 +08:00
|
|
|
}
|
2018-01-20 05:59:42 +08:00
|
|
|
|
2016-09-20 04:49:04 +08:00
|
|
|
void toxav_callback_audio_receive_frame(ToxAV *av, toxav_audio_receive_frame_cb *callback, void *user_data)
|
2014-02-10 06:06:44 +08:00
|
|
|
{
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_lock(av->mutex);
|
2018-08-12 18:55:20 +08:00
|
|
|
av->acb = callback;
|
|
|
|
av->acb_user_data = user_data;
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_unlock(av->mutex);
|
2014-02-16 03:44:33 +08:00
|
|
|
}
|
2018-01-20 05:59:42 +08:00
|
|
|
|
2016-09-20 04:49:04 +08:00
|
|
|
void toxav_callback_video_receive_frame(ToxAV *av, toxav_video_receive_frame_cb *callback, void *user_data)
|
2014-02-16 03:44:33 +08:00
|
|
|
{
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_lock(av->mutex);
|
2018-08-12 18:55:20 +08:00
|
|
|
av->vcb = callback;
|
|
|
|
av->vcb_user_data = user_data;
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_unlock(av->mutex);
|
2014-05-03 07:46:03 +08:00
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
/*******************************************************************************
|
|
|
|
*
|
|
|
|
* :: Internal
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
2020-03-18 11:41:02 +08:00
|
|
|
static void callback_bwc(BWController *bwc, uint32_t friend_number, float loss, void *user_data)
|
2015-02-15 06:37:52 +08:00
|
|
|
{
|
2015-10-11 05:54:23 +08:00
|
|
|
/* Callback which is called when the internal measure mechanism reported packet loss.
|
|
|
|
* We report suggested lowered bitrate to an app. If app is sending both audio and video,
|
|
|
|
* we will report lowered bitrate for video only because in that case video probably
|
|
|
|
* takes more than 90% bandwidth. Otherwise, we report lowered bitrate on audio.
|
|
|
|
* The application may choose to disable video totally if the stream is too bad.
|
|
|
|
*/
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2016-09-18 08:31:55 +08:00
|
|
|
ToxAVCall *call = (ToxAVCall *)user_data;
|
2015-10-11 05:54:23 +08:00
|
|
|
assert(call);
|
|
|
|
|
2018-02-23 10:22:38 +08:00
|
|
|
LOGGER_DEBUG(call->av->m->log, "Reported loss of %f%%", (double)loss * 100);
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2018-01-20 05:59:42 +08:00
|
|
|
/* if less than 10% data loss we do nothing! */
|
|
|
|
if (loss < 0.1f) {
|
2015-10-11 05:54:23 +08:00
|
|
|
return;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
|
|
|
pthread_mutex_lock(call->av->mutex);
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (call->video_bit_rate) {
|
2018-08-12 18:55:20 +08:00
|
|
|
if (!call->av->vbcb) {
|
2018-01-28 19:14:04 +08:00
|
|
|
pthread_mutex_unlock(call->av->mutex);
|
|
|
|
LOGGER_WARNING(call->av->m->log, "No callback to report loss on");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-13 06:01:42 +08:00
|
|
|
call->av->vbcb(call->av, friend_number,
|
|
|
|
call->video_bit_rate - (call->video_bit_rate * loss),
|
|
|
|
call->av->vbcb_user_data);
|
2016-09-01 02:12:19 +08:00
|
|
|
} else if (call->audio_bit_rate) {
|
2018-08-12 18:55:20 +08:00
|
|
|
if (!call->av->abcb) {
|
2018-01-28 19:14:04 +08:00
|
|
|
pthread_mutex_unlock(call->av->mutex);
|
|
|
|
LOGGER_WARNING(call->av->m->log, "No callback to report loss on");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-13 06:01:42 +08:00
|
|
|
call->av->abcb(call->av, friend_number,
|
|
|
|
call->audio_bit_rate - (call->audio_bit_rate * loss),
|
|
|
|
call->av->abcb_user_data);
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
pthread_mutex_unlock(call->av->mutex);
|
|
|
|
}
|
2020-03-18 11:41:02 +08:00
|
|
|
static int callback_invite(void *toxav_inst, MSICall *call)
|
2015-10-11 05:54:23 +08:00
|
|
|
{
|
2016-09-18 08:31:55 +08:00
|
|
|
ToxAV *toxav = (ToxAV *)toxav_inst;
|
2015-10-11 05:54:23 +08:00
|
|
|
pthread_mutex_lock(toxav->mutex);
|
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
ToxAVCall *av_call = call_new(toxav, call->friend_number, nullptr);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
if (av_call == nullptr) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_WARNING(toxav->m->log, "Failed to initialize call...");
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_unlock(toxav->mutex);
|
2015-02-21 08:07:22 +08:00
|
|
|
return -1;
|
2014-05-11 00:00:49 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-02-19 06:23:46 +08:00
|
|
|
call->av_call = av_call;
|
|
|
|
av_call->msi_call = call;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-12 18:55:20 +08:00
|
|
|
if (toxav->ccb) {
|
2018-08-12 20:53:12 +08:00
|
|
|
toxav->ccb(toxav, call->friend_number, call->peer_capabilities & MSI_CAP_S_AUDIO,
|
|
|
|
call->peer_capabilities & MSI_CAP_S_VIDEO, toxav->ccb_user_data);
|
2016-09-01 02:12:19 +08:00
|
|
|
} else {
|
2015-05-13 04:16:00 +08:00
|
|
|
/* No handler to capture the call request, send failure */
|
|
|
|
pthread_mutex_unlock(toxav->mutex);
|
|
|
|
return -1;
|
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_unlock(toxav->mutex);
|
2015-02-21 08:07:22 +08:00
|
|
|
return 0;
|
2014-02-10 06:06:44 +08:00
|
|
|
}
|
2020-03-18 11:41:02 +08:00
|
|
|
static int callback_start(void *toxav_inst, MSICall *call)
|
2015-02-15 06:37:52 +08:00
|
|
|
{
|
2016-09-18 08:31:55 +08:00
|
|
|
ToxAV *toxav = (ToxAV *)toxav_inst;
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_lock(toxav->mutex);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
|
|
|
ToxAVCall *av_call = call_get(toxav, call->friend_number);
|
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
if (av_call == nullptr) {
|
2015-02-23 06:41:40 +08:00
|
|
|
/* Should this ever happen? */
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_unlock(toxav->mutex);
|
2015-02-21 08:07:22 +08:00
|
|
|
return -1;
|
2014-11-18 07:46:46 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-02-23 06:41:40 +08:00
|
|
|
if (!call_prepare_transmission(av_call)) {
|
2016-09-20 04:49:04 +08:00
|
|
|
callback_error(toxav_inst, call);
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_unlock(toxav->mutex);
|
2015-02-23 06:41:40 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2016-09-20 04:49:04 +08:00
|
|
|
if (!invoke_call_state_callback(toxav, call->friend_number, call->peer_capabilities)) {
|
|
|
|
callback_error(toxav_inst, call);
|
2015-05-13 04:16:00 +08:00
|
|
|
pthread_mutex_unlock(toxav->mutex);
|
|
|
|
return -1;
|
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_unlock(toxav->mutex);
|
2015-02-21 08:07:22 +08:00
|
|
|
return 0;
|
2014-03-07 10:13:04 +08:00
|
|
|
}
|
2020-03-18 11:41:02 +08:00
|
|
|
static int callback_end(void *toxav_inst, MSICall *call)
|
2014-07-05 01:28:47 +08:00
|
|
|
{
|
2016-09-18 08:31:55 +08:00
|
|
|
ToxAV *toxav = (ToxAV *)toxav_inst;
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_lock(toxav->mutex);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2016-09-20 04:49:04 +08:00
|
|
|
invoke_call_state_callback(toxav, call->friend_number, TOXAV_FRIEND_CALL_STATE_FINISHED);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-08-09 17:57:39 +08:00
|
|
|
if (call->av_call) {
|
2018-08-13 06:01:42 +08:00
|
|
|
call_kill_transmission(call->av_call);
|
|
|
|
call_remove(call->av_call);
|
2015-08-09 17:57:39 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_unlock(toxav->mutex);
|
2015-02-21 08:07:22 +08:00
|
|
|
return 0;
|
2014-07-05 00:16:53 +08:00
|
|
|
}
|
2020-03-18 11:41:02 +08:00
|
|
|
static int callback_error(void *toxav_inst, MSICall *call)
|
2014-03-07 10:13:04 +08:00
|
|
|
{
|
2016-09-18 08:31:55 +08:00
|
|
|
ToxAV *toxav = (ToxAV *)toxav_inst;
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_lock(toxav->mutex);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2016-09-20 04:49:04 +08:00
|
|
|
invoke_call_state_callback(toxav, call->friend_number, TOXAV_FRIEND_CALL_STATE_ERROR);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-08-09 17:57:39 +08:00
|
|
|
if (call->av_call) {
|
2018-08-13 06:01:42 +08:00
|
|
|
call_kill_transmission(call->av_call);
|
|
|
|
call_remove(call->av_call);
|
2015-08-09 17:57:39 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_unlock(toxav->mutex);
|
2015-02-21 08:07:22 +08:00
|
|
|
return 0;
|
2014-03-07 10:13:04 +08:00
|
|
|
}
|
2020-03-18 11:41:02 +08:00
|
|
|
static int callback_capabilites(void *toxav_inst, MSICall *call)
|
2014-05-17 01:56:40 +08:00
|
|
|
{
|
2016-09-18 08:31:55 +08:00
|
|
|
ToxAV *toxav = (ToxAV *)toxav_inst;
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_lock(toxav->mutex);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-12 20:53:12 +08:00
|
|
|
if (call->peer_capabilities & MSI_CAP_S_AUDIO) {
|
2018-08-13 06:01:42 +08:00
|
|
|
rtp_allow_receiving(call->av_call->audio_rtp);
|
2016-09-01 02:12:19 +08:00
|
|
|
} else {
|
2018-08-13 06:01:42 +08:00
|
|
|
rtp_stop_receiving(call->av_call->audio_rtp);
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-12 20:53:12 +08:00
|
|
|
if (call->peer_capabilities & MSI_CAP_S_VIDEO) {
|
2018-08-13 06:01:42 +08:00
|
|
|
rtp_allow_receiving(call->av_call->video_rtp);
|
2016-09-01 02:12:19 +08:00
|
|
|
} else {
|
2018-08-13 06:01:42 +08:00
|
|
|
rtp_stop_receiving(call->av_call->video_rtp);
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2016-09-20 04:49:04 +08:00
|
|
|
invoke_call_state_callback(toxav, call->friend_number, call->peer_capabilities);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_unlock(toxav->mutex);
|
2015-02-21 08:07:22 +08:00
|
|
|
return 0;
|
2014-06-21 07:58:55 +08:00
|
|
|
}
|
2020-03-18 11:41:02 +08:00
|
|
|
static bool audio_bit_rate_invalid(uint32_t bit_rate)
|
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:
|
2015-04-29 07:01:25 +08:00
|
|
|
* Opus supports all bit rates from 6 kbit/s to 510 kbit/s.
|
2015-02-21 08:07:22 +08:00
|
|
|
*/
|
2015-04-29 07:01:25 +08:00
|
|
|
return bit_rate < 6 || bit_rate > 510;
|
2015-02-15 06:37:52 +08:00
|
|
|
}
|
2020-03-18 11:41:02 +08:00
|
|
|
static bool video_bit_rate_invalid(uint32_t bit_rate)
|
2015-02-15 06:37:52 +08:00
|
|
|
{
|
Split bit_rate_set(), one for audio, one for video.
Fixes #572.
As discussed in the issue, there's a risk that toxcore may not hold the
maximum bitrates libvpx supports, if toxcore insists on using integer
type. I initially proposed to have another flag in set(), so that we can
use unsigned type instead. iphydf came up with a better solution, that is
splitting the original functions, one for audio, one for video. Now, we
could safely replace int32_t with uint32_t.
Also: clean video_bit_rate_invalid()
Though this is not a part of issue #572, as it's used in the
toxav_bit_rate_set(), i cleaned the code. As mannol said, there should be
a check. Uint32_t is large enough to hold the maximum bitrates libvpx
supports, but user may pass a value larger than uint while smaller than
uint32_t. Thanks to the reminding from nurupo, it's no longer a stub
function.
Bitrate error enums are shared for both audio and video
https://github.com/TokTok/c-toxcore/pull/578#issuecomment-360095609, just
as iphydf said.
2018-01-14 21:46:06 +08:00
|
|
|
/* https://www.webmproject.org/docs/webm-sdk/structvpx__codec__enc__cfg.html shows the following:
|
|
|
|
* unsigned int rc_target_bitrate
|
|
|
|
* the range of uint varies from platform to platform
|
|
|
|
* though, uint32_t should be large enough to store bitrates,
|
|
|
|
* we may want to prevent from passing overflowed bitrates to libvpx
|
|
|
|
* more in detail, it's the case where bit_rate is larger than uint, but smaller than uint32_t
|
|
|
|
*/
|
|
|
|
return bit_rate > UINT_MAX;
|
2015-02-21 08:07:22 +08:00
|
|
|
}
|
2020-03-18 11:41:02 +08:00
|
|
|
static bool invoke_call_state_callback(ToxAV *av, uint32_t friend_number, uint32_t state)
|
2015-04-16 08:00:34 +08:00
|
|
|
{
|
2018-08-12 18:55:20 +08:00
|
|
|
if (av->scb) {
|
|
|
|
av->scb(av, friend_number, state, av->scb_user_data);
|
2016-09-01 02:12:19 +08:00
|
|
|
} else {
|
2015-05-13 04:16:00 +08:00
|
|
|
return false;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-05-13 04:16:00 +08:00
|
|
|
return true;
|
2015-04-16 08:00:34 +08:00
|
|
|
}
|
2018-01-20 05:59:42 +08:00
|
|
|
|
2020-03-18 11:41:02 +08:00
|
|
|
static ToxAVCall *call_new(ToxAV *av, uint32_t friend_number, Toxav_Err_Call *error)
|
2015-02-21 08:07:22 +08:00
|
|
|
{
|
2015-03-02 01:45:04 +08:00
|
|
|
/* Assumes mutex locked */
|
2018-08-13 06:01:42 +08:00
|
|
|
Toxav_Err_Call rc = TOXAV_ERR_CALL_OK;
|
2018-01-29 05:30:39 +08:00
|
|
|
ToxAVCall *call = nullptr;
|
2015-10-11 05:54:23 +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;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-02-21 08:07:22 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-18 01:19:58 +08:00
|
|
|
if (m_get_friend_connectionstatus(av->m, friend_number) < 1) {
|
2015-02-21 08:07:22 +08:00
|
|
|
rc = TOXAV_ERR_CALL_FRIEND_NOT_CONNECTED;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-02-21 08:07:22 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
if (call_get(av, friend_number) != nullptr) {
|
2015-02-21 08:07:22 +08:00
|
|
|
rc = TOXAV_ERR_CALL_FRIEND_ALREADY_IN_CALL;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2014-12-04 03:41:01 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2016-09-18 08:31:55 +08:00
|
|
|
call = (ToxAVCall *)calloc(sizeof(ToxAVCall), 1);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
if (call == nullptr) {
|
2015-02-21 08:07:22 +08:00
|
|
|
rc = TOXAV_ERR_CALL_MALLOC;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-02-21 08:07:22 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
call->av = av;
|
2015-04-29 07:01:25 +08:00
|
|
|
call->friend_number = friend_number;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2020-03-28 18:58:00 +08:00
|
|
|
if (create_recursive_mutex(call->toxav_call_mutex)) {
|
2018-09-22 02:05:58 +08:00
|
|
|
free(call);
|
|
|
|
call = nullptr;
|
|
|
|
rc = TOXAV_ERR_CALL_MALLOC;
|
|
|
|
goto RETURN;
|
|
|
|
}
|
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
if (av->calls == nullptr) { /* Creating */
|
2016-09-18 08:31:55 +08:00
|
|
|
av->calls = (ToxAVCall **)calloc(sizeof(ToxAVCall *), friend_number + 1);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
if (av->calls == nullptr) {
|
2020-03-28 18:58:00 +08:00
|
|
|
pthread_mutex_destroy(call->toxav_call_mutex);
|
2015-02-21 08:07:22 +08:00
|
|
|
free(call);
|
2018-01-29 05:30:39 +08:00
|
|
|
call = nullptr;
|
2015-02-21 08:07:22 +08:00
|
|
|
rc = TOXAV_ERR_CALL_MALLOC;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-02-15 06:37:52 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-13 06:01:42 +08:00
|
|
|
av->calls_tail = friend_number;
|
|
|
|
av->calls_head = friend_number;
|
2015-10-24 01:01:51 +08:00
|
|
|
} else if (av->calls_tail < friend_number) { /* Appending */
|
2016-09-18 08:31:55 +08:00
|
|
|
ToxAVCall **tmp = (ToxAVCall **)realloc(av->calls, sizeof(ToxAVCall *) * (friend_number + 1));
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
if (tmp == nullptr) {
|
2020-03-28 18:58:00 +08:00
|
|
|
pthread_mutex_destroy(call->toxav_call_mutex);
|
2015-02-21 08:07:22 +08:00
|
|
|
free(call);
|
2018-01-29 05:30:39 +08:00
|
|
|
call = nullptr;
|
2015-02-21 08:07:22 +08:00
|
|
|
rc = TOXAV_ERR_CALL_MALLOC;
|
2018-08-13 06:01:42 +08:00
|
|
|
goto RETURN;
|
2015-02-15 06:37:52 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
av->calls = tmp;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
/* Set fields in between to null */
|
2018-08-13 06:01:42 +08:00
|
|
|
for (uint32_t i = av->calls_tail + 1; i < friend_number; ++i) {
|
2018-01-29 05:30:39 +08:00
|
|
|
av->calls[i] = nullptr;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
call->prev = av->calls[av->calls_tail];
|
|
|
|
av->calls[av->calls_tail]->next = call;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
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-10-11 05:54:23 +08:00
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
av->calls[friend_number] = call;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-13 06:01:42 +08:00
|
|
|
RETURN:
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (error) {
|
2015-02-15 06:37:52 +08:00
|
|
|
*error = rc;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
return call;
|
2014-11-11 06:59:14 +08:00
|
|
|
}
|
2018-01-20 05:59:42 +08:00
|
|
|
|
2020-03-18 11:41:02 +08:00
|
|
|
static ToxAVCall *call_get(ToxAV *av, uint32_t friend_number)
|
2015-02-19 06:23:46 +08:00
|
|
|
{
|
2015-03-02 01:45:04 +08:00
|
|
|
/* Assumes mutex locked */
|
2018-01-29 05:30:39 +08:00
|
|
|
if (av->calls == nullptr || av->calls_tail < friend_number) {
|
|
|
|
return nullptr;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-02-21 08:07:22 +08:00
|
|
|
return av->calls[friend_number];
|
2015-02-19 06:23:46 +08:00
|
|
|
}
|
2018-01-20 05:59:42 +08:00
|
|
|
|
2020-03-18 11:41:02 +08:00
|
|
|
static ToxAVCall *call_remove(ToxAVCall *call)
|
2015-04-16 08:00:34 +08:00
|
|
|
{
|
2018-01-29 05:30:39 +08:00
|
|
|
if (call == nullptr) {
|
|
|
|
return nullptr;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-29 07:01:25 +08:00
|
|
|
uint32_t friend_number = call->friend_number;
|
2015-10-11 05:54:23 +08:00
|
|
|
ToxAV *av = call->av;
|
|
|
|
|
|
|
|
ToxAVCall *prev = call->prev;
|
|
|
|
ToxAVCall *next = call->next;
|
|
|
|
|
2015-08-09 17:57:39 +08:00
|
|
|
/* Set av call in msi to NULL in order to know if call if ToxAVCall is
|
|
|
|
* removed from the msi call.
|
|
|
|
*/
|
2015-10-23 21:59:21 +08:00
|
|
|
if (call->msi_call) {
|
2018-01-29 05:30:39 +08:00
|
|
|
call->msi_call->av_call = nullptr;
|
2015-10-23 21:59:21 +08:00
|
|
|
}
|
|
|
|
|
2020-03-28 18:58:00 +08:00
|
|
|
pthread_mutex_destroy(call->toxav_call_mutex);
|
2015-04-16 08:00:34 +08:00
|
|
|
free(call);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (prev) {
|
2015-04-16 08:00:34 +08:00
|
|
|
prev->next = next;
|
2016-09-01 02:12:19 +08:00
|
|
|
} else if (next) {
|
2015-04-29 07:01:25 +08:00
|
|
|
av->calls_head = next->friend_number;
|
2016-09-01 02:12:19 +08:00
|
|
|
} else {
|
|
|
|
goto CLEAR;
|
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (next) {
|
2015-04-16 08:00:34 +08:00
|
|
|
next->prev = prev;
|
2016-09-01 02:12:19 +08:00
|
|
|
} else if (prev) {
|
2015-04-29 07:01:25 +08:00
|
|
|
av->calls_tail = prev->friend_number;
|
2016-09-01 02:12:19 +08:00
|
|
|
} else {
|
|
|
|
goto CLEAR;
|
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
av->calls[friend_number] = nullptr;
|
2015-05-02 04:15:12 +08:00
|
|
|
return next;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-16 08:00:34 +08:00
|
|
|
CLEAR:
|
2018-08-13 06:01:42 +08:00
|
|
|
av->calls_head = 0;
|
|
|
|
av->calls_tail = 0;
|
2015-04-16 08:00:34 +08:00
|
|
|
free(av->calls);
|
2018-01-29 05:30:39 +08:00
|
|
|
av->calls = nullptr;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
return nullptr;
|
2015-04-16 08:00:34 +08:00
|
|
|
}
|
2018-01-20 05:59:42 +08:00
|
|
|
|
2020-03-18 11:41:02 +08:00
|
|
|
static bool call_prepare_transmission(ToxAVCall *call)
|
2015-02-15 06:37:52 +08:00
|
|
|
{
|
2015-03-02 01:45:04 +08:00
|
|
|
/* Assumes mutex locked */
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-29 05:30:39 +08:00
|
|
|
if (call == nullptr) {
|
2015-02-21 08:07:22 +08:00
|
|
|
return false;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
|
|
|
ToxAV *av = call->av;
|
|
|
|
|
2021-12-07 05:57:07 +08:00
|
|
|
if (av->acb == nullptr && av->vcb == nullptr) {
|
2015-02-20 07:23:38 +08:00
|
|
|
/* It makes no sense to have CSession without callbacks */
|
|
|
|
return false;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
if (call->active) {
|
2020-04-08 03:17:58 +08:00
|
|
|
LOGGER_WARNING(av->m->log, "Call already active!");
|
2015-02-15 06:37:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (create_recursive_mutex(call->mutex_audio) != 0) {
|
2015-03-29 08:10:34 +08:00
|
|
|
return false;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2016-09-01 02:12:19 +08:00
|
|
|
if (create_recursive_mutex(call->mutex_video) != 0) {
|
2015-04-29 07:01:25 +08:00
|
|
|
goto FAILURE_2;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
|
|
|
/* Prepare bwc */
|
2020-04-08 04:34:50 +08:00
|
|
|
call->bwc = bwc_new(av->m, av->tox, call->friend_number, callback_bwc, call, av->toxav_mono_time);
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2021-12-07 05:57:07 +08:00
|
|
|
if (call->bwc == nullptr) {
|
|
|
|
LOGGER_ERROR(av->m->log, "Failed to create new bwc");
|
|
|
|
goto FAILURE;
|
|
|
|
}
|
|
|
|
|
2020-05-03 08:09:06 +08:00
|
|
|
{ /* Prepare audio */
|
2020-04-07 21:56:29 +08:00
|
|
|
call->audio = ac_new(av->toxav_mono_time, av->m->log, av, call->friend_number, av->acb, av->acb_user_data);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2021-12-07 05:57:07 +08:00
|
|
|
if (call->audio == nullptr) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_ERROR(av->m->log, "Failed to create audio codec session");
|
2015-06-25 07:04:31 +08:00
|
|
|
goto FAILURE;
|
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2020-04-09 01:58:06 +08:00
|
|
|
call->audio_rtp = rtp_new(RTP_TYPE_AUDIO, av->m, av->tox, call->friend_number, call->bwc,
|
2018-08-12 18:55:20 +08:00
|
|
|
call->audio, ac_queue_message);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2021-12-07 05:57:07 +08:00
|
|
|
if (call->audio_rtp == nullptr) {
|
2018-08-13 06:01:42 +08:00
|
|
|
LOGGER_ERROR(av->m->log, "Failed to create audio rtp session");
|
2015-02-15 06:37:52 +08:00
|
|
|
goto FAILURE;
|
|
|
|
}
|
|
|
|
}
|
2021-12-07 05:57:07 +08:00
|
|
|
|
2020-05-03 08:09:06 +08:00
|
|
|
{ /* Prepare video */
|
2020-04-07 21:56:29 +08:00
|
|
|
call->video = vc_new(av->toxav_mono_time, av->m->log, av, call->friend_number, av->vcb, av->vcb_user_data);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2021-12-07 05:57:07 +08:00
|
|
|
if (call->video == nullptr) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_ERROR(av->m->log, "Failed to create video codec session");
|
2015-06-25 07:04:31 +08:00
|
|
|
goto FAILURE;
|
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2020-04-09 01:58:06 +08:00
|
|
|
call->video_rtp = rtp_new(RTP_TYPE_VIDEO, av->m, av->tox, call->friend_number, call->bwc,
|
2018-08-12 18:55:20 +08:00
|
|
|
call->video, vc_queue_message);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2021-12-07 05:57:07 +08:00
|
|
|
if (call->video_rtp == nullptr) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_ERROR(av->m->log, "Failed to create video rtp session");
|
2015-02-15 06:37:52 +08:00
|
|
|
goto FAILURE;
|
|
|
|
}
|
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
call->active = 1;
|
|
|
|
return true;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-02-23 06:41:40 +08:00
|
|
|
FAILURE:
|
2015-10-11 05:54:23 +08:00
|
|
|
bwc_kill(call->bwc);
|
2018-08-12 18:55:20 +08:00
|
|
|
rtp_kill(call->audio_rtp);
|
|
|
|
ac_kill(call->audio);
|
|
|
|
call->audio_rtp = nullptr;
|
|
|
|
call->audio = nullptr;
|
|
|
|
rtp_kill(call->video_rtp);
|
|
|
|
vc_kill(call->video);
|
|
|
|
call->video_rtp = nullptr;
|
|
|
|
call->video = nullptr;
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_destroy(call->mutex_video);
|
2018-09-22 02:05:58 +08:00
|
|
|
FAILURE_2:
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_destroy(call->mutex_audio);
|
2015-02-15 06:37:52 +08:00
|
|
|
return false;
|
|
|
|
}
|
2018-01-20 05:59:42 +08:00
|
|
|
|
2020-03-18 11:41:02 +08:00
|
|
|
static void call_kill_transmission(ToxAVCall *call)
|
2015-02-15 06:37:52 +08:00
|
|
|
{
|
2018-01-29 05:30:39 +08:00
|
|
|
if (call == nullptr || call->active == 0) {
|
2015-02-15 06:37:52 +08:00
|
|
|
return;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-02-15 06:37:52 +08:00
|
|
|
call->active = 0;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_lock(call->mutex_audio);
|
|
|
|
pthread_mutex_unlock(call->mutex_audio);
|
|
|
|
pthread_mutex_lock(call->mutex_video);
|
|
|
|
pthread_mutex_unlock(call->mutex_video);
|
2020-03-28 18:58:00 +08:00
|
|
|
pthread_mutex_lock(call->toxav_call_mutex);
|
|
|
|
pthread_mutex_unlock(call->toxav_call_mutex);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
|
|
|
bwc_kill(call->bwc);
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2018-08-12 18:55:20 +08:00
|
|
|
rtp_kill(call->audio_rtp);
|
|
|
|
ac_kill(call->audio);
|
|
|
|
call->audio_rtp = nullptr;
|
|
|
|
call->audio = nullptr;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-12 18:55:20 +08:00
|
|
|
rtp_kill(call->video_rtp);
|
|
|
|
vc_kill(call->video);
|
|
|
|
call->video_rtp = nullptr;
|
|
|
|
call->video = nullptr;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-22 08:09:37 +08:00
|
|
|
pthread_mutex_destroy(call->mutex_audio);
|
|
|
|
pthread_mutex_destroy(call->mutex_video);
|
2015-02-21 08:07:22 +08:00
|
|
|
}
|