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 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU 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, see <http://www.gnu.org/licenses/>.
18 /** @file clients/playrtp.h
25 /** @brief Maximum samples per packet we'll support
27 * NB that two channels = two samples in this program.
29 #define MAXSAMPLES 2048
31 /** @brief Number of samples to infill by in one go
33 * This is an upper bound - in practice we expect the underlying audio API to
34 * only ask for a much smaller number of samples in any one go.
36 #define INFILL_SAMPLES (44100 * 2) /* 1s */
38 /** @brief Received packet
40 * Received packets are kept in a binary heap (see @ref pheap) ordered by
44 /** @brief Next packet in @ref next_free_packet or @ref received_packets */
47 /** @brief Number of samples in this packet */
50 /** @brief Timestamp from RTP packet
52 * NB that "timestamps" are really sample counters. Use lt() or lt_packet()
53 * to compare timestamps.
60 * - @ref IDLE - the idle bit was set in the RTP packet
63 /** @brief idle bit set in RTP packet*/
66 /** @brief Raw sample data
68 * Only the first @p nsamples samples are defined; the rest is uninitialized
71 uint16_t samples_raw[MAXSAMPLES];
74 /** @brief Structure of free packet list */
77 union free_packet *next;
80 /** @brief Return true iff \f$a < b\f$ in sequence-space arithmetic
82 * Specifically it returns true if \f$(a-b) mod 2^{32} < 2^{31}\f$.
84 * See also lt_packet().
86 static inline int lt(uint32_t a, uint32_t b) {
87 return (uint32_t)(a - b) & 0x80000000;
90 /** @brief Return true iff a >= b in sequence-space arithmetic */
91 static inline int ge(uint32_t a, uint32_t b) {
95 /** @brief Return true iff a > b in sequence-space arithmetic */
96 static inline int gt(uint32_t a, uint32_t b) {
100 /** @brief Return true iff a <= b in sequence-space arithmetic */
101 static inline int le(uint32_t a, uint32_t b) {
105 /** @brief Ordering for packets, used by @ref pheap */
106 static inline int lt_packet(const struct packet *a, const struct packet *b) {
107 return lt(a->timestamp, b->timestamp);
110 /** @brief Return true if @p p contains @p timestamp
112 * Containment implies that a sample @p timestamp exists within the packet.
114 static inline int contains(const struct packet *p, uint32_t timestamp) {
115 const uint32_t packet_start = p->timestamp;
116 const uint32_t packet_end = p->timestamp + p->nsamples;
118 return (ge(timestamp, packet_start)
119 && lt(timestamp, packet_end));
123 * @brief Binary heap of packets ordered by timestamp */
124 HEAP_TYPE(pheap, struct packet *, lt_packet);
126 struct packet *playrtp_new_packet(void);
127 void playrtp_free_packet(struct packet *p);
128 void playrtp_fill_buffer(void);
129 struct packet *playrtp_next_packet(void);
131 extern const char *device;
132 extern struct packet *received_packets;
133 extern struct packet **received_tail;
134 extern pthread_mutex_t receive_lock;
135 extern pthread_cond_t receive_cond;
136 extern uint32_t nreceived;
137 extern struct pheap packets;
138 extern volatile uint32_t nsamples;
139 extern uint32_t next_timestamp;
141 extern pthread_mutex_t lock;
142 extern pthread_cond_t cond;
143 extern unsigned minbuffer;
145 extern int16_t *dump_buffer;
146 extern size_t dump_index;
147 extern size_t dump_size;
149 void playrtp_oss(void), playrtp_alsa(void), playrtp_coreaudio(void);
151 #endif /* PLAYRTP_H */