2 * This file is part of DisOrder.
3 * Copyright (C) 2007 Richard Kettlewell
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.
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.
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
20 /** @file clients/playrtp.h
27 /** @brief Maximum samples per packet we'll support
29 * NB that two channels = two samples in this program.
31 #define MAXSAMPLES 2048
33 /** @brief Number of samples to infill by in one go
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.
38 #define INFILL_SAMPLES (44100 * 2) /* 1s */
40 /** @brief Received packet
42 * Received packets are kept in a binary heap (see @ref pheap) ordered by
46 /** @brief Next packet in @ref next_free_packet or @ref received_packets */
49 /** @brief Number of samples in this packet */
52 /** @brief Timestamp from RTP packet
54 * NB that "timestamps" are really sample counters. Use lt() or lt_packet()
55 * to compare timestamps.
62 * - @ref IDLE - the idle bit was set in the RTP packet
65 /** @brief idle bit set in RTP packet*/
68 /** @brief Raw sample data
70 * Only the first @p nsamples samples are defined; the rest is uninitialized
73 uint16_t samples_raw[MAXSAMPLES];
76 /** @brief Structure of free packet list */
79 union free_packet *next;
82 /** @brief Return true iff \f$a < b\f$ in sequence-space arithmetic
84 * Specifically it returns true if \f$(a-b) mod 2^{32} < 2^{31}\f$.
86 * See also lt_packet().
88 static inline int lt(uint32_t a, uint32_t b) {
89 return (uint32_t)(a - b) & 0x80000000;
92 /** @brief Return true iff a >= b in sequence-space arithmetic */
93 static inline int ge(uint32_t a, uint32_t b) {
97 /** @brief Return true iff a > b in sequence-space arithmetic */
98 static inline int gt(uint32_t a, uint32_t b) {
102 /** @brief Return true iff a <= b in sequence-space arithmetic */
103 static inline int le(uint32_t a, uint32_t b) {
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);
112 /** @brief Return true if @p p contains @p timestamp
114 * Containment implies that a sample @p timestamp exists within the packet.
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;
120 return (ge(timestamp, packet_start)
121 && lt(timestamp, packet_end));
125 * @brief Binary heap of packets ordered by timestamp */
126 HEAP_TYPE(pheap, struct packet *, lt_packet);
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);
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;
143 extern pthread_mutex_t lock;
144 extern pthread_cond_t cond;
145 extern unsigned minbuffer;
147 extern int16_t *dump_buffer;
148 extern size_t dump_index;
149 extern size_t dump_size;
151 void playrtp_oss(void), playrtp_alsa(void), playrtp_coreaudio(void);
153 #endif /* PLAYRTP_H */