debug dump (mac only) for playrtp
[disorder] / clients / playrtp.h
1 /*
2 * This file is part of DisOrder.
3 * Copyright (C) 2007 Richard Kettlewell
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 */
20 /** @file clients/playrtp.h
21 * @brief RTP player
22 */
23
24 #ifndef PLAYRTP_H
25 #define PLAYRTP_H
26
27 /** @brief Maximum samples per packet we'll support
28 *
29 * NB that two channels = two samples in this program.
30 */
31 #define MAXSAMPLES 2048
32
33 /** @brief Number of samples to infill by in one go
34 *
35 * This is an upper bound - in practice we expect the underlying audio API to
36 * only ask for a much smaller number of samples in any one go.
37 */
38 #define INFILL_SAMPLES (44100 * 2) /* 1s */
39
40 /** @brief Received packet
41 *
42 * Received packets are kept in a binary heap (see @ref pheap) ordered by
43 * timestamp.
44 */
45 struct packet {
46 /** @brief Next packet in @ref next_free_packet or @ref received_packets */
47 struct packet *next;
48
49 /** @brief Number of samples in this packet */
50 uint32_t nsamples;
51
52 /** @brief Timestamp from RTP packet
53 *
54 * NB that "timestamps" are really sample counters. Use lt() or lt_packet()
55 * to compare timestamps.
56 */
57 uint32_t timestamp;
58
59 /** @brief Flags
60 *
61 * Valid values are:
62 * - @ref IDLE - the idle bit was set in the RTP packet
63 */
64 unsigned flags;
65 /** @brief idle bit set in RTP packet*/
66 #define IDLE 0x0001
67
68 /** @brief Raw sample data
69 *
70 * Only the first @p nsamples samples are defined; the rest is uninitialized
71 * data.
72 */
73 uint16_t samples_raw[MAXSAMPLES];
74 };
75
76 /** @brief Structure of free packet list */
77 union free_packet {
78 struct packet p;
79 union free_packet *next;
80 };
81
82 /** @brief Return true iff \f$a < b\f$ in sequence-space arithmetic
83 *
84 * Specifically it returns true if \f$(a-b) mod 2^{32} < 2^{31}\f$.
85 *
86 * See also lt_packet().
87 */
88 static inline int lt(uint32_t a, uint32_t b) {
89 return (uint32_t)(a - b) & 0x80000000;
90 }
91
92 /** @brief Return true iff a >= b in sequence-space arithmetic */
93 static inline int ge(uint32_t a, uint32_t b) {
94 return !lt(a, b);
95 }
96
97 /** @brief Return true iff a > b in sequence-space arithmetic */
98 static inline int gt(uint32_t a, uint32_t b) {
99 return lt(b, a);
100 }
101
102 /** @brief Return true iff a <= b in sequence-space arithmetic */
103 static inline int le(uint32_t a, uint32_t b) {
104 return !lt(b, a);
105 }
106
107 /** @brief Ordering for packets, used by @ref pheap */
108 static inline int lt_packet(const struct packet *a, const struct packet *b) {
109 return lt(a->timestamp, b->timestamp);
110 }
111
112 /** @brief Return true if @p p contains @p timestamp
113 *
114 * Containment implies that a sample @p timestamp exists within the packet.
115 */
116 static inline int contains(const struct packet *p, uint32_t timestamp) {
117 const uint32_t packet_start = p->timestamp;
118 const uint32_t packet_end = p->timestamp + p->nsamples;
119
120 return (ge(timestamp, packet_start)
121 && lt(timestamp, packet_end));
122 }
123
124 /** @struct pheap
125 * @brief Binary heap of packets ordered by timestamp */
126 HEAP_TYPE(pheap, struct packet *, lt_packet);
127
128 struct packet *playrtp_new_packet(void);
129 void playrtp_free_packet(struct packet *p);
130 void playrtp_fill_buffer(void);
131 struct packet *playrtp_next_packet(void);
132
133 extern const char *device;
134 extern struct packet *received_packets;
135 extern struct packet **received_tail;
136 extern pthread_mutex_t receive_lock;
137 extern pthread_cond_t receive_cond;
138 extern uint32_t nreceived;
139 extern struct pheap packets;
140 extern volatile uint32_t nsamples;
141 extern uint32_t next_timestamp;
142 extern int active;
143 extern pthread_mutex_t lock;
144 extern pthread_cond_t cond;
145 extern unsigned minbuffer;
146
147 extern int16_t *dump_buffer;
148 extern size_t dump_index;
149 extern size_t dump_size;
150
151 void playrtp_oss(void), playrtp_alsa(void), playrtp_coreaudio(void);
152
153 #endif /* PLAYRTP_H */
154
155 /*
156 Local Variables:
157 c-basic-offset:2
158 comment-column:40
159 fill-column:79
160 indent-tabs-mode:nil
161 End:
162 */