Current progress

This commit is contained in:
mannol 2015-01-24 23:29:54 +01:00
parent e57fb8c12e
commit 1450c22d01
11 changed files with 1649 additions and 291 deletions

View File

@ -31,6 +31,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include <assert.h>
#include <time.h>
@ -39,6 +40,9 @@
#include "rtp.h"
#include "codec.h"
#define DEFAULT_JBUF 6
/* Good quality encode. */
#define MAX_ENCODE_TIME_US VPX_DL_GOOD_QUALITY
#define MAX_DECODE_TIME_US 0
@ -62,12 +66,12 @@ typedef struct {
Payload **packets;
} PayloadBuffer;
static _Bool buffer_full(const PayloadBuffer *b)
static bool buffer_full(const PayloadBuffer *b)
{
return (b->end + 1) % b->size == b->start;
}
static _Bool buffer_empty(const PayloadBuffer *b)
static bool buffer_empty(const PayloadBuffer *b)
{
return b->end == b->start;
}
@ -121,7 +125,7 @@ static void buffer_free(PayloadBuffer *b)
}
/* JITTER BUFFER WORK */
typedef struct _JitterBuffer {
typedef struct {
RTPMessage **queue;
uint32_t size;
uint32_t capacity;
@ -225,99 +229,9 @@ static RTPMessage *jbuf_read(JitterBuffer *q, int32_t *success)
return NULL;
}
static int init_video_decoder(CSSession *cs)
{
int rc = vpx_codec_dec_init_ver(&cs->v_decoder, VIDEO_CODEC_DECODER_INTERFACE, NULL, 0, VPX_DECODER_ABI_VERSION);
if ( rc != VPX_CODEC_OK) {
LOGGER_ERROR("Init video_decoder failed: %s", vpx_codec_err_to_string(rc));
return -1;
}
return 0;
}
static int init_audio_decoder(CSSession *cs)
{
int rc;
cs->audio_decoder = opus_decoder_create(cs->audio_decoder_sample_rate, cs->audio_decoder_channels, &rc );
if ( rc != OPUS_OK ) {
LOGGER_ERROR("Error while starting audio decoder: %s", opus_strerror(rc));
return -1;
}
return 0;
}
static int init_video_encoder(CSSession *cs, uint16_t max_width, uint16_t max_height, uint32_t video_bitrate)
{
vpx_codec_enc_cfg_t cfg;
int rc = vpx_codec_enc_config_default(VIDEO_CODEC_ENCODER_INTERFACE, &cfg, 0);
if (rc != VPX_CODEC_OK) {
LOGGER_ERROR("Failed to get config: %s", vpx_codec_err_to_string(rc));
return -1;
}
cfg.rc_target_bitrate = video_bitrate;
cfg.g_w = max_width;
cfg.g_h = max_height;
cfg.g_pass = VPX_RC_ONE_PASS;
cfg.g_error_resilient = VPX_ERROR_RESILIENT_DEFAULT | VPX_ERROR_RESILIENT_PARTITIONS;
cfg.g_lag_in_frames = 0;
cfg.kf_min_dist = 0;
cfg.kf_max_dist = 48;
cfg.kf_mode = VPX_KF_AUTO;
rc = vpx_codec_enc_init_ver(&cs->v_encoder, VIDEO_CODEC_ENCODER_INTERFACE, &cfg, 0, VPX_ENCODER_ABI_VERSION);
if ( rc != VPX_CODEC_OK) {
LOGGER_ERROR("Failed to initialize encoder: %s", vpx_codec_err_to_string(rc));
return -1;
}
rc = vpx_codec_control(&cs->v_encoder, VP8E_SET_CPUUSED, 8);
if ( rc != VPX_CODEC_OK) {
LOGGER_ERROR("Failed to set encoder control setting: %s", vpx_codec_err_to_string(rc));
return -1;
}
cs->max_width = max_width;
cs->max_height = max_height;
cs->video_bitrate = video_bitrate;
return 0;
}
static int init_audio_encoder(CSSession *cs)
{
int rc = OPUS_OK;
cs->audio_encoder = opus_encoder_create(cs->audio_encoder_sample_rate,
cs->audio_encoder_channels, OPUS_APPLICATION_AUDIO, &rc);
if ( rc != OPUS_OK ) {
LOGGER_ERROR("Error while starting audio encoder: %s", opus_strerror(rc));
return -1;
}
rc = opus_encoder_ctl(cs->audio_encoder, OPUS_SET_BITRATE(cs->audio_encoder_bitrate));
if ( rc != OPUS_OK ) {
LOGGER_ERROR("Error while setting encoder ctl: %s", opus_strerror(rc));
return -1;
}
rc = opus_encoder_ctl(cs->audio_encoder, OPUS_SET_COMPLEXITY(10));
if ( rc != OPUS_OK ) {
LOGGER_ERROR("Error while setting encoder ctl: %s", opus_strerror(rc));
return -1;
}
return 0;
}
/* PUBLIC */
int cs_split_video_payload(CSSession *cs, const uint8_t *payload, uint16_t length)
@ -406,17 +320,17 @@ void cs_do(CSSession *cs)
/* Leave space for (possibly) other thread to queue more data after we read it here */
pthread_mutex_unlock(cs->queue_mutex);
rc = vpx_codec_decode(&cs->v_decoder, p->data, p->size, NULL, MAX_DECODE_TIME_US);
rc = vpx_codec_decode(cs->v_decoder, p->data, p->size, NULL, MAX_DECODE_TIME_US);
free(p);
if (rc != VPX_CODEC_OK) {
LOGGER_ERROR("Error decoding video: %s", vpx_codec_err_to_string(rc));
} else {
vpx_codec_iter_t iter = NULL;
vpx_image_t *dest = vpx_codec_get_frame(&cs->v_decoder, &iter);
vpx_image_t *dest = vpx_codec_get_frame(cs->v_decoder, &iter);
/* Play decoded images */
for (; dest; dest = vpx_codec_get_frame(&cs->v_decoder, &iter)) {
for (; dest; dest = vpx_codec_get_frame(cs->v_decoder, &iter)) {
if (cs->vcb.first)
cs->vcb.first(cs->agent, cs->call_idx, dest, cs->vcb.second);
@ -430,13 +344,17 @@ void cs_do(CSSession *cs)
pthread_mutex_unlock(cs->queue_mutex);
}
int cs_set_video_encoder_resolution(CSSession *cs, uint16_t width, uint16_t height)
int cs_set_sending_video_resolution(CSSession *cs, uint16_t width, uint16_t height)
{
vpx_codec_enc_cfg_t cfg = *cs->v_encoder.config.enc;
if (!cs->v_encoding)
return -1;
/* TODO FIXME reference is safe? */
vpx_codec_enc_cfg_t cfg = *cs->v_encoder[0].config.enc;
if (cfg.g_w == width && cfg.g_h == height)
return 0;
/*
if (width * height > cs->max_width * cs->max_height) {
vpx_codec_ctx_t v_encoder = cs->v_encoder;
@ -447,12 +365,12 @@ int cs_set_video_encoder_resolution(CSSession *cs, uint16_t width, uint16_t heig
vpx_codec_destroy(&v_encoder);
return 0;
}
}*/
LOGGER_DEBUG("New video resolution: %u %u", width, height);
cfg.g_w = width;
cfg.g_h = height;
int rc = vpx_codec_enc_config_set(&cs->v_encoder, &cfg);
int rc = vpx_codec_enc_config_set(cs->v_encoder, &cfg);
if ( rc != VPX_CODEC_OK) {
LOGGER_ERROR("Failed to set encoder control setting: %s", vpx_codec_err_to_string(rc));
@ -462,27 +380,107 @@ int cs_set_video_encoder_resolution(CSSession *cs, uint16_t width, uint16_t heig
return 0;
}
int cs_set_video_encoder_bitrate(CSSession *cs, uint32_t video_bitrate)
int cs_set_sending_video_bitrate(CSSession *cs, uint32_t bitrate)
{
vpx_codec_enc_cfg_t cfg = *cs->v_encoder.config.enc;
if (cfg.rc_target_bitrate == video_bitrate)
if (!cs->v_encoding)
return -1;
/* TODO FIXME reference is safe? */
vpx_codec_enc_cfg_t cfg = *cs->v_encoder[0].config.enc;
if (cfg.rc_target_bitrate == bitrate)
return 0;
LOGGER_DEBUG("New video bitrate: %u", video_bitrate);
cfg.rc_target_bitrate = video_bitrate;
int rc = vpx_codec_enc_config_set(&cs->v_encoder, &cfg);
cfg.rc_target_bitrate = bitrate;
int rc = vpx_codec_enc_config_set(cs->v_encoder, &cfg);
if ( rc != VPX_CODEC_OK) {
LOGGER_ERROR("Failed to set encoder control setting: %s", vpx_codec_err_to_string(rc));
return cs_ErrorSettingVideoBitrate;
}
cs->video_bitrate = video_bitrate;
return 0;
}
CSSession *cs_new(const ToxAvCSettings *cs_self, const ToxAvCSettings *cs_peer, uint32_t jbuf_size, int has_video)
int cs_set_sending_audio_bitrate(CSSession *cs, int32_t rate)
{
if (cs->audio_encoder == NULL)
return -1;
int rc = opus_encoder_ctl(cs->audio_encoder, OPUS_SET_BITRATE(rate));
if ( rc != OPUS_OK ) {
LOGGER_ERROR("Error while setting encoder ctl: %s", opus_strerror(rc));
return -1;
}
return 0;
}
int cs_set_sending_audio_sampling_rate(CSSession* cs, int32_t rate)
{
/* TODO Find a better way? */
if (cs->audio_encoder == NULL)
return -1;
int rc = OPUS_OK;
int last_rate = 0;
rc = opus_encoder_ctl(cs->audio_encoder, OPUS_SET_BITRATE(&last_rate));
if ( rc != OPUS_OK ) {
LOGGER_ERROR("Error while getting encoder ctl: %s", opus_strerror(rc));
return -1;
}
if (rate == last_rate)
return 0;
OpusEncoder* new_enc = opus_encoder_create(rate, cs->channels, OPUS_APPLICATION_AUDIO, &rc);
if ( rc != OPUS_OK ) {
LOGGER_ERROR("Error while starting audio encoder: %s", opus_strerror(rc));
return -1;
}
opus_encoder_destroy(cs->audio_encoder);
cs->audio_encoder = new_enc;
return 0;
}
int cs_set_sending_audio_channels(CSSession* cs, int32_t count)
{
/* TODO Find a better way? */
if (cs->audio_encoder == NULL)
return -1;
if (cs->channels == count)
return 0;
int rc = OPUS_OK;
int bitrate = 0;
rc = opus_encoder_ctl(cs->audio_encoder, OPUS_SET_BITRATE(&bitrate));
if ( rc != OPUS_OK ) {
LOGGER_ERROR("Error while getting encoder ctl: %s", opus_strerror(rc));
return -1;
}
cs->channels = count;
OpusEncoder* new_enc = opus_encoder_create(bitrate, cs->channels, OPUS_APPLICATION_AUDIO, &rc);
if ( rc != OPUS_OK ) {
LOGGER_ERROR("Error while starting audio encoder: %s", opus_strerror(rc));
return -1;
}
opus_encoder_destroy(cs->audio_encoder);
cs->audio_encoder = new_enc;
return 0;
}
CSSession *cs_new(uint32_t s_audio_b, uint32_t p_audio_b, uint32_t s_video_b, uint32_t p_video_b)
{
CSSession *cs = calloc(sizeof(CSSession), 1);
@ -491,75 +489,40 @@ CSSession *cs_new(const ToxAvCSettings *cs_self, const ToxAvCSettings *cs_peer,
return NULL;
}
if (create_recursive_mutex(cs->queue_mutex) != 0) {
LOGGER_WARNING("Failed to create recursive mutex!");
free(cs);
return NULL;
/* TODO this has to be exchanged in msi */
cs->max_video_frame_size = MAX_VIDEOFRAME_SIZE;
cs->video_frame_piece_size = VIDEOFRAME_PIECE_SIZE;
if (s_audio_b > 0 && 0 != cs_enable_audio_sending(cs, s_audio_b)) { /* Sending audio enabled */
LOGGER_WARNING("Failed to enable audio sending!");
goto FAILURE;
}
if ( !(cs->j_buf = jbuf_new(jbuf_size)) ) {
LOGGER_WARNING("Jitter buffer creaton failed!");
goto error;
if (p_audio_b > 0 && 0 != cs_enable_audio_receiving(cs)) { /* Receiving audio enabled */
LOGGER_WARNING("Failed to enable audio receiving!");
goto FAILURE;
}
cs->audio_encoder_bitrate = cs_self->audio_bitrate;
cs->audio_encoder_sample_rate = cs_self->audio_sample_rate;
cs->audio_encoder_channels = cs_self->audio_channels;
cs->audio_encoder_frame_duration = cs_self->audio_frame_duration;
cs->audio_decoder_bitrate = cs_peer->audio_bitrate;
cs->audio_decoder_sample_rate = cs_peer->audio_sample_rate;
cs->audio_decoder_channels = cs_peer->audio_channels;
cs->audio_decoder_frame_duration = cs_peer->audio_frame_duration;
cs->capabilities |= ( 0 == init_audio_encoder(cs) ) ? cs_AudioEncoding : 0;
cs->capabilities |= ( 0 == init_audio_decoder(cs) ) ? cs_AudioDecoding : 0;
if ( !(cs->capabilities & cs_AudioEncoding) || !(cs->capabilities & cs_AudioDecoding) ) goto error;
if ((cs->support_video = has_video)) {
cs->max_video_frame_size = MAX_VIDEOFRAME_SIZE;
cs->video_frame_piece_size = VIDEOFRAME_PIECE_SIZE;
cs->capabilities |= ( 0 == init_video_encoder(cs, cs_self->max_video_width,
cs_self->max_video_height, cs_self->video_bitrate) ) ? cs_VideoEncoding : 0;
cs->capabilities |= ( 0 == init_video_decoder(cs) ) ? cs_VideoDecoding : 0;
if ( !(cs->capabilities & cs_VideoEncoding) || !(cs->capabilities & cs_VideoDecoding) ) goto error;
if ( !(cs->frame_buf = calloc(cs->max_video_frame_size, 1)) ) goto error;
if ( !(cs->split_video_frame = calloc(cs->video_frame_piece_size + VIDEOFRAME_HEADER_SIZE, 1)) )
goto error;
if ( !(cs->vbuf_raw = buffer_new(VIDEO_DECODE_BUFFER_SIZE)) ) goto error;
if (s_video_b > 0 && 0 != cs_enable_video_sending(cs, s_video_b)) { /* Sending video enabled */
LOGGER_WARNING("Failed to enable video sending!");
goto FAILURE;
}
if (p_video_b > 0 && 0 != cs_enable_video_receiving(cs)) { /* Receiving video enabled */
LOGGER_WARNING("Failed to enable video receiving!");
goto FAILURE;
}
return cs;
error:
FAILURE:
LOGGER_WARNING("Error initializing codec session! Application might misbehave!");
pthread_mutex_destroy(cs->queue_mutex);
if ( cs->audio_encoder ) opus_encoder_destroy(cs->audio_encoder);
if ( cs->audio_decoder ) opus_decoder_destroy(cs->audio_decoder);
if (has_video) {
if ( cs->capabilities & cs_VideoDecoding ) vpx_codec_destroy(&cs->v_decoder);
if ( cs->capabilities & cs_VideoEncoding ) vpx_codec_destroy(&cs->v_encoder);
buffer_free(cs->vbuf_raw);
free(cs->frame_buf);
free(cs->split_video_frame);
}
jbuf_free(cs->j_buf);
cs_disable_audio_sending(cs);
cs_disable_audio_receiving(cs);
cs_disable_video_sending(cs);
cs_disable_video_receiving(cs);
free(cs);
return NULL;
@ -567,32 +530,226 @@ error:
void cs_kill(CSSession *cs)
{
if (!cs) return;
if (!cs)
return;
/* queue_message will not be called since it's unregistered before cs_kill is called */
pthread_mutex_destroy(cs->queue_mutex);
if ( cs->audio_encoder )
opus_encoder_destroy(cs->audio_encoder);
if ( cs->audio_decoder )
opus_decoder_destroy(cs->audio_decoder);
if ( cs->capabilities & cs_VideoDecoding )
vpx_codec_destroy(&cs->v_decoder);
if ( cs->capabilities & cs_VideoEncoding )
vpx_codec_destroy(&cs->v_encoder);
jbuf_free(cs->j_buf);
buffer_free(cs->vbuf_raw);
free(cs->frame_buf);
/* NOTE: queue_message() will not be called since
* the callback is unregistered before cs_kill is called.
*/
cs_disable_audio_sending(cs);
cs_disable_audio_receiving(cs);
cs_disable_video_sending(cs);
cs_disable_video_receiving(cs);
LOGGER_DEBUG("Terminated codec state: %p", cs);
free(cs);
}
int cs_enable_audio_sending(CSSession* cs, uint32_t bitrate)
{
if (cs->audio_encoder)
return 0;
/**
* Encoder is initialized with default values. These values (Sampling rate, channel count)
* change on the fly from toxav.
*/
int rc = OPUS_OK;
cs->audio_encoder = opus_encoder_create(48000, 2, OPUS_APPLICATION_AUDIO, &rc);
if ( rc != OPUS_OK ) {
LOGGER_ERROR("Error while starting audio encoder: %s", opus_strerror(rc));
return -1;
}
rc = opus_encoder_ctl(cs->audio_encoder, OPUS_SET_BITRATE(bitrate));
if ( rc != OPUS_OK ) {
LOGGER_ERROR("Error while setting encoder ctl: %s", opus_strerror(rc));
goto FAILURE;
}
rc = opus_encoder_ctl(cs->audio_encoder, OPUS_SET_COMPLEXITY(10));
if ( rc != OPUS_OK ) {
LOGGER_ERROR("Error while setting encoder ctl: %s", opus_strerror(rc));
goto FAILURE;
}
cs->channels = 2;
return 0;
FAILURE:
cs_disable_audio_sending(cs);
return -1;
}
int cs_enable_audio_receiving(CSSession* cs)
{
if (cs->audio_decoder)
return 0;
/**
* Decoder is initialized with default values. These values (Sampling rate, channel count)
* change on the fly from toxav.
*/
int rc;
cs->audio_decoder = opus_decoder_create(48000, 2, &rc );
if ( rc != OPUS_OK ) {
LOGGER_ERROR("Error while starting audio decoder: %s", opus_strerror(rc));
return -1;
}
if ( !(cs->j_buf = jbuf_new(DEFAULT_JBUF)) ) {
LOGGER_WARNING("Jitter buffer creaton failed!");
opus_decoder_destroy(cs->audio_decoder);
cs->audio_decoder = NULL;
return -1;
}
return 0;
}
int cs_enable_video_sending(CSSession* cs, uint32_t bitrate)
{
if (cs->v_encoding)
return 0;
vpx_codec_enc_cfg_t cfg;
int rc = vpx_codec_enc_config_default(VIDEO_CODEC_ENCODER_INTERFACE, &cfg, 0);
if (rc != VPX_CODEC_OK) {
LOGGER_ERROR("Failed to get config: %s", vpx_codec_err_to_string(rc));
return -1;
}
rc = vpx_codec_enc_init_ver(cs->v_encoder, VIDEO_CODEC_ENCODER_INTERFACE, &cfg, 0,
VPX_ENCODER_ABI_VERSION);
if ( rc != VPX_CODEC_OK) {
LOGGER_ERROR("Failed to initialize encoder: %s", vpx_codec_err_to_string(rc));
return -1;
}
/* So that we can use cs_disable_video_sending to clean up */
cs->v_encoding = true;
if ( !(cs->split_video_frame = calloc(cs->video_frame_piece_size + VIDEOFRAME_HEADER_SIZE, 1)) )
goto FAILURE;
cfg.rc_target_bitrate = bitrate;
cfg.g_w = 800;
cfg.g_h = 600;
cfg.g_pass = VPX_RC_ONE_PASS;
cfg.g_error_resilient = VPX_ERROR_RESILIENT_DEFAULT | VPX_ERROR_RESILIENT_PARTITIONS;
cfg.g_lag_in_frames = 0;
cfg.kf_min_dist = 0;
cfg.kf_max_dist = 48;
cfg.kf_mode = VPX_KF_AUTO;
rc = vpx_codec_control(cs->v_encoder, VP8E_SET_CPUUSED, 8);
if ( rc != VPX_CODEC_OK) {
LOGGER_ERROR("Failed to set encoder control setting: %s", vpx_codec_err_to_string(rc));
goto FAILURE;
}
return 0;
FAILURE:
cs_disable_video_sending(cs);
return -1;
}
int cs_enable_video_receiving(CSSession* cs)
{
if (cs->v_decoding)
return 0;
if (create_recursive_mutex(cs->queue_mutex) != 0) {
LOGGER_WARNING("Failed to create recursive mutex!");
return -1;
}
int rc = vpx_codec_dec_init_ver(cs->v_decoder, VIDEO_CODEC_DECODER_INTERFACE,
NULL, 0, VPX_DECODER_ABI_VERSION);
if ( rc != VPX_CODEC_OK) {
LOGGER_ERROR("Init video_decoder failed: %s", vpx_codec_err_to_string(rc));
pthread_mutex_destroy(cs->queue_mutex);
return -1;
}
/* So that we can use cs_disable_video_sending to clean up */
cs->v_decoding = true;
if ( !(cs->frame_buf = calloc(cs->max_video_frame_size, 1)) )
goto FAILURE;
if ( !(cs->vbuf_raw = buffer_new(VIDEO_DECODE_BUFFER_SIZE)) )
goto FAILURE;
return 0;
FAILURE:
cs_disable_video_receiving(cs);
return -1;
}
void cs_disable_audio_sending(CSSession* cs)
{
if ( cs->audio_encoder ) {
opus_encoder_destroy(cs->audio_encoder);
cs->audio_encoder = NULL;
cs->channels = 0;
}
}
void cs_disable_audio_receiving(CSSession* cs)
{
if ( cs->audio_decoder ) {
opus_decoder_destroy(cs->audio_decoder);
cs->audio_decoder = NULL;
jbuf_free(cs->j_buf);
cs->j_buf = NULL;
}
}
void cs_disable_video_sending(CSSession* cs)
{
if (cs->v_encoding) {
cs->v_encoding = false;
free(cs->split_video_frame);
cs->split_video_frame = NULL;
vpx_codec_destroy(cs->v_encoder);
}
}
void cs_disable_video_receiving(CSSession* cs)
{
if (cs->v_decoding) {
cs->v_decoding = false;
buffer_free(cs->vbuf_raw);
cs->vbuf_raw = NULL;
free(cs->frame_buf);
cs->frame_buf = NULL;
vpx_codec_destroy(cs->v_decoder);
pthread_mutex_destroy(cs->queue_mutex);
}
}

View File

@ -75,18 +75,16 @@ typedef struct _CSSession {
*
*
*/
int support_video;
/* video encoding */
vpx_codec_ctx_t v_encoder;
vpx_codec_ctx_t v_encoder[1];
bool v_encoding;
uint32_t frame_counter;
/* video decoding */
vpx_codec_ctx_t v_decoder;
int max_width;
int max_height;
unsigned int video_bitrate;
vpx_codec_ctx_t v_decoder[1];
bool v_decoding;
void *vbuf_raw; /* Un-decoded data */
/* Data handling */
uint8_t *frame_buf; /* buffer for split video payloads */
@ -112,18 +110,10 @@ typedef struct _CSSession {
/* audio encoding */
OpusEncoder *audio_encoder;
int audio_encoder_bitrate;
int audio_encoder_sample_rate;
int audio_encoder_frame_duration;
int audio_encoder_channels;
int32_t channels;
/* audio decoding */
OpusDecoder *audio_decoder;
int audio_decoder_bitrate;
int audio_decoder_sample_rate;
int audio_decoder_frame_duration;
int audio_decoder_channels;
struct _JitterBuffer *j_buf;
@ -138,25 +128,16 @@ typedef struct _CSSession {
*
*/
uint64_t capabilities; /* supports*/
/* Callbacks */
PAIR(CSAudioCallback, void *) acb;
PAIR(CSVideoCallback, void *) vcb;
/* Buffering */
void *vbuf_raw; /* Un-decoded data */
pthread_mutex_t queue_mutex[1];
void *agent; /* Pointer to ToxAv */
int32_t call_idx;
pthread_mutex_t queue_mutex[1];
} CSSession;
/* Make sure to be called BEFORE corresponding rtp_new */
CSSession *cs_new(const ToxAvCSettings *cs_self, const ToxAvCSettings *cs_peer, uint32_t jbuf_size, int has_video);
/* Make sure to be called AFTER corresponding rtp_kill */
void cs_kill(CSSession *cs);
int cs_split_video_payload(CSSession *cs, const uint8_t *payload, uint16_t length);
const uint8_t *cs_get_split_video_frame(CSSession *cs, uint16_t *size);
@ -165,11 +146,35 @@ const uint8_t *cs_get_split_video_frame(CSSession *cs, uint16_t *size);
*/
void cs_do(CSSession *cs);
/**
* Reconfigure video settings; return 0 on success or -1 on failure.
*/
int cs_set_sending_video_resolution(CSSession *cs, uint16_t width, uint16_t height);
int cs_set_sending_video_bitrate(CSSession *cs, uint32_t bitrate);
/* Reconfigure video encoder; return 0 on success or -1 on failure. */
int cs_set_video_encoder_resolution(CSSession *cs, uint16_t width, uint16_t height);
int cs_set_video_encoder_bitrate(CSSession *cs, uint32_t video_bitrate);
int cs_set_sending_audio_bitrate(CSSession* cs, int32_t rate);
/* NOTE: Try not to call these a lot */
int cs_set_sending_audio_sampling_rate(CSSession* cs, int32_t rate);
int cs_set_sending_audio_channels(CSSession* cs, int32_t count);
/**
* Make sure to be called BEFORE corresponding rtp_new
*/
CSSession *cs_new(uint32_t s_audio_b, uint32_t p_audio_b, uint32_t s_video_b, uint32_t p_video_b);
/**
* Make sure to be called AFTER corresponding rtp_kill
*/
void cs_kill(CSSession *cs);
int cs_enable_audio_sending(CSSession* cs, uint32_t bitrate);
int cs_enable_audio_receiving(CSSession* cs);
int cs_enable_video_sending(CSSession* cs, uint32_t bitrate);
int cs_enable_video_receiving(CSSession* cs);
void cs_disable_audio_sending(CSSession* cs);
void cs_disable_audio_receiving(CSSession* cs);
void cs_disable_video_sending(CSSession* cs);
void cs_disable_video_receiving(CSSession* cs);
/* Internal. Called from rtp_handle_message */
void queue_message(RTPSession *session, RTPMessage *msg);

View File

@ -840,7 +840,7 @@ static int handle_recv_invite ( MSISession *session, MSICall *call, MSIMessage *
if ( call ) {
if ( call->peers[0] == (uint32_t)msg->friend_id ) {
if (call->state == msi_CallInviting) {
if (call->state == msi_CallRequesting) {
/* The glare case. A calls B when at the same time
* B calls A. Who has advantage is set bey calculating
* 'bigger' Call id and then that call id is being used in
@ -898,7 +898,7 @@ static int handle_recv_invite ( MSISession *session, MSICall *call, MSIMessage *
}
memcpy ( call->id, msg->callid.value, sizeof(msg->callid.value) );
call->state = msi_CallStarting;
call->state = msi_CallRequested;
add_peer( call, msg->friend_id);
flush_peer_csettings ( call, msg, 0 );
@ -1009,7 +1009,7 @@ static int handle_recv_starting ( MSISession *session, MSICall *call, MSIMessage
invoke_callback(session, call->call_idx, msi_OnSelfCSChange);
} else if ( call->state == msi_CallInviting ) {
} else if ( call->state == msi_CallRequesting ) {
LOGGER_DEBUG("Session: %p Handling 'starting' on call: %d", session, call->call_idx );
call->state = msi_CallActive;
@ -1344,7 +1344,7 @@ int msi_invite ( MSISession *session,
send_message ( session, call, msg_invite, friend_id );
free( msg_invite );
call->state = msi_CallInviting;
call->state = msi_CallRequesting;
call->request_timer_id = timer_alloc ( session, handle_timeout, call->call_idx, m_deftout );
@ -1402,7 +1402,7 @@ int msi_answer ( MSISession *session, int32_t call_index, const MSICSettings *cs
return msi_ErrorNoCall;
}
if ( session->calls[call_index]->state != msi_CallStarting ) {
if ( session->calls[call_index]->state != msi_CallRequested ) {
LOGGER_ERROR("Call is in invalid state!");
pthread_mutex_unlock(session->mutex);
return msi_ErrorInvalidState;
@ -1434,7 +1434,7 @@ int msi_cancel ( MSISession *session, int32_t call_index, uint32_t peer, const c
return msi_ErrorNoCall;
}
if ( session->calls[call_index]->state != msi_CallInviting ) {
if ( session->calls[call_index]->state != msi_CallRequesting ) {
LOGGER_ERROR("Call is in invalid state!");
pthread_mutex_unlock(session->mutex);
return msi_ErrorInvalidState;
@ -1477,7 +1477,7 @@ int msi_reject ( MSISession *session, int32_t call_index, const char *reason )
return msi_ErrorNoCall;
}
if ( session->calls[call_index]->state != msi_CallStarting ) {
if ( session->calls[call_index]->state != msi_CallRequested ) {
LOGGER_ERROR("Call is in invalid state!");
pthread_mutex_unlock(session->mutex);
return msi_ErrorInvalidState;

View File

@ -45,8 +45,8 @@ typedef enum {
* Call state identifiers.
*/
typedef enum {
msi_CallInviting, /* when sending call invite */
msi_CallStarting, /* when getting call invite */
msi_CallRequesting, /* when sending call invite */
msi_CallRequested, /* when getting call invite */
msi_CallActive,
msi_CallHold,
msi_CallOver

View File

@ -422,44 +422,6 @@ RTPMessage *rtp_new_message ( RTPSession *session, const uint8_t *data, uint32_t
int rtp_send_msg ( RTPSession *session, Messenger *messenger, const uint8_t *data, uint16_t length )
{
RTPMessage *msg = rtp_new_message (session, data, length);
if ( !msg ) return -1;
if ( -1 == send_custom_lossy_packet(messenger, session->dest, msg->data, msg->length) ) {
LOGGER_WARNING("Failed to send full packet (len: %d)! std error: %s", length, strerror(errno));
rtp_free_msg ( session, msg );
return rtp_ErrorSending;
}
/* Set sequ number */
session->sequnum = session->sequnum >= MAX_SEQU_NUM ? 0 : session->sequnum + 1;
rtp_free_msg ( session, msg );
return 0;
}
void rtp_free_msg ( RTPSession *session, RTPMessage *msg )
{
if ( !session ) {
if ( msg->ext_header ) {
free ( msg->ext_header->table );
free ( msg->ext_header );
}
} else {
if ( msg->ext_header && session->ext_header != msg->ext_header ) {
free ( msg->ext_header->table );
free ( msg->ext_header );
}
}
free ( msg->header );
free ( msg );
}
RTPSession *rtp_new ( int payload_type, Messenger *messenger, int friend_num )
{
RTPSession *retu = calloc(1, sizeof(RTPSession));
@ -469,16 +431,10 @@ RTPSession *rtp_new ( int payload_type, Messenger *messenger, int friend_num )
return NULL;
}
if ( -1 == custom_lossy_packet_registerhandler(messenger, friend_num, payload_type, rtp_handle_packet, retu)) {
LOGGER_ERROR("Error setting custom register handler for rtp session");
free(retu);
return NULL;
}
LOGGER_DEBUG("Registered packet handler: pt: %d; fid: %d", payload_type, friend_num);
retu->version = RTP_VERSION; /* It's always 2 */
retu->padding = 0; /* If some additional data is needed about the packet */
retu->version = RTP_VERSION; /* It's always 2 */
retu->padding = 0; /* If some additional data is needed about the packet */
retu->extension = 0; /* If extension to header is needed */
retu->cc = 1; /* Amount of contributors */
retu->csrc = NULL; /* Container */
@ -498,23 +454,24 @@ RTPSession *rtp_new ( int payload_type, Messenger *messenger, int friend_num )
free(retu);
return NULL;
}
retu->csrc[0] = retu->ssrc; /* Set my ssrc to the list receive */
/* Also set payload type as prefix */
retu->prefix = payload_type;
retu->m = messenger;
/*
*
*/
return retu;
}
void rtp_kill ( RTPSession *session, Messenger *messenger )
void rtp_kill ( RTPSession *session )
{
if ( !session ) return;
custom_lossy_packet_registerhandler(messenger, session->dest, session->prefix, NULL, NULL);
custom_lossy_packet_registerhandler(session->m, session->dest, session->prefix, NULL, NULL);
free ( session->ext_header );
free ( session->csrc );
@ -523,5 +480,48 @@ void rtp_kill ( RTPSession *session, Messenger *messenger )
/* And finally free session */
free ( session );
}
int rtp_register_for_receiving(RTPSession* session)
{
return custom_lossy_packet_registerhandler(session->m, session->dest, session->prefix,
rtp_handle_packet, session);
}
int rtp_send_msg ( RTPSession *session, Messenger *messenger, const uint8_t *data, uint16_t length )
{
RTPMessage *msg = rtp_new_message (session, data, length);
if ( !msg ) return -1;
if ( -1 == send_custom_lossy_packet(messenger, session->dest, msg->data, msg->length) ) {
LOGGER_WARNING("Failed to send full packet (len: %d)! std error: %s", length, strerror(errno));
rtp_free_msg ( session, msg );
return rtp_ErrorSending;
}
/* Set sequ number */
session->sequnum = session->sequnum >= MAX_SEQU_NUM ? 0 : session->sequnum + 1;
rtp_free_msg ( session, msg );
return 0;
}
void rtp_free_msg ( RTPSession *session, RTPMessage *msg )
{
if ( !session ) {
if ( msg->ext_header ) {
free ( msg->ext_header->table );
free ( msg->ext_header );
}
} else {
if ( msg->ext_header && session->ext_header != msg->ext_header ) {
free ( msg->ext_header->table );
free ( msg->ext_header );
}
}
free ( msg->header );
free ( msg );
}

View File

@ -99,6 +99,7 @@ typedef struct _RTPSession {
int dest;
struct _CSSession *cs;
Messenger* m;
} RTPSession;
@ -110,7 +111,12 @@ RTPSession *rtp_new ( int payload_type, Messenger *messenger, int friend_num );
/**
* Terminate the session.
*/
void rtp_kill ( RTPSession *session, Messenger *messenger );
void rtp_kill ( RTPSession* session );
/**
* By default rtp is not in receiving state
*/
int rtp_register_for_receiving (RTPSession *session);
/**
* Sends msg to _RTPSession::dest

View File

@ -460,7 +460,7 @@ int toxav_prepare_video_frame ( ToxAv *av, int32_t call_index, uint8_t *dest, in
return av_ErrorInvalidState;
}
if (cs_set_video_encoder_resolution(call->cs, input->d_w, input->d_h) < 0) {
if (cs_set_sending_video_resolution(call->cs, input->d_w, input->d_h) < 0) {
pthread_mutex_unlock(call->mutex_control);
return av_ErrorSettingVideoResolution;
}
@ -468,7 +468,7 @@ int toxav_prepare_video_frame ( ToxAv *av, int32_t call_index, uint8_t *dest, in
pthread_mutex_lock(call->mutex_encoding_video);
pthread_mutex_unlock(call->mutex_control);
int rc = vpx_codec_encode(&call->cs->v_encoder, input, call->cs->frame_counter, 1, 0, MAX_ENCODE_TIME_US);
int rc = vpx_codec_encode(call->cs->v_encoder, input, call->cs->frame_counter, 1, 0, MAX_ENCODE_TIME_US);
if ( rc != VPX_CODEC_OK) {
LOGGER_ERROR("Could not encode video frame: %s\n", vpx_codec_err_to_string(rc));
@ -482,7 +482,7 @@ int toxav_prepare_video_frame ( ToxAv *av, int32_t call_index, uint8_t *dest, in
const vpx_codec_cx_pkt_t *pkt;
int copied = 0;
while ( (pkt = vpx_codec_get_cx_data(&call->cs->v_encoder, &iter)) ) {
while ( (pkt = vpx_codec_get_cx_data(call->cs->v_encoder, &iter)) ) {
if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
if ( copied + pkt->data.frame.sz > dest_max ) {
pthread_mutex_unlock(call->mutex_encoding_video);
@ -608,8 +608,6 @@ ToxAvCallState toxav_get_call_state(ToxAv *av, int32_t call_index)
int toxav_capability_supported ( ToxAv *av, int32_t call_index, ToxAvCapabilities capability )
{
return av->calls[call_index].cs ? av->calls[call_index].cs->capabilities & (CSCapabilities) capability : 0;
/* 0 is error here */
}
Tox *toxav_get_tox(ToxAv *av)

710
toxav/toxav_new.c Normal file
View File

@ -0,0 +1,710 @@
/** toxav.c
*
* Copyright (C) 2013 Tox project All Rights Reserved.
*
* This file is part of Tox.
*
* Tox is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Tox is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */
#include "toxav_new.h"
#include "msi.h" /* Includes codec.h and rtp.h */
#include "../toxcore/Messenger.h"
#include "../toxcore/logger.h"
#include "../toxcore/util.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
enum {
audio_index,
video_index,
};
typedef struct iToxAVCall
{
pthread_mutex_t mutex_control[1];
pthread_mutex_t mutex_encoding_audio[1];
pthread_mutex_t mutex_encoding_video[1];
pthread_mutex_t mutex_do[1];
RTPSession *rtps[2]; /** Audio is first and video is second */
CSSession *cs;
bool active;
int32_t friend_number;
int32_t call_idx; /* FIXME msi compat, remove */
struct iToxAVCall *prev;
struct iToxAVCall *next;
} IToxAVCall;
struct toxAV
{
Messenger* m;
MSISession* msi;
/* Two-way storage: first is array of calls and second is list of calls with head and tail */
IToxAVCall** calls;
uint32_t calls_tail;
uint32_t calls_head;
PAIR(toxav_call_cb *, void*) ccb; /* Call callback */
PAIR(toxav_call_state_cb *, void *) scb; /* Call state callback */
PAIR(toxav_receive_audio_frame_cb *, void *) acb; /* Audio frame receive callback */
PAIR(toxav_receive_video_frame_cb *, void *) vcb; /* Video frame receive callback */
/** Decode time measures */
int32_t dmssc; /** Measure count */
int32_t dmsst; /** Last cycle total */
int32_t dmssa; /** Average decoding time in ms */
};
void i_toxav_msi_callback_invite(void* toxav_inst, int32_t call_idx, void *data);
void i_toxav_msi_callback_ringing(void* toxav_inst, int32_t call_idx, void *data);
void i_toxav_msi_callback_start(void* toxav_inst, int32_t call_idx, void *data);
void i_toxav_msi_callback_cancel(void* toxav_inst, int32_t call_idx, void *data);
void i_toxav_msi_callback_reject(void* toxav_inst, int32_t call_idx, void *data);
void i_toxav_msi_callback_end(void* toxav_inst, int32_t call_idx, void *data);
void i_toxav_msi_callback_request_to(void* toxav_inst, int32_t call_idx, void *data); /* TODO remove */
void i_toxav_msi_callback_peer_to(void* toxav_inst, int32_t call_idx, void *data);
void i_toxav_msi_callback_state_change(void* toxav_inst, int32_t call_idx, void *data);
IToxAVCall* i_toxav_get_call(ToxAV* av, uint32_t friend_number);
IToxAVCall* i_toxav_add_call(ToxAV* av, uint32_t friend_number);
void i_toxav_remove_call(ToxAV* av, uint32_t friend_number);
bool i_toxav_audio_bitrate_invalid(uint32_t bitrate);
bool i_toxav_video_bitrate_invalid(uint32_t bitrate);
IToxAVCall* i_toxav_init_call(ToxAV* av, uint32_t friend_number, uint32_t audio_bit_rate, uint32_t video_bit_rate, TOXAV_ERR_CALL* error);
bool i_toxav_prepare_transmission(ToxAV* av, IToxAVCall* call);
void i_toxav_kill_transmission(ToxAV* av, IToxAVCall* call);
ToxAV* toxav_new(Tox* tox, TOXAV_ERR_NEW* error)
{
TOXAV_ERR_NEW rc = TOXAV_ERR_NEW_OK;
ToxAV *av = NULL;
if (tox == NULL) {
rc = TOXAV_ERR_NEW_NULL;
goto FAILURE;
}
if (((Messenger*)tox)->msi_packet) {
rc = TOXAV_ERR_NEW_MULTIPLE;
goto FAILURE;
}
av = calloc ( sizeof(ToxAV), 1);
if (av == NULL) {
LOGGER_WARNING("Allocation failed!");
rc = TOXAV_ERR_NEW_MALLOC;
goto FAILURE;
}
av->m = (Messenger *)tox;
av->msi = msi_new(av->m, 100); /* TODO remove max calls */
if (av->msi == NULL) {
rc = TOXAV_ERR_NEW_MALLOC;
goto FAILURE;
}
av->msi->agent_handler = av;
msi_register_callback(av->msi, i_toxav_msi_callback_invite, msi_OnInvite, NULL);
msi_register_callback(av->msi, i_toxav_msi_callback_ringing, msi_OnRinging, NULL);
msi_register_callback(av->msi, i_toxav_msi_callback_start, msi_OnStart, NULL);
msi_register_callback(av->msi, i_toxav_msi_callback_cancel, msi_OnCancel, NULL);
msi_register_callback(av->msi, i_toxav_msi_callback_reject, msi_OnReject, NULL);
msi_register_callback(av->msi, i_toxav_msi_callback_end, msi_OnEnd, NULL);
msi_register_callback(av->msi, i_toxav_msi_callback_request_to, msi_OnRequestTimeout, NULL);
msi_register_callback(av->msi, i_toxav_msi_callback_peer_to, msi_OnPeerTimeout, NULL);
msi_register_callback(av->msi, i_toxav_msi_callback_state_change, msi_OnPeerCSChange, NULL);
msi_register_callback(av->msi, i_toxav_msi_callback_state_change, msi_OnSelfCSChange, NULL);
if (error)
*error = rc;
return av;
FAILURE:
if (error)
*error = rc;
free(av);
return NULL;
}
void toxav_kill(ToxAV* av)
{
if (av == NULL)
return;
msi_kill(av->msi);
/* TODO iterate over calls */
free(av);
}
Tox* toxav_get_tox(ToxAV* av)
{
return (Tox*) av->m;
}
uint32_t toxav_iteration_interval(const ToxAV* av)
{
}
void toxav_iteration(ToxAV* av)
{
}
bool toxav_call(ToxAV* av, uint32_t friend_number, uint32_t audio_bit_rate, uint32_t video_bit_rate, TOXAV_ERR_CALL* error)
{
IToxAVCall* call = i_toxav_init_call(av, friend_number, audio_bit_rate, video_bit_rate, error);
if (call == NULL) {
return false;
}
/* TODO remove csettings */
MSICSettings csets;
csets.audio_bitrate = audio_bit_rate;
csets.video_bitrate = video_bit_rate;
csets.call_type = video_bit_rate ? msi_TypeVideo : msi_TypeAudio;
if (msi_invite(av->msi, &call->call_idx, &csets, 1000, friend_number) != 0) {
i_toxav_remove_call(av, friend_number);
if (error)
*error = TOXAV_ERR_CALL_MALLOC; /* FIXME: this should be the only reason to fail */
return false;
}
return true;
}
void toxav_callback_call(ToxAV* av, toxav_call_cb* function, void* user_data)
{
av->ccb.first = function;
av->ccb.second = user_data;
}
bool toxav_answer(ToxAV* av, uint32_t friend_number, uint32_t audio_bit_rate, uint32_t video_bit_rate, TOXAV_ERR_ANSWER* error)
{
TOXAV_ERR_ANSWER rc = TOXAV_ERR_ANSWER_OK;
if (m_friend_exists(av->m, friend_number)) {
rc = TOXAV_ERR_ANSWER_FRIEND_NOT_FOUND;
goto END;
}
if ((audio_bit_rate && i_toxav_audio_bitrate_invalid(audio_bit_rate))
||(video_bit_rate && i_toxav_video_bitrate_invalid(video_bit_rate))
) {
rc = TOXAV_ERR_CALL_INVALID_BIT_RATE;
goto END;
}
IToxAVCall* call = i_toxav_get_call(av, friend_number);
if (call == NULL || av->msi->calls[call->call_idx]->state != msi_CallRequested) {
rc = TOXAV_ERR_ANSWER_FRIEND_NOT_CALLING;
goto END;
}
/* TODO remove csettings */
MSICSettings csets;
csets.audio_bitrate = audio_bit_rate;
csets.video_bitrate = video_bit_rate;
csets.call_type = video_bit_rate ? msi_TypeVideo : msi_TypeAudio;
if (msi_answer(av->msi, call->call_idx, &csets) != 0) {
rc = TOXAV_ERR_ANSWER_MALLOC; /* TODO Some error here */
/* TODO Reject call? */
}
END:
if (error)
*error = rc;
return rc == TOXAV_ERR_ANSWER_OK;
}
void toxav_callback_call_state(ToxAV* av, toxav_call_state_cb* function, void* user_data)
{
av->scb.first = function;
av->scb.second = user_data;
}
bool toxav_call_control(ToxAV* av, uint32_t friend_number, TOXAV_CALL_CONTROL control, TOXAV_ERR_CALL_CONTROL* error)
{
}
bool toxav_set_audio_bit_rate(ToxAV* av, uint32_t friend_number, uint32_t audio_bit_rate, TOXAV_ERR_BIT_RATE* error)
{
}
bool toxav_set_video_bit_rate(ToxAV* av, uint32_t friend_number, uint32_t video_bit_rate, TOXAV_ERR_BIT_RATE* error)
{
}
void toxav_callback_request_video_frame(ToxAV* av, toxav_request_video_frame_cb* function, void* user_data)
{
}
bool toxav_send_video_frame(ToxAV* av, uint32_t friend_number, uint16_t width, uint16_t height, const uint8_t* y, const uint8_t* u, const uint8_t* v, const uint8_t* a, TOXAV_ERR_SEND_FRAME* error)
{
}
void toxav_callback_request_audio_frame(ToxAV* av, toxav_request_audio_frame_cb* function, void* user_data)
{
}
bool toxav_send_audio_frame(ToxAV* av, uint32_t friend_number, const int16_t* pcm, size_t sample_count, uint8_t channels, uint32_t sampling_rate, TOXAV_ERR_SEND_FRAME* error)
{
}
void toxav_callback_receive_video_frame(ToxAV* av, toxav_receive_video_frame_cb* function, void* user_data)
{
}
void toxav_callback_receive_audio_frame(ToxAV* av, toxav_receive_audio_frame_cb* function, void* user_data)
{
}
/*******************************************************************************
*
* :: Internal
*
******************************************************************************/
/** TODO:
* - In msi call_idx can be the same as friend id
* - If crutial callback not present send error
* - Remove *data from msi
* - Remove CSettings from msi
*/
void i_toxav_msi_callback_invite(void* toxav_inst, int32_t call_idx, void* data)
{
ToxAV* toxav = toxav_inst;
uint32_t ab = toxav->msi->calls[call_idx]->csettings_peer[0].audio_bitrate;
uint32_t vb = toxav->msi->calls[call_idx]->csettings_peer[0].video_bitrate;
IToxAVCall* call = i_toxav_init_call(toxav, toxav->msi->calls[call_idx]->peers[0], ab, vb, NULL);
if (call == NULL) {
msi_reject(toxav->msi, call_idx, NULL);
return false;
}
call->call_idx = call_idx;
if (toxav->ccb.first)
toxav->ccb.first(toxav, toxav->msi->calls[call_idx]->peers[0], true, true, toxav->ccb.second);
}
void i_toxav_msi_callback_ringing(void* toxav_inst, int32_t call_idx, void* data)
{
ToxAV* toxav = toxav_inst;
if (toxav->scb.first)
toxav->scb.first(toxav, toxav->msi->calls[call_idx]->peers[0],
TOXAV_CALL_STATE_RINGING, toxav->scb.second);
}
void i_toxav_msi_callback_start(void* toxav_inst, int32_t call_idx, void* data)
{
ToxAV* toxav = toxav_inst;
IToxAVCall* call = i_toxav_get_call(toxav, toxav->msi->calls[call_idx]->peers[0]);
if (call == NULL || !i_toxav_prepare_transmission(toxav, call)) {
/* TODO send error */
i_toxav_remove_call(toxav, toxav->msi->calls[call_idx]->peers[0]);
return;
}
TOXAV_CALL_STATE state;
const MSICSettings* csets = toxav->msi->calls[call_idx]->csettings_peer[0];
if (csets->audio_bitrate && csets->video_bitrate)
state = TOXAV_CALL_STATE_SENDING_AV;
else if (csets->video_bitrate == 0)
state = TOXAV_CALL_STATE_SENDING_A;
else
state = TOXAV_CALL_STATE_SENDING_V;
if (toxav->scb.first) /* TODO this */
toxav->scb.first(toxav, call->friend_number, state, toxav->scb.second);
}
void i_toxav_msi_callback_cancel(void* toxav_inst, int32_t call_idx, void* data)
{
ToxAV* toxav = toxav_inst;
if (toxav->scb.first)
toxav->scb.first(toxav, toxav->msi->calls[call_idx]->peers[0],
TOXAV_CALL_STATE_END, toxav->scb.second);
}
void i_toxav_msi_callback_reject(void* toxav_inst, int32_t call_idx, void* data)
{
ToxAV* toxav = toxav_inst;
if (toxav->scb.first)
toxav->scb.first(toxav, toxav->msi->calls[call_idx]->peers[0],
TOXAV_CALL_STATE_END, toxav->scb.second);
}
void i_toxav_msi_callback_end(void* toxav_inst, int32_t call_idx, void* data)
{
ToxAV* toxav = toxav_inst;
if (toxav->scb.first)
toxav->scb.first(toxav, toxav->msi->calls[call_idx]->peers[0],
TOXAV_CALL_STATE_END, toxav->scb.second);
}
void i_toxav_msi_callback_request_to(void* toxav_inst, int32_t call_idx, void* data)
{
/* TODO remove */
ToxAV* toxav = toxav_inst;
if (toxav->scb.first)
toxav->scb.first(toxav, toxav->msi->calls[call_idx]->peers[0],
TOXAV_CALL_STATE_ERROR, toxav->scb.second);
}
void i_toxav_msi_callback_peer_to(void* toxav_inst, int32_t call_idx, void* data)
{
ToxAV* toxav = toxav_inst;
if (toxav->scb.first)
toxav->scb.first(toxav, toxav->msi->calls[call_idx]->peers[0],
TOXAV_CALL_STATE_ERROR, toxav->scb.second);
}
void i_toxav_msi_callback_state_change(void* toxav_inst, int32_t call_idx, void* data)
{
ToxAV* toxav = toxav_inst;
/* TODO something something msi */
}
IToxAVCall* i_toxav_get_call(ToxAV* av, uint32_t friend_number)
{
if (av->calls_tail < friend_number)
return NULL;
return av->calls[friend_number];
}
IToxAVCall* i_toxav_add_call(ToxAV* av, uint32_t friend_number)
{
IToxAVCall* rc = calloc(sizeof(IToxAVCall), 1);
if (rc == NULL)
return NULL;
rc->friend_number = friend_number;
if (create_recursive_mutex(rc->mutex_control) != 0) {
free(rc);
return NULL;
}
if (create_recursive_mutex(rc->mutex_do) != 0) {
pthread_mutex_destroy(rc->mutex_control);
free(rc);
return NULL;
}
if (av->calls == NULL) { /* Creating */
av->calls = calloc (sizeof(IToxAVCall*), friend_number + 1);
if (av->calls == NULL) {
pthread_mutex_destroy(rc->mutex_control);
pthread_mutex_destroy(rc->mutex_do);
free(rc);
return NULL;
}
av->calls_tail = av->calls_head = friend_number;
} else if (av->calls_tail < friend_number) { /* Appending */
void* tmp = realloc(av->calls, sizeof(IToxAVCall*) * friend_number + 1);
if (tmp == NULL) {
pthread_mutex_destroy(rc->mutex_control);
pthread_mutex_destroy(rc->mutex_do);
free(rc);
return NULL;
}
av->calls = tmp;
/* Set fields in between to null */
int32_t i = av->calls_tail;
for (; i < friend_number; i ++)
av->calls[i] = NULL;
rc->prev = av->calls[av->calls_tail];
av->calls[av->calls_tail]->next = rc;
av->calls_tail = friend_number;
} else if (av->calls_head > friend_number) { /* Inserting at front */
rc->next = av->calls[av->calls_head];
av->calls[av->calls_head]->prev = rc;
av->calls_head = friend_number;
}
av->calls[friend_number] = rc;
return rc;
}
void i_toxav_remove_call(ToxAV* av, uint32_t friend_number)
{
IToxAVCall* tc = i_toxav_get_call(av, friend_number);
if (tc == NULL)
return;
IToxAVCall* prev = tc->prev;
IToxAVCall* next = tc->next;
pthread_mutex_destroy(tc->mutex_control);
pthread_mutex_destroy(tc->mutex_do);
free(tc);
if (prev)
prev->next = next;
else if (next)
av->calls_head = next->friend_number;
else goto CLEAR;
if (next)
next->prev = prev;
else if (prev)
av->calls_tail = prev->friend_number;
else goto CLEAR;
av->calls[friend_number] = NULL;
return;
CLEAR:
av->calls_head = av->calls_tail = 0;
free(av->calls);
av->calls = NULL;
}
bool i_toxav_audio_bitrate_invalid(uint32_t bitrate)
{
/* Opus RFC 6716 section-2.1.1 dictates the following:
* Opus supports all bitrates from 6 kbit/s to 510 kbit/s.
*/
return bitrate < 6 || bitrate > 510;
}
bool i_toxav_video_bitrate_invalid(uint32_t bitrate)
{
/* TODO: If anyone knows the answer to this one please fill it up */
return false;
}
IToxAVCall* i_toxav_init_call(ToxAV* av, uint32_t friend_number, uint32_t audio_bit_rate, uint32_t video_bit_rate, TOXAV_ERR_CALL* error)
{
TOXAV_ERR_CALL rc = TOXAV_ERR_CALL_OK;
IToxAVCall* call = NULL;
if (m_friend_exists(av->m, friend_number)) {
rc = TOXAV_ERR_CALL_FRIEND_NOT_FOUND;
goto END;
}
if (m_get_friend_connectionstatus(av->m, friend_number) != 1) {
rc = TOXAV_ERR_CALL_FRIEND_NOT_CONNECTED;
goto END;
}
if (i_toxav_get_call(av, friend_number) != NULL) {
rc = TOXAV_ERR_CALL_FRIEND_ALREADY_IN_CALL;
goto END;
}
if ((audio_bit_rate && i_toxav_audio_bitrate_invalid(audio_bit_rate))
||(video_bit_rate && i_toxav_video_bitrate_invalid(video_bit_rate))
) {
rc = TOXAV_ERR_CALL_INVALID_BIT_RATE;
goto END;
}
IToxAVCall* call = i_toxav_add_call(av, friend_number);
if (call == NULL) {
rc = TOXAV_ERR_CALL_MALLOC;
}
END:
if (error)
*error = rc;
return call;
}
bool i_toxav_prepare_transmission(ToxAV* av, IToxAVCall* call)
{
pthread_mutex_lock(call->mutex_control);
if (call->active) {
pthread_mutex_unlock(call->mutex_control);
LOGGER_WARNING("Call already active!\n");
return true;
}
if (pthread_mutex_init(call->mutex_encoding_audio, NULL) != 0)
goto MUTEX_INIT_ERROR;
if (pthread_mutex_init(call->mutex_encoding_video, NULL) != 0) {
pthread_mutex_destroy(call->mutex_encoding_audio);
goto MUTEX_INIT_ERROR;
}
if (pthread_mutex_init(call->mutex_do, NULL) != 0) {
pthread_mutex_destroy(call->mutex_encoding_audio);
pthread_mutex_destroy(call->mutex_encoding_video);
goto MUTEX_INIT_ERROR;
}
const MSICSettings *c_peer = &av->msi->calls[call->call_idx]->csettings_peer[0];
const MSICSettings *c_self = &av->msi->calls[call->call_idx]->csettings_local;
call->cs = cs_new(c_self->audio_bitrate, c_peer->audio_bitrate,
c_self->video_bitrate, c_peer->video_bitrate);
if ( !call->cs ) {
LOGGER_ERROR("Error while starting Codec State!\n");
goto FAILURE;
}
call->cs->agent = av;
call->cs->call_idx = call->call_idx;
call->cs->acb.first = av->acb.first;
call->cs->acb.second = av->acb.second;
call->cs->vcb.first = av->vcb.first;
call->cs->vcb.second = av->vcb.second;
if (c_self->audio_bitrate > 0 || c_peer->audio_bitrate > 0) { /* Prepare audio rtp */
call->rtps[audio_index] = rtp_new(msi_TypeAudio, av->m, av->msi->calls[call->call_idx]->peers[0]);
if ( !call->rtps[audio_index] ) {
LOGGER_ERROR("Error while starting audio RTP session!\n");
goto FAILURE;
}
call->rtps[audio_index]->cs = call->cs;
if (c_peer->audio_bitrate > 0)
rtp_register_for_receiving(call->rtps[audio_index]);
}
if (c_self->video_bitrate > 0 || c_peer->video_bitrate > 0) { /* Prepare video rtp */
call->rtps[video_index] = rtp_new(msi_TypeVideo, av->m, av->msi->calls[call->call_idx]->peers[0]);
if ( !call->rtps[video_index] ) {
LOGGER_ERROR("Error while starting video RTP session!\n");
goto FAILURE;
}
call->rtps[video_index]->cs = call->cs;
if (c_peer->video_bitrate > 0)
rtp_register_for_receiving(call->rtps[audio_index]);
}
call->active = 1;
pthread_mutex_unlock(call->mutex_control);
return true;
FAILURE:
rtp_kill(call->rtps[audio_index]);
call->rtps[audio_index] = NULL;
rtp_kill(call->rtps[video_index]);
call->rtps[video_index] = NULL;
cs_kill(call->cs);
call->cs = NULL;
call->active = 0;
pthread_mutex_destroy(call->mutex_encoding_audio);
pthread_mutex_destroy(call->mutex_encoding_video);
pthread_mutex_destroy(call->mutex_do);
pthread_mutex_unlock(call->mutex_control);
return false;
MUTEX_INIT_ERROR:
pthread_mutex_unlock(call->mutex_control);
LOGGER_ERROR("Mutex initialization failed!\n");
return false;
}
void i_toxav_kill_transmission(ToxAV* av, IToxAVCall* call)
{
pthread_mutex_lock(call->mutex_control);
if (!call->active) {
pthread_mutex_unlock(call->mutex_control);
LOGGER_WARNING("Action on inactive call: %d", call->call_idx);
return;
}
call->active = 0;
pthread_mutex_lock(call->mutex_encoding_audio);
pthread_mutex_unlock(call->mutex_encoding_audio);
pthread_mutex_lock(call->mutex_encoding_video);
pthread_mutex_unlock(call->mutex_encoding_video);
pthread_mutex_lock(call->mutex_do);
pthread_mutex_unlock(call->mutex_do);
rtp_kill(call->rtps[audio_index]);
call->rtps[audio_index] = NULL;
rtp_kill(call->rtps[video_index]);
call->rtps[video_index] = NULL;
cs_kill(call->cs);
call->cs = NULL;
pthread_mutex_destroy(call->mutex_encoding_audio);
pthread_mutex_destroy(call->mutex_encoding_video);
pthread_mutex_destroy(call->mutex_do);
pthread_mutex_unlock(call->mutex_control);
}

481
toxav/toxav_new.h Normal file
View File

@ -0,0 +1,481 @@
#pragma once
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
/** \page av Public audio/video API for Tox clients.
*
* Unlike the Core API, this API is fully thread-safe. The library will ensure
* the proper synchronisation of parallel calls.
*/
/**
* The type of the Tox Audio/Video subsystem object.
*/
typedef struct toxAV ToxAV;
#ifndef TOX_DEFINED
#define TOX_DEFINED
/**
* The type of a Tox instance. Repeated here so this file does not have a direct
* dependency on the Core interface.
*/
typedef struct Tox Tox;
#endif
/*******************************************************************************
*
* :: Creation and destruction
*
******************************************************************************/
typedef enum TOXAV_ERR_NEW {
TOXAV_ERR_NEW_OK,
TOXAV_ERR_NEW_NULL,
/**
* Memory allocation failure while trying to allocate structures required for
* the A/V session.
*/
TOXAV_ERR_NEW_MALLOC,
/**
* Attempted to create a second session for the same Tox instance.
*/
TOXAV_ERR_NEW_MULTIPLE
} TOXAV_ERR_NEW;
/**
* Start new A/V session. There can only be only one session per Tox instance.
*/
ToxAV *toxav_new(Tox *tox, TOXAV_ERR_NEW *error);
/**
* Releases all resources associated with the A/V session.
*
* If any calls were ongoing, these will be forcibly terminated without
* notifying peers. After calling this function, no other functions may be
* called and the av pointer becomes invalid.
*/
void toxav_kill(ToxAV *av);
/**
* Returns the Tox instance the A/V object was created for.
*/
Tox *toxav_get_tox(ToxAV *av);
/*******************************************************************************
*
* :: A/V event loop
*
******************************************************************************/
/**
* Returns the interval in milliseconds when the next toxav_iteration should be
* called. If no call is active at the moment, this function returns 200.
*/
uint32_t toxav_iteration_interval(ToxAV const *av);
/**
* Main loop for the session. This function needs to be called in intervals of
* toxav_iteration_interval() milliseconds. It is best called in the same loop
* as tox_iteration.
*/
void toxav_iteration(ToxAV *av);
/*******************************************************************************
*
* :: Call setup
*
******************************************************************************/
typedef enum TOXAV_ERR_CALL {
TOXAV_ERR_CALL_OK,
/**
* A resource allocation error occurred while trying to create the structures
* required for the call.
*/
TOXAV_ERR_CALL_MALLOC,
/**
* The friend number did not designate a valid friend.
*/
TOXAV_ERR_CALL_FRIEND_NOT_FOUND,
/**
* The friend was valid, but not currently connected.
*/
TOXAV_ERR_CALL_FRIEND_NOT_CONNECTED,
/**
* Attempted to call a friend while already in an audio or video call with
* them.
*/
TOXAV_ERR_CALL_FRIEND_ALREADY_IN_CALL,
/**
* Audio or video bit rate is invalid.
*/
TOXAV_ERR_CALL_INVALID_BIT_RATE
} TOXAV_ERR_CALL;
/**
* Call a friend. This will start ringing the friend.
*
* It is the client's responsibility to stop ringing after a certain timeout,
* if such behaviour is desired. If the client does not stop ringing, the A/V
* library will not stop until the friend is disconnected.
*
* @param friend_number The friend number of the friend that should be called.
* @param audio_bit_rate Audio bit rate in Kb/sec. Set this to 0 to disable
* audio sending.
* @param video_bit_rate Video bit rate in Kb/sec. Set this to 0 to disable
* video sending.
*/
bool toxav_call(ToxAV *av, uint32_t friend_number, uint32_t audio_bit_rate, uint32_t video_bit_rate, TOXAV_ERR_CALL *error);
/**
* The function type for the `call` callback.
*/
typedef void toxav_call_cb(ToxAV *av, uint32_t friend_number, bool audio_enabled, bool video_enabled, void *user_data);
/**
* Set the callback for the `call` event. Pass NULL to unset.
*
* This event is triggered when a call is received from a friend.
*/
void toxav_callback_call(ToxAV *av, toxav_call_cb *function, void *user_data);
typedef enum TOXAV_ERR_ANSWER {
TOXAV_ERR_ANSWER_OK,
/**
* A resource allocation error occurred while trying to create the structures
* required for the call.
*/
TOXAV_ERR_ANSWER_MALLOC,
/**
* The friend number did not designate a valid friend.
*/
TOXAV_ERR_ANSWER_FRIEND_NOT_FOUND,
/**
* The friend was valid, but they are not currently trying to initiate a call.
* This is also returned if this client is already in a call with the friend.
*/
TOXAV_ERR_ANSWER_FRIEND_NOT_CALLING,
/**
* Audio or video bit rate is invalid.
*/
TOXAV_ERR_ANSWER_INVALID_BIT_RATE
} TOXAV_ERR_ANSWER;
/**
* Accept an incoming call.
*
* If an allocation error occurs while answering a call, both participants will
* receive TOXAV_CALL_STATE_ERROR and the call will end.
*
* @param friend_number The friend number of the friend that is calling.
* @param audio_bit_rate Audio bit rate in Kb/sec. Set this to 0 to disable
* audio sending.
* @param video_bit_rate Video bit rate in Kb/sec. Set this to 0 to disable
* video sending.
*/
bool toxav_answer(ToxAV *av, uint32_t friend_number, uint32_t audio_bit_rate, uint32_t video_bit_rate, TOXAV_ERR_ANSWER *error);
/*******************************************************************************
*
* :: Call state graph
*
******************************************************************************/
typedef enum TOXAV_CALL_STATE {
/**
* The friend's client is aware of the call. This happens after calling
* toxav_call and the initial call request has been received.
*/
TOXAV_CALL_STATE_RINGING,
/**
* Not sending anything. Either the friend requested that this client stops
* sending anything, or the client turned off both audio and video by setting
* the respective bit rates to 0.
*
* If both sides are in this state, the call is effectively on hold, but not
* in the PAUSED state.
*/
TOXAV_CALL_STATE_NOT_SENDING,
/**
* Sending audio only. Either the friend requested that this client stops
* sending video, or the client turned off video by setting the video bit rate
* to 0.
*/
TOXAV_CALL_STATE_SENDING_A,
/**
* Sending video only. Either the friend requested that this client stops
* sending audio (muted), or the client turned off audio by setting the audio
* bit rate to 0.
*/
TOXAV_CALL_STATE_SENDING_V,
/**
* Sending both audio and video.
*/
TOXAV_CALL_STATE_SENDING_AV,
/**
* The call is on hold. Both sides stop sending and receiving.
*/
TOXAV_CALL_STATE_PAUSED,
/**
* The call has finished. This is the final state after which no more state
* transitions can occur for the call.
*/
TOXAV_CALL_STATE_END,
/**
* Sent by the AV core if an error occurred on the remote end.
*/
TOXAV_CALL_STATE_ERROR
} TOXAV_CALL_STATE;
/**
* The function type for the `call_state` callback.
*
* @param friend_number The friend number for which the call state changed.
* @param state The new call state.
*/
typedef void toxav_call_state_cb(ToxAV *av, uint32_t friend_number, TOXAV_CALL_STATE state, void *user_data);
/**
* Set the callback for the `call_state` event. Pass NULL to unset.
*
* This event is triggered when a call state transition occurs.
*/
void toxav_callback_call_state(ToxAV *av, toxav_call_state_cb *function, void *user_data);
/*******************************************************************************
*
* :: Call control
*
******************************************************************************/
typedef enum TOXAV_CALL_CONTROL {
/**
* Resume a previously paused call. Only valid if the pause was caused by this
* client. Not valid before the call is accepted.
*/
TOXAV_CALL_CONTROL_RESUME,
/**
* Put a call on hold. Not valid before the call is accepted.
*/
TOXAV_CALL_CONTROL_PAUSE,
/**
* Reject a call if it was not answered, yet. Cancel a call after it was
* answered.
*/
TOXAV_CALL_CONTROL_CANCEL,
/**
* Request that the friend stops sending audio. Regardless of the friend's
* compliance, this will cause the `receive_audio_frame` event to stop being
* triggered on receiving an audio frame from the friend.
*/
TOXAV_CALL_CONTROL_MUTE_AUDIO,
/**
* Request that the friend stops sending video. Regardless of the friend's
* compliance, this will cause the `receive_video_frame` event to stop being
* triggered on receiving an video frame from the friend.
*/
TOXAV_CALL_CONTROL_MUTE_VIDEO
} TOXAV_CALL_CONTROL;
typedef enum TOXAV_ERR_CALL_CONTROL {
TOXAV_ERR_CALL_CONTROL_OK,
/**
* The friend_number passed did not designate a valid friend.
*/
TOXAV_ERR_CALL_CONTROL_FRIEND_NOT_FOUND,
/**
* This client is currently not in a call with the friend. Before the call is
* answered, only CANCEL is a valid control.
*/
TOXAV_ERR_CALL_CONTROL_FRIEND_NOT_IN_CALL,
/**
* Attempted to resume a call that was not paused.
*/
TOXAV_ERR_CALL_CONTROL_NOT_PAUSED,
/**
* Attempted to resume a call that was paused by the other party. Also set if
* the client attempted to send a system-only control.
*/
TOXAV_ERR_CALL_CONTROL_DENIED,
/**
* The call was already paused on this client. It is valid to pause if the
* other party paused the call. The call will resume after both parties sent
* the RESUME control.
*/
TOXAV_ERR_CALL_CONTROL_ALREADY_PAUSED
} TOXAV_ERR_CALL_CONTROL;
/**
* Sends a call control command to a friend.
*
* @param friend_number The friend number of the friend this client is in a call
* with.
* @param control The control command to send.
*
* @return true on success.
*/
bool toxav_call_control(ToxAV *av, uint32_t friend_number, TOXAV_CALL_CONTROL control, TOXAV_ERR_CALL_CONTROL *error);
/*******************************************************************************
*
* :: Controlling bit rates
*
******************************************************************************/
typedef enum TOXAV_ERR_BIT_RATE {
TOXAV_ERR_BIT_RATE_OK,
/**
* The bit rate passed was not one of the supported values.
*/
TOXAV_ERR_BIT_RATE_INVALID
} TOXAV_ERR_BIT_RATE;
/**
* Set the audio bit rate to be used in subsequent audio frames.
*
* @param friend_number The friend number of the friend for which to set the
* audio bit rate.
* @param audio_bit_rate The new audio bit rate in Kb/sec. Set to 0 to disable
* audio sending.
*
* @see toxav_call for the valid bit rates.
*/
bool toxav_set_audio_bit_rate(ToxAV *av, uint32_t friend_number, uint32_t audio_bit_rate, TOXAV_ERR_BIT_RATE *error);
/**
* Set the video bit rate to be used in subsequent video frames.
*
* @param friend_number The friend number of the friend for which to set the
* video bit rate.
* @param video_bit_rate The new video bit rate in Kb/sec. Set to 0 to disable
* video sending.
*
* @see toxav_call for the valid bit rates.
*/
bool toxav_set_video_bit_rate(ToxAV *av, uint32_t friend_number, uint32_t video_bit_rate, TOXAV_ERR_BIT_RATE *error);
/*******************************************************************************
*
* :: A/V sending
*
******************************************************************************/
/**
* Common error codes for the send_*_frame functions.
*/
typedef enum TOXAV_ERR_SEND_FRAME {
TOXAV_ERR_SEND_FRAME_OK,
/**
* In case of video, one of Y, U, or V was NULL. In case of audio, the samples
* data pointer was NULL.
*/
TOXAV_ERR_SEND_FRAME_NULL,
/**
* The friend_number passed did not designate a valid friend.
*/
TOXAV_ERR_SEND_FRAME_FRIEND_NOT_FOUND,
/**
* This client is currently not in a call with the friend.
*/
TOXAV_ERR_SEND_FRAME_FRIEND_NOT_IN_CALL,
/**
* No video frame had been requested through the `request_video_frame` event,
* but the client tried to send one, anyway.
*/
TOXAV_ERR_SEND_FRAME_NOT_REQUESTED,
/**
* One of the frame parameters was invalid. E.g. the resolution may be too
* small or too large, or the audio sampling rate may be unsupported.
*/
TOXAV_ERR_SEND_FRAME_INVALID
} TOXAV_ERR_SEND_FRAME;
/**
* The function type for the `request_video_frame` callback.
*
* @param friend_number The friend number of the friend for which the next video
* frame should be sent.
*/
typedef void toxav_request_video_frame_cb(ToxAV *av, uint32_t friend_number, void *user_data);
/**
* Set the callback for the `request_video_frame` event. Pass NULL to unset.
*/
void toxav_callback_request_video_frame(ToxAV *av, toxav_request_video_frame_cb *function, void *user_data);
/**
* Send a video frame to a friend.
*
* This is called in response to receiving the `request_video_frame` event.
*
* Each plane should contain (width * height) pixels. The Alpha plane can be
* NULL, in which case every pixel is assumed fully opaque.
*
* @param friend_number The friend number of the friend to which to send a video
* frame.
* @param width Width of the frame in pixels.
* @param height Height of the frame in pixels.
* @param y Y (Luminance) plane data.
* @param u U (Chroma) plane data.
* @param v V (Chroma) plane data.
* @param a A (Alpha) plane data.
*/
bool toxav_send_video_frame(ToxAV *av, uint32_t friend_number,
uint16_t width, uint16_t height,
uint8_t const *y, uint8_t const *u, uint8_t const *v, uint8_t const *a,
TOXAV_ERR_SEND_FRAME *error);
/**
* The function type for the `request_audio_frame` callback.
*
* @param friend_number The friend number of the friend for which the next audio
* frame should be sent.
*/
typedef void toxav_request_audio_frame_cb(ToxAV *av, uint32_t friend_number, void *user_data);
/**
* Set the callback for the `request_audio_frame` event. Pass NULL to unset.
*/
void toxav_callback_request_audio_frame(ToxAV *av, toxav_request_audio_frame_cb *function, void *user_data);
/**
* Send an audio frame to a friend.
*
* This is called in response to receiving the `request_audio_frame` event.
*
* The expected format of the PCM data is: [s1c1][s1c2][...][s2c1][s2c2][...]...
* Meaning: sample 1 for channel 1, sample 1 for channel 2, ...
* For mono audio, this has no meaning, every sample is subsequent. For stereo,
* this means the expected format is LRLRLR... with samples for left and right
* alternating.
*
* @param friend_number The friend number of the friend to which to send an
* audio frame.
* @param pcm An array of audio samples. The size of this array must be
* sample_count * channels.
* @param sample_count Number of samples in this frame. Valid numbers here are
* ((sample rate) * (audio length) / 1000), where audio length can be
* 2.5, 5, 10, 20, 40 or 60 millseconds.
* @param channels Number of audio channels. Must be at least 1 for mono.
* For voice over IP, more than 2 channels (stereo) typically doesn't make
* sense, but up to 255 channels are supported.
* @param sampling_rate Audio sampling rate used in this frame. Valid sampling
* rates are 8000, 12000, 16000, 24000, or 48000.
*/
bool toxav_send_audio_frame(ToxAV *av, uint32_t friend_number,
int16_t const *pcm,
size_t sample_count,
uint8_t channels,
uint32_t sampling_rate,
TOXAV_ERR_SEND_FRAME *error);
/*******************************************************************************
*
* :: A/V receiving
*
******************************************************************************/
/**
* The function type for the `receive_video_frame` callback.
*
* Each plane contains (width * height) pixels. The Alpha plane can be NULL, in
* which case every pixel should be assumed fully opaque.
*
* @param friend_number The friend number of the friend who sent a video frame.
* @param width Width of the frame in pixels.
* @param height Height of the frame in pixels.
* @param y Y (Luminance) plane data.
* @param u U (Chroma) plane data.
* @param v V (Chroma) plane data.
* @param a A (Alpha) plane data.
*/
typedef void toxav_receive_video_frame_cb(ToxAV *av, uint32_t friend_number,
uint16_t width, uint16_t height,
uint8_t const *y, uint8_t const *u, uint8_t const *v, uint8_t const *a,
void *user_data);
/**
* Set the callback for the `receive_video_frame` event. Pass NULL to unset.
*/
void toxav_callback_receive_video_frame(ToxAV *av, toxav_receive_video_frame_cb *function, void *user_data);
/**
* The function type for the `receive_audio_frame` callback.
*
* @param friend_number The friend number of the friend who sent an audio frame.
* @param pcm An array of audio samples (sample_count * channels elements).
* @param sample_count The number of audio samples per channel in the PCM array.
* @param channels Number of audio channels.
* @param sampling_rate Sampling rate used in this frame.
*
* @see toxav_send_audio_frame for the audio format.
*/
typedef void toxav_receive_audio_frame_cb(ToxAV *av, uint32_t friend_number,
int16_t const *pcm,
size_t sample_count,
uint8_t channels,
uint32_t sampling_rate,
void *user_data);
/**
* Set the callback for the `receive_audio_frame` event. Pass NULL to unset.
*/
void toxav_callback_receive_audio_frame(ToxAV *av, toxav_receive_audio_frame_cb *function, void *user_data);

View File

@ -42,7 +42,7 @@
#endif
typedef struct logger {
struct logger {
FILE *log_file;
LOG_LEVEL level;
uint64_t start_time; /* Time when lib loaded */
@ -55,7 +55,7 @@ typedef struct logger {
/* For thread synchronisation */
pthread_mutex_t mutex[1];
} logger;
};
logger *global = NULL;

View File

@ -53,6 +53,7 @@ typedef int (*load_state_callback_func)(void *outer, const uint8_t *data, uint32
int load_state(load_state_callback_func load_state_callback, void *outer,
const uint8_t *data, uint32_t length, uint16_t cookie_inner);
/* Returns -1 if failed or 0 if success */
int create_recursive_mutex(pthread_mutex_t *mutex);
#endif /* __UTIL_H__ */