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
61 * - @ref SILENT - packet is entirely silent
64 /** @brief idle bit set in RTP packet*/
67 /** @brief RTP packet is entirely silent */
70 /** @brief Raw sample data
72 * Only the first @p nsamples samples are defined; the rest is uninitialized
75 uint16_t samples_raw[MAXSAMPLES];
78 /** @brief Structure of free packet list */
81 union free_packet *next;
84 /** @brief Return true iff \f$a < b\f$ in sequence-space arithmetic
86 * Specifically it returns true if \f$(a-b) mod 2^{32} < 2^{31}\f$.
88 * See also lt_packet().
90 static inline int lt(uint32_t a, uint32_t b) {
91 return (uint32_t)(a - b) & 0x80000000;
94 /** @brief Return true iff a >= b in sequence-space arithmetic */
95 static inline int ge(uint32_t a, uint32_t b) {
99 /** @brief Return true iff a > b in sequence-space arithmetic */
100 static inline int gt(uint32_t a, uint32_t b) {
104 /** @brief Return true iff a <= b in sequence-space arithmetic */
105 static inline int le(uint32_t a, uint32_t b) {
109 /** @brief Ordering for packets, used by @ref pheap */
110 static inline int lt_packet(const struct packet *a, const struct packet *b) {
111 return lt(a->timestamp, b->timestamp);
114 /** @brief Return true if @p p contains @p timestamp
116 * Containment implies that a sample @p timestamp exists within the packet.
118 static inline int contains(const struct packet *p, uint32_t timestamp) {
119 const uint32_t packet_start = p->timestamp;
120 const uint32_t packet_end = p->timestamp + p->nsamples;
122 return (ge(timestamp, packet_start)
123 && lt(timestamp, packet_end));
127 * @brief Binary heap of packets ordered by timestamp */
128 HEAP_TYPE(pheap, struct packet *, lt_packet);
130 struct packet *playrtp_new_packet(void);
131 void playrtp_free_packet(struct packet *p);
132 void playrtp_fill_buffer(void);
133 struct packet *playrtp_next_packet(void);
135 extern struct packet *received_packets;
136 extern struct packet **received_tail;
137 extern pthread_mutex_t receive_lock;
138 extern pthread_cond_t receive_cond;
139 extern uint32_t nreceived;
140 extern struct pheap packets;
141 extern volatile uint32_t nsamples;
142 extern uint32_t next_timestamp;
144 extern pthread_mutex_t lock;
145 extern pthread_cond_t cond;
146 extern unsigned minbuffer;
148 extern int16_t *dump_buffer;
149 extern size_t dump_index;
150 extern size_t dump_size;
152 void playrtp_oss(void), playrtp_alsa(void), playrtp_coreaudio(void);
154 #endif /* PLAYRTP_H */