2014-12-17 00:47:42 +00:00
|
|
|
/*
|
|
|
|
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
|
|
|
* Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
|
|
|
|
*
|
|
|
|
* Version: MPL 1.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Anthony Minessale II <anthm@freeswitch.org>
|
|
|
|
* Portions created by the Initial Developer are Copyright (C)
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Anthony Minessale II <anthm@freeswitch.org>
|
|
|
|
*
|
|
|
|
* switch_vidderbuffer.c -- Video Buffer
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include <switch.h>
|
|
|
|
#include <switch_vidderbuffer.h>
|
|
|
|
|
2014-12-19 06:03:37 +00:00
|
|
|
#define MAX_MISSING_SEQ 20
|
2014-12-17 00:47:42 +00:00
|
|
|
#define vb_debug(_vb, _level, _format, ...) if (_vb->debug_level >= _level) switch_log_printf(SWITCH_CHANNEL_LOG_CLEAN, SWITCH_LOG_ALERT, "VB:%p level:%d line:%d ->" _format, (void *) _vb, _level, __LINE__, __VA_ARGS__)
|
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
struct switch_vb_s;
|
|
|
|
|
2014-12-17 00:47:42 +00:00
|
|
|
typedef struct switch_vb_node_s {
|
2015-01-09 19:36:57 +00:00
|
|
|
struct switch_vb_s *parent;
|
2014-12-17 00:47:42 +00:00
|
|
|
switch_rtp_packet_t packet;
|
|
|
|
uint32_t len;
|
|
|
|
uint8_t visible;
|
|
|
|
struct switch_vb_node_s *next;
|
|
|
|
} switch_vb_node_t;
|
|
|
|
|
|
|
|
struct switch_vb_s {
|
2015-01-09 19:36:57 +00:00
|
|
|
struct switch_vb_node_s *node_list;
|
2014-12-19 08:26:09 +00:00
|
|
|
uint32_t last_target_seq;
|
2015-01-09 19:36:57 +00:00
|
|
|
uint32_t highest_read_ts;
|
|
|
|
uint32_t highest_read_seq;
|
|
|
|
uint32_t highest_wrote_ts;
|
|
|
|
uint32_t highest_wrote_seq;
|
2014-12-17 00:47:42 +00:00
|
|
|
uint16_t target_seq;
|
2015-01-09 19:36:57 +00:00
|
|
|
uint32_t visible_nodes;
|
2014-12-17 00:47:42 +00:00
|
|
|
uint32_t complete_frames;
|
|
|
|
uint32_t frame_len;
|
|
|
|
uint32_t min_frame_len;
|
|
|
|
uint32_t max_frame_len;
|
2015-01-09 19:36:57 +00:00
|
|
|
uint8_t write_init;
|
|
|
|
uint8_t read_init;
|
2014-12-17 00:47:42 +00:00
|
|
|
uint8_t debug_level;
|
2015-01-09 19:36:57 +00:00
|
|
|
uint16_t next_seq;
|
|
|
|
switch_inthash_t *missing_seq_hash;
|
|
|
|
switch_inthash_t *node_hash;
|
2015-01-24 07:54:59 +00:00
|
|
|
switch_mutex_t *mutex;
|
|
|
|
switch_memory_pool_t *pool;
|
|
|
|
int free_pool;
|
2014-12-17 00:47:42 +00:00
|
|
|
};
|
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
static inline switch_vb_node_t *new_node(switch_vb_t *vb)
|
2014-12-17 00:47:42 +00:00
|
|
|
{
|
|
|
|
switch_vb_node_t *np, *last = NULL;
|
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
for (np = vb->node_list; np; np = np->next) {
|
2014-12-17 00:47:42 +00:00
|
|
|
if (!np->visible) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
last = np;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!np) {
|
|
|
|
|
2015-01-24 07:54:59 +00:00
|
|
|
np = switch_core_alloc(vb->pool, sizeof(*np));
|
2014-12-17 00:47:42 +00:00
|
|
|
|
|
|
|
if (last) {
|
|
|
|
last->next = np;
|
|
|
|
} else {
|
2015-01-09 19:36:57 +00:00
|
|
|
vb->node_list = np;
|
2014-12-17 00:47:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
switch_assert(np);
|
|
|
|
|
|
|
|
np->visible = 1;
|
2015-01-09 19:36:57 +00:00
|
|
|
vb->visible_nodes++;
|
|
|
|
np->parent = vb;
|
2014-12-19 06:03:37 +00:00
|
|
|
|
2014-12-17 00:47:42 +00:00
|
|
|
return np;
|
|
|
|
}
|
|
|
|
|
2015-01-30 00:15:41 +00:00
|
|
|
#if 0
|
2015-01-09 19:36:57 +00:00
|
|
|
static inline switch_vb_node_t *find_seq(switch_vb_t *vb, uint16_t seq)
|
2014-12-17 00:47:42 +00:00
|
|
|
{
|
2015-01-09 19:36:57 +00:00
|
|
|
switch_vb_node_t *np;
|
|
|
|
for (np = vb->node_list; np; np = np->next) {
|
|
|
|
if (!np->visible) continue;
|
|
|
|
|
|
|
|
if (ntohs(np->packet.header.seq) == ntohs(seq)) {
|
|
|
|
return np;
|
|
|
|
}
|
2014-12-17 00:47:42 +00:00
|
|
|
}
|
2014-12-19 06:03:37 +00:00
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
return NULL;
|
2014-12-17 00:47:42 +00:00
|
|
|
}
|
2015-01-30 00:15:41 +00:00
|
|
|
#endif
|
2014-12-17 00:47:42 +00:00
|
|
|
|
|
|
|
static inline void hide_node(switch_vb_node_t *node)
|
|
|
|
{
|
2015-01-24 07:54:59 +00:00
|
|
|
if (node->visible) {
|
|
|
|
node->visible = 0;
|
|
|
|
node->parent->visible_nodes--;
|
|
|
|
}
|
2015-01-09 19:36:57 +00:00
|
|
|
switch_core_inthash_delete(node->parent->node_hash, node->packet.header.seq);
|
2014-12-17 00:47:42 +00:00
|
|
|
}
|
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
static inline void hide_nodes(switch_vb_t *vb)
|
2014-12-17 00:47:42 +00:00
|
|
|
{
|
|
|
|
switch_vb_node_t *np;
|
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
for (np = vb->node_list; np; np = np->next) {
|
2014-12-17 00:47:42 +00:00
|
|
|
hide_node(np);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
static inline void drop_ts(switch_vb_t *vb, uint32_t ts)
|
2014-12-17 00:47:42 +00:00
|
|
|
{
|
2015-01-09 19:36:57 +00:00
|
|
|
switch_vb_node_t *np;
|
|
|
|
int x = 0;
|
2014-12-17 07:03:31 +00:00
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
for (np = vb->node_list; np; np = np->next) {
|
|
|
|
if (!np->visible) continue;
|
2014-12-17 00:47:42 +00:00
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
if (ts == np->packet.header.ts) {
|
|
|
|
hide_node(np);
|
|
|
|
x++;
|
|
|
|
}
|
2014-12-17 00:47:42 +00:00
|
|
|
}
|
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
if (x) vb->complete_frames--;
|
2014-12-17 00:47:42 +00:00
|
|
|
}
|
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
static inline uint32_t vb_find_lowest_ts(switch_vb_t *vb)
|
2014-12-17 00:47:42 +00:00
|
|
|
{
|
2015-01-09 19:36:57 +00:00
|
|
|
switch_vb_node_t *np, *lowest = NULL;
|
2014-12-19 06:03:37 +00:00
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
for (np = vb->node_list; np; np = np->next) {
|
|
|
|
if (!np->visible) continue;
|
2014-12-17 00:47:42 +00:00
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
if (!lowest || ntohl(lowest->packet.header.ts) > ntohl(np->packet.header.ts)) {
|
|
|
|
lowest = np;
|
2014-12-17 07:03:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
return lowest ? lowest->packet.header.ts : 0;
|
2014-12-17 07:03:31 +00:00
|
|
|
}
|
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
static inline void drop_oldest_frame(switch_vb_t *vb)
|
2014-12-19 06:03:37 +00:00
|
|
|
{
|
2015-01-09 19:36:57 +00:00
|
|
|
uint32_t ts = vb_find_lowest_ts(vb);
|
2014-12-19 06:03:37 +00:00
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
drop_ts(vb, ts);
|
|
|
|
vb_debug(vb, 1, "Dropping oldest frame ts:%u\n", ntohl(ts));
|
2014-12-19 06:03:37 +00:00
|
|
|
}
|
2014-12-17 07:03:31 +00:00
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
static inline void add_node(switch_vb_t *vb, switch_rtp_packet_t *packet, switch_size_t len)
|
2014-12-17 00:47:42 +00:00
|
|
|
{
|
2015-01-09 19:36:57 +00:00
|
|
|
switch_vb_node_t *node = new_node(vb);
|
2014-12-17 07:03:31 +00:00
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
node->packet = *packet;
|
|
|
|
node->len = len;
|
|
|
|
memcpy(node->packet.body, packet->body, len);
|
2014-12-17 00:47:42 +00:00
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
switch_core_inthash_insert(vb->node_hash, node->packet.header.seq, node);
|
2014-12-17 00:47:42 +00:00
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
vb_debug(vb, (packet->header.m ? 1 : 2), "PUT packet last_ts:%u ts:%u seq:%u%s\n",
|
|
|
|
ntohl(vb->highest_wrote_ts), ntohl(node->packet.header.ts), ntohs(node->packet.header.seq), packet->header.m ? " <MARK>" : "");
|
2014-12-17 00:47:42 +00:00
|
|
|
|
2015-01-24 07:54:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (vb->write_init && ((abs(htons(packet->header.seq) - htons(vb->highest_wrote_seq)) > 10) ||
|
|
|
|
(abs(ntohl(node->packet.header.ts) - ntohl(vb->highest_wrote_ts)) > 270000))) {
|
|
|
|
vb_debug(vb, 2, "%s", "CHANGE DETECTED, PUNT\n");
|
|
|
|
switch_vb_reset(vb);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
if (!vb->write_init || ntohs(packet->header.seq) > ntohs(vb->highest_wrote_seq) ||
|
|
|
|
(ntohs(vb->highest_wrote_seq) > USHRT_MAX - 10 && ntohs(packet->header.seq) <= 10) ) {
|
|
|
|
vb->highest_wrote_seq = packet->header.seq;
|
2014-12-19 06:03:37 +00:00
|
|
|
}
|
2014-12-17 07:03:31 +00:00
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
if (vb->write_init && htons(packet->header.seq) >= htons(vb->highest_wrote_seq) && (ntohl(node->packet.header.ts) > ntohl(vb->highest_wrote_ts))) {
|
|
|
|
vb->complete_frames++;
|
|
|
|
vb_debug(vb, 2, "WRITE frame ts: %u complete=%u/%u n:%u\n", ntohl(node->packet.header.ts), vb->complete_frames , vb->frame_len, vb->visible_nodes);
|
|
|
|
vb->highest_wrote_ts = packet->header.ts;
|
|
|
|
} else if (!vb->write_init) {
|
|
|
|
vb->highest_wrote_ts = packet->header.ts;
|
2014-12-17 00:47:42 +00:00
|
|
|
}
|
2015-01-09 19:36:57 +00:00
|
|
|
|
|
|
|
if (!vb->write_init) vb->write_init = 1;
|
2014-12-17 00:47:42 +00:00
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
if (vb->complete_frames > vb->max_frame_len) {
|
|
|
|
drop_oldest_frame(vb);
|
2014-12-17 07:03:31 +00:00
|
|
|
}
|
2014-12-17 00:47:42 +00:00
|
|
|
}
|
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
static inline void increment_seq(switch_vb_t *vb)
|
2014-12-17 00:47:42 +00:00
|
|
|
{
|
2015-01-09 19:36:57 +00:00
|
|
|
vb->target_seq = htons((ntohs(vb->target_seq) + 1));
|
2014-12-17 00:47:42 +00:00
|
|
|
}
|
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
static inline void set_read_seq(switch_vb_t *vb, uint16_t seq)
|
|
|
|
{
|
|
|
|
vb->last_target_seq = seq;
|
|
|
|
vb->target_seq = htons((ntohs(vb->last_target_seq) + 1));
|
|
|
|
}
|
2014-12-17 00:47:42 +00:00
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
static inline switch_vb_node_t *vb_find_lowest_seq(switch_vb_t *vb)
|
2014-12-17 00:47:42 +00:00
|
|
|
{
|
|
|
|
switch_vb_node_t *np, *lowest = NULL;
|
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
for (np = vb->node_list; np; np = np->next) {
|
2014-12-17 00:47:42 +00:00
|
|
|
if (!np->visible) continue;
|
|
|
|
|
|
|
|
if (!lowest || ntohs(lowest->packet.header.seq) > ntohs(np->packet.header.seq)) {
|
|
|
|
lowest = np;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return lowest;
|
|
|
|
}
|
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
static inline switch_status_t vb_next_packet(switch_vb_t *vb, switch_vb_node_t **nodep)
|
2014-12-17 00:47:42 +00:00
|
|
|
{
|
2015-01-09 19:36:57 +00:00
|
|
|
switch_vb_node_t *np = NULL, *node = NULL;
|
2014-12-19 06:03:37 +00:00
|
|
|
switch_status_t status;
|
2014-12-17 00:47:42 +00:00
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
if (np) status = 0, status++;
|
|
|
|
|
|
|
|
if (!vb->target_seq) {
|
|
|
|
if ((node = vb_find_lowest_seq(vb))) {
|
|
|
|
vb_debug(vb, 2, "No target seq using seq: %u as a starting point\n", ntohs(node->packet.header.seq));
|
2014-12-19 08:26:09 +00:00
|
|
|
} else {
|
2015-01-09 19:36:57 +00:00
|
|
|
vb_debug(vb, 1, "%s", "No nodes available....\n");
|
|
|
|
}
|
|
|
|
} else if ((node = switch_core_inthash_find(vb->node_hash, vb->target_seq))) {
|
|
|
|
vb_debug(vb, 2, "FOUND desired seq: %u\n", ntohs(vb->target_seq));
|
|
|
|
} else {
|
|
|
|
int x;
|
|
|
|
|
|
|
|
vb_debug(vb, 2, "MISSING desired seq: %u\n", ntohs(vb->target_seq));
|
|
|
|
|
|
|
|
for (x = 0; x < 10; x++) {
|
|
|
|
increment_seq(vb);
|
|
|
|
if ((node = switch_core_inthash_find(vb->node_hash, vb->target_seq))) {
|
|
|
|
vb_debug(vb, 2, "FOUND incremental seq: %u\n", ntohs(vb->target_seq));
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
vb_debug(vb, 2, "MISSING incremental seq: %u\n", ntohs(vb->target_seq));
|
|
|
|
}
|
2014-12-19 08:26:09 +00:00
|
|
|
}
|
2014-12-19 06:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*nodep = node;
|
|
|
|
|
|
|
|
if (node) {
|
2015-01-09 19:36:57 +00:00
|
|
|
set_read_seq(vb, node->packet.header.seq);
|
2014-12-19 06:03:37 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
2014-12-17 00:47:42 +00:00
|
|
|
}
|
|
|
|
|
2014-12-19 06:03:37 +00:00
|
|
|
return SWITCH_STATUS_NOTFOUND;
|
|
|
|
|
2014-12-17 00:47:42 +00:00
|
|
|
}
|
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
static inline void free_nodes(switch_vb_t *vb)
|
2014-12-17 00:47:42 +00:00
|
|
|
{
|
2015-01-09 19:36:57 +00:00
|
|
|
vb->node_list = NULL;
|
2014-12-17 00:47:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-17 07:03:31 +00:00
|
|
|
SWITCH_DECLARE(int) switch_vb_poll(switch_vb_t *vb)
|
|
|
|
{
|
|
|
|
return (vb->complete_frames >= vb->frame_len);
|
|
|
|
}
|
2014-12-17 00:47:42 +00:00
|
|
|
|
|
|
|
SWITCH_DECLARE(int) switch_vb_frame_count(switch_vb_t *vb)
|
|
|
|
{
|
|
|
|
return vb->complete_frames;
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(void) switch_vb_debug_level(switch_vb_t *vb, uint8_t level)
|
|
|
|
{
|
|
|
|
vb->debug_level = level;
|
|
|
|
}
|
|
|
|
|
2015-01-24 07:54:59 +00:00
|
|
|
SWITCH_DECLARE(void) switch_vb_reset(switch_vb_t *vb)
|
2014-12-17 00:47:42 +00:00
|
|
|
{
|
2015-01-24 07:54:59 +00:00
|
|
|
|
|
|
|
switch_mutex_lock(vb->mutex);
|
|
|
|
switch_core_inthash_destroy(&vb->missing_seq_hash);
|
|
|
|
switch_core_inthash_init(&vb->missing_seq_hash);
|
|
|
|
switch_mutex_unlock(vb->mutex);
|
|
|
|
|
|
|
|
vb_debug(vb, 2, "%s", "RESET BUFFER\n");
|
|
|
|
|
2014-12-19 06:03:37 +00:00
|
|
|
|
2014-12-19 08:26:09 +00:00
|
|
|
vb->last_target_seq = 0;
|
2014-12-17 00:47:42 +00:00
|
|
|
vb->target_seq = 0;
|
2015-01-24 07:54:59 +00:00
|
|
|
vb->write_init = 0;
|
|
|
|
vb->highest_wrote_seq = 0;
|
|
|
|
vb->highest_wrote_ts = 0;
|
|
|
|
vb->next_seq = 0;
|
|
|
|
vb->highest_read_ts = 0;
|
|
|
|
vb->highest_read_seq = 0;
|
|
|
|
vb->complete_frames = 0;
|
|
|
|
vb->read_init = 0;
|
|
|
|
vb->next_seq = 0;
|
|
|
|
vb->complete_frames = 0;
|
|
|
|
|
|
|
|
hide_nodes(vb);
|
2014-12-17 00:47:42 +00:00
|
|
|
}
|
|
|
|
|
2015-01-24 07:54:59 +00:00
|
|
|
SWITCH_DECLARE(switch_status_t) switch_vb_create(switch_vb_t **vbp, uint32_t min_frame_len, uint32_t max_frame_len, switch_memory_pool_t *pool)
|
2014-12-17 00:47:42 +00:00
|
|
|
{
|
|
|
|
switch_vb_t *vb;
|
2015-01-24 07:54:59 +00:00
|
|
|
int free_pool = 0;
|
|
|
|
|
|
|
|
if (!pool) {
|
|
|
|
switch_core_new_memory_pool(&pool);
|
|
|
|
free_pool = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
vb = switch_core_alloc(pool, sizeof(*vb));
|
|
|
|
vb->free_pool = free_pool;
|
2014-12-17 00:47:42 +00:00
|
|
|
vb->min_frame_len = vb->frame_len = min_frame_len;
|
|
|
|
vb->max_frame_len = max_frame_len;
|
2015-01-24 07:54:59 +00:00
|
|
|
vb->pool = pool;
|
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
switch_core_inthash_init(&vb->missing_seq_hash);
|
|
|
|
switch_core_inthash_init(&vb->node_hash);
|
2015-01-24 07:54:59 +00:00
|
|
|
switch_mutex_init(&vb->mutex, SWITCH_MUTEX_NESTED, pool);
|
|
|
|
|
2014-12-17 00:47:42 +00:00
|
|
|
|
|
|
|
*vbp = vb;
|
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_status_t) switch_vb_destroy(switch_vb_t **vbp)
|
|
|
|
{
|
|
|
|
switch_vb_t *vb = *vbp;
|
|
|
|
*vbp = NULL;
|
2015-01-09 19:36:57 +00:00
|
|
|
|
|
|
|
switch_core_inthash_destroy(&vb->missing_seq_hash);
|
|
|
|
switch_core_inthash_destroy(&vb->node_hash);
|
2014-12-17 00:47:42 +00:00
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
free_nodes(vb);
|
2015-01-24 07:54:59 +00:00
|
|
|
|
|
|
|
if (vb->free_pool) {
|
|
|
|
switch_core_destroy_memory_pool(&vb->pool);
|
|
|
|
}
|
2015-01-09 19:36:57 +00:00
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(uint32_t) switch_vb_pop_nack(switch_vb_t *vb)
|
|
|
|
{
|
|
|
|
switch_hash_index_t *hi = NULL;
|
|
|
|
uint32_t nack = 0;
|
|
|
|
uint16_t least = 0;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
void *val;
|
|
|
|
const void *var;
|
|
|
|
|
2015-01-24 07:54:59 +00:00
|
|
|
switch_mutex_lock(vb->mutex);
|
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
for (hi = switch_core_hash_first(vb->missing_seq_hash); hi; hi = switch_core_hash_next(&hi)) {
|
|
|
|
uint16_t seq;
|
|
|
|
|
|
|
|
switch_core_hash_this(hi, &var, NULL, &val);
|
|
|
|
seq = ntohs(*((uint16_t *) var));
|
|
|
|
|
|
|
|
if (!least || seq < least) {
|
|
|
|
least = seq;
|
|
|
|
}
|
2014-12-17 07:03:31 +00:00
|
|
|
}
|
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
if (least && switch_core_inthash_delete(vb->missing_seq_hash, (uint32_t)htons(least))) {
|
|
|
|
vb_debug(vb, 3, "Found smallest NACKABLE seq %u\n", least);
|
|
|
|
nack = (uint32_t) htons(least);
|
|
|
|
|
|
|
|
for (i = 1; i > 17; i++) {
|
|
|
|
if (switch_core_inthash_delete(vb->missing_seq_hash, (uint32_t)htons(least + i))) {
|
|
|
|
vb_debug(vb, 3, "Found addtl NACKABLE seq %u\n", least + i);
|
|
|
|
nack |= (1 << (16 + i));
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-24 07:54:59 +00:00
|
|
|
|
|
|
|
switch_mutex_unlock(vb->mutex);
|
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
|
|
|
|
return nack;
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_status_t) switch_vb_push_packet(switch_vb_t *vb, switch_rtp_packet_t *packet, switch_size_t len)
|
|
|
|
{
|
|
|
|
add_node(vb, packet, len);
|
2014-12-17 00:47:42 +00:00
|
|
|
|
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_status_t) switch_vb_put_packet(switch_vb_t *vb, switch_rtp_packet_t *packet, switch_size_t len)
|
|
|
|
{
|
2015-01-09 19:36:57 +00:00
|
|
|
uint32_t i;
|
|
|
|
uint16_t want = ntohs(vb->next_seq), got = ntohs(packet->header.seq);
|
|
|
|
|
2015-01-24 07:54:59 +00:00
|
|
|
switch_mutex_lock(vb->mutex);
|
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
if (!want) want = got;
|
2014-12-17 07:03:31 +00:00
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
if (got > want) {
|
|
|
|
for (i = want; i < got; i++) {
|
|
|
|
vb_debug(vb, 2, "MARK SEQ MISSING %u\n", i);
|
|
|
|
switch_core_inthash_insert(vb->missing_seq_hash, (uint32_t)htons(i), (void *)SWITCH_TRUE);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (switch_core_inthash_delete(vb->missing_seq_hash, (uint32_t)htons(got))) {
|
|
|
|
vb_debug(vb, 2, "MARK SEQ FOUND %u\n", got);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (got >= want) {
|
|
|
|
vb->next_seq = htons(ntohs(packet->header.seq) + 1);
|
2014-12-17 07:03:31 +00:00
|
|
|
}
|
2015-01-09 19:36:57 +00:00
|
|
|
|
|
|
|
add_node(vb, packet, len);
|
|
|
|
|
2015-01-24 07:54:59 +00:00
|
|
|
switch_mutex_unlock(vb->mutex);
|
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_status_t) switch_vb_get_packet_by_seq(switch_vb_t *vb, uint16_t seq, switch_rtp_packet_t *packet, switch_size_t *len)
|
|
|
|
{
|
|
|
|
switch_vb_node_t *node;
|
2014-12-17 07:03:31 +00:00
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
if ((node = switch_core_inthash_find(vb->node_hash, seq))) {
|
|
|
|
vb_debug(vb, 2, "Found buffered seq: %u\n", ntohs(seq));
|
|
|
|
*packet = node->packet;
|
|
|
|
*len = node->len;
|
|
|
|
memcpy(packet->body, node->packet.body, node->len);
|
2014-12-17 00:47:42 +00:00
|
|
|
return SWITCH_STATUS_SUCCESS;
|
2015-01-09 19:36:57 +00:00
|
|
|
} else {
|
|
|
|
vb_debug(vb, 2, "Missing buffered seq: %u\n", ntohs(seq));
|
2014-12-17 00:47:42 +00:00
|
|
|
}
|
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
return SWITCH_STATUS_NOTFOUND;
|
2014-12-17 00:47:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SWITCH_DECLARE(switch_status_t) switch_vb_get_packet(switch_vb_t *vb, switch_rtp_packet_t *packet, switch_size_t *len)
|
|
|
|
{
|
|
|
|
switch_vb_node_t *node = NULL;
|
2014-12-19 06:03:37 +00:00
|
|
|
switch_status_t status;
|
2014-12-17 07:03:31 +00:00
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
vb_debug(vb, 2, "GET PACKET %u/%u n:%d\n", vb->complete_frames , vb->frame_len, vb->visible_nodes);
|
2014-12-19 08:26:09 +00:00
|
|
|
|
2014-12-17 00:47:42 +00:00
|
|
|
if (vb->complete_frames < vb->frame_len) {
|
|
|
|
vb_debug(vb, 2, "BUFFERING %u/%u\n", vb->complete_frames , vb->frame_len);
|
|
|
|
return SWITCH_STATUS_MORE_DATA;
|
|
|
|
}
|
|
|
|
|
2015-01-09 19:36:57 +00:00
|
|
|
if ((status = vb_next_packet(vb, &node)) == SWITCH_STATUS_SUCCESS) {
|
|
|
|
vb_debug(vb, 2, "Found next frame cur ts: %u seq: %u\n", htonl(node->packet.header.ts), htons(node->packet.header.seq));
|
|
|
|
|
|
|
|
if (!vb->read_init || ntohs(node->packet.header.seq) > ntohs(vb->highest_read_seq) ||
|
|
|
|
(ntohs(vb->highest_read_seq) > USHRT_MAX - 10 && ntohs(node->packet.header.seq) <= 10) ) {
|
|
|
|
vb->highest_read_seq = node->packet.header.seq;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vb->read_init && htons(node->packet.header.seq) >= htons(vb->highest_read_seq) && (ntohl(node->packet.header.ts) > ntohl(vb->highest_read_ts))) {
|
|
|
|
vb->complete_frames--;
|
|
|
|
vb_debug(vb, 2, "READ frame ts: %u complete=%u/%u n:%u\n", ntohl(node->packet.header.ts), vb->complete_frames , vb->frame_len, vb->visible_nodes);
|
|
|
|
vb->highest_read_ts = node->packet.header.ts;
|
|
|
|
} else if (!vb->read_init) {
|
|
|
|
vb->highest_read_ts = node->packet.header.ts;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!vb->read_init) vb->read_init = 1;
|
|
|
|
|
|
|
|
|
2014-12-19 06:03:37 +00:00
|
|
|
} else {
|
2015-01-24 07:54:59 +00:00
|
|
|
switch_vb_reset(vb);
|
2014-12-17 00:47:42 +00:00
|
|
|
|
2014-12-19 06:03:37 +00:00
|
|
|
switch(status) {
|
|
|
|
case SWITCH_STATUS_RESTART:
|
|
|
|
vb_debug(vb, 2, "%s", "Error encountered ask for new keyframe\n");
|
|
|
|
return SWITCH_STATUS_RESTART;
|
|
|
|
case SWITCH_STATUS_NOTFOUND:
|
|
|
|
default:
|
|
|
|
vb_debug(vb, 2, "%s", "No frames found wait for more\n");
|
|
|
|
return SWITCH_STATUS_MORE_DATA;
|
|
|
|
}
|
|
|
|
}
|
2014-12-17 07:03:31 +00:00
|
|
|
|
2014-12-17 00:47:42 +00:00
|
|
|
if (node) {
|
2014-12-19 08:26:09 +00:00
|
|
|
status = SWITCH_STATUS_SUCCESS;
|
|
|
|
|
2014-12-17 00:47:42 +00:00
|
|
|
*packet = node->packet;
|
|
|
|
*len = node->len;
|
|
|
|
memcpy(packet->body, node->packet.body, node->len);
|
2015-01-09 19:36:57 +00:00
|
|
|
hide_node(node);
|
2014-12-19 08:26:09 +00:00
|
|
|
|
2015-01-24 07:54:59 +00:00
|
|
|
vb_debug(vb, 1, "GET packet ts:%u seq:%u %s\n", ntohl(packet->header.ts), ntohs(packet->header.seq), packet->header.m ? " <MARK>" : "");
|
2014-12-17 07:03:31 +00:00
|
|
|
|
2014-12-19 08:26:09 +00:00
|
|
|
return status;
|
2014-12-17 00:47:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return SWITCH_STATUS_MORE_DATA;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* For Emacs:
|
|
|
|
* Local Variables:
|
|
|
|
* mode:c
|
|
|
|
* indent-tabs-mode:t
|
|
|
|
* tab-width:4
|
|
|
|
* c-basic-offset:4
|
|
|
|
* End:
|
|
|
|
* For VIM:
|
|
|
|
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
|
|
|
|
*/
|