uint32_t highest_wrote_ts;
uint32_t highest_wrote_seq;
uint16_t target_seq;
- uint16_t seq_out;
uint32_t visible_nodes;
- uint32_t total_frames;
uint32_t complete_frames;
uint32_t frame_len;
uint32_t min_frame_len;
uint16_t next_seq;
switch_inthash_t *missing_seq_hash;
switch_inthash_t *node_hash;
+ switch_mutex_t *mutex;
+ switch_memory_pool_t *pool;
+ int free_pool;
};
static inline switch_vb_node_t *new_node(switch_vb_t *vb)
if (!np) {
- switch_zmalloc(np, sizeof(*np));
+ np = switch_core_alloc(vb->pool, sizeof(*np));
if (last) {
last->next = np;
static inline void hide_node(switch_vb_node_t *node)
{
- node->visible = 0;
- node->parent->visible_nodes--;
+ if (node->visible) {
+ node->visible = 0;
+ node->parent->visible_nodes--;
+ }
switch_core_inthash_delete(node->parent->node_hash, node->packet.header.seq);
}
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>" : "");
+
+
+
+
+ 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);
+ }
+
+
+
+
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;
static inline void free_nodes(switch_vb_t *vb)
{
- switch_vb_node_t *np = vb->node_list, *cur;
-
- while(np) {
- cur = np;
- np = np->next;
- free(cur);
- }
-
vb->node_list = NULL;
}
vb->debug_level = level;
}
-SWITCH_DECLARE(void) switch_vb_reset(switch_vb_t *vb, switch_bool_t flush)
+SWITCH_DECLARE(void) switch_vb_reset(switch_vb_t *vb)
{
- vb_debug(vb, 2, "RESET BUFFER flush: %d\n", (int)flush);
+
+ 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");
+
vb->last_target_seq = 0;
vb->target_seq = 0;
-
- if (flush) {
- //do_flush(vb);
- }
+ 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);
}
-SWITCH_DECLARE(switch_status_t) switch_vb_create(switch_vb_t **vbp, uint32_t min_frame_len, uint32_t max_frame_len)
+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)
{
switch_vb_t *vb;
- switch_zmalloc(vb, sizeof(*vb));
-
+ 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;
vb->min_frame_len = vb->frame_len = min_frame_len;
vb->max_frame_len = max_frame_len;
- //vb->seq_out = (uint16_t) rand();
+ vb->pool = pool;
+
switch_core_inthash_init(&vb->missing_seq_hash);
switch_core_inthash_init(&vb->node_hash);
+ switch_mutex_init(&vb->mutex, SWITCH_MUTEX_NESTED, pool);
+
*vbp = vb;
switch_core_inthash_destroy(&vb->node_hash);
free_nodes(vb);
- free(vb);
+
+ if (vb->free_pool) {
+ switch_core_destroy_memory_pool(&vb->pool);
+ }
return SWITCH_STATUS_SUCCESS;
}
void *val;
const void *var;
+ switch_mutex_lock(vb->mutex);
+
for (hi = switch_core_hash_first(vb->missing_seq_hash); hi; hi = switch_core_hash_next(&hi)) {
uint16_t seq;
}
}
}
+
+ switch_mutex_unlock(vb->mutex);
+
return nack;
}
uint32_t i;
uint16_t want = ntohs(vb->next_seq), got = ntohs(packet->header.seq);
+ switch_mutex_lock(vb->mutex);
+
if (!want) want = got;
if (got > want) {
add_node(vb, packet, len);
+ switch_mutex_unlock(vb->mutex);
+
return SWITCH_STATUS_SUCCESS;
}
} else {
- switch_vb_reset(vb, SWITCH_FALSE);
+ switch_vb_reset(vb);
switch(status) {
case SWITCH_STATUS_RESTART:
memcpy(packet->body, node->packet.body, node->len);
hide_node(node);
- vb_debug(vb, 1, "GET packet ts:%u seq:%u~%u%s\n", ntohl(packet->header.ts), ntohs(packet->header.seq), vb->seq_out, packet->header.m ? " <MARK>" : "");
- //packet->header.seq = htons(vb->seq_out++);
+ vb_debug(vb, 1, "GET packet ts:%u seq:%u %s\n", ntohl(packet->header.ts), ntohs(packet->header.seq), packet->header.m ? " <MARK>" : "");
return status;
}