1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
/***
This file is part of PulseAudio.
Copyright 2013 Matthias Wabersich
Copyright 2013 Hajime Fujita
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
PulseAudio 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.
You should have received a copy of the GNU Lesser General Public License
along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA.
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
#include <pulse/xmalloc.h>
#include <pulsecore/core-error.h>
#include <pulsecore/macro.h>
#include "raop-packet-buffer.h"
struct pa_raop_packet_buffer {
pa_memchunk *packets;
pa_mempool *mempool;
size_t size;
size_t count;
uint16_t seq;
size_t pos;
};
pa_raop_packet_buffer *pa_raop_packet_buffer_new(pa_mempool *mempool, const size_t size) {
pa_raop_packet_buffer *pb = pa_xnew0(pa_raop_packet_buffer, 1);
pa_assert(mempool);
pa_assert(size > 0);
pb->count = 0;
pb->size = size;
pb->mempool = mempool;
pb->packets = pa_xnew0(pa_memchunk, size);
pb->seq = pb->pos = 0;
return pb;
}
void pa_raop_packet_buffer_free(pa_raop_packet_buffer *pb) {
size_t i;
pa_assert(pb);
for (i = 0; pb->packets && i < pb->size; i++) {
if (pb->packets[i].memblock)
pa_memblock_unref(pb->packets[i].memblock);
pa_memchunk_reset(&pb->packets[i]);
}
pa_xfree(pb->packets);
pb->packets = NULL;
pa_xfree(pb);
}
void pa_raop_packet_buffer_reset(pa_raop_packet_buffer *pb, uint16_t seq) {
size_t i;
pa_assert(pb);
pa_assert(pb->packets);
pb->pos = 0;
pb->count = 0;
pb->seq = (!seq) ? UINT16_MAX : seq - 1;
for (i = 0; i < pb->size; i++) {
if (pb->packets[i].memblock)
pa_memblock_unref(pb->packets[i].memblock);
pa_memchunk_reset(&pb->packets[i]);
}
}
pa_memchunk *pa_raop_packet_buffer_prepare(pa_raop_packet_buffer *pb, uint16_t seq, const size_t size) {
pa_memchunk *packet = NULL;
size_t i;
pa_assert(pb);
pa_assert(pb->packets);
if (seq == 0) {
/* 0 means seq reached UINT16_MAX and has been wrapped... */
pa_assert(pb->seq == UINT16_MAX);
pb->seq = 0;
} else {
/* ...otherwise, seq MUST have be increased! */
pa_assert(seq == pb->seq + 1);
pb->seq++;
}
i = (pb->pos + 1) % pb->size;
if (pb->packets[i].memblock)
pa_memblock_unref(pb->packets[i].memblock);
pa_memchunk_reset(&pb->packets[i]);
pb->packets[i].memblock = pa_memblock_new(pb->mempool, size);
pb->packets[i].length = size;
pb->packets[i].index = 0;
packet = &pb->packets[i];
if (pb->count < pb->size)
pb->count++;
pb->pos = i;
return packet;
}
pa_memchunk *pa_raop_packet_buffer_retrieve(pa_raop_packet_buffer *pb, uint16_t seq) {
pa_memchunk *packet = NULL;
size_t delta, i;
pa_assert(pb);
pa_assert(pb->packets);
if (seq == pb->seq)
packet = &pb->packets[pb->pos];
else {
if (seq < pb->seq) {
/* Regular case: pb->seq did not wrapped since seq. */
delta = pb->seq - seq;
} else {
/* Tricky case: pb->seq wrapped since seq! */
delta = pb->seq + (UINT16_MAX - seq);
}
/* If the requested packet is too old, do nothing and return */
if (delta > pb->count)
return NULL;
i = (pb->size + pb->pos - delta) % pb->size;
if (delta < pb->size && pb->packets[i].memblock)
packet = &pb->packets[i];
}
return packet;
}
|