toxcore/toxav/toxav.c

1211 lines
35 KiB
C
Raw Normal View History

2014-02-10 06:06:44 +08:00
/** toxav.c
2014-02-17 09:01:30 +08:00
*
2014-02-10 06:06:44 +08:00
* 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;
2014-05-11 00:00:49 +08:00
#define _GNU_SOURCE /* implicit declaration warning */
2014-02-10 06:06:44 +08:00
#include "rtp.h"
2014-06-21 07:58:55 +08:00
#include "codec.h"
2014-02-10 06:06:44 +08:00
#include "msi.h"
2014-05-11 00:00:49 +08:00
#include "toxav.h"
#include "group.h"
2014-02-10 06:06:44 +08:00
2014-04-28 01:21:26 +08:00
#include "../toxcore/logger.h"
2014-05-11 00:00:49 +08:00
#include <assert.h>
2014-02-10 06:06:44 +08:00
#include <stdlib.h>
#include <string.h>
2014-02-16 03:44:33 +08:00
/* Assume 24 fps*/
2014-06-25 06:07:31 +08:00
#define MAX_ENCODE_TIME_US ((1000 / 24) * 1000)
#define MAX_DECODE_TIME_US 0
#define MAX_VIDEOFRAME_SIZE 0x40000 /* 256KiB */
#define VIDEOFRAME_PIECE_SIZE 0x500 /* 1.25 KiB*/
#define VIDEOFRAME_HEADER_SIZE 0x2
2014-02-10 06:06:44 +08:00
#define inline__ inline __attribute__((always_inline))
/* call index invalid: true if invalid */
#define cii(c_idx, session) (c_idx < 0 || c_idx >= session->max_calls)
2014-07-27 06:26:58 +08:00
const ToxAvCSettings av_DefaultSettings = {
TypeAudio,
2014-07-27 09:26:32 +08:00
2014-07-27 06:26:58 +08:00
500,
1280,
720,
2014-07-27 09:26:32 +08:00
2014-07-27 06:26:58 +08:00
64000,
20,
48000,
1
};
const uint32_t av_jbufdc = 3;
const uint32_t av_VADd = 40;
2014-02-10 06:06:44 +08:00
static const uint8_t audio_index = 0, video_index = 1;
2014-08-04 21:50:32 +08:00
typedef struct {
uint32_t size;
uint8_t data[0];
} DECODE_PACKET;
#define VIDEO_DECODE_QUEUE_SIZE 2
#define AUDIO_DECODE_QUEUE_SIZE 16
2014-05-11 00:00:49 +08:00
typedef struct _CallSpecific {
RTPSession *crtps[2]; /** Audio is first and video is second */
CodecState *cs;/** Each call have its own encoders and decoders.
* You can, but don't have to, reuse encoders for
* multiple calls. If you choose to reuse encoders,
* make sure to also reuse encoded payload for every call.
* Decoders have to be unique for each call. FIXME: Now add refcounted encoders and
* reuse them really.
*/
JitterBuffer *j_buf; /** Jitter buffer for audio */
uint32_t frame_limit; /* largest address written to in frame_buf for current input frame*/
uint8_t frame_id, frame_outid; /* id of input and output video frame */
void *frame_buf; /* buffer for split video payloads */
2014-07-03 23:13:11 +08:00
_Bool call_active;
2014-07-05 23:36:12 +08:00
pthread_mutex_t mutex;
2014-02-10 06:06:44 +08:00
2014-08-04 21:50:32 +08:00
/* used in the "decode on another thread" system */
volatile _Bool exit, decoding;
uint8_t video_decode_read, video_decode_write, audio_decode_read, audio_decode_write;
pthread_mutex_t decode_cond_mutex;
pthread_cond_t decode_cond;
DECODE_PACKET *volatile video_decode_queue[VIDEO_DECODE_QUEUE_SIZE];
DECODE_PACKET *volatile audio_decode_queue[AUDIO_DECODE_QUEUE_SIZE];
} CallSpecific;
2014-02-10 06:06:44 +08:00
struct _ToxAv {
2014-02-17 09:01:30 +08:00
Messenger *messenger;
MSISession *msi_session; /** Main msi session */
CallSpecific *calls; /** Per-call params */
2014-07-08 04:10:10 +08:00
2014-08-02 23:29:27 +08:00
void (*audio_callback)(ToxAv *, int32_t, int16_t *, int, void *);
void (*video_callback)(ToxAv *, int32_t, vpx_image_t *, void *);
void *audio_callback_userdata;
void *video_callback_userdata;
2014-07-08 04:10:10 +08:00
2014-04-28 01:21:26 +08:00
uint32_t max_calls;
};
2014-02-17 09:01:30 +08:00
2014-08-02 01:21:03 +08:00
static void *toxav_decoding(void *arg);
2014-08-01 02:56:32 +08:00
2014-07-27 09:26:32 +08:00
static MSICSettings msicsettings_cast (const ToxAvCSettings *from)
2014-07-27 01:29:49 +08:00
{
MSICSettings csettings;
csettings.call_type = from->call_type;
2014-02-17 09:01:30 +08:00
2014-07-27 01:29:49 +08:00
csettings.video_bitrate = from->video_bitrate;
csettings.max_video_width = from->max_video_width;
csettings.max_video_height = from->max_video_height;
2014-07-27 01:29:49 +08:00
csettings.audio_bitrate = from->audio_bitrate;
csettings.audio_frame_duration = from->audio_frame_duration;
csettings.audio_sample_rate = from->audio_sample_rate;
csettings.audio_channels = from->audio_channels;
2014-07-27 09:26:32 +08:00
2014-07-27 01:29:49 +08:00
return csettings;
}
2014-07-27 09:26:32 +08:00
static ToxAvCSettings toxavcsettings_cast (const MSICSettings *from)
2014-07-27 01:29:49 +08:00
{
2014-07-27 06:26:58 +08:00
ToxAvCSettings csettings;
2014-07-27 01:29:49 +08:00
csettings.call_type = from->call_type;
2014-07-27 09:26:32 +08:00
2014-07-27 01:29:49 +08:00
csettings.video_bitrate = from->video_bitrate;
csettings.max_video_width = from->max_video_width;
csettings.max_video_height = from->max_video_height;
2014-02-10 06:06:44 +08:00
2014-07-27 01:29:49 +08:00
csettings.audio_bitrate = from->audio_bitrate;
csettings.audio_frame_duration = from->audio_frame_duration;
csettings.audio_sample_rate = from->audio_sample_rate;
csettings.audio_channels = from->audio_channels;
2014-07-27 09:26:32 +08:00
2014-07-27 01:29:49 +08:00
return csettings;
}
2014-04-28 01:21:26 +08:00
2014-02-16 04:29:41 +08:00
/**
* @brief Start new A/V session. There can only be one session at the time. If you register more
* it will result in undefined behaviour.
2014-02-17 09:01:30 +08:00
*
2014-02-16 04:29:41 +08:00
* @param messenger The messenger handle.
* @param userdata The agent handling A/V session (i.e. phone).
2014-02-16 04:29:41 +08:00
* @param video_width Width of video frame.
* @param video_height Height of video frame.
* @return ToxAv*
* @retval NULL On error.
*/
ToxAv *toxav_new( Tox *messenger, int32_t max_calls)
2014-02-16 03:44:33 +08:00
{
2014-02-17 09:01:30 +08:00
ToxAv *av = calloc ( sizeof(ToxAv), 1);
2014-05-24 22:02:01 +08:00
if (av == NULL) {
LOGGER_WARNING("Allocation failed!");
2014-02-16 03:44:33 +08:00
return NULL;
2014-05-24 22:02:01 +08:00
}
2014-02-16 03:44:33 +08:00
av->messenger = (Messenger *)messenger;
2014-04-28 01:21:26 +08:00
av->msi_session = msi_init_session(av->messenger, max_calls);
2014-02-10 06:06:44 +08:00
av->msi_session->agent_handler = av;
2014-05-11 00:00:49 +08:00
av->calls = calloc(sizeof(CallSpecific), max_calls);
2014-04-28 01:21:26 +08:00
av->max_calls = max_calls;
2014-02-17 09:01:30 +08:00
2014-02-10 06:06:44 +08:00
return av;
}
2014-02-16 04:29:41 +08:00
/**
* @brief Remove A/V session.
2014-02-17 09:01:30 +08:00
*
2014-02-16 04:29:41 +08:00
* @param av Handler.
* @return void
*/
2014-02-17 09:01:30 +08:00
void toxav_kill ( ToxAv *av )
{
2014-08-04 06:15:00 +08:00
uint32_t i;
2014-08-01 02:56:32 +08:00
for (i = 0; i < av->max_calls; i ++) {
if ( av->calls[i].crtps[audio_index] )
2014-05-11 00:00:49 +08:00
rtp_terminate_session(av->calls[i].crtps[audio_index], av->msi_session->messenger_handle);
if ( av->calls[i].crtps[video_index] )
2014-05-11 00:00:49 +08:00
rtp_terminate_session(av->calls[i].crtps[video_index], av->msi_session->messenger_handle);
2014-02-17 09:01:30 +08:00
2014-05-18 05:15:54 +08:00
if ( av->calls[i].j_buf ) terminate_queue(av->calls[i].j_buf);
2014-05-18 05:15:54 +08:00
if ( av->calls[i].cs ) codec_terminate_session(av->calls[i].cs);
2014-02-10 06:06:44 +08:00
}
msi_terminate_session(av->msi_session);
2014-05-11 00:00:49 +08:00
free(av->calls);
2014-02-10 06:06:44 +08:00
free(av);
}
2014-02-16 04:29:41 +08:00
/**
* @brief Register callback for call state.
2014-02-17 09:01:30 +08:00
*
* @param av Handler.
2014-02-16 04:29:41 +08:00
* @param callback The callback
* @param id One of the ToxAvCallbackID values
* @return void
*/
void toxav_register_callstate_callback ( ToxAv *av, ToxAVCallback callback, ToxAvCallbackID id, void *userdata )
2014-02-10 06:06:44 +08:00
{
msi_register_callback(av->msi_session, (MSICallbackType)callback, (MSICallbackID) id, userdata);
2014-02-10 06:06:44 +08:00
}
2014-07-08 04:10:10 +08:00
/**
2014-09-10 08:31:37 +08:00
* @brief Register callback for receiving audio data
2014-07-08 04:10:10 +08:00
*
* @param callback The callback
* @return void
*/
2014-08-02 23:29:27 +08:00
void toxav_register_audio_recv_callback (ToxAv *av, void (*callback)(ToxAv *, int32_t, int16_t *, int, void *),
void *user_data)
2014-07-08 04:10:10 +08:00
{
av->audio_callback = callback;
av->audio_callback_userdata = user_data;
2014-07-08 04:10:10 +08:00
}
/**
2014-09-10 08:31:37 +08:00
* @brief Register callback for receiving video data
2014-07-08 04:10:10 +08:00
*
* @param callback The callback
* @return void
*/
2014-08-02 23:29:27 +08:00
void toxav_register_video_recv_callback (ToxAv *av, void (*callback)(ToxAv *, int32_t, vpx_image_t *, void *),
void *user_data)
2014-07-08 04:10:10 +08:00
{
av->video_callback = callback;
av->video_callback_userdata = user_data;
2014-07-08 04:10:10 +08:00
}
2014-02-16 04:29:41 +08:00
/**
* @brief Call user. Use its friend_id.
2014-02-17 09:01:30 +08:00
*
2014-02-16 04:29:41 +08:00
* @param av Handler.
* @param user The user.
* @param call_type Call type.
* @param ringing_seconds Ringing timeout.
* @return int
* @retval 0 Success.
* @retval ToxAvError On error.
*/
2014-07-27 09:26:32 +08:00
int toxav_call (ToxAv *av, int32_t *call_index, int user, const ToxAvCSettings *csettings, int ringing_seconds )
2014-02-10 06:06:44 +08:00
{
2014-07-27 01:29:49 +08:00
return msi_invite(av->msi_session, call_index, msicsettings_cast(csettings), ringing_seconds * 1000, user);
2014-02-10 06:06:44 +08:00
}
2014-02-16 04:29:41 +08:00
/**
* @brief Hangup active call.
2014-02-17 09:01:30 +08:00
*
2014-02-16 04:29:41 +08:00
* @param av Handler.
* @return int
* @retval 0 Success.
* @retval ToxAvError On error.
*/
int toxav_hangup ( ToxAv *av, int32_t call_index )
2014-02-10 06:06:44 +08:00
{
if ( cii(call_index, av->msi_session) || !av->msi_session->calls[call_index] ) {
2014-02-10 06:06:44 +08:00
return ErrorNoCall;
}
2014-02-17 09:01:30 +08:00
2014-04-28 01:21:26 +08:00
if ( av->msi_session->calls[call_index]->state != call_active ) {
2014-02-10 06:06:44 +08:00
return ErrorInvalidState;
}
2014-02-17 09:01:30 +08:00
2014-04-28 01:21:26 +08:00
return msi_hangup(av->msi_session, call_index);
2014-02-10 06:06:44 +08:00
}
2014-02-16 04:29:41 +08:00
/**
* @brief Answer incomming call.
2014-02-17 09:01:30 +08:00
*
2014-02-16 04:29:41 +08:00
* @param av Handler.
* @param call_type Answer with...
* @return int
* @retval 0 Success.
* @retval ToxAvError On error.
*/
2014-07-27 09:26:32 +08:00
int toxav_answer ( ToxAv *av, int32_t call_index, const ToxAvCSettings *csettings )
2014-02-10 06:06:44 +08:00
{
if ( cii(call_index, av->msi_session) || !av->msi_session->calls[call_index] ) {
2014-02-10 06:06:44 +08:00
return ErrorNoCall;
}
2014-02-17 09:01:30 +08:00
2014-04-28 01:21:26 +08:00
if ( av->msi_session->calls[call_index]->state != call_starting ) {
2014-02-10 06:06:44 +08:00
return ErrorInvalidState;
}
2014-02-17 09:01:30 +08:00
2014-07-27 01:29:49 +08:00
return msi_answer(av->msi_session, call_index, msicsettings_cast(csettings));
2014-02-10 06:06:44 +08:00
}
2014-02-16 04:29:41 +08:00
/**
* @brief Reject incomming call.
2014-02-17 09:01:30 +08:00
*
2014-02-16 04:29:41 +08:00
* @param av Handler.
* @param reason Optional reason. Set NULL if none.
* @return int
* @retval 0 Success.
* @retval ToxAvError On error.
*/
int toxav_reject ( ToxAv *av, int32_t call_index, const char *reason )
2014-02-10 06:06:44 +08:00
{
if ( cii(call_index, av->msi_session) || !av->msi_session->calls[call_index] ) {
2014-02-10 06:06:44 +08:00
return ErrorNoCall;
}
2014-04-28 01:21:26 +08:00
if ( av->msi_session->calls[call_index]->state != call_starting ) {
2014-02-10 06:06:44 +08:00
return ErrorInvalidState;
}
2014-07-24 01:26:55 +08:00
return msi_reject(av->msi_session, call_index, reason);
2014-02-10 06:06:44 +08:00
}
2014-02-16 04:29:41 +08:00
/**
* @brief Cancel outgoing request.
2014-02-17 09:01:30 +08:00
*
2014-02-16 04:29:41 +08:00
* @param av Handler.
* @param reason Optional reason.
2014-03-23 02:33:56 +08:00
* @param peer_id peer friend_id
2014-02-16 04:29:41 +08:00
* @return int
* @retval 0 Success.
* @retval ToxAvError On error.
*/
int toxav_cancel ( ToxAv *av, int32_t call_index, int peer_id, const char *reason )
2014-02-10 06:06:44 +08:00
{
if ( cii(call_index, av->msi_session) || !av->msi_session->calls[call_index] ) {
2014-02-10 06:06:44 +08:00
return ErrorNoCall;
}
2014-02-17 09:01:30 +08:00
if ( av->msi_session->calls[call_index]->state != call_inviting ) {
return ErrorInvalidState;
}
2014-04-28 01:21:26 +08:00
return msi_cancel(av->msi_session, call_index, peer_id, reason);
2014-02-10 06:06:44 +08:00
}
/**
* @brief Notify peer that we are changing call type
*
* @param av Handler.
* @return int
* @param call_type Change to...
* @retval 0 Success.
* @retval ToxAvError On error.
*/
2014-07-27 09:26:32 +08:00
int toxav_change_settings(ToxAv *av, int32_t call_index, const ToxAvCSettings *csettings)
{
if ( cii(call_index, av->msi_session) || !av->msi_session->calls[call_index] ) {
return ErrorNoCall;
}
2014-07-27 01:29:49 +08:00
return msi_change_csettings(av->msi_session, call_index, msicsettings_cast(csettings));
}
2014-02-16 04:29:41 +08:00
/**
* @brief Terminate transmission. Note that transmission will be terminated without informing remote peer.
2014-02-17 09:01:30 +08:00
*
2014-02-16 04:29:41 +08:00
* @param av Handler.
* @return int
* @retval 0 Success.
* @retval ToxAvError On error.
*/
int toxav_stop_call ( ToxAv *av, int32_t call_index )
2014-02-10 06:06:44 +08:00
{
if ( cii(call_index, av->msi_session) || !av->msi_session->calls[call_index] ) {
2014-02-10 06:06:44 +08:00
return ErrorNoCall;
}
2014-02-17 09:01:30 +08:00
2014-04-28 01:21:26 +08:00
return msi_stopcall(av->msi_session, call_index);
2014-02-10 06:06:44 +08:00
}
2014-02-16 04:29:41 +08:00
/**
* @brief Must be call before any RTP transmission occurs.
2014-02-17 09:01:30 +08:00
*
2014-02-16 04:29:41 +08:00
* @param av Handler.
* @return int
* @retval 0 Success.
* @retval ToxAvError On error.
*/
2014-07-27 09:26:32 +08:00
int toxav_prepare_transmission ( ToxAv *av, int32_t call_index, uint32_t jbuf_capacity, uint32_t VAD_treshold,
int support_video )
2014-02-10 06:06:44 +08:00
{
2014-07-03 23:13:11 +08:00
if ( !av->msi_session || cii(call_index, av->msi_session) ||
2014-07-27 09:26:32 +08:00
!av->msi_session->calls[call_index] || !av->msi_session->calls[call_index]->csettings_peer ||
av->calls[call_index].call_active) {
LOGGER_ERROR("Error while starting RTP session: invalid call!\n");
2014-05-03 07:46:03 +08:00
return ErrorInternal;
2014-02-10 06:06:44 +08:00
}
2014-02-17 09:01:30 +08:00
CallSpecific *call = &av->calls[call_index];
call->crtps[audio_index] =
rtp_init_session(type_audio, av->messenger, av->msi_session->calls[call_index]->peers[0]);
2014-02-17 09:01:30 +08:00
2014-05-11 00:00:49 +08:00
if ( !call->crtps[audio_index] ) {
LOGGER_ERROR("Error while starting audio RTP session!\n");
2014-07-05 21:11:25 +08:00
return ErrorInternal;
2014-02-10 06:06:44 +08:00
}
2014-02-17 09:01:30 +08:00
2014-07-08 04:10:10 +08:00
call->crtps[audio_index]->call_index = call_index;
call->crtps[audio_index]->av = av;
if ( support_video ) {
call->crtps[video_index] =
rtp_init_session(type_video, av->messenger, av->msi_session->calls[call_index]->peers[0]);
2014-04-28 01:21:26 +08:00
2014-05-11 00:00:49 +08:00
if ( !call->crtps[video_index] ) {
LOGGER_ERROR("Error while starting video RTP session!\n");
2014-07-05 21:11:25 +08:00
goto error;
}
2014-07-08 04:10:10 +08:00
call->crtps[video_index]->call_index = call_index;
call->crtps[video_index]->av = av;
call->frame_limit = 0;
call->frame_id = 0;
call->frame_outid = 0;
call->frame_buf = calloc(MAX_VIDEOFRAME_SIZE, 1);
2014-06-24 04:34:06 +08:00
if (!call->frame_buf) {
LOGGER_WARNING("Frame buffer allocation failed!");
2014-07-05 21:11:25 +08:00
goto error;
}
2014-02-10 06:06:44 +08:00
}
2014-07-27 01:29:49 +08:00
if ( !(call->j_buf = create_queue(jbuf_capacity)) ) {
LOGGER_WARNING("Jitter buffer creaton failed!");
2014-07-05 21:11:25 +08:00
goto error;
}
ToxAvCSettings csettings_peer = toxavcsettings_cast(&av->msi_session->calls[call_index]->csettings_peer[0]);
ToxAvCSettings csettings_local = toxavcsettings_cast(&av->msi_session->calls[call_index]->csettings_local);
2014-07-27 01:29:49 +08:00
LOGGER_DEBUG(
2014-07-27 09:26:32 +08:00
"Type: %u \n"
"Video bitrate: %u \n"
"Video height: %u \n"
"Video width: %u \n"
"Audio bitrate: %u \n"
"Audio framedur: %u \n"
"Audio sample rate: %u \n"
"Audio channels: %u \n",
csettings_peer.call_type,
csettings_peer.video_bitrate,
csettings_peer.max_video_height,
csettings_peer.max_video_width,
csettings_peer.audio_bitrate,
csettings_peer.audio_frame_duration,
csettings_peer.audio_sample_rate,
csettings_peer.audio_channels );
if ( (call->cs = codec_init_session(csettings_local.audio_bitrate,
csettings_local.audio_frame_duration,
csettings_local.audio_sample_rate,
csettings_local.audio_channels,
csettings_peer.audio_channels,
2014-07-27 01:29:49 +08:00
VAD_treshold,
csettings_local.max_video_width,
csettings_local.max_video_height,
csettings_local.video_bitrate) )) {
2014-07-06 00:25:30 +08:00
if ( pthread_mutex_init(&call->mutex, NULL) != 0 ) goto error;
2014-08-04 21:50:32 +08:00
//todo: add error checks
pthread_mutex_init(&call->decode_cond_mutex, NULL);
pthread_cond_init(&call->decode_cond, NULL);
2014-08-05 00:08:43 +08:00
void **arg = malloc(2 * sizeof(void *));
2014-08-04 21:50:32 +08:00
arg[0] = av;
arg[1] = call;
pthread_t temp;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, 1 << 18);
pthread_create(&temp, &attr, toxav_decoding, arg);
pthread_attr_destroy(&attr);
2014-07-27 01:29:49 +08:00
LOGGER_WARNING("Got here");
call->call_active = 1;
2014-07-06 00:25:30 +08:00
return ErrorNone;
}
2014-07-05 21:11:25 +08:00
error:
rtp_terminate_session(call->crtps[audio_index], av->messenger);
rtp_terminate_session(call->crtps[video_index], av->messenger);
free(call->frame_buf);
terminate_queue(call->j_buf);
2014-07-05 21:11:25 +08:00
codec_terminate_session(call->cs);
2014-07-03 23:13:11 +08:00
return ErrorInternal;
2014-02-10 06:06:44 +08:00
}
2014-02-16 04:29:41 +08:00
/**
* @brief Call this at the end of the transmission.
2014-02-17 09:01:30 +08:00
*
2014-02-16 04:29:41 +08:00
* @param av Handler.
* @return int
* @retval 0 Success.
* @retval ToxAvError On error.
*/
2014-05-17 01:56:40 +08:00
int toxav_kill_transmission ( ToxAv *av, int32_t call_index )
2014-02-10 06:06:44 +08:00
{
if (cii(call_index, av->msi_session)) {
LOGGER_WARNING("Invalid call index: %d", call_index);
return ErrorNoCall;
}
2014-06-15 22:36:57 +08:00
CallSpecific *call = &av->calls[call_index];
2014-07-05 23:36:12 +08:00
pthread_mutex_lock(&call->mutex);
if (!call->call_active) {
pthread_mutex_unlock(&call->mutex);
LOGGER_WARNING("Action on inactive call: %d", call_index);
return ErrorNoCall;
}
call->call_active = 0;
2014-07-06 00:25:30 +08:00
rtp_terminate_session(call->crtps[audio_index], av->messenger);
call->crtps[audio_index] = NULL;
rtp_terminate_session(call->crtps[video_index], av->messenger);
call->crtps[video_index] = NULL;
terminate_queue(call->j_buf);
call->j_buf = NULL;
2014-08-01 02:56:32 +08:00
int i;
DECODE_PACKET *p;
2014-08-04 21:50:32 +08:00
call->exit = 1;
pthread_mutex_lock(&call->decode_cond_mutex);
pthread_cond_signal(&call->decode_cond);
pthread_cond_wait(&call->decode_cond, &call->decode_cond_mutex);
pthread_mutex_unlock(&call->decode_cond_mutex);
pthread_mutex_destroy(&call->decode_cond_mutex);
pthread_cond_destroy(&call->decode_cond);
2014-08-02 01:21:03 +08:00
for (i = 0; i != VIDEO_DECODE_QUEUE_SIZE; i++) {
2014-08-04 21:50:32 +08:00
p = call->video_decode_queue[i];
call->video_decode_queue[i] = NULL;
2014-08-02 01:21:03 +08:00
2014-08-04 21:50:32 +08:00
if (p) {
free(p);
}
}
2014-08-02 01:21:03 +08:00
for (i = 0; i != AUDIO_DECODE_QUEUE_SIZE; i++) {
2014-08-04 21:50:32 +08:00
p = call->audio_decode_queue[i];
call->audio_decode_queue[i] = NULL;
2014-08-02 01:21:03 +08:00
2014-08-04 21:50:32 +08:00
if (p) {
free(p);
}
2014-08-01 02:56:32 +08:00
}
2014-07-06 00:25:30 +08:00
codec_terminate_session(call->cs);
call->cs = NULL;
free(call->frame_buf);
2014-07-05 23:36:12 +08:00
pthread_mutex_unlock(&call->mutex);
pthread_mutex_destroy(&call->mutex);
2014-07-03 23:13:11 +08:00
2014-08-06 08:58:49 +08:00
memset(call, 0, sizeof(CallSpecific));
2014-02-10 06:06:44 +08:00
return ErrorNone;
}
2014-02-16 04:29:41 +08:00
/**
* @brief Send RTP payload.
2014-02-17 09:01:30 +08:00
*
2014-02-16 04:29:41 +08:00
* @param av Handler.
* @param type Type of payload.
* @param payload The payload.
* @param length Size of it.
* @return int
* @retval 0 Success.
* @retval -1 Failure.
*/
static int toxav_send_rtp_payload(ToxAv *av, int32_t call_index, ToxAvCallType type, const uint8_t *payload,
unsigned int length)
2014-02-10 06:06:44 +08:00
{
2014-07-06 00:25:30 +08:00
CallSpecific *call = &av->calls[call_index];
2014-07-05 21:11:25 +08:00
if (call->crtps[type - TypeAudio]) {
2014-06-24 04:34:06 +08:00
if (type == TypeAudio) {
2014-07-05 21:11:25 +08:00
return rtp_send_msg(call->crtps[type - TypeAudio], av->messenger, payload, length);
} else {
2014-06-24 04:34:06 +08:00
if (length == 0 || length > MAX_VIDEOFRAME_SIZE) {
LOGGER_ERROR("Invalid video frame size: %u\n", length);
2014-07-05 21:11:25 +08:00
return ErrorInternal;
}
2014-06-24 04:34:06 +08:00
/* number of pieces - 1*/
uint8_t numparts = (length - 1) / VIDEOFRAME_PIECE_SIZE;
uint8_t load[2 + VIDEOFRAME_PIECE_SIZE];
2014-07-05 21:11:25 +08:00
load[0] = call->frame_outid++;
load[1] = 0;
int i;
2014-06-24 04:34:06 +08:00
for (i = 0; i < numparts; i++) {
memcpy(load + VIDEOFRAME_HEADER_SIZE, payload, VIDEOFRAME_PIECE_SIZE);
payload += VIDEOFRAME_PIECE_SIZE;
2014-07-06 00:25:30 +08:00
if (rtp_send_msg(call->crtps[type - TypeAudio], av->messenger,
2014-07-05 21:11:25 +08:00
load, VIDEOFRAME_HEADER_SIZE + VIDEOFRAME_PIECE_SIZE) != 0) {
2014-07-06 00:25:30 +08:00
2014-07-05 21:11:25 +08:00
return ErrorInternal;
}
2014-06-24 04:34:06 +08:00
load[1]++;
}
/* remainder = length % VIDEOFRAME_PIECE_SIZE, VIDEOFRAME_PIECE_SIZE if = 0 */
length = ((length - 1) % VIDEOFRAME_PIECE_SIZE) + 1;
memcpy(load + VIDEOFRAME_HEADER_SIZE, payload, length);
2014-07-06 00:25:30 +08:00
2014-07-05 21:11:25 +08:00
return rtp_send_msg(call->crtps[type - TypeAudio], av->messenger, load, VIDEOFRAME_HEADER_SIZE + length);
}
} else {
2014-07-05 21:11:25 +08:00
return ErrorNoRtpSession;
}
2014-02-10 06:06:44 +08:00
}
2014-02-16 04:29:41 +08:00
/**
* @brief Encode and send video packet.
2014-02-17 09:01:30 +08:00
*
2014-02-16 04:29:41 +08:00
* @param av Handler.
* @param input The packet.
* @return int
* @retval 0 Success.
* @retval ToxAvError On error.
*/
int toxav_send_video ( ToxAv *av, int32_t call_index, const uint8_t *frame, unsigned int frame_size)
2014-02-10 06:06:44 +08:00
{
if (cii(call_index, av->msi_session)) {
LOGGER_WARNING("Invalid call index: %d", call_index);
return ErrorNoCall;
}
2014-07-03 23:13:11 +08:00
CallSpecific *call = &av->calls[call_index];
pthread_mutex_lock(&call->mutex);
if (!call->call_active) {
pthread_mutex_unlock(&call->mutex);
LOGGER_WARNING("Action on inactive call: %d", call_index);
return ErrorNoCall;
}
2014-06-15 22:36:57 +08:00
2014-07-05 21:11:25 +08:00
int rc = toxav_send_rtp_payload(av, call_index, TypeVideo, frame, frame_size);
pthread_mutex_unlock(&call->mutex);
2014-07-06 00:25:30 +08:00
2014-07-05 21:11:25 +08:00
return rc;
2014-02-16 03:44:33 +08:00
}
2014-05-11 00:00:49 +08:00
/**
* @brief Encode video frame
*
2014-05-11 00:00:49 +08:00
* @param av Handler
* @param dest Where to
* @param dest_max Max size
* @param input What to encode
* @return int
* @retval ToxAvError On error.
* @retval >0 On success
*/
int toxav_prepare_video_frame(ToxAv *av, int32_t call_index, uint8_t *dest, int dest_max, vpx_image_t *input)
2014-02-10 06:06:44 +08:00
{
if (cii(call_index, av->msi_session)) {
LOGGER_WARNING("Invalid call index: %d", call_index);
return ErrorNoCall;
}
2014-07-03 23:13:11 +08:00
2014-06-15 22:36:57 +08:00
CallSpecific *call = &av->calls[call_index];
2014-07-05 23:36:12 +08:00
pthread_mutex_lock(&call->mutex);
2014-07-06 00:25:30 +08:00
if (!call->call_active) {
pthread_mutex_unlock(&call->mutex);
LOGGER_WARNING("Action on inactive call: %d", call_index);
return ErrorNoCall;
}
if (reconfigure_video_encoder_resolution(call->cs, input->d_w, input->d_h) != 0) {
pthread_mutex_unlock(&call->mutex);
return ErrorInternal;
}
2014-05-11 00:00:49 +08:00
int rc = vpx_codec_encode(&call->cs->v_encoder, input, call->cs->frame_counter, 1, 0, MAX_ENCODE_TIME_US);
2014-05-03 07:46:03 +08:00
if ( rc != VPX_CODEC_OK) {
LOGGER_ERROR("Could not encode video frame: %s\n", vpx_codec_err_to_string(rc));
2014-07-05 23:36:12 +08:00
pthread_mutex_unlock(&call->mutex);
2014-02-10 06:06:44 +08:00
return ErrorInternal;
2014-02-16 03:44:33 +08:00
}
2014-02-17 09:01:30 +08:00
2014-05-11 00:00:49 +08:00
++call->cs->frame_counter;
2014-02-17 09:01:30 +08:00
2014-02-16 03:44:33 +08:00
vpx_codec_iter_t iter = NULL;
const vpx_codec_cx_pkt_t *pkt;
2014-05-11 00:00:49 +08:00
int copied = 0;
2014-02-17 09:01:30 +08:00
2014-05-11 00:00:49 +08:00
while ( (pkt = vpx_codec_get_cx_data(&call->cs->v_encoder, &iter)) ) {
2014-02-16 03:44:33 +08:00
if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
2014-07-06 00:25:30 +08:00
if ( copied + pkt->data.frame.sz > dest_max ) {
2014-07-05 23:36:12 +08:00
pthread_mutex_unlock(&call->mutex);
2014-07-05 21:11:25 +08:00
return ErrorPacketTooLarge;
}
memcpy(dest + copied, pkt->data.frame.buf, pkt->data.frame.sz);
2014-05-11 00:00:49 +08:00
copied += pkt->data.frame.sz;
2014-02-16 03:44:33 +08:00
}
}
2014-02-17 09:01:30 +08:00
2014-07-05 23:36:12 +08:00
pthread_mutex_unlock(&call->mutex);
2014-05-11 00:00:49 +08:00
return copied;
2014-02-16 03:44:33 +08:00
}
2014-02-16 04:29:41 +08:00
/**
2014-05-03 07:46:03 +08:00
* @brief Send audio frame.
2014-02-17 09:01:30 +08:00
*
2014-02-16 04:29:41 +08:00
* @param av Handler.
* @param data The audio data encoded with toxav_prepare_audio_frame().
* @param size Its size in number of bytes.
2014-02-16 04:29:41 +08:00
* @return int
* @retval 0 Success.
* @retval ToxAvError On error.
*/
int toxav_send_audio ( ToxAv *av, int32_t call_index, const uint8_t *data, unsigned int size)
2014-02-16 03:44:33 +08:00
{
if (size > MAX_CRYPTO_DATA_SIZE)
return ErrorInternal;
2014-07-03 23:13:11 +08:00
if (cii(call_index, av->msi_session) || !av->calls[call_index].call_active) {
LOGGER_WARNING("Action on inactive call: %d", call_index);
return ErrorNoCall;
}
2014-07-03 23:13:11 +08:00
CallSpecific *call = &av->calls[call_index];
pthread_mutex_lock(&call->mutex);
if (!call->call_active) {
pthread_mutex_unlock(&call->mutex);
LOGGER_WARNING("Action on inactive call: %d", call_index);
return ErrorNoCall;
}
2014-06-15 22:36:57 +08:00
int rc = toxav_send_rtp_payload(av, call_index, TypeAudio, data, size);
pthread_mutex_unlock(&call->mutex);
2014-07-06 00:25:30 +08:00
2014-07-05 21:11:25 +08:00
return rc;
2014-05-03 07:46:03 +08:00
}
2014-02-17 09:01:30 +08:00
2014-05-03 07:46:03 +08:00
/**
* @brief Encode audio frame
*
2014-05-03 07:46:03 +08:00
* @param av Handler
* @param dest dest
* @param dest_max Max dest size
* @param frame The frame
* @param frame_size The frame size
* @return int
* @retval ToxAvError On error.
* @retval >0 On success
*/
int toxav_prepare_audio_frame ( ToxAv *av, int32_t call_index, uint8_t *dest, int dest_max, const int16_t *frame,
int frame_size)
2014-02-16 03:44:33 +08:00
{
2014-07-03 23:13:11 +08:00
if (cii(call_index, av->msi_session) || !av->calls[call_index].call_active) {
LOGGER_WARNING("Action on inactive call: %d", call_index);
return ErrorNoCall;
}
2014-07-03 23:13:11 +08:00
CallSpecific *call = &av->calls[call_index];
pthread_mutex_lock(&call->mutex);
2014-06-15 22:36:57 +08:00
2014-07-06 00:25:30 +08:00
if (!call->call_active) {
pthread_mutex_unlock(&call->mutex);
LOGGER_WARNING("Action on inactive call: %d", call_index);
return ErrorNoCall;
}
2014-02-17 09:01:30 +08:00
int32_t rc = opus_encode(call->cs->audio_encoder, frame, frame_size, dest, dest_max);
pthread_mutex_unlock(&call->mutex);
2014-07-05 21:11:25 +08:00
2014-05-11 00:00:49 +08:00
if (rc < 0) {
LOGGER_ERROR("Failed to encode payload: %s\n", opus_strerror(rc));
2014-02-16 03:44:33 +08:00
return ErrorInternal;
2014-05-11 00:00:49 +08:00
}
2014-02-16 03:44:33 +08:00
2014-05-03 07:46:03 +08:00
return rc;
2014-02-10 06:06:44 +08:00
}
2014-02-16 04:29:41 +08:00
/**
* @brief Get peer transmission type. It can either be audio or video.
2014-02-17 09:01:30 +08:00
*
2014-02-16 04:29:41 +08:00
* @param av Handler.
* @param peer The peer
* @return int
* @retval ToxAvCallType On success.
* @retval ToxAvError On error.
*/
2014-07-27 09:26:32 +08:00
int toxav_get_peer_csettings ( ToxAv *av, int32_t call_index, int peer, ToxAvCSettings *dest )
2014-02-10 06:06:44 +08:00
{
2014-06-15 22:36:57 +08:00
if ( peer < 0 || cii(call_index, av->msi_session) || !av->msi_session->calls[call_index]
|| av->msi_session->calls[call_index]->peer_count <= peer )
2014-02-10 06:06:44 +08:00
return ErrorInternal;
2014-02-17 09:01:30 +08:00
2014-07-27 01:29:49 +08:00
*dest = toxavcsettings_cast(&av->msi_session->calls[call_index]->csettings_peer[peer]);
return ErrorNone;
2014-02-10 06:06:44 +08:00
}
/**
* @brief Get id of peer participating in conversation
*
* @param av Handler
* @param peer peer index
* @return int
* @retval ToxAvError No peer id
*/
int toxav_get_peer_id ( ToxAv *av, int32_t call_index, int peer )
{
2014-06-15 22:36:57 +08:00
if ( peer < 0 || cii(call_index, av->msi_session) || !av->msi_session->calls[call_index]
|| av->msi_session->calls[call_index]->peer_count <= peer )
return ErrorInternal;
2014-04-28 01:21:26 +08:00
return av->msi_session->calls[call_index]->peers[peer];
}
2014-07-05 00:16:53 +08:00
/**
* @brief Get id of peer participating in conversation
*
* @param av Handler
* @param peer peer index
* @return int
* @retval ToxAvError No peer id
*/
2014-07-05 01:28:47 +08:00
ToxAvCallState toxav_get_call_state(ToxAv *av, int32_t call_index)
{
2014-07-05 00:16:53 +08:00
if ( cii(call_index, av->msi_session) || !av->msi_session->calls[call_index] )
return av_CallNonExistant;
2014-07-05 01:28:47 +08:00
2014-07-05 00:16:53 +08:00
return av->msi_session->calls[call_index]->state;
}
2014-02-16 04:29:41 +08:00
/**
* @brief Is certain capability supported
*
* @param av Handler
* @return int
* @retval 1 Yes.
* @retval 0 No.
*/
inline__ int toxav_capability_supported ( ToxAv *av, int32_t call_index, ToxAvCapabilities capability )
{
2014-05-17 01:56:40 +08:00
return av->calls[call_index].cs ? av->calls[call_index].cs->capabilities & (Capabilities) capability : 0;
/* 0 is error here */
}
inline__ Tox *toxav_get_tox(ToxAv *av)
2014-05-17 01:56:40 +08:00
{
return (Tox *)av->messenger;
2014-06-21 07:58:55 +08:00
}
int toxav_has_activity(ToxAv *av, int32_t call_index, int16_t *PCM, uint16_t frame_size, float ref_energy)
2014-06-21 07:58:55 +08:00
{
if ( !av->calls[call_index].cs ) return ErrorInvalidCodecState;
return energy_VAD(av->calls[call_index].cs, PCM, frame_size, ref_energy);
}
2014-07-08 04:10:10 +08:00
2014-08-01 02:56:32 +08:00
2014-08-04 21:50:32 +08:00
static void decode_video(ToxAv *av, CallSpecific *call, DECODE_PACKET *p)
2014-08-01 02:56:32 +08:00
{
2014-08-04 21:50:32 +08:00
int32_t call_index = call - av->calls;
2014-08-01 02:56:32 +08:00
int rc = vpx_codec_decode(&call->cs->v_decoder, p->data, p->size, NULL, MAX_DECODE_TIME_US);
2014-08-02 01:21:03 +08:00
2014-08-01 02:56:32 +08:00
if (rc != VPX_CODEC_OK) {
LOGGER_ERROR("Error decoding video: %s\n", vpx_codec_err_to_string(rc));
2014-08-01 02:56:32 +08:00
}
vpx_codec_iter_t iter = NULL;
vpx_image_t *img;
img = vpx_codec_get_frame(&call->cs->v_decoder, &iter);
if (img && av->video_callback) {
2014-08-04 21:50:32 +08:00
av->video_callback(av, call_index, img, av->video_callback_userdata);
2014-08-01 02:56:32 +08:00
} else {
LOGGER_WARNING("Video packet dropped due to missing callback or no image!");
}
free(p);
}
2014-08-04 21:50:32 +08:00
static void decode_audio(ToxAv *av, CallSpecific *call, DECODE_PACKET *p)
{
2014-08-04 21:50:32 +08:00
int32_t call_index = call - av->calls;
2014-08-02 01:21:03 +08:00
// ToxAvCSettings csettings;
// toxav_get_peer_csettings(av, call_index, 0, &csettings);
int frame_size = 10000; /* FIXME: not static? */
int16_t dest[frame_size];
int dec_size = opus_decode(call->cs->audio_decoder, p->data, p->size, dest, frame_size, (p->size == 0));
free(p);
2014-08-02 01:21:03 +08:00
if (dec_size < 0) {
LOGGER_WARNING("Decoding error: %s", opus_strerror(dec_size));
return;
}
if ( av->audio_callback )
av->audio_callback(av, call_index, dest, dec_size, av->audio_callback_userdata);
else
LOGGER_WARNING("Audio packet dropped due to missing callback!");
}
2014-08-02 01:21:03 +08:00
static void *toxav_decoding(void *arg)
2014-08-01 02:56:32 +08:00
{
2014-08-04 21:50:32 +08:00
void **pp = arg;
ToxAv *av = pp[0];
CallSpecific *call = pp[1];
free(pp);
2014-08-01 02:56:32 +08:00
2014-08-05 00:09:03 +08:00
while (1) {
2014-08-01 02:56:32 +08:00
DECODE_PACKET *p;
_Bool video = 0;
2014-08-04 21:50:32 +08:00
pthread_mutex_lock(&call->decode_cond_mutex);
2014-08-05 00:08:43 +08:00
if (call->exit) {
2014-08-04 21:50:32 +08:00
break;
}
2014-08-05 00:08:43 +08:00
uint8_t r;
/* first check for available packets, otherwise wait for condition*/
2014-08-04 21:50:32 +08:00
r = call->audio_decode_read;
p = call->audio_decode_queue[r];
2014-08-02 01:21:03 +08:00
if (!p) {
2014-08-04 21:50:32 +08:00
r = call->video_decode_read;
p = call->video_decode_queue[r];
2014-08-02 01:21:03 +08:00
if (!p) {
2014-08-04 21:50:32 +08:00
pthread_cond_wait(&call->decode_cond, &call->decode_cond_mutex);
r = call->audio_decode_read;
p = call->audio_decode_queue[r];
2014-08-02 01:21:03 +08:00
if (!p) {
2014-08-04 21:50:32 +08:00
r = call->video_decode_read;
p = call->video_decode_queue[r];
video = 1;
}
} else {
video = 1;
}
2014-08-01 02:56:32 +08:00
}
2014-08-02 01:21:03 +08:00
if (video) {
if (p) {
2014-08-04 21:50:32 +08:00
call->video_decode_queue[r] = NULL;
call->video_decode_read = (r + 1) % VIDEO_DECODE_QUEUE_SIZE;
}
} else {
2014-08-04 21:50:32 +08:00
call->audio_decode_queue[r] = NULL;
call->audio_decode_read = (r + 1) % AUDIO_DECODE_QUEUE_SIZE;
}
2014-08-04 21:50:32 +08:00
pthread_mutex_unlock(&call->decode_cond_mutex);
2014-08-01 02:56:32 +08:00
2014-08-02 01:21:03 +08:00
if (p) {
if (video) {
2014-08-04 21:50:32 +08:00
decode_video(av, call, p);
} else {
2014-08-04 21:50:32 +08:00
decode_audio(av, call, p);
}
2014-08-01 02:56:32 +08:00
}
}
2014-08-04 21:50:32 +08:00
call->exit = 0;
pthread_cond_signal(&call->decode_cond);
pthread_mutex_unlock(&call->decode_cond_mutex);
2014-08-01 02:56:32 +08:00
return NULL;
}
2014-07-08 04:10:10 +08:00
void toxav_handle_packet(RTPSession *_session, RTPMessage *_msg)
{
ToxAv *av = _session->av;
int32_t call_index = _session->call_index;
CallSpecific *call = &av->calls[call_index];
if (!call->call_active) return;
if (_session->payload_type == type_audio % 128) {
2014-07-08 04:10:10 +08:00
queue(call->j_buf, _msg);
int success = 0;
2014-07-08 04:10:10 +08:00
while ((_msg = dequeue(call->j_buf, &success)) || success == 2) {
DECODE_PACKET *p;
2014-08-02 01:21:03 +08:00
if (success == 2) {
p = malloc(sizeof(DECODE_PACKET));
2014-08-02 01:21:03 +08:00
if (p) {
p->size = 0;
}
2014-07-08 04:10:10 +08:00
} else {
p = malloc(sizeof(DECODE_PACKET) + _msg->length);
2014-08-02 01:21:03 +08:00
if (p) {
p->size = _msg->length;
memcpy(p->data, _msg->data, _msg->length);
}
2014-08-02 01:21:03 +08:00
2014-07-08 04:10:10 +08:00
rtp_free_msg(NULL, _msg);
}
2014-08-02 01:21:03 +08:00
if (p) {
/* do the decoding on another thread */
2014-08-04 21:50:32 +08:00
pthread_mutex_lock(&call->decode_cond_mutex);
uint8_t w = call->audio_decode_write;
2014-08-04 21:50:32 +08:00
if (call->audio_decode_queue[w] == NULL) {
call->audio_decode_queue[w] = p;
call->audio_decode_write = (w + 1) % AUDIO_DECODE_QUEUE_SIZE;
pthread_cond_signal(&call->decode_cond);
} else {
LOGGER_DEBUG("Dropped audio frame\n");
free(p);
}
2014-07-08 04:10:10 +08:00
2014-08-04 21:50:32 +08:00
pthread_mutex_unlock(&call->decode_cond_mutex);
} else {
//malloc failed
}
2014-07-08 04:10:10 +08:00
}
2014-07-27 09:26:32 +08:00
2014-07-08 04:10:10 +08:00
} else {
uint8_t *packet = _msg->data;
int recved_size = _msg->length;
if (recved_size < VIDEOFRAME_HEADER_SIZE) {
2014-07-08 04:10:10 +08:00
goto end;
}
uint8_t i = packet[0] - call->frame_id;
if (i == 0) {
/* piece of current frame */
} else if (i > 0 && i < 128) {
2014-09-10 08:31:37 +08:00
/* received a piece of a frame ahead, flush current frame and start reading this new frame */
2014-08-01 02:56:32 +08:00
DECODE_PACKET *p = malloc(sizeof(DECODE_PACKET) + call->frame_limit);
2014-08-02 01:21:03 +08:00
if (p) {
p->size = call->frame_limit;
memcpy(p->data, call->frame_buf, call->frame_limit);
/* do the decoding on another thread */
2014-08-04 21:50:32 +08:00
pthread_mutex_lock(&call->decode_cond_mutex);
uint8_t w = call->video_decode_write;
2014-08-04 21:50:32 +08:00
if (call->video_decode_queue[w] == NULL) {
call->video_decode_queue[w] = p;
call->video_decode_write = (w + 1) % VIDEO_DECODE_QUEUE_SIZE;
pthread_cond_signal(&call->decode_cond);
} else {
LOGGER_DEBUG("Dropped video frame\n");
free(p);
}
2014-08-01 02:56:32 +08:00
2014-08-04 21:50:32 +08:00
pthread_mutex_unlock(&call->decode_cond_mutex);
2014-08-01 02:56:32 +08:00
} else {
//malloc failed
2014-08-01 02:56:32 +08:00
}
2014-07-08 04:10:10 +08:00
call->frame_id = packet[0];
memset(call->frame_buf, 0, call->frame_limit);
call->frame_limit = 0;
} else {
/* old packet, dont read */
LOGGER_DEBUG("Old packet: %u\n", i);
goto end;
}
if (packet[1] > (MAX_VIDEOFRAME_SIZE - VIDEOFRAME_PIECE_SIZE + 1) /
VIDEOFRAME_PIECE_SIZE) { //TODO, fix this check? not sure
/* packet out of buffer range */
goto end;
}
LOGGER_DEBUG("Video Packet: %u %u\n", packet[0], packet[1]);
memcpy(call->frame_buf + packet[1] * VIDEOFRAME_PIECE_SIZE, packet + VIDEOFRAME_HEADER_SIZE,
recved_size - VIDEOFRAME_HEADER_SIZE);
uint32_t limit = packet[1] * VIDEOFRAME_PIECE_SIZE + recved_size - VIDEOFRAME_HEADER_SIZE;
if (limit > call->frame_limit) {
call->frame_limit = limit;
LOGGER_DEBUG("Limit: %u\n", call->frame_limit);
}
end:
;
2014-07-08 04:10:10 +08:00
rtp_free_msg(NULL, _msg);
}
}
/* Create a new toxav group.
*
* return group number on success.
* return -1 on failure.
2014-11-11 22:48:52 +08:00
*
* 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.
2014-11-11 22:48:52 +08:00
*
* 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);
}