mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
Control part of new api already kind of works
This commit is contained in:
parent
39680f31d0
commit
aad857527c
|
@ -35,4 +35,21 @@ libtoxav_la_LIBADD = libtoxcore.la \
|
|||
$(PTHREAD_LIBS) \
|
||||
$(AV_LIBS)
|
||||
|
||||
|
||||
noinst_PROGRAMS += av_test
|
||||
|
||||
av_test_SOURCES = ../toxav/av_test.c
|
||||
|
||||
av_test_CFLAGS = $(LIBSODIUM_CFLAGS) \
|
||||
$(NACL_CFLAGS)
|
||||
|
||||
av_test_LDADD = $(LIBSODIUM_LDFLAGS) \
|
||||
$(NACL_LDFLAGS) \
|
||||
libtoxav.la \
|
||||
libtoxcore.la \
|
||||
$(LIBSODIUM_LIBS) \
|
||||
$(NACL_OBJECTS) \
|
||||
$(NACL_LIBS)
|
||||
|
||||
|
||||
endif
|
339
toxav/av_test.c
Normal file
339
toxav/av_test.c
Normal file
|
@ -0,0 +1,339 @@
|
|||
#include "toxav.h"
|
||||
#include "../toxcore/tox.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <string.h>
|
||||
|
||||
#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
|
||||
#define c_sleep(x) Sleep(1*x)
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#define c_sleep(x) usleep(1000*x)
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
bool incoming;
|
||||
bool ringing;
|
||||
bool ended;
|
||||
bool errored;
|
||||
bool sending;
|
||||
bool paused;
|
||||
} CallControl;
|
||||
|
||||
|
||||
/**
|
||||
* Callbacks
|
||||
*/
|
||||
void t_toxav_call_cb(ToxAV *av, uint32_t friend_number, bool audio_enabled, bool video_enabled, void *user_data)
|
||||
{
|
||||
printf("Handling CALL callback\n");
|
||||
((CallControl*)user_data)->incoming = true;
|
||||
}
|
||||
void t_toxav_call_state_cb(ToxAV *av, uint32_t friend_number, TOXAV_CALL_STATE state, void *user_data)
|
||||
{
|
||||
printf("Handling CALL STATE callback: ");
|
||||
|
||||
if (((CallControl*)user_data)->ringing)
|
||||
((CallControl*)user_data)->ringing = false;
|
||||
|
||||
if (((CallControl*)user_data)->paused)
|
||||
((CallControl*)user_data)->paused = false;
|
||||
|
||||
switch (state)
|
||||
{
|
||||
case TOXAV_CALL_STATE_RINGING: {
|
||||
printf("Ringing");
|
||||
((CallControl*)user_data)->ringing = true;
|
||||
} break;
|
||||
|
||||
case TOXAV_CALL_STATE_NOT_SENDING: {
|
||||
printf("Not sending");
|
||||
((CallControl*)user_data)->sending = false;
|
||||
} break;
|
||||
|
||||
case TOXAV_CALL_STATE_SENDING_A: {
|
||||
printf("Sending Audio");
|
||||
((CallControl*)user_data)->sending = true;
|
||||
} break;
|
||||
|
||||
case TOXAV_CALL_STATE_SENDING_V: {
|
||||
printf("Sending Video");
|
||||
((CallControl*)user_data)->sending = true;
|
||||
} break;
|
||||
|
||||
case TOXAV_CALL_STATE_SENDING_AV: {
|
||||
printf("Sending Both");
|
||||
((CallControl*)user_data)->sending = true;
|
||||
} break;
|
||||
|
||||
case TOXAV_CALL_STATE_PAUSED: {
|
||||
printf("Paused");
|
||||
((CallControl*)user_data)->paused = true;
|
||||
((CallControl*)user_data)->sending = false;
|
||||
} break;
|
||||
|
||||
case TOXAV_CALL_STATE_END: {
|
||||
printf("Ended");
|
||||
((CallControl*)user_data)->ended = true;
|
||||
((CallControl*)user_data)->sending = false;
|
||||
} break;
|
||||
|
||||
case TOXAV_CALL_STATE_ERROR: {
|
||||
printf("Error");
|
||||
((CallControl*)user_data)->errored = true;
|
||||
((CallControl*)user_data)->sending = false;
|
||||
} break;
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
}
|
||||
void t_toxav_receive_video_frame_cb(ToxAV *av, uint32_t friend_number,
|
||||
uint16_t width, uint16_t height,
|
||||
uint8_t const *planes[], int32_t const stride[],
|
||||
void *user_data)
|
||||
{
|
||||
printf("Handling VIDEO FRAME callback\n");
|
||||
}
|
||||
void t_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)
|
||||
{
|
||||
printf("Handling AUDIO FRAME callback\n");
|
||||
}
|
||||
void t_accept_friend_request_cb(Tox *m, const uint8_t *public_key, const uint8_t *data, uint16_t length, void *userdata)
|
||||
{
|
||||
if (length == 7 && memcmp("gentoo", data, 7) == 0) {
|
||||
tox_add_friend_norequest(m, public_key);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*/
|
||||
void prepare(Tox* Bsn, Tox* Alice, Tox* Bob)
|
||||
{
|
||||
long long unsigned int cur_time = time(NULL);
|
||||
|
||||
uint32_t to_compare = 974536;
|
||||
uint8_t address[TOX_FRIEND_ADDRESS_SIZE];
|
||||
|
||||
tox_callback_friend_request(Alice, t_accept_friend_request_cb, &to_compare);
|
||||
tox_get_address(Alice, address);
|
||||
|
||||
assert(tox_add_friend(Bob, address, (uint8_t *)"gentoo", 7) >= 0);
|
||||
|
||||
uint8_t off = 1;
|
||||
|
||||
while (1) {
|
||||
tox_do(Bsn);
|
||||
tox_do(Alice);
|
||||
tox_do(Bob);
|
||||
|
||||
if (tox_isconnected(Bsn) && tox_isconnected(Alice) && tox_isconnected(Bob) && off) {
|
||||
printf("Toxes are online, took %llu seconds\n", time(NULL) - cur_time);
|
||||
off = 0;
|
||||
}
|
||||
|
||||
if (tox_get_friend_connection_status(Alice, 0) == 1 && tox_get_friend_connection_status(Bob, 0) == 1)
|
||||
break;
|
||||
|
||||
c_sleep(20);
|
||||
}
|
||||
|
||||
printf("All set after %llu seconds!\n", time(NULL) - cur_time);
|
||||
}
|
||||
void prepareAV(ToxAV* AliceAV, void* AliceUD, ToxAV* BobAV, void* BobUD)
|
||||
{
|
||||
/* Alice */
|
||||
toxav_callback_call(AliceAV, t_toxav_call_cb, AliceUD);
|
||||
toxav_callback_call_state(AliceAV, t_toxav_call_state_cb, AliceUD);
|
||||
toxav_callback_receive_video_frame(AliceAV, t_toxav_receive_video_frame_cb, AliceUD);
|
||||
toxav_callback_receive_audio_frame(AliceAV, t_toxav_receive_audio_frame_cb, AliceUD);
|
||||
|
||||
/* Bob */
|
||||
toxav_callback_call(BobAV, t_toxav_call_cb, BobUD);
|
||||
toxav_callback_call_state(BobAV, t_toxav_call_state_cb, BobUD);
|
||||
toxav_callback_receive_video_frame(BobAV, t_toxav_receive_video_frame_cb, BobUD);
|
||||
toxav_callback_receive_audio_frame(BobAV, t_toxav_receive_audio_frame_cb, BobUD);
|
||||
}
|
||||
void iterate(Tox* Bsn, ToxAV* AliceAV, ToxAV* BobAV)
|
||||
{
|
||||
tox_do(Bsn);
|
||||
tox_do(toxav_get_tox(AliceAV));
|
||||
tox_do(toxav_get_tox(BobAV));
|
||||
|
||||
toxav_iteration(AliceAV);
|
||||
toxav_iteration(BobAV);
|
||||
|
||||
c_sleep(20);
|
||||
}
|
||||
|
||||
|
||||
int main (int argc, char** argv)
|
||||
{
|
||||
Tox *Bsn = tox_new(0);
|
||||
Tox *Alice = tox_new(0);
|
||||
Tox *Bob = tox_new(0);
|
||||
|
||||
assert(Bsn && Alice && Bob);
|
||||
|
||||
prepare(Bsn, Alice, Bob);
|
||||
|
||||
|
||||
ToxAV *AliceAV, *BobAV;
|
||||
CallControl AliceCC, BobCC;
|
||||
|
||||
{
|
||||
TOXAV_ERR_NEW rc;
|
||||
AliceAV = toxav_new(Alice, &rc);
|
||||
assert(rc == TOXAV_ERR_NEW_OK);
|
||||
|
||||
BobAV = toxav_new(Bob, &rc);
|
||||
assert(rc == TOXAV_ERR_NEW_OK);
|
||||
|
||||
prepareAV(AliceAV, &AliceCC, BobAV, &BobCC);
|
||||
printf("Created 2 instances of ToxAV\n");
|
||||
}
|
||||
|
||||
|
||||
#define REGULAR_CALL_FLOW(A_BR, V_BR) \
|
||||
{ \
|
||||
memset(&AliceCC, 0, sizeof(CallControl)); \
|
||||
memset(&BobCC, 0, sizeof(CallControl)); \
|
||||
\
|
||||
TOXAV_ERR_CALL rc; \
|
||||
toxav_call(AliceAV, 0, A_BR, V_BR, &rc); \
|
||||
\
|
||||
if (rc != TOXAV_ERR_CALL_OK) { \
|
||||
printf("toxav_call failed: %d\n", rc); \
|
||||
exit(1); \
|
||||
} \
|
||||
\
|
||||
\
|
||||
long long unsigned int start_time = time(NULL); \
|
||||
\
|
||||
\
|
||||
while (!AliceCC.ended || !BobCC.ended) { \
|
||||
\
|
||||
if (BobCC.incoming) { \
|
||||
TOXAV_ERR_ANSWER rc; \
|
||||
toxav_answer(BobAV, 0, 48, 4000, &rc); \
|
||||
\
|
||||
if (rc != TOXAV_ERR_ANSWER_OK) { \
|
||||
printf("toxav_answer failed: %d\n", rc); \
|
||||
exit(1); \
|
||||
} \
|
||||
BobCC.incoming = false; \
|
||||
} \
|
||||
else if (AliceCC.sending && BobCC.sending) { \
|
||||
/* TODO rtp */ \
|
||||
\
|
||||
if (time(NULL) - start_time == 5) { \
|
||||
\
|
||||
TOXAV_ERR_CALL_CONTROL rc; \
|
||||
toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_CANCEL, &rc); \
|
||||
\
|
||||
if (rc != TOXAV_ERR_CALL_CONTROL_OK) { \
|
||||
printf("toxav_call_control failed: %d\n", rc); \
|
||||
exit(1); \
|
||||
} \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
iterate(Bsn, AliceAV, BobAV); \
|
||||
} \
|
||||
printf("Success!\n");\
|
||||
}
|
||||
|
||||
printf("\nTrying regular call (Audio and Video)...\n");
|
||||
// REGULAR_CALL_FLOW(48, 4000);
|
||||
|
||||
printf("\nTrying regular call (Audio only)...\n");
|
||||
// REGULAR_CALL_FLOW(48, 0);
|
||||
|
||||
printf("\nTrying regular call (Video only)...\n");
|
||||
// REGULAR_CALL_FLOW(0, 4000);
|
||||
|
||||
#undef REGULAR_CALL_FLOW
|
||||
|
||||
{ /* Alice calls; Bob rejects */
|
||||
printf("\nTrying reject flow...\n");
|
||||
|
||||
memset(&AliceCC, 0, sizeof(CallControl));
|
||||
memset(&BobCC, 0, sizeof(CallControl));
|
||||
|
||||
{
|
||||
TOXAV_ERR_CALL rc;
|
||||
toxav_call(AliceAV, 0, 48, 0, &rc);
|
||||
|
||||
if (rc != TOXAV_ERR_CALL_OK) {
|
||||
printf("toxav_call failed: %d\n", rc);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
while (!BobCC.incoming)
|
||||
iterate(Bsn, AliceAV, BobAV);
|
||||
|
||||
/* Reject */
|
||||
{
|
||||
TOXAV_ERR_CALL_CONTROL rc;
|
||||
toxav_call_control(BobAV, 0, TOXAV_CALL_CONTROL_CANCEL, &rc);
|
||||
|
||||
if (rc != TOXAV_ERR_CALL_CONTROL_OK) {
|
||||
printf("toxav_call_control failed: %d\n", rc);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
while (!AliceCC.ended || !BobCC.ended)
|
||||
iterate(Bsn, AliceAV, BobAV);
|
||||
|
||||
printf("Success!\n");
|
||||
}
|
||||
|
||||
{ /* Alice calls; Alice cancels while ringing */
|
||||
printf("\nTrying cancel (while ringing) flow...\n");
|
||||
|
||||
memset(&AliceCC, 0, sizeof(CallControl));
|
||||
memset(&BobCC, 0, sizeof(CallControl));
|
||||
|
||||
{
|
||||
TOXAV_ERR_CALL rc;
|
||||
toxav_call(AliceAV, 0, 48, 0, &rc);
|
||||
|
||||
if (rc != TOXAV_ERR_CALL_OK) {
|
||||
printf("toxav_call failed: %d\n", rc);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
while (!BobCC.incoming)
|
||||
iterate(Bsn, AliceAV, BobAV);
|
||||
|
||||
/* Cancel */
|
||||
{
|
||||
TOXAV_ERR_CALL_CONTROL rc;
|
||||
toxav_call_control(AliceAV, 0, TOXAV_CALL_CONTROL_CANCEL, &rc);
|
||||
|
||||
if (rc != TOXAV_ERR_CALL_CONTROL_OK) {
|
||||
printf("toxav_call_control failed: %d\n", rc);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
while (!AliceCC.ended || !BobCC.ended)
|
||||
iterate(Bsn, AliceAV, BobAV);
|
||||
|
||||
printf("Success!\n");
|
||||
}
|
||||
|
||||
printf("\nTest successful!\n");
|
||||
return 0;
|
||||
}
|
|
@ -40,7 +40,6 @@
|
|||
#include "rtp.h"
|
||||
#include "codec.h"
|
||||
|
||||
|
||||
#define DEFAULT_JBUF 6
|
||||
|
||||
/* Good quality encode. */
|
||||
|
@ -125,7 +124,7 @@ static void buffer_free(PayloadBuffer *b)
|
|||
}
|
||||
|
||||
/* JITTER BUFFER WORK */
|
||||
typedef struct {
|
||||
typedef struct JitterBuffer {
|
||||
RTPMessage **queue;
|
||||
uint32_t size;
|
||||
uint32_t capacity;
|
||||
|
@ -260,48 +259,51 @@ void cs_do(CSSession *cs)
|
|||
int success = 0;
|
||||
|
||||
pthread_mutex_lock(cs->queue_mutex);
|
||||
RTPMessage *msg;
|
||||
|
||||
uint16_t fsize = 5760; /* Max frame size for 48 kHz */
|
||||
int16_t tmp[fsize * 2];
|
||||
|
||||
while ((msg = jbuf_read(cs->j_buf, &success)) || success == 2) {
|
||||
pthread_mutex_unlock(cs->queue_mutex);
|
||||
if (cs->audio_decoder) { /* If receiving enabled */
|
||||
RTPMessage *msg;
|
||||
|
||||
if (success == 2) {
|
||||
rc = opus_decode(cs->audio_decoder, 0, 0, tmp, fsize, 1);
|
||||
} else {
|
||||
/* Get values from packet and decode.
|
||||
* It also checks for validity of an opus packet
|
||||
*/
|
||||
rc = convert_bw_to_sampling_rate(opus_packet_get_bandwidth(msg->data));
|
||||
if (rc != -1) {
|
||||
cs->last_packet_sampling_rate = rc;
|
||||
cs->last_pack_channels = opus_packet_get_nb_channels(msg->data);
|
||||
uint16_t fsize = 5760; /* Max frame size for 48 kHz */
|
||||
int16_t tmp[fsize * 2];
|
||||
|
||||
while ((msg = jbuf_read(cs->j_buf, &success)) || success == 2) {
|
||||
pthread_mutex_unlock(cs->queue_mutex);
|
||||
|
||||
cs->last_packet_frame_duration =
|
||||
( opus_packet_get_samples_per_frame(msg->data, cs->last_packet_sampling_rate) * 1000 )
|
||||
/ cs->last_packet_sampling_rate;
|
||||
|
||||
if (success == 2) {
|
||||
rc = opus_decode(cs->audio_decoder, 0, 0, tmp, fsize, 1);
|
||||
} else {
|
||||
LOGGER_WARNING("Failed to load packet values!");
|
||||
/* Get values from packet and decode.
|
||||
* It also checks for validity of an opus packet
|
||||
*/
|
||||
rc = convert_bw_to_sampling_rate(opus_packet_get_bandwidth(msg->data));
|
||||
if (rc != -1) {
|
||||
cs->last_packet_sampling_rate = rc;
|
||||
cs->last_pack_channels = opus_packet_get_nb_channels(msg->data);
|
||||
|
||||
cs->last_packet_frame_duration =
|
||||
( opus_packet_get_samples_per_frame(msg->data, cs->last_packet_sampling_rate) * 1000 )
|
||||
/ cs->last_packet_sampling_rate;
|
||||
|
||||
} else {
|
||||
LOGGER_WARNING("Failed to load packet values!");
|
||||
rtp_free_msg(NULL, msg);
|
||||
continue;
|
||||
}
|
||||
|
||||
rc = opus_decode(cs->audio_decoder, msg->data, msg->length, tmp, fsize, 0);
|
||||
rtp_free_msg(NULL, msg);
|
||||
continue;
|
||||
}
|
||||
|
||||
rc = opus_decode(cs->audio_decoder, msg->data, msg->length, tmp, fsize, 0);
|
||||
rtp_free_msg(NULL, msg);
|
||||
if (rc < 0) {
|
||||
LOGGER_WARNING("Decoding error: %s", opus_strerror(rc));
|
||||
} else if (cs->acb.first) {
|
||||
/* Play */
|
||||
cs->acb.first(cs->agent, cs->friend_number, tmp, rc,
|
||||
cs->last_pack_channels, cs->last_packet_sampling_rate, cs->acb.second);
|
||||
}
|
||||
|
||||
pthread_mutex_lock(cs->queue_mutex);
|
||||
}
|
||||
|
||||
if (rc < 0) {
|
||||
LOGGER_WARNING("Decoding error: %s", opus_strerror(rc));
|
||||
} else if (((ToxAV*)cs->agent)->acb.first) {
|
||||
/* Play */
|
||||
((ToxAV*)cs->agent)->acb.first(cs->agent, cs->call_idx, tmp, rc,
|
||||
((ToxAV*)cs->agent)->acb.second);
|
||||
}
|
||||
|
||||
pthread_mutex_lock(cs->queue_mutex);
|
||||
}
|
||||
|
||||
if (cs->vbuf_raw && !buffer_empty(cs->vbuf_raw)) {
|
||||
|
@ -322,11 +324,11 @@ void cs_do(CSSession *cs)
|
|||
|
||||
/* Play decoded images */
|
||||
for (; dest; dest = vpx_codec_get_frame(cs->v_decoder, &iter)) {
|
||||
if (((ToxAV*)cs->agent)->vcb.first)
|
||||
((ToxAV*)cs->agent)->vcb.first(cs->agent, cs->call_idx, dest,
|
||||
((ToxAV*)cs->agent)->vcb.second);
|
||||
|
||||
vpx_img_free(dest);
|
||||
if (cs->vcb.first)
|
||||
cs->vcb.first(cs->agent, cs->call_idx, dest->d_w, dest->d_h,
|
||||
(const uint8_t**)dest->planes, dest->stride, cs->vcb.second);
|
||||
|
||||
vpx_img_free(dest);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -117,7 +117,7 @@ typedef struct _CSSession {
|
|||
int32_t last_pack_channels;
|
||||
int32_t last_packet_sampling_rate;
|
||||
int32_t last_packet_frame_duration;
|
||||
struct _JitterBuffer *j_buf;
|
||||
struct JitterBuffer *j_buf;
|
||||
|
||||
|
||||
/* Voice activity detection */
|
||||
|
@ -132,6 +132,10 @@ typedef struct _CSSession {
|
|||
*/
|
||||
void *agent; /* Pointer to ToxAV TODO make this pointer to ToxAV*/
|
||||
int32_t call_idx;
|
||||
int32_t friend_number;
|
||||
|
||||
PAIR(toxav_receive_audio_frame_cb *, void *) acb; /* Audio frame receive callback */
|
||||
PAIR(toxav_receive_video_frame_cb *, void *) vcb; /* Video frame receive callback */
|
||||
|
||||
pthread_mutex_t queue_mutex[1];
|
||||
} CSSession;
|
||||
|
|
1406
toxav/toxav.c
1406
toxav/toxav.c
File diff suppressed because it is too large
Load Diff
796
toxav/toxav.h
796
toxav/toxav.h
|
@ -1,329 +1,483 @@
|
|||
/** toxav.h
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
#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.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef __TOXAV
|
||||
#define __TOXAV
|
||||
#include <inttypes.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct _ToxAv ToxAv;
|
||||
|
||||
/* vpx_image_t */
|
||||
#include <vpx/vpx_image.h>
|
||||
|
||||
typedef void ( *ToxAVCallback ) ( void *agent, int32_t call_idx, void *arg );
|
||||
typedef void ( *ToxAvAudioCallback ) (void *agent, int32_t call_idx, const int16_t *PCM, uint16_t size, void *data);
|
||||
typedef void ( *ToxAvVideoCallback ) (void *agent, int32_t call_idx, const vpx_image_t *img, void *data);
|
||||
|
||||
#ifndef __TOX_DEFINED__
|
||||
#define __TOX_DEFINED__
|
||||
typedef struct Tox Tox;
|
||||
#endif
|
||||
|
||||
#define RTP_PAYLOAD_SIZE 65535
|
||||
|
||||
|
||||
/**
|
||||
* Callbacks ids that handle the call states.
|
||||
*/
|
||||
typedef enum {
|
||||
av_OnInvite, /* Incoming call */
|
||||
av_OnRinging, /* When peer is ready to accept/reject the call */
|
||||
av_OnStart, /* Call (RTP transmission) started */
|
||||
av_OnCancel, /* The side that initiated call canceled invite */
|
||||
av_OnReject, /* The side that was invited rejected the call */
|
||||
av_OnEnd, /* Call that was active ended */
|
||||
av_OnRequestTimeout, /* When the requested action didn't get response in specified time */
|
||||
av_OnPeerTimeout, /* Peer timed out; stop the call */
|
||||
av_OnPeerCSChange, /* Peer changing Csettings. Prepare for changed AV */
|
||||
av_OnSelfCSChange /* Csettings change confirmation. Once triggered peer is ready to recv changed AV */
|
||||
} ToxAvCallbackID;
|
||||
|
||||
|
||||
/**
|
||||
* Call type identifier.
|
||||
*/
|
||||
typedef enum {
|
||||
av_TypeAudio = 192,
|
||||
av_TypeVideo
|
||||
} ToxAvCallType;
|
||||
|
||||
|
||||
typedef enum {
|
||||
av_CallNonExistent = -1,
|
||||
av_CallInviting, /* when sending call invite */
|
||||
av_CallStarting, /* when getting call invite */
|
||||
av_CallActive,
|
||||
av_CallHold,
|
||||
av_CallHungUp
|
||||
} ToxAvCallState;
|
||||
|
||||
/**
|
||||
* Error indicators. Values under -20 are reserved for toxcore.
|
||||
*/
|
||||
typedef enum {
|
||||
av_ErrorNone = 0,
|
||||
av_ErrorUnknown = -1, /* Unknown error */
|
||||
av_ErrorNoCall = -20, /* Trying to perform call action while not in a call */
|
||||
av_ErrorInvalidState = -21, /* Trying to perform call action while in invalid state*/
|
||||
av_ErrorAlreadyInCallWithPeer = -22, /* Trying to call peer when already in a call with peer */
|
||||
av_ErrorReachedCallLimit = -23, /* Cannot handle more calls */
|
||||
av_ErrorInitializingCodecs = -30, /* Failed creating CSSession */
|
||||
av_ErrorSettingVideoResolution = -31, /* Error setting resolution */
|
||||
av_ErrorSettingVideoBitrate = -32, /* Error setting bitrate */
|
||||
av_ErrorSplittingVideoPayload = -33, /* Error splitting video payload */
|
||||
av_ErrorEncodingVideo = -34, /* vpx_codec_encode failed */
|
||||
av_ErrorEncodingAudio = -35, /* opus_encode failed */
|
||||
av_ErrorSendingPayload = -40, /* Sending lossy packet failed */
|
||||
av_ErrorCreatingRtpSessions = -41, /* One of the rtp sessions failed to initialize */
|
||||
av_ErrorNoRtpSession = -50, /* Trying to perform rtp action on invalid session */
|
||||
av_ErrorInvalidCodecState = -51, /* Codec state not initialized */
|
||||
av_ErrorPacketTooLarge = -52, /* Split packet exceeds it's limit */
|
||||
} ToxAvError;
|
||||
|
||||
|
||||
/**
|
||||
* Locally supported capabilities.
|
||||
*/
|
||||
typedef enum {
|
||||
av_AudioEncoding = 1 << 0,
|
||||
av_AudioDecoding = 1 << 1,
|
||||
av_VideoEncoding = 1 << 2,
|
||||
av_VideoDecoding = 1 << 3
|
||||
} ToxAvCapabilities;
|
||||
|
||||
|
||||
/**
|
||||
* Encoding settings.
|
||||
*/
|
||||
typedef struct _ToxAvCSettings {
|
||||
ToxAvCallType call_type;
|
||||
|
||||
uint32_t video_bitrate; /* In kbits/s */
|
||||
uint16_t max_video_width; /* In px */
|
||||
uint16_t max_video_height; /* In px */
|
||||
|
||||
uint32_t audio_bitrate; /* In bits/s */
|
||||
uint16_t audio_frame_duration; /* In ms */
|
||||
uint32_t audio_sample_rate; /* In Hz */
|
||||
uint32_t audio_channels;
|
||||
} ToxAvCSettings;
|
||||
|
||||
extern const ToxAvCSettings av_DefaultSettings;
|
||||
|
||||
/**
|
||||
* Start new A/V session. There can only be one session at the time.
|
||||
*/
|
||||
ToxAv *toxav_new(Tox *messenger, int32_t max_calls);
|
||||
|
||||
/**
|
||||
* Remove A/V session.
|
||||
*/
|
||||
void toxav_kill(ToxAv *av);
|
||||
|
||||
/**
|
||||
* Returns the interval in milliseconds when the next toxav_do() should be called.
|
||||
* If no call is active at the moment returns 200.
|
||||
*/
|
||||
uint32_t toxav_do_interval(ToxAv *av);
|
||||
|
||||
/**
|
||||
* Main loop for the session. Best called right after tox_do();
|
||||
*/
|
||||
void toxav_do(ToxAv *av);
|
||||
|
||||
/**
|
||||
* Register callback for call state.
|
||||
*/
|
||||
void toxav_register_callstate_callback (ToxAv *av, ToxAVCallback cb, ToxAvCallbackID id, void *userdata);
|
||||
|
||||
/**
|
||||
* Register callback for audio data.
|
||||
*/
|
||||
void toxav_register_audio_callback (ToxAv *av, ToxAvAudioCallback cb, void *userdata);
|
||||
|
||||
/**
|
||||
* Register callback for video data.
|
||||
*/
|
||||
void toxav_register_video_callback (ToxAv *av, ToxAvVideoCallback cb, void *userdata);
|
||||
|
||||
/**
|
||||
* Call user. Use its friend_id.
|
||||
*/
|
||||
int toxav_call(ToxAv *av,
|
||||
int32_t *call_index,
|
||||
int friend_id,
|
||||
const ToxAvCSettings *csettings,
|
||||
int ringing_seconds);
|
||||
|
||||
/**
|
||||
* Hangup active call.
|
||||
*/
|
||||
int toxav_hangup(ToxAv *av, int32_t call_index);
|
||||
|
||||
/**
|
||||
* Answer incoming call. Pass the csettings that you will use.
|
||||
*/
|
||||
int toxav_answer(ToxAv *av, int32_t call_index, const ToxAvCSettings *csettings );
|
||||
|
||||
/**
|
||||
* Reject incoming call.
|
||||
*/
|
||||
int toxav_reject(ToxAv *av, int32_t call_index, const char *reason);
|
||||
|
||||
/**
|
||||
* Cancel outgoing request.
|
||||
*/
|
||||
int toxav_cancel(ToxAv *av, int32_t call_index, int peer_id, const char *reason);
|
||||
|
||||
/**
|
||||
* Notify peer that we are changing codec settings.
|
||||
*/
|
||||
int toxav_change_settings(ToxAv *av, int32_t call_index, const ToxAvCSettings *csettings);
|
||||
|
||||
/**
|
||||
* Terminate transmission. Note that transmission will be
|
||||
* terminated without informing remote peer. Usually called when we can't inform peer.
|
||||
*/
|
||||
int toxav_stop_call(ToxAv *av, int32_t call_index);
|
||||
|
||||
/**
|
||||
* Allocates transmission data. Must be call before calling toxav_prepare_* and toxav_send_*.
|
||||
* Also, it must be called when call is started
|
||||
*/
|
||||
int toxav_prepare_transmission(ToxAv *av, int32_t call_index, int support_video);
|
||||
|
||||
/**
|
||||
* Clears transmission data. Call this at the end of the transmission.
|
||||
*/
|
||||
int toxav_kill_transmission(ToxAv *av, int32_t call_index);
|
||||
|
||||
/**
|
||||
* Encode video frame.
|
||||
*/
|
||||
int toxav_prepare_video_frame ( ToxAv *av,
|
||||
int32_t call_index,
|
||||
uint8_t *dest,
|
||||
int dest_max,
|
||||
vpx_image_t *input);
|
||||
|
||||
/**
|
||||
* Send encoded video packet.
|
||||
*/
|
||||
int toxav_send_video ( ToxAv *av, int32_t call_index, const uint8_t *frame, uint32_t frame_size);
|
||||
|
||||
/**
|
||||
* Encode audio frame.
|
||||
*/
|
||||
int toxav_prepare_audio_frame ( ToxAv *av,
|
||||
int32_t call_index,
|
||||
uint8_t *dest,
|
||||
int dest_max,
|
||||
const int16_t *frame,
|
||||
int frame_size);
|
||||
|
||||
/**
|
||||
* Send encoded audio frame.
|
||||
*/
|
||||
int toxav_send_audio ( ToxAv *av, int32_t call_index, const uint8_t *frame, unsigned int size);
|
||||
|
||||
/**
|
||||
* Get codec settings from the peer. These were exchanged during call initialization
|
||||
* or when peer send us new csettings.
|
||||
*/
|
||||
int toxav_get_peer_csettings ( ToxAv *av, int32_t call_index, int peer, ToxAvCSettings *dest );
|
||||
|
||||
/**
|
||||
* Get friend id of peer participating in conversation.
|
||||
*/
|
||||
int toxav_get_peer_id ( ToxAv *av, int32_t call_index, int peer );
|
||||
|
||||
/**
|
||||
* Get current call state.
|
||||
*/
|
||||
ToxAvCallState toxav_get_call_state ( ToxAv *av, int32_t call_index );
|
||||
|
||||
/**
|
||||
* Is certain capability supported. Used to determine if encoding/decoding is ready.
|
||||
*/
|
||||
int toxav_capability_supported ( ToxAv *av, int32_t call_index, ToxAvCapabilities capability );
|
||||
|
||||
/**
|
||||
* Returns tox reference.
|
||||
*/
|
||||
Tox *toxav_get_tox (ToxAv *av);
|
||||
|
||||
/**
|
||||
* Returns number of active calls or -1 on error.
|
||||
*/
|
||||
int toxav_get_active_count (ToxAv *av);
|
||||
|
||||
/* Create a new toxav group.
|
||||
/*******************************************************************************
|
||||
*
|
||||
* :: Creation and destruction
|
||||
*
|
||||
* return group number on success.
|
||||
* return -1 on failure.
|
||||
*
|
||||
* Audio data callback format:
|
||||
* audio_callback(Tox *tox, int groupnumber, int peernumber, const int16_t *pcm, unsigned int samples, uint8_t channels, unsigned int sample_rate, void *userdata)
|
||||
*
|
||||
* Note that total size of pcm in bytes is equal to (samples * channels * sizeof(int16_t)).
|
||||
******************************************************************************/
|
||||
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.
|
||||
*/
|
||||
int toxav_add_av_groupchat(Tox *tox, void (*audio_callback)(Tox *, int, int, const int16_t *, unsigned int, uint8_t,
|
||||
unsigned int, void *), void *userdata);
|
||||
|
||||
/* Join a AV group (you need to have been invited first.)
|
||||
ToxAV *toxav_new(Tox *tox, TOXAV_ERR_NEW *error);
|
||||
/**
|
||||
* Releases all resources associated with the A/V session.
|
||||
*
|
||||
* returns group number on success
|
||||
* returns -1 on failure.
|
||||
*
|
||||
* Audio data callback format (same as the one for toxav_add_av_groupchat()):
|
||||
* audio_callback(Tox *tox, int groupnumber, int peernumber, const int16_t *pcm, unsigned int samples, uint8_t channels, unsigned int sample_rate, void *userdata)
|
||||
*
|
||||
* Note that total size of pcm in bytes is equal to (samples * channels * sizeof(int16_t)).
|
||||
* 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.
|
||||
*/
|
||||
int toxav_join_av_groupchat(Tox *tox, int32_t friendnumber, const uint8_t *data, uint16_t length,
|
||||
void (*audio_callback)(Tox *, int, int, const int16_t *, unsigned int, uint8_t, unsigned int, void *), void *userdata);
|
||||
|
||||
/* Send audio to the group chat.
|
||||
*
|
||||
* return 0 on success.
|
||||
* return -1 on failure.
|
||||
*
|
||||
* Note that total size of pcm in bytes is equal to (samples * channels * sizeof(int16_t)).
|
||||
*
|
||||
* Valid number of samples are ((sample rate) * (audio length (Valid ones are: 2.5, 5, 10, 20, 40 or 60 ms)) / 1000)
|
||||
* Valid number of channels are 1 or 2.
|
||||
* Valid sample rates are 8000, 12000, 16000, 24000, or 48000.
|
||||
*
|
||||
* Recommended values are: samples = 960, channels = 1, sample_rate = 48000
|
||||
void toxav_kill(ToxAV *av);
|
||||
/**
|
||||
* Returns the Tox instance the A/V object was created for.
|
||||
*/
|
||||
int toxav_group_send_audio(Tox *tox, int groupnumber, const int16_t *pcm, unsigned int samples, uint8_t channels,
|
||||
unsigned int sample_rate);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __TOXAV */
|
||||
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.
|
||||
*
|
||||
* Y - plane should be of size: height * width
|
||||
* U - plane should be of size: (height/2) * (width/2)
|
||||
* V - plane should be of size: (height/2) * (width/2)
|
||||
*
|
||||
* @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.
|
||||
*/
|
||||
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,
|
||||
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 planes Plane data. To access Y (Luminance) plane use index 0,
|
||||
* To access U (Chroma) plane use index 1,
|
||||
* To access V (Chroma) plane use index 2.
|
||||
* The size of plane data is derived from width and height where
|
||||
* Y = width * height, U = (width/2) * (height/2) and V = (width/2) * (height/2).
|
||||
* @param stride Strides data. Indexing is the same as in 'planes' param.
|
||||
*/
|
||||
typedef void toxav_receive_video_frame_cb(ToxAV *av, uint32_t friend_number,
|
||||
uint16_t width, uint16_t height,
|
||||
uint8_t const *planes[], int32_t const stride[],
|
||||
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);
|
|
@ -1,920 +0,0 @@
|
|||
/** 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>
|
||||
|
||||
#define MAX_ENCODE_TIME_US ((1000 / 24) * 1000)
|
||||
|
||||
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 */
|
||||
|
||||
uint32_t interval; /** Calculated interval */
|
||||
};
|
||||
|
||||
|
||||
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->interval = 200;
|
||||
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)
|
||||
{
|
||||
return av->interval;
|
||||
}
|
||||
|
||||
void toxav_iteration(ToxAV* av)
|
||||
{
|
||||
msi_do(av->msi);
|
||||
|
||||
uint64_t start = current_time_monotonic();
|
||||
uint32_t rc = 200 + av->dmssa; /* If no call is active interval is 200 */
|
||||
|
||||
IToxAVCall* i = av->calls[av->calls_head];
|
||||
for (; i; i = i->next) {
|
||||
if (i->active) {
|
||||
cs_do(i->cs);
|
||||
rc = MIN(i->cs->last_packet_frame_duration, rc);
|
||||
}
|
||||
}
|
||||
|
||||
av->interval = rc < av->dmssa ? 0 : rc - av->dmssa;
|
||||
av->dmsst += current_time_monotonic() - start;
|
||||
|
||||
if (++av->dmssc == 3) {
|
||||
av->dmssa = av->dmsst / 3 + 2 /* NOTE Magic Offset for precission */;
|
||||
av->dmssc = 0;
|
||||
av->dmsst = 0;
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
TOXAV_ERR_CALL_CONTROL rc = TOXAV_ERR_CALL_CONTROL_OK;
|
||||
|
||||
if (m_friend_exists(av->m, friend_number)) {
|
||||
rc = TOXAV_ERR_CALL_CONTROL_FRIEND_NOT_FOUND;
|
||||
goto END;
|
||||
}
|
||||
|
||||
|
||||
IToxAVCall* call = i_toxav_get_call(av, friend_number);
|
||||
if (call == NULL) {
|
||||
rc = TOXAV_ERR_CALL_CONTROL_FRIEND_NOT_IN_CALL;
|
||||
goto END;
|
||||
}
|
||||
|
||||
/* TODO rest of these */
|
||||
switch (control)
|
||||
{
|
||||
case TOXAV_CALL_CONTROL_RESUME: {
|
||||
|
||||
} break;
|
||||
|
||||
case TOXAV_CALL_CONTROL_PAUSE: {
|
||||
|
||||
} break;
|
||||
|
||||
case TOXAV_CALL_CONTROL_CANCEL: {
|
||||
if (av->msi->calls[call->call_idx]->state == msi_CallActive) {
|
||||
/* Hang up */
|
||||
msi_hangup(av->msi, call->call_idx);
|
||||
} else if (av->msi->calls[call->call_idx]->state == msi_CallRequested) {
|
||||
/* Reject the call */
|
||||
msi_reject(av->msi, call->call_idx);
|
||||
} else if (av->msi->calls[call->call_idx]->state == msi_CallRequesting) {
|
||||
/* Cancel the call */
|
||||
msi_cancel(av->msi, call->call_idx);
|
||||
}
|
||||
} break;
|
||||
|
||||
case TOXAV_CALL_CONTROL_MUTE_AUDIO: {
|
||||
|
||||
} break;
|
||||
|
||||
case TOXAV_CALL_CONTROL_MUTE_VIDEO: {
|
||||
|
||||
} break;
|
||||
}
|
||||
|
||||
END:
|
||||
if (error)
|
||||
*error = rc;
|
||||
|
||||
return rc == TOXAV_ERR_CALL_CONTROL_OK;
|
||||
}
|
||||
|
||||
bool toxav_set_audio_bit_rate(ToxAV* av, uint32_t friend_number, uint32_t audio_bit_rate, TOXAV_ERR_BIT_RATE* error)
|
||||
{
|
||||
/* TODO */
|
||||
}
|
||||
|
||||
bool toxav_set_video_bit_rate(ToxAV* av, uint32_t friend_number, uint32_t video_bit_rate, TOXAV_ERR_BIT_RATE* error)
|
||||
{
|
||||
/* TODO */
|
||||
}
|
||||
|
||||
void toxav_callback_request_video_frame(ToxAV* av, toxav_request_video_frame_cb* function, void* user_data)
|
||||
{
|
||||
/* TODO */
|
||||
}
|
||||
|
||||
bool toxav_send_video_frame(ToxAV* av, uint32_t friend_number, uint16_t width, uint16_t height, const uint8_t* y, const uint8_t* u, const uint8_t* v, TOXAV_ERR_SEND_FRAME* error)
|
||||
{
|
||||
TOXAV_ERR_SEND_FRAME rc = TOXAV_ERR_SEND_FRAME_OK;
|
||||
IToxAVCall* call;
|
||||
|
||||
if (m_friend_exists(av->m, friend_number)) {
|
||||
rc = TOXAV_ERR_SEND_FRAME_FRIEND_NOT_FOUND;
|
||||
goto END;
|
||||
}
|
||||
|
||||
call = i_toxav_get_call(av, friend_number);
|
||||
if (call == NULL) {
|
||||
rc = TOXAV_ERR_SEND_FRAME_FRIEND_NOT_IN_CALL;
|
||||
goto END;
|
||||
}
|
||||
|
||||
if (av->msi->calls[call->call_idx]->state != msi_CallActive) {
|
||||
/* TODO */
|
||||
rc = TOXAV_ERR_SEND_FRAME_NOT_REQUESTED;
|
||||
goto END;
|
||||
}
|
||||
|
||||
if ( y == NULL || u == NULL || v == NULL ) {
|
||||
rc = TOXAV_ERR_SEND_FRAME_NULL;
|
||||
goto END;
|
||||
}
|
||||
|
||||
if ( cs_set_sending_video_resolution(call->cs, width, height) != 0 ) {
|
||||
rc = TOXAV_ERR_SEND_FRAME_INVALID;
|
||||
goto END;
|
||||
}
|
||||
|
||||
{ /* Encode */
|
||||
vpx_image_t img;
|
||||
img.w = img.h = img.d_w = img.d_h = 0;
|
||||
vpx_img_alloc(&img, VPX_IMG_FMT_VPXI420, width, height, 1);
|
||||
|
||||
/* I420 "It comprises an NxM Y plane followed by (N/2)x(M/2) V and U planes."
|
||||
* http://fourcc.org/yuv.php#IYUV
|
||||
*/
|
||||
memcpy(img.planes[VPX_PLANE_Y], y, width * height);
|
||||
memcpy(img.planes[VPX_PLANE_U], u, (width/2) * (height/2));
|
||||
memcpy(img.planes[VPX_PLANE_V], v, (width/2) * (height/2));
|
||||
|
||||
int vrc = vpx_codec_encode(call->cs->v_encoder, &img,
|
||||
call->cs->frame_counter, 1, 0, MAX_ENCODE_TIME_US);
|
||||
|
||||
vpx_img_free(&img); /* FIXME don't free? */
|
||||
if ( vrc != VPX_CODEC_OK) {
|
||||
LOGGER_ERROR("Could not encode video frame: %s\n", vpx_codec_err_to_string(vrc));
|
||||
rc = TOXAV_ERR_SEND_FRAME_INVALID;
|
||||
goto END;
|
||||
}
|
||||
}
|
||||
|
||||
++call->cs->frame_counter;
|
||||
|
||||
{ /* Split and send */
|
||||
vpx_codec_iter_t iter = NULL;
|
||||
const vpx_codec_cx_pkt_t *pkt;
|
||||
|
||||
cs_init_video_splitter_cycle(call->cs);
|
||||
|
||||
while ( (pkt = vpx_codec_get_cx_data(call->cs->v_encoder, &iter)) ) {
|
||||
if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
|
||||
int parts = cs_update_video_splitter_cycle(call->cs, pkt->data.frame.buf,
|
||||
pkt->data.frame.sz);
|
||||
|
||||
if (parts < 0) /* Should never happen though */
|
||||
continue;
|
||||
|
||||
uint16_t part_size;
|
||||
const uint8_t *iter;
|
||||
|
||||
int i;
|
||||
for (i = 0; i < parts; i++) {
|
||||
iter = cs_iterate_split_video_frame(call->cs, &part_size);
|
||||
|
||||
if (rtp_send_msg(call->rtps[video_index], iter, part_size) < 0)
|
||||
goto END;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
END:
|
||||
if (error)
|
||||
*error = rc;
|
||||
|
||||
return rc == TOXAV_ERR_SEND_FRAME_OK;
|
||||
}
|
||||
|
||||
void toxav_callback_request_audio_frame(ToxAV* av, toxav_request_audio_frame_cb* function, void* user_data)
|
||||
{
|
||||
/* TODO */
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
TOXAV_ERR_SEND_FRAME rc = TOXAV_ERR_SEND_FRAME_OK;
|
||||
IToxAVCall* call;
|
||||
|
||||
if (m_friend_exists(av->m, friend_number)) {
|
||||
rc = TOXAV_ERR_SEND_FRAME_FRIEND_NOT_FOUND;
|
||||
goto END;
|
||||
}
|
||||
|
||||
call = i_toxav_get_call(av, friend_number);
|
||||
if (call == NULL) {
|
||||
rc = TOXAV_ERR_SEND_FRAME_FRIEND_NOT_IN_CALL;
|
||||
goto END;
|
||||
}
|
||||
|
||||
if (av->msi->calls[call->call_idx]->state != msi_CallActive) {
|
||||
/* TODO */
|
||||
rc = TOXAV_ERR_SEND_FRAME_NOT_REQUESTED;
|
||||
goto END;
|
||||
}
|
||||
|
||||
if ( pcm == NULL ) {
|
||||
rc = TOXAV_ERR_SEND_FRAME_NULL;
|
||||
goto END;
|
||||
}
|
||||
|
||||
if ( channels != 1 || channels != 2 ) {
|
||||
rc = TOXAV_ERR_SEND_FRAME_INVALID;
|
||||
goto END;
|
||||
}
|
||||
|
||||
{ /* Encode and send */
|
||||
/* TODO redundant? */
|
||||
cs_set_sending_audio_channels(call->cs, channels);
|
||||
cs_set_sending_audio_sampling_rate(call->cs, sampling_rate);
|
||||
|
||||
uint8_t dest[sample_count * channels * 2 /* sizeof(uint16_t) */];
|
||||
int vrc = opus_encode(call->cs->audio_encoder, pcm, sample_count, dest, sizeof (dest));
|
||||
|
||||
if (vrc < 0) {
|
||||
LOGGER_WARNING("Failed to encode frame");
|
||||
rc = TOXAV_ERR_SEND_FRAME_INVALID;
|
||||
goto END;
|
||||
}
|
||||
|
||||
vrc = rtp_send_msg(call->rtps[audio_index], dest, vrc);
|
||||
/* TODO check for error? */
|
||||
}
|
||||
|
||||
END:
|
||||
if (error)
|
||||
*error = rc;
|
||||
|
||||
return rc == TOXAV_ERR_SEND_FRAME_OK;
|
||||
}
|
||||
|
||||
void toxav_callback_receive_video_frame(ToxAV* av, toxav_receive_video_frame_cb* function, void* user_data)
|
||||
{
|
||||
av->vcb.first = function;
|
||||
av->vcb.second = user_data;
|
||||
}
|
||||
|
||||
void toxav_callback_receive_audio_frame(ToxAV* av, toxav_receive_audio_frame_cb* function, void* user_data)
|
||||
{
|
||||
av->acb.first = function;
|
||||
av->acb.second = 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;
|
||||
|
||||
|
||||
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);
|
||||
}
|
|
@ -1,481 +0,0 @@
|
|||
#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.
|
||||
*
|
||||
* Y - plane should be of size: height * width
|
||||
* U - plane should be of size: (height/2) * (width/2)
|
||||
* V - plane should be of size: (height/2) * (width/2)
|
||||
*
|
||||
* @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.
|
||||
*/
|
||||
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,
|
||||
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);
|
689
toxav/toxav_new_1.c
Normal file
689
toxav/toxav_new_1.c
Normal file
|
@ -0,0 +1,689 @@
|
|||
/** 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 */
|
||||
|
||||
#define __TOX_DEFINED__
|
||||
typedef struct Messenger Tox;
|
||||
|
||||
#define _GNU_SOURCE /* implicit declaration warning */
|
||||
|
||||
#include "codec.h"
|
||||
#include "msi.h"
|
||||
#include "group.h"
|
||||
|
||||
#include "../toxcore/logger.h"
|
||||
#include "../toxcore/util.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* Assume 24 fps*/
|
||||
#define MAX_ENCODE_TIME_US ((1000 / 24) * 1000)
|
||||
|
||||
/* true if invalid call index */
|
||||
#define CALL_INVALID_INDEX(idx, max) (idx < 0 || idx >= max)
|
||||
|
||||
const ToxAvCSettings av_DefaultSettings = {
|
||||
av_TypeAudio,
|
||||
|
||||
500,
|
||||
1280,
|
||||
720,
|
||||
|
||||
32000,
|
||||
20,
|
||||
48000,
|
||||
1
|
||||
};
|
||||
|
||||
static const uint32_t jbuf_capacity = 6;
|
||||
static const uint8_t audio_index = 0, video_index = 1;
|
||||
|
||||
typedef struct _ToxAvCall {
|
||||
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 *crtps[2]; /** Audio is first and video is second */
|
||||
CSSession *cs;
|
||||
_Bool active;
|
||||
} ToxAvCall;
|
||||
|
||||
struct _ToxAv {
|
||||
Messenger *messenger;
|
||||
MSISession *msi_session; /** Main msi session */
|
||||
ToxAvCall *calls; /** Per-call params */
|
||||
uint32_t max_calls;
|
||||
|
||||
PAIR(ToxAvAudioCallback, void *) acb;
|
||||
PAIR(ToxAvVideoCallback, void *) vcb;
|
||||
|
||||
/* Decode time measure */
|
||||
int32_t dectmsscount; /** Measure count */
|
||||
int32_t dectmsstotal; /** Last cycle total */
|
||||
int32_t avgdectms; /** Average decoding time in ms */
|
||||
};
|
||||
|
||||
static const MSICSettings *msicsettings_cast (const ToxAvCSettings *from)
|
||||
{
|
||||
assert(sizeof(MSICSettings) == sizeof(ToxAvCSettings));
|
||||
return (const MSICSettings *) from;
|
||||
}
|
||||
|
||||
static const ToxAvCSettings *toxavcsettings_cast (const MSICSettings *from)
|
||||
{
|
||||
assert(sizeof(MSICSettings) == sizeof(ToxAvCSettings));
|
||||
return (const ToxAvCSettings *) from;
|
||||
|
||||
}
|
||||
|
||||
ToxAv *toxav_new( Tox *messenger, int32_t max_calls)
|
||||
{
|
||||
ToxAv *av = calloc ( sizeof(ToxAv), 1);
|
||||
|
||||
if (av == NULL) {
|
||||
LOGGER_WARNING("Allocation failed!");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
av->messenger = (Messenger *)messenger;
|
||||
av->msi_session = msi_new(av->messenger, max_calls);
|
||||
av->msi_session->agent_handler = av;
|
||||
av->calls = calloc(sizeof(ToxAvCall), max_calls);
|
||||
av->max_calls = max_calls;
|
||||
|
||||
unsigned int i;
|
||||
|
||||
for (i = 0; i < max_calls; ++i) {
|
||||
if (create_recursive_mutex(av->calls[i].mutex_control) != 0 ) {
|
||||
LOGGER_WARNING("Failed to init call(%u) mutex!", i);
|
||||
msi_kill(av->msi_session);
|
||||
|
||||
free(av->calls);
|
||||
free(av);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return av;
|
||||
}
|
||||
|
||||
void toxav_kill ( ToxAv *av )
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
for (i = 0; i < av->max_calls; i ++) {
|
||||
if ( av->calls[i].crtps[audio_index] )
|
||||
rtp_kill(av->calls[i].crtps[audio_index], av->msi_session->messenger_handle);
|
||||
|
||||
|
||||
if ( av->calls[i].crtps[video_index] )
|
||||
rtp_kill(av->calls[i].crtps[video_index], av->msi_session->messenger_handle);
|
||||
|
||||
if ( av->calls[i].cs )
|
||||
cs_kill(av->calls[i].cs);
|
||||
|
||||
pthread_mutex_destroy(av->calls[i].mutex_control);
|
||||
}
|
||||
|
||||
msi_kill(av->msi_session);
|
||||
|
||||
free(av->calls);
|
||||
free(av);
|
||||
}
|
||||
|
||||
uint32_t toxav_do_interval(ToxAv *av)
|
||||
{
|
||||
int i = 0;
|
||||
uint32_t rc = 200 + av->avgdectms; /* Return 200 if no call is active */
|
||||
|
||||
for (; i < av->max_calls; i ++) {
|
||||
pthread_mutex_lock(av->calls[i].mutex_control);
|
||||
|
||||
if (av->calls[i].active) {
|
||||
/* This should work. Video payload will always come in greater intervals */
|
||||
rc = MIN(av->calls[i].cs->audio_decoder_frame_duration, rc);
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(av->calls[i].mutex_control);
|
||||
}
|
||||
|
||||
return rc < av->avgdectms ? 0 : rc - av->avgdectms;
|
||||
}
|
||||
|
||||
void toxav_do(ToxAv *av)
|
||||
{
|
||||
msi_do(av->msi_session);
|
||||
|
||||
uint64_t start = current_time_monotonic();
|
||||
|
||||
uint32_t i = 0;
|
||||
|
||||
for (; i < av->max_calls; i ++) {
|
||||
pthread_mutex_lock(av->calls[i].mutex_control);
|
||||
|
||||
if (av->calls[i].active) {
|
||||
pthread_mutex_lock(av->calls[i].mutex_do);
|
||||
pthread_mutex_unlock(av->calls[i].mutex_control);
|
||||
cs_do(av->calls[i].cs);
|
||||
pthread_mutex_unlock(av->calls[i].mutex_do);
|
||||
} else {
|
||||
pthread_mutex_unlock(av->calls[i].mutex_control);
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t end = current_time_monotonic();
|
||||
|
||||
/* TODO maybe use variable for sizes */
|
||||
av->dectmsstotal += end - start;
|
||||
|
||||
if (++av->dectmsscount == 3) {
|
||||
av->avgdectms = av->dectmsstotal / 3 + 2 /* NOTE Magic Offset */;
|
||||
av->dectmsscount = 0;
|
||||
av->dectmsstotal = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void toxav_register_callstate_callback ( ToxAv *av, ToxAVCallback cb, ToxAvCallbackID id, void *userdata )
|
||||
{
|
||||
msi_register_callback(av->msi_session, (MSICallbackType)cb, (MSICallbackID) id, userdata);
|
||||
}
|
||||
|
||||
void toxav_register_audio_callback(ToxAv *av, ToxAvAudioCallback cb, void *userdata)
|
||||
{
|
||||
av->acb.first = cb;
|
||||
av->acb.second = userdata;
|
||||
}
|
||||
|
||||
void toxav_register_video_callback(ToxAv *av, ToxAvVideoCallback cb, void *userdata)
|
||||
{
|
||||
av->vcb.first = cb;
|
||||
av->vcb.second = userdata;
|
||||
}
|
||||
|
||||
int toxav_call (ToxAv *av,
|
||||
int32_t *call_index,
|
||||
int user,
|
||||
const ToxAvCSettings *csettings,
|
||||
int ringing_seconds )
|
||||
{
|
||||
return msi_invite(av->msi_session, call_index, msicsettings_cast(csettings), ringing_seconds * 1000, user);
|
||||
}
|
||||
|
||||
int toxav_hangup ( ToxAv *av, int32_t call_index )
|
||||
{
|
||||
return msi_hangup(av->msi_session, call_index);
|
||||
}
|
||||
|
||||
int toxav_answer ( ToxAv *av, int32_t call_index, const ToxAvCSettings *csettings )
|
||||
{
|
||||
return msi_answer(av->msi_session, call_index, msicsettings_cast(csettings));
|
||||
}
|
||||
|
||||
int toxav_reject ( ToxAv *av, int32_t call_index, const char *reason )
|
||||
{
|
||||
return msi_reject(av->msi_session, call_index, reason);
|
||||
}
|
||||
|
||||
int toxav_cancel ( ToxAv *av, int32_t call_index, int peer_id, const char *reason )
|
||||
{
|
||||
return msi_cancel(av->msi_session, call_index, peer_id, reason);
|
||||
}
|
||||
|
||||
int toxav_change_settings(ToxAv *av, int32_t call_index, const ToxAvCSettings *csettings)
|
||||
{
|
||||
return msi_change_csettings(av->msi_session, call_index, msicsettings_cast(csettings));
|
||||
}
|
||||
|
||||
int toxav_stop_call ( ToxAv *av, int32_t call_index )
|
||||
{
|
||||
return msi_stopcall(av->msi_session, call_index);
|
||||
}
|
||||
|
||||
int toxav_prepare_transmission ( ToxAv *av, int32_t call_index, int support_video )
|
||||
{
|
||||
if ( !av->msi_session || CALL_INVALID_INDEX(call_index, av->msi_session->max_calls) ||
|
||||
!av->msi_session->calls[call_index] || !av->msi_session->calls[call_index]->csettings_peer) {
|
||||
LOGGER_ERROR("Error while starting RTP session: invalid call!\n");
|
||||
return av_ErrorNoCall;
|
||||
}
|
||||
|
||||
ToxAvCall *call = &av->calls[call_index];
|
||||
|
||||
pthread_mutex_lock(call->mutex_control);
|
||||
|
||||
if (call->active) {
|
||||
pthread_mutex_unlock(call->mutex_control);
|
||||
LOGGER_ERROR("Error while starting RTP session: call already active!\n");
|
||||
return av_ErrorAlreadyInCallWithPeer;
|
||||
}
|
||||
|
||||
if (pthread_mutex_init(call->mutex_encoding_audio, NULL) != 0
|
||||
|| pthread_mutex_init(call->mutex_encoding_video, NULL) != 0 || pthread_mutex_init(call->mutex_do, NULL) != 0) {
|
||||
pthread_mutex_unlock(call->mutex_control);
|
||||
LOGGER_ERROR("Error while starting RTP session: mutex initializing failed!\n");
|
||||
return av_ErrorUnknown;
|
||||
}
|
||||
|
||||
const ToxAvCSettings *c_peer = toxavcsettings_cast
|
||||
(&av->msi_session->calls[call_index]->csettings_peer[0]);
|
||||
const ToxAvCSettings *c_self = toxavcsettings_cast
|
||||
(&av->msi_session->calls[call_index]->csettings_local);
|
||||
|
||||
LOGGER_DEBUG(
|
||||
"Type: %u(s) %u(p)\n"
|
||||
"Video bitrate: %u(s) %u(p)\n"
|
||||
"Video height: %u(s) %u(p)\n"
|
||||
"Video width: %u(s) %u(p)\n"
|
||||
"Audio bitrate: %u(s) %u(p)\n"
|
||||
"Audio framedur: %u(s) %u(p)\n"
|
||||
"Audio sample rate: %u(s) %u(p)\n"
|
||||
"Audio channels: %u(s) %u(p)\n",
|
||||
c_self->call_type, c_peer->call_type,
|
||||
c_self->video_bitrate, c_peer->video_bitrate,
|
||||
c_self->max_video_height, c_peer->max_video_height,
|
||||
c_self->max_video_width, c_peer->max_video_width,
|
||||
c_self->audio_bitrate, c_peer->audio_bitrate,
|
||||
c_self->audio_frame_duration, c_peer->audio_frame_duration,
|
||||
c_self->audio_sample_rate, c_peer->audio_sample_rate,
|
||||
c_self->audio_channels, c_peer->audio_channels );
|
||||
|
||||
if ( !(call->cs = cs_new(c_self, c_peer, jbuf_capacity, support_video)) ) {
|
||||
LOGGER_ERROR("Error while starting Codec State!\n");
|
||||
pthread_mutex_unlock(call->mutex_control);
|
||||
return av_ErrorInitializingCodecs;
|
||||
}
|
||||
|
||||
call->cs->agent = av;
|
||||
call->cs->call_idx = call_index;
|
||||
|
||||
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;
|
||||
|
||||
|
||||
call->crtps[audio_index] =
|
||||
rtp_new(msi_TypeAudio, av->messenger, av->msi_session->calls[call_index]->peers[0]);
|
||||
|
||||
if ( !call->crtps[audio_index] ) {
|
||||
LOGGER_ERROR("Error while starting audio RTP session!\n");
|
||||
goto error;
|
||||
}
|
||||
|
||||
call->crtps[audio_index]->cs = call->cs;
|
||||
|
||||
if ( support_video ) {
|
||||
call->crtps[video_index] =
|
||||
rtp_new(msi_TypeVideo, av->messenger, av->msi_session->calls[call_index]->peers[0]);
|
||||
|
||||
if ( !call->crtps[video_index] ) {
|
||||
LOGGER_ERROR("Error while starting video RTP session!\n");
|
||||
goto error;
|
||||
}
|
||||
|
||||
call->crtps[video_index]->cs = call->cs;
|
||||
}
|
||||
|
||||
call->active = 1;
|
||||
pthread_mutex_unlock(call->mutex_control);
|
||||
return av_ErrorNone;
|
||||
error:
|
||||
rtp_kill(call->crtps[audio_index], av->messenger);
|
||||
call->crtps[audio_index] = NULL;
|
||||
rtp_kill(call->crtps[video_index], av->messenger);
|
||||
call->crtps[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 av_ErrorCreatingRtpSessions;
|
||||
}
|
||||
|
||||
int toxav_kill_transmission ( ToxAv *av, int32_t call_index )
|
||||
{
|
||||
if (CALL_INVALID_INDEX(call_index, av->msi_session->max_calls)) {
|
||||
LOGGER_WARNING("Invalid call index: %d", call_index);
|
||||
return av_ErrorNoCall;
|
||||
}
|
||||
|
||||
ToxAvCall *call = &av->calls[call_index];
|
||||
|
||||
pthread_mutex_lock(call->mutex_control);
|
||||
|
||||
if (!call->active) {
|
||||
pthread_mutex_unlock(call->mutex_control);
|
||||
LOGGER_WARNING("Action on inactive call: %d", call_index);
|
||||
return av_ErrorInvalidState;
|
||||
}
|
||||
|
||||
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->crtps[audio_index], av->messenger);
|
||||
call->crtps[audio_index] = NULL;
|
||||
rtp_kill(call->crtps[video_index], av->messenger);
|
||||
call->crtps[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);
|
||||
|
||||
return av_ErrorNone;
|
||||
}
|
||||
|
||||
static int toxav_send_rtp_payload(ToxAv *av,
|
||||
ToxAvCall *call,
|
||||
ToxAvCallType type,
|
||||
const uint8_t *payload,
|
||||
unsigned int length)
|
||||
{
|
||||
if (call->crtps[type - av_TypeAudio]) {
|
||||
|
||||
/* Audio */
|
||||
if (type == av_TypeAudio)
|
||||
return rtp_send_msg(call->crtps[audio_index], av->messenger, payload, length);
|
||||
|
||||
/* Video */
|
||||
int parts = cs_split_video_payload(call->cs, payload, length);
|
||||
|
||||
if (parts < 0) return parts;
|
||||
|
||||
uint16_t part_size;
|
||||
const uint8_t *iter;
|
||||
|
||||
int i;
|
||||
|
||||
for (i = 0; i < parts; i++) {
|
||||
iter = cs_iterate_split_video_frame(call->cs, &part_size);
|
||||
|
||||
if (rtp_send_msg(call->crtps[video_index], av->messenger, iter, part_size) < 0)
|
||||
return av_ErrorSendingPayload;
|
||||
}
|
||||
|
||||
return av_ErrorNone;
|
||||
|
||||
} else return av_ErrorNoRtpSession;
|
||||
}
|
||||
|
||||
int toxav_prepare_video_frame ( ToxAv *av, int32_t call_index, uint8_t *dest, int dest_max, vpx_image_t *input)
|
||||
{
|
||||
if (CALL_INVALID_INDEX(call_index, av->msi_session->max_calls)) {
|
||||
LOGGER_WARNING("Invalid call index: %d", call_index);
|
||||
return av_ErrorNoCall;
|
||||
}
|
||||
|
||||
|
||||
ToxAvCall *call = &av->calls[call_index];
|
||||
pthread_mutex_lock(call->mutex_control);
|
||||
|
||||
if (!call->active) {
|
||||
pthread_mutex_unlock(call->mutex_control);
|
||||
LOGGER_WARNING("Action on inactive call: %d", call_index);
|
||||
return av_ErrorInvalidState;
|
||||
}
|
||||
|
||||
if (cs_set_sending_video_resolution(call->cs, input->d_w, input->d_h) < 0) {
|
||||
pthread_mutex_unlock(call->mutex_control);
|
||||
return av_ErrorSettingVideoResolution;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
if ( rc != VPX_CODEC_OK) {
|
||||
LOGGER_ERROR("Could not encode video frame: %s\n", vpx_codec_err_to_string(rc));
|
||||
pthread_mutex_unlock(call->mutex_encoding_video);
|
||||
return av_ErrorEncodingVideo;
|
||||
}
|
||||
|
||||
++call->cs->frame_counter;
|
||||
|
||||
vpx_codec_iter_t iter = NULL;
|
||||
const vpx_codec_cx_pkt_t *pkt;
|
||||
int copied = 0;
|
||||
|
||||
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);
|
||||
return av_ErrorPacketTooLarge;
|
||||
}
|
||||
|
||||
memcpy(dest + copied, pkt->data.frame.buf, pkt->data.frame.sz);
|
||||
copied += pkt->data.frame.sz;
|
||||
}
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(call->mutex_encoding_video);
|
||||
return copied;
|
||||
}
|
||||
|
||||
int toxav_send_video ( ToxAv *av, int32_t call_index, const uint8_t *frame, unsigned int frame_size)
|
||||
{
|
||||
|
||||
if (CALL_INVALID_INDEX(call_index, av->msi_session->max_calls)) {
|
||||
LOGGER_WARNING("Invalid call index: %d", call_index);
|
||||
return av_ErrorNoCall;
|
||||
}
|
||||
|
||||
ToxAvCall *call = &av->calls[call_index];
|
||||
pthread_mutex_lock(call->mutex_control);
|
||||
|
||||
|
||||
if (!call->active) {
|
||||
pthread_mutex_unlock(call->mutex_control);
|
||||
LOGGER_WARNING("Action on inactive call: %d", call_index);
|
||||
return av_ErrorInvalidState;
|
||||
}
|
||||
|
||||
int rc = toxav_send_rtp_payload(av, call, av_TypeVideo, frame, frame_size);
|
||||
pthread_mutex_unlock(call->mutex_control);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
int toxav_prepare_audio_frame ( ToxAv *av,
|
||||
int32_t call_index,
|
||||
uint8_t *dest,
|
||||
int dest_max,
|
||||
const int16_t *frame,
|
||||
int frame_size)
|
||||
{
|
||||
if (CALL_INVALID_INDEX(call_index, av->msi_session->max_calls)) {
|
||||
LOGGER_WARNING("Action on nonexisting call: %d", call_index);
|
||||
return av_ErrorNoCall;
|
||||
}
|
||||
|
||||
ToxAvCall *call = &av->calls[call_index];
|
||||
pthread_mutex_lock(call->mutex_control);
|
||||
|
||||
if (!call->active) {
|
||||
pthread_mutex_unlock(call->mutex_control);
|
||||
LOGGER_WARNING("Action on inactive call: %d", call_index);
|
||||
return av_ErrorInvalidState;
|
||||
}
|
||||
|
||||
pthread_mutex_lock(call->mutex_encoding_audio);
|
||||
pthread_mutex_unlock(call->mutex_control);
|
||||
int32_t rc = opus_encode(call->cs->audio_encoder, frame, frame_size, dest, dest_max);
|
||||
pthread_mutex_unlock(call->mutex_encoding_audio);
|
||||
|
||||
if (rc < 0) {
|
||||
LOGGER_ERROR("Failed to encode payload: %s\n", opus_strerror(rc));
|
||||
return av_ErrorEncodingAudio;
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
int toxav_send_audio ( ToxAv *av, int32_t call_index, const uint8_t *data, unsigned int size)
|
||||
{
|
||||
if (CALL_INVALID_INDEX(call_index, av->msi_session->max_calls)) {
|
||||
LOGGER_WARNING("Action on nonexisting call: %d", call_index);
|
||||
return av_ErrorNoCall;
|
||||
}
|
||||
|
||||
ToxAvCall *call = &av->calls[call_index];
|
||||
pthread_mutex_lock(call->mutex_control);
|
||||
|
||||
|
||||
if (!call->active) {
|
||||
pthread_mutex_unlock(call->mutex_control);
|
||||
LOGGER_WARNING("Action on inactive call: %d", call_index);
|
||||
return av_ErrorInvalidState;
|
||||
}
|
||||
|
||||
int rc = toxav_send_rtp_payload(av, call, av_TypeAudio, data, size);
|
||||
pthread_mutex_unlock(call->mutex_control);
|
||||
return rc;
|
||||
}
|
||||
|
||||
int toxav_get_peer_csettings ( ToxAv *av, int32_t call_index, int peer, ToxAvCSettings *dest )
|
||||
{
|
||||
if ( peer < 0 || CALL_INVALID_INDEX(call_index, av->msi_session->max_calls) ||
|
||||
!av->msi_session->calls[call_index] || av->msi_session->calls[call_index]->peer_count <= peer )
|
||||
return av_ErrorNoCall;
|
||||
|
||||
*dest = *toxavcsettings_cast(&av->msi_session->calls[call_index]->csettings_peer[peer]);
|
||||
return av_ErrorNone;
|
||||
}
|
||||
|
||||
int toxav_get_peer_id ( ToxAv *av, int32_t call_index, int peer )
|
||||
{
|
||||
if ( peer < 0 || CALL_INVALID_INDEX(call_index, av->msi_session->max_calls) || !av->msi_session->calls[call_index]
|
||||
|| av->msi_session->calls[call_index]->peer_count <= peer )
|
||||
return av_ErrorNoCall;
|
||||
|
||||
return av->msi_session->calls[call_index]->peers[peer];
|
||||
}
|
||||
|
||||
ToxAvCallState toxav_get_call_state(ToxAv *av, int32_t call_index)
|
||||
{
|
||||
if ( CALL_INVALID_INDEX(call_index, av->msi_session->max_calls) || !av->msi_session->calls[call_index] )
|
||||
return av_CallNonExistent;
|
||||
|
||||
return av->msi_session->calls[call_index]->state;
|
||||
|
||||
}
|
||||
|
||||
int toxav_capability_supported ( ToxAv *av, int32_t call_index, ToxAvCapabilities capability )
|
||||
{
|
||||
}
|
||||
|
||||
Tox *toxav_get_tox(ToxAv *av)
|
||||
{
|
||||
return (Tox *)av->messenger;
|
||||
}
|
||||
|
||||
int toxav_get_active_count(ToxAv *av)
|
||||
{
|
||||
if (!av) return -1;
|
||||
|
||||
int rc = 0, i = 0;
|
||||
|
||||
for (; i < av->max_calls; i++) {
|
||||
pthread_mutex_lock(av->calls[i].mutex_control);
|
||||
|
||||
if (av->calls[i].active) rc++;
|
||||
|
||||
pthread_mutex_unlock(av->calls[i].mutex_control);
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* Create a new toxav group.
|
||||
*
|
||||
* return group number on success.
|
||||
* return -1 on failure.
|
||||
*
|
||||
* Audio data callback format:
|
||||
* audio_callback(Tox *tox, int groupnumber, int peernumber, const int16_t *pcm, unsigned int samples, uint8_t channels, unsigned int sample_rate, void *userdata)
|
||||
*
|
||||
* Note that total size of pcm in bytes is equal to (samples * channels * sizeof(int16_t)).
|
||||
*/
|
||||
int toxav_add_av_groupchat(Tox *tox, void (*audio_callback)(Messenger *, int, int, const int16_t *, unsigned int,
|
||||
uint8_t, unsigned int, void *), void *userdata)
|
||||
{
|
||||
Messenger *m = tox;
|
||||
return add_av_groupchat(m->group_chat_object, audio_callback, userdata);
|
||||
}
|
||||
|
||||
/* Join a AV group (you need to have been invited first.)
|
||||
*
|
||||
* returns group number on success
|
||||
* returns -1 on failure.
|
||||
*
|
||||
* Audio data callback format (same as the one for toxav_add_av_groupchat()):
|
||||
* audio_callback(Tox *tox, int groupnumber, int peernumber, const int16_t *pcm, unsigned int samples, uint8_t channels, unsigned int sample_rate, void *userdata)
|
||||
*
|
||||
* Note that total size of pcm in bytes is equal to (samples * channels * sizeof(int16_t)).
|
||||
*/
|
||||
int toxav_join_av_groupchat(Tox *tox, int32_t friendnumber, const uint8_t *data, uint16_t length,
|
||||
void (*audio_callback)(Messenger *, int, int, const int16_t *, unsigned int, uint8_t, unsigned int, void *),
|
||||
void *userdata)
|
||||
{
|
||||
Messenger *m = tox;
|
||||
return join_av_groupchat(m->group_chat_object, friendnumber, data, length, audio_callback, userdata);
|
||||
}
|
||||
|
||||
/* Send audio to the group chat.
|
||||
*
|
||||
* return 0 on success.
|
||||
* return -1 on failure.
|
||||
*
|
||||
* Note that total size of pcm in bytes is equal to (samples * channels * sizeof(int16_t)).
|
||||
*
|
||||
* Valid number of samples are ((sample rate) * (audio length (Valid ones are: 2.5, 5, 10, 20, 40 or 60 ms)) / 1000)
|
||||
* Valid number of channels are 1 or 2.
|
||||
* Valid sample rates are 8000, 12000, 16000, 24000, or 48000.
|
||||
*
|
||||
* Recommended values are: samples = 960, channels = 1, sample_rate = 48000
|
||||
*/
|
||||
int toxav_group_send_audio(Tox *tox, int groupnumber, const int16_t *pcm, unsigned int samples, uint8_t channels,
|
||||
unsigned int sample_rate)
|
||||
{
|
||||
Messenger *m = tox;
|
||||
return group_send_audio(m->group_chat_object, groupnumber, pcm, samples, channels, sample_rate);
|
||||
}
|
||||
|
329
toxav/toxav_new_1.h
Normal file
329
toxav/toxav_new_1.h
Normal file
|
@ -0,0 +1,329 @@
|
|||
/** toxav.h
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#ifndef __TOXAV
|
||||
#define __TOXAV
|
||||
#include <inttypes.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct _ToxAv ToxAv;
|
||||
|
||||
/* vpx_image_t */
|
||||
#include <vpx/vpx_image.h>
|
||||
|
||||
typedef void ( *ToxAVCallback ) ( void *agent, int32_t call_idx, void *arg );
|
||||
typedef void ( *ToxAvAudioCallback ) (void *agent, int32_t call_idx, const int16_t *PCM, uint16_t size, void *data);
|
||||
typedef void ( *ToxAvVideoCallback ) (void *agent, int32_t call_idx, const vpx_image_t *img, void *data);
|
||||
|
||||
#ifndef __TOX_DEFINED__
|
||||
#define __TOX_DEFINED__
|
||||
typedef struct Tox Tox;
|
||||
#endif
|
||||
|
||||
#define RTP_PAYLOAD_SIZE 65535
|
||||
|
||||
|
||||
/**
|
||||
* Callbacks ids that handle the call states.
|
||||
*/
|
||||
typedef enum {
|
||||
av_OnInvite, /* Incoming call */
|
||||
av_OnRinging, /* When peer is ready to accept/reject the call */
|
||||
av_OnStart, /* Call (RTP transmission) started */
|
||||
av_OnCancel, /* The side that initiated call canceled invite */
|
||||
av_OnReject, /* The side that was invited rejected the call */
|
||||
av_OnEnd, /* Call that was active ended */
|
||||
av_OnRequestTimeout, /* When the requested action didn't get response in specified time */
|
||||
av_OnPeerTimeout, /* Peer timed out; stop the call */
|
||||
av_OnPeerCSChange, /* Peer changing Csettings. Prepare for changed AV */
|
||||
av_OnSelfCSChange /* Csettings change confirmation. Once triggered peer is ready to recv changed AV */
|
||||
} ToxAvCallbackID;
|
||||
|
||||
|
||||
/**
|
||||
* Call type identifier.
|
||||
*/
|
||||
typedef enum {
|
||||
av_TypeAudio = 192,
|
||||
av_TypeVideo
|
||||
} ToxAvCallType;
|
||||
|
||||
|
||||
typedef enum {
|
||||
av_CallNonExistent = -1,
|
||||
av_CallInviting, /* when sending call invite */
|
||||
av_CallStarting, /* when getting call invite */
|
||||
av_CallActive,
|
||||
av_CallHold,
|
||||
av_CallHungUp
|
||||
} ToxAvCallState;
|
||||
|
||||
/**
|
||||
* Error indicators. Values under -20 are reserved for toxcore.
|
||||
*/
|
||||
typedef enum {
|
||||
av_ErrorNone = 0,
|
||||
av_ErrorUnknown = -1, /* Unknown error */
|
||||
av_ErrorNoCall = -20, /* Trying to perform call action while not in a call */
|
||||
av_ErrorInvalidState = -21, /* Trying to perform call action while in invalid state*/
|
||||
av_ErrorAlreadyInCallWithPeer = -22, /* Trying to call peer when already in a call with peer */
|
||||
av_ErrorReachedCallLimit = -23, /* Cannot handle more calls */
|
||||
av_ErrorInitializingCodecs = -30, /* Failed creating CSSession */
|
||||
av_ErrorSettingVideoResolution = -31, /* Error setting resolution */
|
||||
av_ErrorSettingVideoBitrate = -32, /* Error setting bitrate */
|
||||
av_ErrorSplittingVideoPayload = -33, /* Error splitting video payload */
|
||||
av_ErrorEncodingVideo = -34, /* vpx_codec_encode failed */
|
||||
av_ErrorEncodingAudio = -35, /* opus_encode failed */
|
||||
av_ErrorSendingPayload = -40, /* Sending lossy packet failed */
|
||||
av_ErrorCreatingRtpSessions = -41, /* One of the rtp sessions failed to initialize */
|
||||
av_ErrorNoRtpSession = -50, /* Trying to perform rtp action on invalid session */
|
||||
av_ErrorInvalidCodecState = -51, /* Codec state not initialized */
|
||||
av_ErrorPacketTooLarge = -52, /* Split packet exceeds it's limit */
|
||||
} ToxAvError;
|
||||
|
||||
|
||||
/**
|
||||
* Locally supported capabilities.
|
||||
*/
|
||||
typedef enum {
|
||||
av_AudioEncoding = 1 << 0,
|
||||
av_AudioDecoding = 1 << 1,
|
||||
av_VideoEncoding = 1 << 2,
|
||||
av_VideoDecoding = 1 << 3
|
||||
} ToxAvCapabilities;
|
||||
|
||||
|
||||
/**
|
||||
* Encoding settings.
|
||||
*/
|
||||
typedef struct _ToxAvCSettings {
|
||||
ToxAvCallType call_type;
|
||||
|
||||
uint32_t video_bitrate; /* In kbits/s */
|
||||
uint16_t max_video_width; /* In px */
|
||||
uint16_t max_video_height; /* In px */
|
||||
|
||||
uint32_t audio_bitrate; /* In bits/s */
|
||||
uint16_t audio_frame_duration; /* In ms */
|
||||
uint32_t audio_sample_rate; /* In Hz */
|
||||
uint32_t audio_channels;
|
||||
} ToxAvCSettings;
|
||||
|
||||
extern const ToxAvCSettings av_DefaultSettings;
|
||||
|
||||
/**
|
||||
* Start new A/V session. There can only be one session at the time.
|
||||
*/
|
||||
ToxAv *toxav_new(Tox *messenger, int32_t max_calls);
|
||||
|
||||
/**
|
||||
* Remove A/V session.
|
||||
*/
|
||||
void toxav_kill(ToxAv *av);
|
||||
|
||||
/**
|
||||
* Returns the interval in milliseconds when the next toxav_do() should be called.
|
||||
* If no call is active at the moment returns 200.
|
||||
*/
|
||||
uint32_t toxav_do_interval(ToxAv *av);
|
||||
|
||||
/**
|
||||
* Main loop for the session. Best called right after tox_do();
|
||||
*/
|
||||
void toxav_do(ToxAv *av);
|
||||
|
||||
/**
|
||||
* Register callback for call state.
|
||||
*/
|
||||
void toxav_register_callstate_callback (ToxAv *av, ToxAVCallback cb, ToxAvCallbackID id, void *userdata);
|
||||
|
||||
/**
|
||||
* Register callback for audio data.
|
||||
*/
|
||||
void toxav_register_audio_callback (ToxAv *av, ToxAvAudioCallback cb, void *userdata);
|
||||
|
||||
/**
|
||||
* Register callback for video data.
|
||||
*/
|
||||
void toxav_register_video_callback (ToxAv *av, ToxAvVideoCallback cb, void *userdata);
|
||||
|
||||
/**
|
||||
* Call user. Use its friend_id.
|
||||
*/
|
||||
int toxav_call(ToxAv *av,
|
||||
int32_t *call_index,
|
||||
int friend_id,
|
||||
const ToxAvCSettings *csettings,
|
||||
int ringing_seconds);
|
||||
|
||||
/**
|
||||
* Hangup active call.
|
||||
*/
|
||||
int toxav_hangup(ToxAv *av, int32_t call_index);
|
||||
|
||||
/**
|
||||
* Answer incoming call. Pass the csettings that you will use.
|
||||
*/
|
||||
int toxav_answer(ToxAv *av, int32_t call_index, const ToxAvCSettings *csettings );
|
||||
|
||||
/**
|
||||
* Reject incoming call.
|
||||
*/
|
||||
int toxav_reject(ToxAv *av, int32_t call_index, const char *reason);
|
||||
|
||||
/**
|
||||
* Cancel outgoing request.
|
||||
*/
|
||||
int toxav_cancel(ToxAv *av, int32_t call_index, int peer_id, const char *reason);
|
||||
|
||||
/**
|
||||
* Notify peer that we are changing codec settings.
|
||||
*/
|
||||
int toxav_change_settings(ToxAv *av, int32_t call_index, const ToxAvCSettings *csettings);
|
||||
|
||||
/**
|
||||
* Terminate transmission. Note that transmission will be
|
||||
* terminated without informing remote peer. Usually called when we can't inform peer.
|
||||
*/
|
||||
int toxav_stop_call(ToxAv *av, int32_t call_index);
|
||||
|
||||
/**
|
||||
* Allocates transmission data. Must be call before calling toxav_prepare_* and toxav_send_*.
|
||||
* Also, it must be called when call is started
|
||||
*/
|
||||
int toxav_prepare_transmission(ToxAv *av, int32_t call_index, int support_video);
|
||||
|
||||
/**
|
||||
* Clears transmission data. Call this at the end of the transmission.
|
||||
*/
|
||||
int toxav_kill_transmission(ToxAv *av, int32_t call_index);
|
||||
|
||||
/**
|
||||
* Encode video frame.
|
||||
*/
|
||||
int toxav_prepare_video_frame ( ToxAv *av,
|
||||
int32_t call_index,
|
||||
uint8_t *dest,
|
||||
int dest_max,
|
||||
vpx_image_t *input);
|
||||
|
||||
/**
|
||||
* Send encoded video packet.
|
||||
*/
|
||||
int toxav_send_video ( ToxAv *av, int32_t call_index, const uint8_t *frame, uint32_t frame_size);
|
||||
|
||||
/**
|
||||
* Encode audio frame.
|
||||
*/
|
||||
int toxav_prepare_audio_frame ( ToxAv *av,
|
||||
int32_t call_index,
|
||||
uint8_t *dest,
|
||||
int dest_max,
|
||||
const int16_t *frame,
|
||||
int frame_size);
|
||||
|
||||
/**
|
||||
* Send encoded audio frame.
|
||||
*/
|
||||
int toxav_send_audio ( ToxAv *av, int32_t call_index, const uint8_t *frame, unsigned int size);
|
||||
|
||||
/**
|
||||
* Get codec settings from the peer. These were exchanged during call initialization
|
||||
* or when peer send us new csettings.
|
||||
*/
|
||||
int toxav_get_peer_csettings ( ToxAv *av, int32_t call_index, int peer, ToxAvCSettings *dest );
|
||||
|
||||
/**
|
||||
* Get friend id of peer participating in conversation.
|
||||
*/
|
||||
int toxav_get_peer_id ( ToxAv *av, int32_t call_index, int peer );
|
||||
|
||||
/**
|
||||
* Get current call state.
|
||||
*/
|
||||
ToxAvCallState toxav_get_call_state ( ToxAv *av, int32_t call_index );
|
||||
|
||||
/**
|
||||
* Is certain capability supported. Used to determine if encoding/decoding is ready.
|
||||
*/
|
||||
int toxav_capability_supported ( ToxAv *av, int32_t call_index, ToxAvCapabilities capability );
|
||||
|
||||
/**
|
||||
* Returns tox reference.
|
||||
*/
|
||||
Tox *toxav_get_tox (ToxAv *av);
|
||||
|
||||
/**
|
||||
* Returns number of active calls or -1 on error.
|
||||
*/
|
||||
int toxav_get_active_count (ToxAv *av);
|
||||
|
||||
/* Create a new toxav group.
|
||||
*
|
||||
* return group number on success.
|
||||
* return -1 on failure.
|
||||
*
|
||||
* Audio data callback format:
|
||||
* audio_callback(Tox *tox, int groupnumber, int peernumber, const int16_t *pcm, unsigned int samples, uint8_t channels, unsigned int sample_rate, void *userdata)
|
||||
*
|
||||
* Note that total size of pcm in bytes is equal to (samples * channels * sizeof(int16_t)).
|
||||
*/
|
||||
int toxav_add_av_groupchat(Tox *tox, void (*audio_callback)(Tox *, int, int, const int16_t *, unsigned int, uint8_t,
|
||||
unsigned int, void *), void *userdata);
|
||||
|
||||
/* Join a AV group (you need to have been invited first.)
|
||||
*
|
||||
* returns group number on success
|
||||
* returns -1 on failure.
|
||||
*
|
||||
* Audio data callback format (same as the one for toxav_add_av_groupchat()):
|
||||
* audio_callback(Tox *tox, int groupnumber, int peernumber, const int16_t *pcm, unsigned int samples, uint8_t channels, unsigned int sample_rate, void *userdata)
|
||||
*
|
||||
* Note that total size of pcm in bytes is equal to (samples * channels * sizeof(int16_t)).
|
||||
*/
|
||||
int toxav_join_av_groupchat(Tox *tox, int32_t friendnumber, const uint8_t *data, uint16_t length,
|
||||
void (*audio_callback)(Tox *, int, int, const int16_t *, unsigned int, uint8_t, unsigned int, void *), void *userdata);
|
||||
|
||||
/* Send audio to the group chat.
|
||||
*
|
||||
* return 0 on success.
|
||||
* return -1 on failure.
|
||||
*
|
||||
* Note that total size of pcm in bytes is equal to (samples * channels * sizeof(int16_t)).
|
||||
*
|
||||
* Valid number of samples are ((sample rate) * (audio length (Valid ones are: 2.5, 5, 10, 20, 40 or 60 ms)) / 1000)
|
||||
* Valid number of channels are 1 or 2.
|
||||
* Valid sample rates are 8000, 12000, 16000, 24000, or 48000.
|
||||
*
|
||||
* Recommended values are: samples = 960, channels = 1, sample_rate = 48000
|
||||
*/
|
||||
int toxav_group_send_audio(Tox *tox, int groupnumber, const int16_t *pcm, unsigned int samples, uint8_t channels,
|
||||
unsigned int sample_rate);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __TOXAV */
|
Loading…
Reference in New Issue
Block a user