2014-07-03 22:58:00 +08:00
|
|
|
/** codec.h
|
2014-02-17 09:01:30 +08:00
|
|
|
*
|
2014-02-16 05:36:15 +08:00
|
|
|
* Audio and video codec intitialization, encoding/decoding and playback
|
2013-10-13 22:14:27 +08:00
|
|
|
*
|
2014-02-16 05:36:15 +08:00
|
|
|
* Copyright (C) 2013 Tox project All Rights Reserved.
|
2013-10-13 22:14:27 +08:00
|
|
|
*
|
2014-02-16 05:36:15 +08:00
|
|
|
* This file is part of Tox.
|
2013-10-13 22:14:27 +08:00
|
|
|
*
|
2014-02-16 05:36:15 +08:00
|
|
|
* 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.
|
2013-10-13 22:14:27 +08:00
|
|
|
*
|
2014-02-16 05:36:15 +08:00
|
|
|
* 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.
|
2013-10-13 22:14:27 +08:00
|
|
|
*
|
2014-02-16 05:36:15 +08:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Tox. If not, see <http://www.gnu.org/licenses/>.
|
2013-10-13 22:14:27 +08:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2015-02-17 06:30:20 +08:00
|
|
|
#ifndef CODEC_H
|
|
|
|
#define CODEC_H
|
2013-10-13 22:14:27 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
#include "toxav.h"
|
|
|
|
#include "rtp.h"
|
|
|
|
|
2013-10-13 22:14:27 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <math.h>
|
2014-02-16 03:44:33 +08:00
|
|
|
#include <pthread.h>
|
2014-02-01 19:52:48 +08:00
|
|
|
|
2014-02-16 03:44:33 +08:00
|
|
|
#include <vpx/vpx_decoder.h>
|
|
|
|
#include <vpx/vpx_encoder.h>
|
|
|
|
#include <vpx/vp8dx.h>
|
|
|
|
#include <vpx/vp8cx.h>
|
2014-11-18 07:46:46 +08:00
|
|
|
#include <vpx/vpx_image.h>
|
2014-02-16 03:44:33 +08:00
|
|
|
#define VIDEO_CODEC_DECODER_INTERFACE (vpx_codec_vp8_dx())
|
|
|
|
#define VIDEO_CODEC_ENCODER_INTERFACE (vpx_codec_vp8_cx())
|
2013-10-13 22:14:27 +08:00
|
|
|
|
2014-02-01 19:52:48 +08:00
|
|
|
/* Audio encoding/decoding */
|
2014-04-13 19:27:53 +08:00
|
|
|
#include <opus.h>
|
2013-10-13 22:14:27 +08:00
|
|
|
|
2014-11-29 20:42:19 +08:00
|
|
|
#define PAIR(TYPE1__, TYPE2__) struct { TYPE1__ first; TYPE2__ second; }
|
|
|
|
|
2014-11-25 09:24:59 +08:00
|
|
|
typedef void (*CSAudioCallback) (void *agent, int32_t call_idx, const int16_t *PCM, uint16_t size, void *data);
|
|
|
|
typedef void (*CSVideoCallback) (void *agent, int32_t call_idx, const vpx_image_t *img, void *data);
|
2014-11-18 07:46:46 +08:00
|
|
|
|
2014-11-29 20:42:19 +08:00
|
|
|
/**
|
|
|
|
* Codec capabilities
|
|
|
|
*/
|
|
|
|
typedef enum {
|
|
|
|
cs_AudioEncoding = 1 << 0,
|
|
|
|
cs_AudioDecoding = 1 << 1,
|
|
|
|
cs_VideoEncoding = 1 << 2,
|
|
|
|
cs_VideoDecoding = 1 << 3
|
|
|
|
} CSCapabilities;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Codec errors.
|
|
|
|
*/
|
|
|
|
typedef enum {
|
|
|
|
cs_ErrorSettingVideoResolution = -30,
|
|
|
|
cs_ErrorSettingVideoBitrate = -31,
|
|
|
|
cs_ErrorSplittingVideoPayload = -32,
|
|
|
|
} CSError;
|
2013-10-13 22:14:27 +08:00
|
|
|
|
2014-11-29 20:42:19 +08:00
|
|
|
/**
|
|
|
|
* Codec session - controling codec
|
|
|
|
*/
|
2014-11-18 07:46:46 +08:00
|
|
|
typedef struct _CSSession {
|
2014-06-21 07:58:55 +08:00
|
|
|
|
2014-11-25 09:24:59 +08:00
|
|
|
/* VIDEO
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
2013-10-13 22:14:27 +08:00
|
|
|
|
|
|
|
/* video encoding */
|
2015-01-25 06:29:54 +08:00
|
|
|
vpx_codec_ctx_t v_encoder[1];
|
|
|
|
bool v_encoding;
|
2014-02-16 03:44:33 +08:00
|
|
|
uint32_t frame_counter;
|
2013-10-13 22:14:27 +08:00
|
|
|
|
|
|
|
/* video decoding */
|
2015-01-25 06:29:54 +08:00
|
|
|
vpx_codec_ctx_t v_decoder[1];
|
|
|
|
bool v_decoding;
|
|
|
|
void *vbuf_raw; /* Un-decoded data */
|
2014-11-25 09:24:59 +08:00
|
|
|
|
|
|
|
/* Data handling */
|
2014-11-18 07:46:46 +08:00
|
|
|
uint8_t *frame_buf; /* buffer for split video payloads */
|
|
|
|
uint32_t frame_size; /* largest address written to in frame_buf for current input frame*/
|
|
|
|
uint8_t frameid_in, frameid_out; /* id of input and output video frame */
|
|
|
|
uint32_t last_timestamp; /* calculating cycles */
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
/* Limits */
|
|
|
|
uint32_t video_frame_piece_size;
|
|
|
|
uint32_t max_video_frame_size;
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2015-02-02 06:43:54 +08:00
|
|
|
/* Splitting */
|
2014-11-18 07:46:46 +08:00
|
|
|
uint8_t *split_video_frame;
|
|
|
|
const uint8_t *processing_video_frame;
|
|
|
|
uint16_t processing_video_frame_size;
|
|
|
|
|
2014-11-25 09:24:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* AUDIO
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
2014-11-18 07:46:46 +08:00
|
|
|
|
2013-10-13 22:14:27 +08:00
|
|
|
/* audio encoding */
|
2014-02-01 19:52:48 +08:00
|
|
|
OpusEncoder *audio_encoder;
|
2015-02-02 06:43:54 +08:00
|
|
|
int32_t encoder_channels;
|
2015-01-25 06:29:54 +08:00
|
|
|
|
2013-10-13 22:14:27 +08:00
|
|
|
/* audio decoding */
|
2014-02-01 19:52:48 +08:00
|
|
|
OpusDecoder *audio_decoder;
|
2015-02-02 06:43:54 +08:00
|
|
|
int32_t last_pack_channels;
|
|
|
|
int32_t last_packet_sampling_rate;
|
|
|
|
int32_t last_packet_frame_duration;
|
2015-02-15 06:37:52 +08:00
|
|
|
struct JitterBuffer *j_buf;
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-06-21 07:58:55 +08:00
|
|
|
/* Voice activity detection */
|
2014-06-22 01:04:00 +08:00
|
|
|
uint32_t EVAD_tolerance; /* In frames */
|
|
|
|
uint32_t EVAD_tolerance_cr;
|
2013-10-13 22:14:27 +08:00
|
|
|
|
2014-02-01 19:52:48 +08:00
|
|
|
|
2014-05-03 07:46:03 +08:00
|
|
|
|
2014-11-25 09:24:59 +08:00
|
|
|
/* OTHER
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
2015-02-02 06:43:54 +08:00
|
|
|
void *agent; /* Pointer to ToxAV TODO make this pointer to ToxAV*/
|
2014-11-18 07:46:46 +08:00
|
|
|
int32_t call_idx;
|
2015-02-15 06:37:52 +08:00
|
|
|
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 */
|
2015-01-25 06:29:54 +08:00
|
|
|
|
|
|
|
pthread_mutex_t queue_mutex[1];
|
2014-11-18 07:46:46 +08:00
|
|
|
} CSSession;
|
2014-02-01 19:52:48 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
|
|
|
|
/**
|
2015-02-02 06:43:54 +08:00
|
|
|
* Generic
|
2014-11-18 07:46:46 +08:00
|
|
|
*/
|
2014-11-25 09:24:59 +08:00
|
|
|
void cs_do(CSSession *cs);
|
2013-10-13 22:14:27 +08:00
|
|
|
|
2015-02-02 06:43:54 +08:00
|
|
|
/* Make sure to be called BEFORE corresponding rtp_new */
|
|
|
|
CSSession *cs_new(uint32_t s_audio_b, uint32_t p_audio_b, uint32_t s_video_b, uint32_t p_video_b);
|
|
|
|
/* Make sure to be called AFTER corresponding rtp_kill */
|
|
|
|
void cs_kill(CSSession *cs);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* VIDEO HANDLING
|
2015-01-25 06:29:54 +08:00
|
|
|
*/
|
2015-02-02 06:43:54 +08:00
|
|
|
void cs_init_video_splitter_cycle(CSSession *cs);
|
|
|
|
int cs_update_video_splitter_cycle(CSSession* cs, const uint8_t* payload, uint16_t length);
|
|
|
|
const uint8_t *cs_iterate_split_video_frame(CSSession *cs, uint16_t *size);
|
|
|
|
|
2015-01-25 06:29:54 +08:00
|
|
|
int cs_set_sending_video_resolution(CSSession *cs, uint16_t width, uint16_t height);
|
|
|
|
int cs_set_sending_video_bitrate(CSSession *cs, uint32_t bitrate);
|
|
|
|
|
2015-02-02 06:43:54 +08:00
|
|
|
int cs_enable_video_sending(CSSession* cs, uint32_t bitrate);
|
|
|
|
int cs_enable_video_receiving(CSSession* cs);
|
|
|
|
|
|
|
|
void cs_disable_video_sending(CSSession* cs);
|
|
|
|
void cs_disable_video_receiving(CSSession* cs);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* AUDIO HANDLING
|
|
|
|
*/
|
2015-01-25 06:29:54 +08:00
|
|
|
int cs_set_sending_audio_bitrate(CSSession* cs, int32_t rate);
|
|
|
|
int cs_set_sending_audio_sampling_rate(CSSession* cs, int32_t rate);
|
|
|
|
int cs_set_sending_audio_channels(CSSession* cs, int32_t count);
|
|
|
|
|
2015-02-02 06:43:54 +08:00
|
|
|
int cs_enable_audio_sending(CSSession* cs, uint32_t bitrate, int channels);
|
2015-01-25 06:29:54 +08:00
|
|
|
int cs_enable_audio_receiving(CSSession* cs);
|
2013-10-13 22:14:27 +08:00
|
|
|
|
2015-01-25 06:29:54 +08:00
|
|
|
void cs_disable_audio_sending(CSSession* cs);
|
|
|
|
void cs_disable_audio_receiving(CSSession* cs);
|
2015-02-02 06:43:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-06-21 07:58:55 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
/* Internal. Called from rtp_handle_message */
|
|
|
|
void queue_message(RTPSession *session, RTPMessage *msg);
|
2015-02-17 06:30:20 +08:00
|
|
|
#endif /* CODEC_H */
|