int queue(struct jitter_buffer *q, RTPMessage *pk) { if (q->size == q->capacity) { /* Full, empty queue */ empty_queue(q); /*rtp_free_msg(NULL, pk);*/ return 0; } if (q->size > 8) q->queue_ready = 1; ++q->size; ++q->rear; if (q->rear == q->capacity) q->rear = 0; q->queue[q->rear] = pk; int a; int b; int j; a = q->rear; for (j = 0; j < q->size - 1; ++j) { b = a - 1; if (b < 0) b += q->capacity; if (sequence_number_older(q->queue[b]->header->sequnum, q->queue[a]->header->sequnum, q->queue[b]->header->timestamp, q->queue[a]->header->timestamp)) { RTPMessage *temp; temp = q->queue[a]; q->queue[a] = q->queue[b]; q->queue[b] = temp; /*printf("had to swap\n");*/ } else { break; } a -= 1; if (a < 0) a += q->capacity; } if (pk) return 1; return 0; }
int queue(struct jitter_buffer *q, rtp_msg_t *pk) { if (q->size == q->capacity) { printf("buffer full, emptying buffer...\n"); empty_queue(q); return 0; } if (q->size > 8) q->queue_ready = 1; ++q->size; ++q->rear; if (q->rear == q->capacity) q->rear = 0; q->queue[q->rear] = pk; int a; int b; int j; a = q->rear; for (j = 0; j < q->size - 1; ++j) { b = a - 1; if (b < 0) b += q->capacity; if (sequence_number_older(q->queue[b]->_header->_sequence_number, q->queue[a]->_header->_sequence_number, q->queue[b]->_header->_timestamp, q->queue[a]->_header->_timestamp)) { rtp_msg_t *temp; temp = q->queue[a]; q->queue[a] = q->queue[b]; q->queue[b] = temp; printf("had to swap\n"); } else { break; } a -= 1; if (a < 0) a += q->capacity; } if (pk) return 1; return 0; }
/* success is 0 when there is nothing to dequeue, 1 when there's a good packet, 2 when there's a lost packet */ RTPMessage *dequeue(struct jitter_buffer *q, int *success) { if (q->size == 0 || q->queue_ready == 0) { /* Empty queue */ q->queue_ready = 0; *success = 0; return NULL; } int front = q->front; if (q->id_set == 0) { q->current_id = q->queue[front]->header->sequnum; q->current_ts = q->queue[front]->header->timestamp; q->id_set = 1; } else { int next_id = q->queue[front]->header->sequnum; int next_ts = q->queue[front]->header->timestamp; /* if this packet is indeed the expected packet */ if (next_id == (q->current_id + 1) % MAX_SEQU_NUM) { q->current_id = next_id; q->current_ts = next_ts; } else { if (sequence_number_older(next_id, q->current_id, next_ts, q->current_ts)) { /*printf("nextid: %d current: %d\n", next_id, q->current_id);*/ q->current_id = (q->current_id + 1) % MAX_SEQU_NUM; *success = 2; /* tell the decoder the packet is lost */ return NULL; } else { /* packet too old */ /*printf("packet too old\n");*/ *success = 0; return NULL; } } } q->size--; q->front++; if (q->front == q->capacity) q->front = 0; *success = 1; q->current_id = q->queue[front]->header->sequnum; q->current_ts = q->queue[front]->header->timestamp; return q->queue[front]; }