2020-03-12 09:10:33 +08:00
|
|
|
/* SPDX-License-Identifier: GPL-3.0-or-later
|
2018-08-26 07:36:42 +08:00
|
|
|
* Copyright © 2016-2018 The TokTok team.
|
2017-01-14 23:46:31 +08:00
|
|
|
* Copyright © 2013-2015 Tox project.
|
2015-04-21 08:31:12 +08:00
|
|
|
*/
|
2016-09-11 22:47:51 +08:00
|
|
|
#include "video.h"
|
2015-04-21 08:31:12 +08:00
|
|
|
|
2018-02-27 10:07:59 +08:00
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-04-21 08:31:12 +08:00
|
|
|
#include "msi.h"
|
2016-09-22 22:20:33 +08:00
|
|
|
#include "ring_buffer.h"
|
2015-04-22 08:09:37 +08:00
|
|
|
#include "rtp.h"
|
2015-04-21 08:31:12 +08:00
|
|
|
|
2022-03-04 04:15:09 +08:00
|
|
|
#include "../toxcore/ccompat.h"
|
2015-04-21 08:31:12 +08:00
|
|
|
#include "../toxcore/logger.h"
|
2018-07-08 16:43:42 +08:00
|
|
|
#include "../toxcore/mono_time.h"
|
2015-04-21 08:31:12 +08:00
|
|
|
#include "../toxcore/network.h"
|
|
|
|
|
2018-01-20 05:59:42 +08:00
|
|
|
/**
|
|
|
|
* Soft deadline the decoder should attempt to meet, in "us" (microseconds).
|
|
|
|
* Set to zero for unlimited.
|
|
|
|
*
|
|
|
|
* By convention, the value 1 is used to mean "return as fast as possible."
|
|
|
|
*/
|
2018-07-20 06:00:21 +08:00
|
|
|
// TODO(zoff99): don't hardcode this, let the application choose it
|
2018-01-20 05:59:42 +08:00
|
|
|
#define WANTED_MAX_DECODER_FPS 40
|
|
|
|
|
|
|
|
/**
|
|
|
|
* VPX_DL_REALTIME (1)
|
|
|
|
* deadline parameter analogous to VPx REALTIME mode.
|
|
|
|
*
|
|
|
|
* VPX_DL_GOOD_QUALITY (1000000)
|
|
|
|
* deadline parameter analogous to VPx GOOD QUALITY mode.
|
|
|
|
*
|
|
|
|
* VPX_DL_BEST_QUALITY (0)
|
|
|
|
* deadline parameter analogous to VPx BEST QUALITY mode.
|
|
|
|
*/
|
|
|
|
#define MAX_DECODE_TIME_US (1000000 / WANTED_MAX_DECODER_FPS) // to allow x fps
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Codec control function to set encoder internal speed settings. Changes in
|
|
|
|
* this value influences, among others, the encoder's selection of motion
|
|
|
|
* estimation methods. Values greater than 0 will increase encoder speed at the
|
|
|
|
* expense of quality.
|
|
|
|
*
|
2022-03-05 08:57:20 +08:00
|
|
|
* Note Valid range for VP8: `-16..16`
|
2018-01-20 05:59:42 +08:00
|
|
|
*/
|
|
|
|
#define VP8E_SET_CPUUSED_VALUE 16
|
|
|
|
|
|
|
|
/**
|
2022-03-05 08:57:20 +08:00
|
|
|
* Initialize encoder with this value.
|
|
|
|
*
|
|
|
|
* Target bandwidth to use for this stream, in kilobits per second.
|
2018-01-20 05:59:42 +08:00
|
|
|
*/
|
|
|
|
#define VIDEO_BITRATE_INITIAL_VALUE 5000
|
|
|
|
#define VIDEO_DECODE_BUFFER_SIZE 5 // this buffer has normally max. 1 entry
|
|
|
|
|
2018-09-26 08:36:59 +08:00
|
|
|
static vpx_codec_iface_t *video_codec_decoder_interface(void)
|
2018-08-13 04:59:35 +08:00
|
|
|
{
|
|
|
|
return vpx_codec_vp8_dx();
|
|
|
|
}
|
2018-09-26 08:36:59 +08:00
|
|
|
static vpx_codec_iface_t *video_codec_encoder_interface(void)
|
2018-08-13 04:59:35 +08:00
|
|
|
{
|
|
|
|
return vpx_codec_vp8_cx();
|
|
|
|
}
|
2018-01-20 05:59:42 +08:00
|
|
|
|
|
|
|
#define VIDEO_CODEC_DECODER_MAX_WIDTH 800 // its a dummy value, because the struct needs a value there
|
|
|
|
#define VIDEO_CODEC_DECODER_MAX_HEIGHT 600 // its a dummy value, because the struct needs a value there
|
|
|
|
|
|
|
|
#define VPX_MAX_DIST_START 40
|
|
|
|
|
|
|
|
#define VPX_MAX_ENCODER_THREADS 4
|
|
|
|
#define VPX_MAX_DECODER_THREADS 4
|
2018-08-20 06:58:04 +08:00
|
|
|
#define VIDEO_VP8_DECODER_POST_PROCESSING_ENABLED 0
|
2018-01-20 05:59:42 +08:00
|
|
|
|
2018-06-24 01:16:28 +08:00
|
|
|
static void vc_init_encoder_cfg(const Logger *log, vpx_codec_enc_cfg_t *cfg, int16_t kf_max_dist)
|
2018-01-20 05:59:42 +08:00
|
|
|
{
|
2022-02-24 02:55:28 +08:00
|
|
|
const vpx_codec_err_t rc = vpx_codec_enc_config_default(video_codec_encoder_interface(), cfg, 0);
|
2018-01-20 05:59:42 +08:00
|
|
|
|
|
|
|
if (rc != VPX_CODEC_OK) {
|
|
|
|
LOGGER_ERROR(log, "vc_init_encoder_cfg:Failed to get config: %s", vpx_codec_err_to_string(rc));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Target bandwidth to use for this stream, in kilobits per second */
|
|
|
|
cfg->rc_target_bitrate = VIDEO_BITRATE_INITIAL_VALUE;
|
|
|
|
cfg->g_w = VIDEO_CODEC_DECODER_MAX_WIDTH;
|
|
|
|
cfg->g_h = VIDEO_CODEC_DECODER_MAX_HEIGHT;
|
|
|
|
cfg->g_pass = VPX_RC_ONE_PASS;
|
|
|
|
cfg->g_error_resilient = VPX_ERROR_RESILIENT_DEFAULT | VPX_ERROR_RESILIENT_PARTITIONS;
|
|
|
|
cfg->g_lag_in_frames = 0;
|
|
|
|
|
|
|
|
/* Allow lagged encoding
|
|
|
|
*
|
|
|
|
* If set, this value allows the encoder to consume a number of input
|
|
|
|
* frames before producing output frames. This allows the encoder to
|
|
|
|
* base decisions for the current frame on future frames. This does
|
|
|
|
* increase the latency of the encoding pipeline, so it is not appropriate
|
|
|
|
* in all situations (ex: realtime encoding).
|
|
|
|
*
|
|
|
|
* Note that this is a maximum value -- the encoder may produce frames
|
|
|
|
* sooner than the given limit. Set this value to 0 to disable this
|
|
|
|
* feature.
|
|
|
|
*/
|
|
|
|
cfg->kf_min_dist = 0;
|
|
|
|
cfg->kf_mode = VPX_KF_AUTO; // Encoder determines optimal placement automatically
|
|
|
|
cfg->rc_end_usage = VPX_VBR; // what quality mode?
|
|
|
|
|
|
|
|
/*
|
|
|
|
* VPX_VBR Variable Bit Rate (VBR) mode
|
|
|
|
* VPX_CBR Constant Bit Rate (CBR) mode
|
|
|
|
* VPX_CQ Constrained Quality (CQ) mode -> give codec a hint that we may be on low bandwidth connection
|
|
|
|
* VPX_Q Constant Quality (Q) mode
|
|
|
|
*/
|
|
|
|
if (kf_max_dist > 1) {
|
|
|
|
cfg->kf_max_dist = kf_max_dist; // a full frame every x frames minimum (can be more often, codec decides automatically)
|
|
|
|
LOGGER_DEBUG(log, "kf_max_dist=%d (1)", cfg->kf_max_dist);
|
|
|
|
} else {
|
|
|
|
cfg->kf_max_dist = VPX_MAX_DIST_START;
|
|
|
|
LOGGER_DEBUG(log, "kf_max_dist=%d (2)", cfg->kf_max_dist);
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg->g_threads = VPX_MAX_ENCODER_THREADS; // Maximum number of threads to use
|
|
|
|
/* TODO: set these to something reasonable */
|
|
|
|
// cfg->g_timebase.num = 1;
|
|
|
|
// cfg->g_timebase.den = 60; // 60 fps
|
|
|
|
cfg->rc_resize_allowed = 1; // allow encoder to resize to smaller resolution
|
|
|
|
cfg->rc_resize_up_thresh = 40;
|
|
|
|
cfg->rc_resize_down_thresh = 5;
|
|
|
|
|
|
|
|
/* TODO: make quality setting an API call, but start with normal quality */
|
|
|
|
#if 0
|
|
|
|
/* Highest-resolution encoder settings */
|
|
|
|
cfg->rc_dropframe_thresh = 0;
|
|
|
|
cfg->rc_resize_allowed = 0;
|
|
|
|
cfg->rc_min_quantizer = 2;
|
|
|
|
cfg->rc_max_quantizer = 56;
|
|
|
|
cfg->rc_undershoot_pct = 100;
|
|
|
|
cfg->rc_overshoot_pct = 15;
|
|
|
|
cfg->rc_buf_initial_sz = 500;
|
|
|
|
cfg->rc_buf_optimal_sz = 600;
|
|
|
|
cfg->rc_buf_sz = 1000;
|
|
|
|
#endif
|
|
|
|
}
|
2015-04-21 08:31:12 +08:00
|
|
|
|
2018-08-27 04:34:26 +08:00
|
|
|
VCSession *vc_new(Mono_Time *mono_time, const Logger *log, ToxAV *av, uint32_t friend_number,
|
2018-08-18 01:22:18 +08:00
|
|
|
toxav_video_receive_frame_cb *cb, void *cb_data)
|
2015-04-21 08:31:12 +08:00
|
|
|
{
|
2022-01-10 02:42:20 +08:00
|
|
|
VCSession *vc = (VCSession *)calloc(1, sizeof(VCSession));
|
2016-09-18 08:31:55 +08:00
|
|
|
vpx_codec_err_t rc;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2022-02-18 10:05:44 +08:00
|
|
|
if (vc == nullptr) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_WARNING(log, "Allocation failed! Application might misbehave!");
|
2018-01-29 05:30:39 +08:00
|
|
|
return nullptr;
|
2015-04-21 08:31:12 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-21 08:31:12 +08:00
|
|
|
if (create_recursive_mutex(vc->queue_mutex) != 0) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_WARNING(log, "Failed to create recursive mutex!");
|
2015-04-21 08:31:12 +08:00
|
|
|
free(vc);
|
2018-01-29 05:30:39 +08:00
|
|
|
return nullptr;
|
2015-04-21 08:31:12 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2022-02-24 02:55:28 +08:00
|
|
|
const int cpu_used_value = VP8E_SET_CPUUSED_VALUE;
|
2018-01-20 05:59:42 +08:00
|
|
|
|
2018-08-13 04:59:35 +08:00
|
|
|
vc->vbuf_raw = rb_new(VIDEO_DECODE_BUFFER_SIZE);
|
|
|
|
|
2022-02-18 10:05:44 +08:00
|
|
|
if (vc->vbuf_raw == nullptr) {
|
2015-04-21 08:31:12 +08:00
|
|
|
goto BASE_CLEANUP;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-20 05:59:42 +08:00
|
|
|
/*
|
|
|
|
* VPX_CODEC_USE_FRAME_THREADING
|
|
|
|
* Enable frame-based multi-threading
|
|
|
|
*
|
|
|
|
* VPX_CODEC_USE_ERROR_CONCEALMENT
|
|
|
|
* Conceal errors in decoded frames
|
|
|
|
*/
|
|
|
|
vpx_codec_dec_cfg_t dec_cfg;
|
|
|
|
dec_cfg.threads = VPX_MAX_DECODER_THREADS; // Maximum number of threads to use
|
|
|
|
dec_cfg.w = VIDEO_CODEC_DECODER_MAX_WIDTH;
|
|
|
|
dec_cfg.h = VIDEO_CODEC_DECODER_MAX_HEIGHT;
|
|
|
|
|
|
|
|
LOGGER_DEBUG(log, "Using VP8 codec for decoder (0)");
|
2018-08-13 04:59:35 +08:00
|
|
|
rc = vpx_codec_dec_init(vc->decoder, video_codec_decoder_interface(), &dec_cfg,
|
2018-01-20 05:59:42 +08:00
|
|
|
VPX_CODEC_USE_FRAME_THREADING | VPX_CODEC_USE_POSTPROC);
|
|
|
|
|
|
|
|
if (rc == VPX_CODEC_INCAPABLE) {
|
|
|
|
LOGGER_WARNING(log, "Postproc not supported by this decoder (0)");
|
2018-08-13 04:59:35 +08:00
|
|
|
rc = vpx_codec_dec_init(vc->decoder, video_codec_decoder_interface(), &dec_cfg, VPX_CODEC_USE_FRAME_THREADING);
|
2018-01-20 05:59:42 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
|
|
|
if (rc != VPX_CODEC_OK) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_ERROR(log, "Init video_decoder failed: %s", vpx_codec_err_to_string(rc));
|
2015-04-21 08:31:12 +08:00
|
|
|
goto BASE_CLEANUP;
|
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-20 06:58:04 +08:00
|
|
|
if (VIDEO_VP8_DECODER_POST_PROCESSING_ENABLED == 1) {
|
2018-01-20 05:59:42 +08:00
|
|
|
vp8_postproc_cfg_t pp = {VP8_DEBLOCK, 1, 0};
|
2022-02-24 02:55:28 +08:00
|
|
|
const vpx_codec_err_t cc_res = vpx_codec_control(vc->decoder, VP8_SET_POSTPROC, &pp);
|
2015-10-24 04:52:32 +08:00
|
|
|
|
2018-01-20 05:59:42 +08:00
|
|
|
if (cc_res != VPX_CODEC_OK) {
|
|
|
|
LOGGER_WARNING(log, "Failed to turn on postproc");
|
|
|
|
} else {
|
|
|
|
LOGGER_DEBUG(log, "turn on postproc: OK");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
vp8_postproc_cfg_t pp = {0, 0, 0};
|
|
|
|
vpx_codec_err_t cc_res = vpx_codec_control(vc->decoder, VP8_SET_POSTPROC, &pp);
|
|
|
|
|
|
|
|
if (cc_res != VPX_CODEC_OK) {
|
|
|
|
LOGGER_WARNING(log, "Failed to turn OFF postproc");
|
|
|
|
} else {
|
|
|
|
LOGGER_DEBUG(log, "Disable postproc: OK");
|
|
|
|
}
|
2015-10-24 04:52:32 +08:00
|
|
|
}
|
|
|
|
|
2018-01-20 05:59:42 +08:00
|
|
|
/* Set encoder to some initial values
|
|
|
|
*/
|
|
|
|
vpx_codec_enc_cfg_t cfg;
|
|
|
|
vc_init_encoder_cfg(log, &cfg, 1);
|
2015-10-24 04:52:32 +08:00
|
|
|
|
2018-01-20 05:59:42 +08:00
|
|
|
LOGGER_DEBUG(log, "Using VP8 codec for encoder (0.1)");
|
2018-08-13 04:59:35 +08:00
|
|
|
rc = vpx_codec_enc_init(vc->encoder, video_codec_encoder_interface(), &cfg, VPX_CODEC_USE_FRAME_THREADING);
|
2015-10-24 04:52:32 +08:00
|
|
|
|
|
|
|
if (rc != VPX_CODEC_OK) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_ERROR(log, "Failed to initialize encoder: %s", vpx_codec_err_to_string(rc));
|
2015-10-24 04:52:32 +08:00
|
|
|
goto BASE_CLEANUP_1;
|
|
|
|
}
|
|
|
|
|
2018-01-20 05:59:42 +08:00
|
|
|
rc = vpx_codec_control(vc->encoder, VP8E_SET_CPUUSED, cpu_used_value);
|
2015-10-24 04:52:32 +08:00
|
|
|
|
|
|
|
if (rc != VPX_CODEC_OK) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_ERROR(log, "Failed to set encoder control setting: %s", vpx_codec_err_to_string(rc));
|
2015-10-24 04:52:32 +08:00
|
|
|
vpx_codec_destroy(vc->encoder);
|
|
|
|
goto BASE_CLEANUP_1;
|
2015-04-21 08:31:12 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-20 05:59:42 +08:00
|
|
|
/*
|
2020-05-17 06:04:10 +08:00
|
|
|
* VPX_CTRL_USE_TYPE(VP8E_SET_NOISE_SENSITIVITY, unsigned int)
|
|
|
|
* control function to set noise sensitivity
|
|
|
|
* 0: off, 1: OnYOnly, 2: OnYUV, 3: OnYUVAggressive, 4: Adaptive
|
|
|
|
*/
|
2020-03-14 10:22:39 +08:00
|
|
|
#if 0
|
|
|
|
rc = vpx_codec_control(vc->encoder, VP8E_SET_NOISE_SENSITIVITY, 2);
|
2018-01-20 05:59:42 +08:00
|
|
|
|
2020-03-14 10:22:39 +08:00
|
|
|
if (rc != VPX_CODEC_OK) {
|
|
|
|
LOGGER_ERROR(log, "Failed to set encoder control setting: %s", vpx_codec_err_to_string(rc));
|
|
|
|
vpx_codec_destroy(vc->encoder);
|
|
|
|
goto BASE_CLEANUP_1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2018-08-18 01:22:18 +08:00
|
|
|
vc->linfts = current_time_monotonic(mono_time);
|
2015-04-21 08:31:12 +08:00
|
|
|
vc->lcfd = 60;
|
2018-08-12 18:55:20 +08:00
|
|
|
vc->vcb = cb;
|
|
|
|
vc->vcb_user_data = cb_data;
|
2015-04-29 07:01:25 +08:00
|
|
|
vc->friend_number = friend_number;
|
2015-05-02 04:15:12 +08:00
|
|
|
vc->av = av;
|
2016-08-19 20:07:45 +08:00
|
|
|
vc->log = log;
|
2015-04-21 08:31:12 +08:00
|
|
|
return vc;
|
2015-10-24 04:52:32 +08:00
|
|
|
BASE_CLEANUP_1:
|
|
|
|
vpx_codec_destroy(vc->decoder);
|
2015-04-21 08:31:12 +08:00
|
|
|
BASE_CLEANUP:
|
|
|
|
pthread_mutex_destroy(vc->queue_mutex);
|
2018-07-20 06:05:43 +08:00
|
|
|
rb_kill(vc->vbuf_raw);
|
2015-04-21 08:31:12 +08:00
|
|
|
free(vc);
|
2018-01-29 05:30:39 +08:00
|
|
|
return nullptr;
|
2015-04-21 08:31:12 +08:00
|
|
|
}
|
2018-01-20 05:59:42 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
void vc_kill(VCSession *vc)
|
2015-04-21 08:31:12 +08:00
|
|
|
{
|
2022-02-18 10:05:44 +08:00
|
|
|
if (vc == nullptr) {
|
2015-04-21 08:31:12 +08:00
|
|
|
return;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-04-26 06:31:03 +08:00
|
|
|
vpx_codec_destroy(vc->encoder);
|
|
|
|
vpx_codec_destroy(vc->decoder);
|
2015-10-11 05:54:23 +08:00
|
|
|
void *p;
|
|
|
|
|
2018-07-20 06:05:43 +08:00
|
|
|
while (rb_read(vc->vbuf_raw, &p)) {
|
2015-10-11 05:54:23 +08:00
|
|
|
free(p);
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-07-20 06:05:43 +08:00
|
|
|
rb_kill(vc->vbuf_raw);
|
2015-04-21 08:31:12 +08:00
|
|
|
pthread_mutex_destroy(vc->queue_mutex);
|
2018-02-23 10:22:38 +08:00
|
|
|
LOGGER_DEBUG(vc->log, "Terminated video handler: %p", (void *)vc);
|
2015-04-21 08:31:12 +08:00
|
|
|
free(vc);
|
|
|
|
}
|
2018-01-20 05:59:42 +08:00
|
|
|
|
2016-09-20 04:49:04 +08:00
|
|
|
void vc_iterate(VCSession *vc)
|
2015-04-21 08:31:12 +08:00
|
|
|
{
|
2022-02-18 10:05:44 +08:00
|
|
|
if (vc == nullptr) {
|
2015-04-21 08:31:12 +08:00
|
|
|
return;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-07-20 05:47:51 +08:00
|
|
|
pthread_mutex_lock(vc->queue_mutex);
|
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
struct RTPMessage *p;
|
2016-09-01 03:54:20 +08:00
|
|
|
|
2018-07-20 06:05:43 +08:00
|
|
|
if (!rb_read(vc->vbuf_raw, (void **)&p)) {
|
2018-07-20 05:47:51 +08:00
|
|
|
LOGGER_TRACE(vc->log, "no Video frame data available");
|
|
|
|
pthread_mutex_unlock(vc->queue_mutex);
|
|
|
|
return;
|
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2022-02-24 02:55:28 +08:00
|
|
|
const uint16_t log_rb_size = rb_size(vc->vbuf_raw);
|
2018-07-20 05:47:51 +08:00
|
|
|
pthread_mutex_unlock(vc->queue_mutex);
|
|
|
|
const struct RTPHeader *const header = &p->header;
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-20 05:59:42 +08:00
|
|
|
uint32_t full_data_len;
|
|
|
|
|
2022-02-21 22:10:31 +08:00
|
|
|
if ((header->flags & RTP_LARGE_FRAME) != 0) {
|
2018-07-20 05:47:51 +08:00
|
|
|
full_data_len = header->data_length_full;
|
|
|
|
LOGGER_DEBUG(vc->log, "vc_iterate:001:full_data_len=%d", (int)full_data_len);
|
|
|
|
} else {
|
|
|
|
full_data_len = p->len;
|
|
|
|
LOGGER_DEBUG(vc->log, "vc_iterate:002");
|
|
|
|
}
|
2018-01-20 05:59:42 +08:00
|
|
|
|
2018-07-20 05:47:51 +08:00
|
|
|
LOGGER_DEBUG(vc->log, "vc_iterate: rb_read p->len=%d p->header.xe=%d", (int)full_data_len, p->header.xe);
|
2019-09-06 22:23:25 +08:00
|
|
|
LOGGER_DEBUG(vc->log, "vc_iterate: rb_read rb size=%d", (int)log_rb_size);
|
2018-07-20 05:47:51 +08:00
|
|
|
const vpx_codec_err_t rc = vpx_codec_decode(vc->decoder, p->data, full_data_len, nullptr, MAX_DECODE_TIME_US);
|
|
|
|
free(p);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-07-20 05:47:51 +08:00
|
|
|
if (rc != VPX_CODEC_OK) {
|
|
|
|
LOGGER_ERROR(vc->log, "Error decoding video: %d %s", (int)rc, vpx_codec_err_to_string(rc));
|
|
|
|
return;
|
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-07-20 05:47:51 +08:00
|
|
|
/* Play decoded images */
|
|
|
|
vpx_codec_iter_t iter = nullptr;
|
|
|
|
|
2018-08-13 04:59:35 +08:00
|
|
|
for (vpx_image_t *dest = vpx_codec_get_frame(vc->decoder, &iter);
|
|
|
|
dest != nullptr;
|
|
|
|
dest = vpx_codec_get_frame(vc->decoder, &iter)) {
|
2022-02-18 04:22:12 +08:00
|
|
|
if (vc->vcb != nullptr) {
|
2018-08-12 18:55:20 +08:00
|
|
|
vc->vcb(vc->av, vc->friend_number, dest->d_w, dest->d_h,
|
2022-02-22 06:37:42 +08:00
|
|
|
dest->planes[0], dest->planes[1], dest->planes[2],
|
2018-08-12 18:55:20 +08:00
|
|
|
dest->stride[0], dest->stride[1], dest->stride[2], vc->vcb_user_data);
|
2015-04-21 08:31:12 +08:00
|
|
|
}
|
|
|
|
|
2018-07-20 05:47:51 +08:00
|
|
|
vpx_img_free(dest); // is this needed? none of the VPx examples show that
|
2015-04-21 08:31:12 +08:00
|
|
|
}
|
|
|
|
}
|
2018-01-20 05:59:42 +08:00
|
|
|
|
2018-08-27 04:34:26 +08:00
|
|
|
int vc_queue_message(Mono_Time *mono_time, void *vcp, struct RTPMessage *msg)
|
2015-04-21 08:31:12 +08:00
|
|
|
{
|
2018-01-20 05:59:42 +08:00
|
|
|
/* This function is called with complete messages
|
|
|
|
* they have already been assembled.
|
|
|
|
* this function gets called from handle_rtp_packet() and handle_rtp_packet_v3()
|
2015-04-21 08:31:12 +08:00
|
|
|
*/
|
2022-02-21 22:10:31 +08:00
|
|
|
if (vcp == nullptr || msg == nullptr) {
|
|
|
|
free(msg);
|
2020-04-05 15:25:50 +08:00
|
|
|
|
2015-04-22 08:09:37 +08:00
|
|
|
return -1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2016-09-18 08:31:55 +08:00
|
|
|
VCSession *vc = (VCSession *)vcp;
|
2018-01-20 05:59:42 +08:00
|
|
|
const struct RTPHeader *const header = &msg->header;
|
2016-08-19 20:07:45 +08:00
|
|
|
|
2018-08-12 20:53:12 +08:00
|
|
|
if (msg->header.pt == (RTP_TYPE_VIDEO + 2) % 128) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_WARNING(vc->log, "Got dummy!");
|
2015-10-11 05:54:23 +08:00
|
|
|
free(msg);
|
2015-04-22 08:09:37 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-08-12 20:53:12 +08:00
|
|
|
if (msg->header.pt != RTP_TYPE_VIDEO % 128) {
|
2018-01-20 05:59:42 +08:00
|
|
|
LOGGER_WARNING(vc->log, "Invalid payload type! pt=%d", (int)msg->header.pt);
|
2015-10-11 05:54:23 +08:00
|
|
|
free(msg);
|
2015-04-22 08:09:37 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2015-04-21 08:31:12 +08:00
|
|
|
|
2015-10-11 05:54:23 +08:00
|
|
|
pthread_mutex_lock(vc->queue_mutex);
|
2018-01-20 05:59:42 +08:00
|
|
|
|
2022-02-21 22:10:31 +08:00
|
|
|
if ((header->flags & RTP_LARGE_FRAME) != 0 && header->pt == RTP_TYPE_VIDEO % 128) {
|
2018-01-20 05:59:42 +08:00
|
|
|
LOGGER_DEBUG(vc->log, "rb_write msg->len=%d b0=%d b1=%d", (int)msg->len, (int)msg->data[0], (int)msg->data[1]);
|
2015-04-21 08:31:12 +08:00
|
|
|
}
|
|
|
|
|
2018-07-20 06:05:43 +08:00
|
|
|
free(rb_write(vc->vbuf_raw, msg));
|
2018-01-20 05:59:42 +08:00
|
|
|
|
|
|
|
/* Calculate time it took for peer to send us this frame */
|
2022-02-24 02:55:28 +08:00
|
|
|
const uint32_t t_lcfd = current_time_monotonic(mono_time) - vc->linfts;
|
2018-01-20 05:59:42 +08:00
|
|
|
vc->lcfd = t_lcfd > 100 ? vc->lcfd : t_lcfd;
|
2018-08-18 01:22:18 +08:00
|
|
|
vc->linfts = current_time_monotonic(mono_time);
|
2018-01-20 05:59:42 +08:00
|
|
|
pthread_mutex_unlock(vc->queue_mutex);
|
2015-04-22 08:09:37 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2018-01-20 05:59:42 +08:00
|
|
|
|
|
|
|
int vc_reconfigure_encoder(VCSession *vc, uint32_t bit_rate, uint16_t width, uint16_t height, int16_t kf_max_dist)
|
2015-04-22 08:09:37 +08:00
|
|
|
{
|
2022-02-18 10:05:44 +08:00
|
|
|
if (vc == nullptr) {
|
2015-04-26 06:31:03 +08:00
|
|
|
return -1;
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-20 05:59:42 +08:00
|
|
|
vpx_codec_enc_cfg_t cfg2 = *vc->encoder->config.enc;
|
2016-09-18 08:31:55 +08:00
|
|
|
vpx_codec_err_t rc;
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2018-01-20 05:59:42 +08:00
|
|
|
if (cfg2.rc_target_bitrate == bit_rate && cfg2.g_w == width && cfg2.g_h == height && kf_max_dist == -1) {
|
2015-04-22 08:09:37 +08:00
|
|
|
return 0; /* Nothing changed */
|
2016-09-01 02:12:19 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2018-01-20 05:59:42 +08:00
|
|
|
if (cfg2.g_w == width && cfg2.g_h == height && kf_max_dist == -1) {
|
2015-10-24 04:52:32 +08:00
|
|
|
/* Only bit rate changed */
|
2018-01-20 05:59:42 +08:00
|
|
|
LOGGER_INFO(vc->log, "bitrate change from: %u to: %u", (uint32_t)cfg2.rc_target_bitrate, (uint32_t)bit_rate);
|
|
|
|
cfg2.rc_target_bitrate = bit_rate;
|
|
|
|
rc = vpx_codec_enc_config_set(vc->encoder, &cfg2);
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2015-10-24 04:52:32 +08:00
|
|
|
if (rc != VPX_CODEC_OK) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_ERROR(vc->log, "Failed to set encoder control setting: %s", vpx_codec_err_to_string(rc));
|
2015-10-24 04:52:32 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2015-11-04 02:42:05 +08:00
|
|
|
} else {
|
2015-10-24 04:52:32 +08:00
|
|
|
/* Resolution is changed, must reinitialize encoder since libvpx v1.4 doesn't support
|
|
|
|
* reconfiguring encoder to use resolutions greater than initially set.
|
|
|
|
*/
|
2018-02-23 10:22:38 +08:00
|
|
|
LOGGER_DEBUG(vc->log, "Have to reinitialize vpx encoder on session %p", (void *)vc);
|
2018-01-20 05:59:42 +08:00
|
|
|
vpx_codec_ctx_t new_c;
|
|
|
|
vpx_codec_enc_cfg_t cfg;
|
|
|
|
vc_init_encoder_cfg(vc->log, &cfg, kf_max_dist);
|
2015-10-24 04:52:32 +08:00
|
|
|
cfg.rc_target_bitrate = bit_rate;
|
|
|
|
cfg.g_w = width;
|
|
|
|
cfg.g_h = height;
|
|
|
|
|
2018-01-20 05:59:42 +08:00
|
|
|
LOGGER_DEBUG(vc->log, "Using VP8 codec for encoder");
|
2018-08-13 04:59:35 +08:00
|
|
|
rc = vpx_codec_enc_init(&new_c, video_codec_encoder_interface(), &cfg, VPX_CODEC_USE_FRAME_THREADING);
|
2015-10-24 04:52:32 +08:00
|
|
|
|
|
|
|
if (rc != VPX_CODEC_OK) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_ERROR(vc->log, "Failed to initialize encoder: %s", vpx_codec_err_to_string(rc));
|
2015-10-24 04:52:32 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2022-02-24 02:55:28 +08:00
|
|
|
const int cpu_used_value = VP8E_SET_CPUUSED_VALUE;
|
2018-01-20 05:59:42 +08:00
|
|
|
|
|
|
|
rc = vpx_codec_control(&new_c, VP8E_SET_CPUUSED, cpu_used_value);
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-10-24 04:52:32 +08:00
|
|
|
if (rc != VPX_CODEC_OK) {
|
2016-08-19 20:07:45 +08:00
|
|
|
LOGGER_ERROR(vc->log, "Failed to set encoder control setting: %s", vpx_codec_err_to_string(rc));
|
2015-10-24 04:52:32 +08:00
|
|
|
vpx_codec_destroy(&new_c);
|
|
|
|
return -1;
|
|
|
|
}
|
2015-11-04 02:42:05 +08:00
|
|
|
|
2015-10-24 04:52:32 +08:00
|
|
|
vpx_codec_destroy(vc->encoder);
|
|
|
|
memcpy(vc->encoder, &new_c, sizeof(new_c));
|
2015-04-21 08:31:12 +08:00
|
|
|
}
|
2015-10-11 05:54:23 +08:00
|
|
|
|
2015-10-24 04:52:32 +08:00
|
|
|
return 0;
|
2015-10-11 05:54:23 +08:00
|
|
|
}
|