2014-07-03 22:58:00 +08:00
|
|
|
/** codec.c
|
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
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif /* HAVE_CONFIG_H */
|
|
|
|
|
2014-05-03 07:46:03 +08:00
|
|
|
#include "../toxcore/logger.h"
|
2014-11-29 20:42:19 +08:00
|
|
|
#include "../toxcore/util.h"
|
2014-05-03 07:46:03 +08:00
|
|
|
|
2013-10-13 22:14:27 +08:00
|
|
|
#include <stdio.h>
|
2014-02-16 03:44:33 +08:00
|
|
|
#include <stdlib.h>
|
2013-10-13 22:14:27 +08:00
|
|
|
#include <math.h>
|
2014-02-01 19:52:48 +08:00
|
|
|
#include <assert.h>
|
2014-11-18 07:46:46 +08:00
|
|
|
#include <time.h>
|
2013-10-13 22:14:27 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
#include "msi.h"
|
2014-02-10 06:06:44 +08:00
|
|
|
#include "rtp.h"
|
2014-06-21 07:58:55 +08:00
|
|
|
#include "codec.h"
|
|
|
|
|
2014-12-13 08:55:47 +08:00
|
|
|
/* Good quality encode. */
|
2014-11-18 07:46:46 +08:00
|
|
|
#define MAX_DECODE_TIME_US 0
|
|
|
|
|
|
|
|
// TODO this has to be exchanged in msi
|
|
|
|
#define MAX_VIDEOFRAME_SIZE 0x40000 /* 256KiB */
|
|
|
|
#define VIDEOFRAME_PIECE_SIZE 0x500 /* 1.25 KiB*/
|
|
|
|
#define VIDEOFRAME_HEADER_SIZE 0x2
|
|
|
|
|
|
|
|
/* FIXME: Might not be enough */
|
|
|
|
#define VIDEO_DECODE_BUFFER_SIZE 20
|
|
|
|
|
2014-11-29 20:42:19 +08:00
|
|
|
#define ARRAY(TYPE__) struct { uint16_t size; TYPE__ data[]; }
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
typedef ARRAY(uint8_t) Payload;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint16_t size; /* Max size */
|
|
|
|
uint16_t start;
|
|
|
|
uint16_t end;
|
|
|
|
Payload **packets;
|
|
|
|
} PayloadBuffer;
|
|
|
|
|
|
|
|
static _Bool buffer_full(const PayloadBuffer *b)
|
2014-03-07 10:13:04 +08:00
|
|
|
{
|
2014-11-18 07:46:46 +08:00
|
|
|
return (b->end + 1) % b->size == b->start;
|
|
|
|
}
|
|
|
|
|
|
|
|
static _Bool buffer_empty(const PayloadBuffer *b)
|
|
|
|
{
|
|
|
|
return b->end == b->start;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void buffer_write(PayloadBuffer *b, Payload *p)
|
|
|
|
{
|
|
|
|
b->packets[b->end] = p;
|
|
|
|
b->end = (b->end + 1) % b->size;
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
if (b->end == b->start) b->start = (b->start + 1) % b->size; /* full, overwrite */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void buffer_read(PayloadBuffer *b, Payload **p)
|
|
|
|
{
|
|
|
|
*p = b->packets[b->start];
|
|
|
|
b->start = (b->start + 1) % b->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void buffer_clear(PayloadBuffer *b)
|
|
|
|
{
|
|
|
|
while (!buffer_empty(b)) {
|
2014-11-25 09:24:59 +08:00
|
|
|
Payload *p;
|
2014-11-18 07:46:46 +08:00
|
|
|
buffer_read(b, &p);
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-25 09:24:59 +08:00
|
|
|
static PayloadBuffer *buffer_new(int size)
|
2014-11-18 07:46:46 +08:00
|
|
|
{
|
|
|
|
PayloadBuffer *buf = calloc(sizeof(PayloadBuffer), 1);
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
if (!buf) return NULL;
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
buf->size = size + 1; /* include empty elem */
|
2014-11-25 09:24:59 +08:00
|
|
|
|
|
|
|
if (!(buf->packets = calloc(buf->size, sizeof(Payload *)))) {
|
2014-11-18 07:46:46 +08:00
|
|
|
free(buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
return buf;
|
|
|
|
}
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
static void buffer_free(PayloadBuffer *b)
|
|
|
|
{
|
|
|
|
if (b) {
|
|
|
|
buffer_clear(b);
|
|
|
|
free(b->packets);
|
|
|
|
free(b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* JITTER BUFFER WORK */
|
|
|
|
typedef struct _JitterBuffer {
|
|
|
|
RTPMessage **queue;
|
|
|
|
uint32_t size;
|
|
|
|
uint32_t capacity;
|
|
|
|
uint16_t bottom;
|
|
|
|
uint16_t top;
|
|
|
|
} JitterBuffer;
|
|
|
|
|
|
|
|
static JitterBuffer *jbuf_new(uint32_t capacity)
|
2014-03-07 10:13:04 +08:00
|
|
|
{
|
2014-07-25 07:45:38 +08:00
|
|
|
unsigned int size = 1;
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-12-13 07:39:39 +08:00
|
|
|
while (size <= (capacity * 4)) {
|
2014-07-25 07:45:38 +08:00
|
|
|
size *= 2;
|
2014-03-07 10:13:04 +08:00
|
|
|
}
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-05-11 00:00:49 +08:00
|
|
|
JitterBuffer *q;
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-05-11 00:00:49 +08:00
|
|
|
if ( !(q = calloc(sizeof(JitterBuffer), 1)) ) return NULL;
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-07-25 07:45:38 +08:00
|
|
|
if (!(q->queue = calloc(sizeof(RTPMessage *), size))) {
|
2014-05-31 06:29:43 +08:00
|
|
|
free(q);
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-07-25 07:45:38 +08:00
|
|
|
q->size = size;
|
|
|
|
q->capacity = capacity;
|
2013-10-13 22:14:27 +08:00
|
|
|
return q;
|
|
|
|
}
|
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
static void jbuf_clear(JitterBuffer *q)
|
2013-10-13 22:14:27 +08:00
|
|
|
{
|
2014-07-25 07:45:38 +08:00
|
|
|
for (; q->bottom != q->top; ++q->bottom) {
|
2014-07-25 09:00:42 +08:00
|
|
|
if (q->queue[q->bottom % q->size]) {
|
2014-07-25 07:45:38 +08:00
|
|
|
rtp_free_msg(NULL, q->queue[q->bottom % q->size]);
|
2014-07-25 09:00:42 +08:00
|
|
|
q->queue[q->bottom % q->size] = NULL;
|
|
|
|
}
|
2014-07-25 07:45:38 +08:00
|
|
|
}
|
2014-07-25 08:56:07 +08:00
|
|
|
}
|
2014-07-06 00:25:30 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
static void jbuf_free(JitterBuffer *q)
|
2014-07-25 08:56:07 +08:00
|
|
|
{
|
|
|
|
if (!q) return;
|
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
jbuf_clear(q);
|
2014-07-25 07:45:38 +08:00
|
|
|
free(q->queue);
|
2014-05-11 00:00:49 +08:00
|
|
|
free(q);
|
2013-10-13 22:14:27 +08:00
|
|
|
}
|
|
|
|
|
2014-12-13 09:55:43 +08:00
|
|
|
static int jbuf_write(JitterBuffer *q, RTPMessage *m)
|
2013-10-13 22:14:27 +08:00
|
|
|
{
|
2014-11-18 07:46:46 +08:00
|
|
|
uint16_t sequnum = m->header->sequnum;
|
2013-10-13 22:14:27 +08:00
|
|
|
|
2014-07-25 07:45:38 +08:00
|
|
|
unsigned int num = sequnum % q->size;
|
2013-10-13 22:14:27 +08:00
|
|
|
|
2014-08-04 06:15:00 +08:00
|
|
|
if ((uint32_t)(sequnum - q->bottom) > q->size) {
|
2014-11-18 07:46:46 +08:00
|
|
|
jbuf_clear(q);
|
2014-11-30 05:13:50 +08:00
|
|
|
q->bottom = sequnum - q->capacity;
|
2014-11-18 07:46:46 +08:00
|
|
|
q->queue[num] = m;
|
2014-07-25 08:56:07 +08:00
|
|
|
q->top = sequnum + 1;
|
2014-12-13 09:55:43 +08:00
|
|
|
return 0;
|
2014-07-25 08:56:07 +08:00
|
|
|
}
|
|
|
|
|
2014-07-25 07:45:38 +08:00
|
|
|
if (q->queue[num])
|
2014-12-13 09:55:43 +08:00
|
|
|
return -1;
|
2013-10-13 22:14:27 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
q->queue[num] = m;
|
2013-10-13 22:14:27 +08:00
|
|
|
|
2014-07-25 07:45:38 +08:00
|
|
|
if ((sequnum - q->bottom) >= (q->top - q->bottom))
|
|
|
|
q->top = sequnum + 1;
|
2014-11-30 05:13:50 +08:00
|
|
|
|
2014-12-13 09:55:43 +08:00
|
|
|
return 0;
|
2014-07-25 07:45:38 +08:00
|
|
|
}
|
2013-10-13 22:14:27 +08:00
|
|
|
|
2014-11-25 09:24:59 +08:00
|
|
|
/* Success is 0 when there is nothing to dequeue,
|
|
|
|
* 1 when there's a good packet,
|
2014-11-18 07:46:46 +08:00
|
|
|
* 2 when there's a lost packet */
|
|
|
|
static RTPMessage *jbuf_read(JitterBuffer *q, int32_t *success)
|
2013-10-13 22:14:27 +08:00
|
|
|
{
|
2014-07-25 07:45:38 +08:00
|
|
|
if (q->top == q->bottom) {
|
|
|
|
*success = 0;
|
|
|
|
return NULL;
|
2013-10-13 22:14:27 +08:00
|
|
|
}
|
|
|
|
|
2014-07-25 07:45:38 +08:00
|
|
|
unsigned int num = q->bottom % q->size;
|
2013-10-13 22:14:27 +08:00
|
|
|
|
2014-07-25 07:45:38 +08:00
|
|
|
if (q->queue[num]) {
|
|
|
|
RTPMessage *ret = q->queue[num];
|
|
|
|
q->queue[num] = NULL;
|
|
|
|
++q->bottom;
|
|
|
|
*success = 1;
|
|
|
|
return ret;
|
|
|
|
}
|
2013-10-13 22:14:27 +08:00
|
|
|
|
2014-08-04 06:15:00 +08:00
|
|
|
if ((uint32_t)(q->top - q->bottom) > q->capacity) {
|
2014-07-25 07:45:38 +08:00
|
|
|
++q->bottom;
|
|
|
|
*success = 2;
|
|
|
|
return NULL;
|
2013-10-13 22:14:27 +08:00
|
|
|
}
|
2014-07-25 07:45:38 +08:00
|
|
|
|
|
|
|
*success = 0;
|
|
|
|
return NULL;
|
2013-10-13 22:14:27 +08:00
|
|
|
}
|
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
static int init_video_decoder(CSSession *cs)
|
2013-10-13 22:14:27 +08:00
|
|
|
{
|
2014-05-03 07:46:03 +08:00
|
|
|
int rc = vpx_codec_dec_init_ver(&cs->v_decoder, VIDEO_CODEC_DECODER_INTERFACE, NULL, 0, VPX_DECODER_ABI_VERSION);
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-05-03 07:46:03 +08:00
|
|
|
if ( rc != VPX_CODEC_OK) {
|
|
|
|
LOGGER_ERROR("Init video_decoder failed: %s", vpx_codec_err_to_string(rc));
|
2014-02-10 06:06:44 +08:00
|
|
|
return -1;
|
2013-10-13 22:14:27 +08:00
|
|
|
}
|
2014-02-16 03:44:33 +08:00
|
|
|
|
2014-02-10 06:06:44 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2013-10-13 22:14:27 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
static int init_audio_decoder(CSSession *cs)
|
2014-02-10 06:06:44 +08:00
|
|
|
{
|
|
|
|
int rc;
|
2014-11-18 07:46:46 +08:00
|
|
|
cs->audio_decoder = opus_decoder_create(cs->audio_decoder_sample_rate, cs->audio_decoder_channels, &rc );
|
2014-02-17 09:01:30 +08:00
|
|
|
|
|
|
|
if ( rc != OPUS_OK ) {
|
2014-05-03 07:46:03 +08:00
|
|
|
LOGGER_ERROR("Error while starting audio decoder: %s", opus_strerror(rc));
|
2014-02-10 06:06:44 +08:00
|
|
|
return -1;
|
2014-02-17 09:01:30 +08:00
|
|
|
}
|
|
|
|
|
2014-02-10 06:06:44 +08:00
|
|
|
return 0;
|
2013-10-13 22:14:27 +08:00
|
|
|
}
|
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
static int init_video_encoder(CSSession *cs, uint16_t max_width, uint16_t max_height, uint32_t video_bitrate)
|
2013-10-13 22:14:27 +08:00
|
|
|
{
|
2014-02-16 03:44:33 +08:00
|
|
|
vpx_codec_enc_cfg_t cfg;
|
2014-05-03 07:46:03 +08:00
|
|
|
int rc = vpx_codec_enc_config_default(VIDEO_CODEC_ENCODER_INTERFACE, &cfg, 0);
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
if (rc != VPX_CODEC_OK) {
|
2014-05-03 07:46:03 +08:00
|
|
|
LOGGER_ERROR("Failed to get config: %s", vpx_codec_err_to_string(rc));
|
2014-02-10 06:06:44 +08:00
|
|
|
return -1;
|
2013-10-13 22:14:27 +08:00
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-02-16 03:44:33 +08:00
|
|
|
cfg.rc_target_bitrate = video_bitrate;
|
2014-07-06 02:36:19 +08:00
|
|
|
cfg.g_w = max_width;
|
|
|
|
cfg.g_h = max_height;
|
2014-06-25 06:07:31 +08:00
|
|
|
cfg.g_pass = VPX_RC_ONE_PASS;
|
|
|
|
cfg.g_error_resilient = VPX_ERROR_RESILIENT_DEFAULT | VPX_ERROR_RESILIENT_PARTITIONS;
|
|
|
|
cfg.g_lag_in_frames = 0;
|
|
|
|
cfg.kf_min_dist = 0;
|
2014-12-13 08:55:47 +08:00
|
|
|
cfg.kf_max_dist = 48;
|
2014-07-06 02:36:19 +08:00
|
|
|
cfg.kf_mode = VPX_KF_AUTO;
|
|
|
|
|
2014-05-03 07:46:03 +08:00
|
|
|
rc = vpx_codec_enc_init_ver(&cs->v_encoder, VIDEO_CODEC_ENCODER_INTERFACE, &cfg, 0, VPX_ENCODER_ABI_VERSION);
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-05-03 07:46:03 +08:00
|
|
|
if ( rc != VPX_CODEC_OK) {
|
|
|
|
LOGGER_ERROR("Failed to initialize encoder: %s", vpx_codec_err_to_string(rc));
|
2014-02-10 06:06:44 +08:00
|
|
|
return -1;
|
2013-10-13 22:14:27 +08:00
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-10-18 07:54:42 +08:00
|
|
|
rc = vpx_codec_control(&cs->v_encoder, VP8E_SET_CPUUSED, 8);
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-07-03 22:58:00 +08:00
|
|
|
if ( rc != VPX_CODEC_OK) {
|
|
|
|
LOGGER_ERROR("Failed to set encoder control setting: %s", vpx_codec_err_to_string(rc));
|
|
|
|
return -1;
|
|
|
|
}
|
2014-07-03 23:13:11 +08:00
|
|
|
|
2014-12-17 02:10:28 +08:00
|
|
|
cs->max_width = max_width;
|
|
|
|
cs->max_height = max_height;
|
|
|
|
cs->video_bitrate = video_bitrate;
|
|
|
|
|
2014-02-10 06:06:44 +08:00
|
|
|
return 0;
|
2013-10-13 22:14:27 +08:00
|
|
|
}
|
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
static int init_audio_encoder(CSSession *cs)
|
2013-10-13 22:14:27 +08:00
|
|
|
{
|
2014-05-03 07:46:03 +08:00
|
|
|
int rc = OPUS_OK;
|
2014-11-25 09:24:59 +08:00
|
|
|
cs->audio_encoder = opus_encoder_create(cs->audio_encoder_sample_rate,
|
2014-11-18 07:46:46 +08:00
|
|
|
cs->audio_encoder_channels, OPUS_APPLICATION_AUDIO, &rc);
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-05-03 07:46:03 +08:00
|
|
|
if ( rc != OPUS_OK ) {
|
|
|
|
LOGGER_ERROR("Error while starting audio encoder: %s", opus_strerror(rc));
|
|
|
|
return -1;
|
|
|
|
}
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
rc = opus_encoder_ctl(cs->audio_encoder, OPUS_SET_BITRATE(cs->audio_encoder_bitrate));
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-05-03 07:46:03 +08:00
|
|
|
if ( rc != OPUS_OK ) {
|
|
|
|
LOGGER_ERROR("Error while setting encoder ctl: %s", opus_strerror(rc));
|
|
|
|
return -1;
|
|
|
|
}
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-05-03 07:46:03 +08:00
|
|
|
rc = opus_encoder_ctl(cs->audio_encoder, OPUS_SET_COMPLEXITY(10));
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-05-03 07:46:03 +08:00
|
|
|
if ( rc != OPUS_OK ) {
|
|
|
|
LOGGER_ERROR("Error while setting encoder ctl: %s", opus_strerror(rc));
|
|
|
|
return -1;
|
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-05-03 07:46:03 +08:00
|
|
|
return 0;
|
2013-10-13 22:14:27 +08:00
|
|
|
}
|
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
/* PUBLIC */
|
2014-11-25 09:24:59 +08:00
|
|
|
int cs_split_video_payload(CSSession *cs, const uint8_t *payload, uint16_t length)
|
2013-10-13 22:14:27 +08:00
|
|
|
{
|
2014-11-18 07:46:46 +08:00
|
|
|
if (!cs || !length || length > cs->max_video_frame_size) {
|
|
|
|
LOGGER_ERROR("Invalid CodecState or video frame size: %u", length);
|
2014-11-29 20:42:19 +08:00
|
|
|
return cs_ErrorSplittingVideoPayload;
|
2014-11-18 07:46:46 +08:00
|
|
|
}
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
cs->split_video_frame[0] = cs->frameid_out++;
|
|
|
|
cs->split_video_frame[1] = 0;
|
|
|
|
cs->processing_video_frame = payload;
|
|
|
|
cs->processing_video_frame_size = length;
|
2014-02-16 03:44:33 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
return ((length - 1) / cs->video_frame_piece_size) + 1;
|
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-11-25 09:24:59 +08:00
|
|
|
const uint8_t *cs_get_split_video_frame(CSSession *cs, uint16_t *size)
|
|
|
|
{
|
2014-11-18 07:46:46 +08:00
|
|
|
if (!cs || !size) return NULL;
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
if (cs->processing_video_frame_size > cs->video_frame_piece_size) {
|
2014-11-25 09:24:59 +08:00
|
|
|
memcpy(cs->split_video_frame + VIDEOFRAME_HEADER_SIZE,
|
2014-11-18 07:46:46 +08:00
|
|
|
cs->processing_video_frame,
|
|
|
|
cs->video_frame_piece_size);
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
cs->processing_video_frame += cs->video_frame_piece_size;
|
|
|
|
cs->processing_video_frame_size -= cs->video_frame_piece_size;
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
*size = cs->video_frame_piece_size + VIDEOFRAME_HEADER_SIZE;
|
2014-05-26 00:27:48 +08:00
|
|
|
} else {
|
2014-11-25 09:24:59 +08:00
|
|
|
memcpy(cs->split_video_frame + VIDEOFRAME_HEADER_SIZE,
|
2014-11-18 07:46:46 +08:00
|
|
|
cs->processing_video_frame,
|
|
|
|
cs->processing_video_frame_size);
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
*size = cs->processing_video_frame_size + VIDEOFRAME_HEADER_SIZE;
|
|
|
|
}
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
cs->split_video_frame[1]++;
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
return cs->split_video_frame;
|
|
|
|
}
|
|
|
|
|
2014-11-25 09:24:59 +08:00
|
|
|
void cs_do(CSSession *cs)
|
2014-11-18 07:46:46 +08:00
|
|
|
{
|
2014-11-29 20:42:19 +08:00
|
|
|
/* Codec session should always be protected by call mutex so no need to check for cs validity
|
|
|
|
*/
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
if (!cs) return;
|
2014-11-25 09:24:59 +08:00
|
|
|
|
|
|
|
Payload *p;
|
2014-11-18 07:46:46 +08:00
|
|
|
int rc;
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-12-13 07:35:52 +08:00
|
|
|
int success = 0;
|
2014-11-30 05:09:24 +08:00
|
|
|
|
2014-12-13 07:35:52 +08:00
|
|
|
pthread_mutex_lock(cs->queue_mutex);
|
|
|
|
RTPMessage *msg;
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-12-13 07:35:52 +08:00
|
|
|
while ((msg = jbuf_read(cs->j_buf, &success)) || success == 2) {
|
2014-11-29 20:42:19 +08:00
|
|
|
pthread_mutex_unlock(cs->queue_mutex);
|
2014-11-30 05:09:24 +08:00
|
|
|
|
2014-12-08 07:23:55 +08:00
|
|
|
uint16_t fsize = ((cs->audio_decoder_sample_rate * cs->audio_decoder_frame_duration) / 1000);
|
|
|
|
int16_t tmp[fsize * cs->audio_decoder_channels];
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-12-13 07:35:52 +08:00
|
|
|
if (success == 2) {
|
|
|
|
rc = opus_decode(cs->audio_decoder, 0, 0, tmp, fsize, 1);
|
|
|
|
} else {
|
|
|
|
rc = opus_decode(cs->audio_decoder, msg->data, msg->length, tmp, fsize, 0);
|
2014-12-13 09:55:43 +08:00
|
|
|
rtp_free_msg(NULL, msg);
|
2014-12-13 07:35:52 +08:00
|
|
|
}
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-12-13 07:35:52 +08:00
|
|
|
if (rc < 0) {
|
2014-11-18 07:46:46 +08:00
|
|
|
LOGGER_WARNING("Decoding error: %s", opus_strerror(rc));
|
2014-12-13 07:35:52 +08:00
|
|
|
} else if (cs->acb.first) {
|
2014-11-18 07:46:46 +08:00
|
|
|
/* Play */
|
2014-11-29 20:42:19 +08:00
|
|
|
cs->acb.first(cs->agent, cs->call_idx, tmp, rc, cs->acb.second);
|
2014-12-13 07:35:52 +08:00
|
|
|
}
|
2014-11-30 05:09:24 +08:00
|
|
|
|
2014-11-29 20:42:19 +08:00
|
|
|
pthread_mutex_lock(cs->queue_mutex);
|
2014-11-18 07:46:46 +08:00
|
|
|
}
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
if (cs->vbuf_raw && !buffer_empty(cs->vbuf_raw)) {
|
|
|
|
/* Decode video */
|
|
|
|
buffer_read(cs->vbuf_raw, &p);
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-29 20:42:19 +08:00
|
|
|
/* Leave space for (possibly) other thread to queue more data after we read it here */
|
|
|
|
pthread_mutex_unlock(cs->queue_mutex);
|
2014-11-30 05:09:24 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
rc = vpx_codec_decode(&cs->v_decoder, p->data, p->size, NULL, MAX_DECODE_TIME_US);
|
|
|
|
free(p);
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
if (rc != VPX_CODEC_OK) {
|
|
|
|
LOGGER_ERROR("Error decoding video: %s", vpx_codec_err_to_string(rc));
|
|
|
|
} else {
|
|
|
|
vpx_codec_iter_t iter = NULL;
|
2014-11-25 09:24:59 +08:00
|
|
|
vpx_image_t *dest = vpx_codec_get_frame(&cs->v_decoder, &iter);
|
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
/* Play decoded images */
|
|
|
|
for (; dest; dest = vpx_codec_get_frame(&cs->v_decoder, &iter)) {
|
2014-11-29 20:42:19 +08:00
|
|
|
if (cs->vcb.first)
|
|
|
|
cs->vcb.first(cs->agent, cs->call_idx, dest, cs->vcb.second);
|
2014-11-30 05:09:24 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
vpx_img_free(dest);
|
|
|
|
}
|
|
|
|
}
|
2014-11-30 05:09:24 +08:00
|
|
|
|
2014-11-29 20:42:19 +08:00
|
|
|
return;
|
2014-02-16 03:44:33 +08:00
|
|
|
}
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
pthread_mutex_unlock(cs->queue_mutex);
|
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
int cs_set_video_encoder_resolution(CSSession *cs, uint16_t width, uint16_t height)
|
|
|
|
{
|
|
|
|
vpx_codec_enc_cfg_t cfg = *cs->v_encoder.config.enc;
|
|
|
|
|
|
|
|
if (cfg.g_w == width && cfg.g_h == height)
|
|
|
|
return 0;
|
|
|
|
|
2014-12-17 02:10:28 +08:00
|
|
|
if (width * height > cs->max_width * cs->max_height) {
|
|
|
|
vpx_codec_ctx_t v_encoder = cs->v_encoder;
|
|
|
|
|
|
|
|
if (init_video_encoder(cs, width, height, cs->video_bitrate) == -1) {
|
|
|
|
cs->v_encoder = v_encoder;
|
|
|
|
return cs_ErrorSettingVideoResolution;
|
|
|
|
}
|
|
|
|
|
|
|
|
vpx_codec_destroy(&v_encoder);
|
|
|
|
return 0;
|
|
|
|
}
|
2014-11-18 07:46:46 +08:00
|
|
|
|
|
|
|
LOGGER_DEBUG("New video resolution: %u %u", width, height);
|
|
|
|
cfg.g_w = width;
|
|
|
|
cfg.g_h = height;
|
|
|
|
int rc = vpx_codec_enc_config_set(&cs->v_encoder, &cfg);
|
|
|
|
|
|
|
|
if ( rc != VPX_CODEC_OK) {
|
|
|
|
LOGGER_ERROR("Failed to set encoder control setting: %s", vpx_codec_err_to_string(rc));
|
2014-11-29 20:42:19 +08:00
|
|
|
return cs_ErrorSettingVideoResolution;
|
2014-05-11 00:00:49 +08:00
|
|
|
}
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2014-06-22 20:39:26 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
int cs_set_video_encoder_bitrate(CSSession *cs, uint32_t video_bitrate)
|
|
|
|
{
|
|
|
|
vpx_codec_enc_cfg_t cfg = *cs->v_encoder.config.enc;
|
|
|
|
|
|
|
|
if (cfg.rc_target_bitrate == video_bitrate)
|
|
|
|
return 0;
|
2014-06-22 20:39:26 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
LOGGER_DEBUG("New video bitrate: %u", video_bitrate);
|
|
|
|
cfg.rc_target_bitrate = video_bitrate;
|
|
|
|
int rc = vpx_codec_enc_config_set(&cs->v_encoder, &cfg);
|
|
|
|
|
|
|
|
if ( rc != VPX_CODEC_OK) {
|
|
|
|
LOGGER_ERROR("Failed to set encoder control setting: %s", vpx_codec_err_to_string(rc));
|
2014-11-29 20:42:19 +08:00
|
|
|
return cs_ErrorSettingVideoBitrate;
|
2014-02-16 03:44:33 +08:00
|
|
|
}
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-12-17 02:10:28 +08:00
|
|
|
cs->video_bitrate = video_bitrate;
|
2014-11-18 07:46:46 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
CSSession *cs_new(const ToxAvCSettings *cs_self, const ToxAvCSettings *cs_peer, uint32_t jbuf_size, int has_video)
|
|
|
|
{
|
|
|
|
CSSession *cs = calloc(sizeof(CSSession), 1);
|
2014-02-17 09:01:30 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
if (!cs) {
|
|
|
|
LOGGER_WARNING("Allocation failed! Application might misbehave!");
|
2014-05-11 00:00:49 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-11-29 20:42:19 +08:00
|
|
|
if (create_recursive_mutex(cs->queue_mutex) != 0) {
|
|
|
|
LOGGER_WARNING("Failed to create recursive mutex!");
|
2014-12-13 09:41:27 +08:00
|
|
|
free(cs);
|
2014-11-29 20:42:19 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2014-11-30 05:09:24 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
if ( !(cs->j_buf = jbuf_new(jbuf_size)) ) {
|
|
|
|
LOGGER_WARNING("Jitter buffer creaton failed!");
|
|
|
|
goto error;
|
|
|
|
}
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
cs->audio_encoder_bitrate = cs_self->audio_bitrate;
|
|
|
|
cs->audio_encoder_sample_rate = cs_self->audio_sample_rate;
|
|
|
|
cs->audio_encoder_channels = cs_self->audio_channels;
|
|
|
|
cs->audio_encoder_frame_duration = cs_self->audio_frame_duration;
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
cs->audio_decoder_bitrate = cs_peer->audio_bitrate;
|
|
|
|
cs->audio_decoder_sample_rate = cs_peer->audio_sample_rate;
|
|
|
|
cs->audio_decoder_channels = cs_peer->audio_channels;
|
|
|
|
cs->audio_decoder_frame_duration = cs_peer->audio_frame_duration;
|
2014-06-22 20:39:26 +08:00
|
|
|
|
|
|
|
|
2014-11-29 20:42:19 +08:00
|
|
|
cs->capabilities |= ( 0 == init_audio_encoder(cs) ) ? cs_AudioEncoding : 0;
|
|
|
|
cs->capabilities |= ( 0 == init_audio_decoder(cs) ) ? cs_AudioDecoding : 0;
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-29 20:42:19 +08:00
|
|
|
if ( !(cs->capabilities & cs_AudioEncoding) || !(cs->capabilities & cs_AudioDecoding) ) goto error;
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
if ((cs->support_video = has_video)) {
|
|
|
|
cs->max_video_frame_size = MAX_VIDEOFRAME_SIZE;
|
|
|
|
cs->video_frame_piece_size = VIDEOFRAME_PIECE_SIZE;
|
2014-11-25 09:24:59 +08:00
|
|
|
|
|
|
|
cs->capabilities |= ( 0 == init_video_encoder(cs, cs_self->max_video_width,
|
2014-11-29 20:42:19 +08:00
|
|
|
cs_self->max_video_height, cs_self->video_bitrate) ) ? cs_VideoEncoding : 0;
|
|
|
|
cs->capabilities |= ( 0 == init_video_decoder(cs) ) ? cs_VideoDecoding : 0;
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-29 20:42:19 +08:00
|
|
|
if ( !(cs->capabilities & cs_VideoEncoding) || !(cs->capabilities & cs_VideoDecoding) ) goto error;
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
if ( !(cs->frame_buf = calloc(cs->max_video_frame_size, 1)) ) goto error;
|
2014-11-25 09:24:59 +08:00
|
|
|
|
|
|
|
if ( !(cs->split_video_frame = calloc(cs->video_frame_piece_size + VIDEOFRAME_HEADER_SIZE, 1)) )
|
2014-11-18 07:46:46 +08:00
|
|
|
goto error;
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
if ( !(cs->vbuf_raw = buffer_new(VIDEO_DECODE_BUFFER_SIZE)) ) goto error;
|
|
|
|
}
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
return cs;
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
error:
|
|
|
|
LOGGER_WARNING("Error initializing codec session! Application might misbehave!");
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-29 20:42:19 +08:00
|
|
|
pthread_mutex_destroy(cs->queue_mutex);
|
2014-11-30 05:09:24 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
if ( cs->audio_encoder ) opus_encoder_destroy(cs->audio_encoder);
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
if ( cs->audio_decoder ) opus_decoder_destroy(cs->audio_decoder);
|
2014-11-25 09:24:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
if (has_video) {
|
2014-11-29 20:42:19 +08:00
|
|
|
if ( cs->capabilities & cs_VideoDecoding ) vpx_codec_destroy(&cs->v_decoder);
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-29 20:42:19 +08:00
|
|
|
if ( cs->capabilities & cs_VideoEncoding ) vpx_codec_destroy(&cs->v_encoder);
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
buffer_free(cs->vbuf_raw);
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
free(cs->frame_buf);
|
|
|
|
free(cs->split_video_frame);
|
|
|
|
}
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
jbuf_free(cs->j_buf);
|
|
|
|
free(cs);
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
return NULL;
|
2013-10-13 22:14:27 +08:00
|
|
|
}
|
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
void cs_kill(CSSession *cs)
|
2013-10-13 22:14:27 +08:00
|
|
|
{
|
2014-07-05 21:11:25 +08:00
|
|
|
if (!cs) return;
|
2014-07-06 00:25:30 +08:00
|
|
|
|
2014-11-29 20:42:19 +08:00
|
|
|
/* queue_message will not be called since it's unregistered before cs_kill is called */
|
2014-11-18 07:46:46 +08:00
|
|
|
pthread_mutex_destroy(cs->queue_mutex);
|
2014-11-25 09:24:59 +08:00
|
|
|
|
|
|
|
|
2014-05-26 00:27:48 +08:00
|
|
|
if ( cs->audio_encoder )
|
2014-02-10 06:06:44 +08:00
|
|
|
opus_encoder_destroy(cs->audio_encoder);
|
2014-05-26 00:27:48 +08:00
|
|
|
|
|
|
|
if ( cs->audio_decoder )
|
2014-02-10 06:06:44 +08:00
|
|
|
opus_decoder_destroy(cs->audio_decoder);
|
2014-02-16 03:44:33 +08:00
|
|
|
|
2014-11-29 20:42:19 +08:00
|
|
|
if ( cs->capabilities & cs_VideoDecoding )
|
2014-03-07 10:13:04 +08:00
|
|
|
vpx_codec_destroy(&cs->v_decoder);
|
2014-05-26 00:27:48 +08:00
|
|
|
|
2014-11-29 20:42:19 +08:00
|
|
|
if ( cs->capabilities & cs_VideoEncoding )
|
2014-03-07 10:13:04 +08:00
|
|
|
vpx_codec_destroy(&cs->v_encoder);
|
2014-07-06 00:25:30 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
jbuf_free(cs->j_buf);
|
|
|
|
buffer_free(cs->vbuf_raw);
|
|
|
|
free(cs->frame_buf);
|
2015-02-07 04:16:07 +08:00
|
|
|
free(cs->split_video_frame);
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-07-05 21:11:25 +08:00
|
|
|
LOGGER_DEBUG("Terminated codec state: %p", cs);
|
|
|
|
free(cs);
|
2014-02-10 06:06:44 +08:00
|
|
|
}
|
2014-06-21 07:58:55 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Called from RTP */
|
|
|
|
void queue_message(RTPSession *session, RTPMessage *msg)
|
|
|
|
{
|
2014-11-29 20:42:19 +08:00
|
|
|
/* This function is unregistered during call termination befor destroing
|
|
|
|
* Codec session so no need to check for validity of cs
|
|
|
|
*/
|
2014-11-18 07:46:46 +08:00
|
|
|
CSSession *cs = session->cs;
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-29 20:42:19 +08:00
|
|
|
if (!cs) return;
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
/* Audio */
|
2014-11-29 20:42:19 +08:00
|
|
|
if (session->payload_type == msi_TypeAudio % 128) {
|
2014-12-13 07:35:52 +08:00
|
|
|
pthread_mutex_lock(cs->queue_mutex);
|
2014-12-13 09:55:43 +08:00
|
|
|
int ret = jbuf_write(cs->j_buf, msg);
|
2014-12-13 07:35:52 +08:00
|
|
|
pthread_mutex_unlock(cs->queue_mutex);
|
2014-12-13 09:55:43 +08:00
|
|
|
|
|
|
|
if (ret == -1) {
|
|
|
|
rtp_free_msg(NULL, msg);
|
|
|
|
}
|
2014-11-18 07:46:46 +08:00
|
|
|
}
|
|
|
|
/* Video */
|
|
|
|
else {
|
|
|
|
uint8_t *packet = msg->data;
|
|
|
|
uint32_t packet_size = msg->length;
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
if (packet_size < VIDEOFRAME_HEADER_SIZE)
|
|
|
|
goto end;
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-12-13 06:47:57 +08:00
|
|
|
uint8_t diff = packet[0] - cs->frameid_in;
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-12-13 06:47:57 +08:00
|
|
|
if (diff != 0) {
|
|
|
|
if (diff < 225) { /* New frame */
|
|
|
|
/* Flush last frames' data and get ready for this frame */
|
|
|
|
Payload *p = malloc(sizeof(Payload) + cs->frame_size);
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-12-13 06:47:57 +08:00
|
|
|
if (p) {
|
|
|
|
pthread_mutex_lock(cs->queue_mutex);
|
|
|
|
|
|
|
|
if (buffer_full(cs->vbuf_raw)) {
|
|
|
|
LOGGER_DEBUG("Dropped video frame");
|
|
|
|
Payload *tp;
|
|
|
|
buffer_read(cs->vbuf_raw, &tp);
|
|
|
|
free(tp);
|
|
|
|
} else {
|
|
|
|
p->size = cs->frame_size;
|
|
|
|
memcpy(p->data, cs->frame_buf, cs->frame_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer_write(cs->vbuf_raw, p);
|
|
|
|
pthread_mutex_unlock(cs->queue_mutex);
|
2014-11-25 09:24:59 +08:00
|
|
|
} else {
|
2014-12-13 06:47:57 +08:00
|
|
|
LOGGER_WARNING("Allocation failed! Program might misbehave!");
|
|
|
|
goto end;
|
2014-11-18 07:46:46 +08:00
|
|
|
}
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-12-13 06:47:57 +08:00
|
|
|
cs->last_timestamp = msg->header->timestamp;
|
|
|
|
cs->frameid_in = packet[0];
|
|
|
|
memset(cs->frame_buf, 0, cs->frame_size);
|
|
|
|
cs->frame_size = 0;
|
|
|
|
|
|
|
|
} else { /* Old frame; drop */
|
|
|
|
LOGGER_DEBUG("Old packet: %u", packet[0]);
|
2014-11-18 07:46:46 +08:00
|
|
|
goto end;
|
|
|
|
}
|
2014-12-13 06:47:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t piece_number = packet[1];
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-12-13 06:47:57 +08:00
|
|
|
uint32_t length_before_piece = ((piece_number - 1) * cs->video_frame_piece_size);
|
|
|
|
uint32_t framebuf_new_length = length_before_piece + (packet_size - VIDEOFRAME_HEADER_SIZE);
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-12-13 06:47:57 +08:00
|
|
|
if (framebuf_new_length > cs->max_video_frame_size) {
|
2014-11-18 07:46:46 +08:00
|
|
|
goto end;
|
|
|
|
}
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-11-18 07:46:46 +08:00
|
|
|
/* Otherwise it's part of the frame so just process */
|
|
|
|
/* LOGGER_DEBUG("Video Packet: %u %u", packet[0], packet[1]); */
|
2014-12-13 06:47:57 +08:00
|
|
|
|
|
|
|
memcpy(cs->frame_buf + length_before_piece,
|
2014-11-18 07:46:46 +08:00
|
|
|
packet + VIDEOFRAME_HEADER_SIZE,
|
|
|
|
packet_size - VIDEOFRAME_HEADER_SIZE);
|
2014-11-25 09:24:59 +08:00
|
|
|
|
2014-12-13 06:47:57 +08:00
|
|
|
if (framebuf_new_length > cs->frame_size) {
|
|
|
|
cs->frame_size = framebuf_new_length;
|
|
|
|
}
|
2014-11-25 09:24:59 +08:00
|
|
|
|
|
|
|
end:
|
2014-11-18 07:46:46 +08:00
|
|
|
rtp_free_msg(NULL, msg);
|
|
|
|
}
|
2014-12-13 09:41:27 +08:00
|
|
|
}
|