chiark / gitweb /
Further ALSA underrun fiddling.
[disorder] / clients / playrtp.h
CommitLineData
8e3fe3d8 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 */
45struct 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 */
77union 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 */
88static 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 */
93static 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 */
98static 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 */
103static 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 */
108static inline int lt_packet(const struct packet *a, const struct packet *b) {
109 return lt(a->timestamp, b->timestamp);
110}
111
c593cf7c 112/** @brief Return true if @p p contains @p timestamp
113 *
114 * Containment implies that a sample @p timestamp exists within the packet.
115 */
116static 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
8e3fe3d8 124/** @struct pheap
125 * @brief Binary heap of packets ordered by timestamp */
126HEAP_TYPE(pheap, struct packet *, lt_packet);
127
c593cf7c 128struct packet *playrtp_new_packet(void);
129void playrtp_free_packet(struct packet *p);
130void playrtp_fill_buffer(void);
131struct packet *playrtp_next_packet(void);
8e3fe3d8 132
133extern const char *device;
134extern struct packet *received_packets;
135extern struct packet **received_tail;
136extern pthread_mutex_t receive_lock;
137extern pthread_cond_t receive_cond;
138extern uint32_t nreceived;
139extern struct pheap packets;
140extern volatile uint32_t nsamples;
141extern uint32_t next_timestamp;
142extern int active;
143extern pthread_mutex_t lock;
144extern pthread_cond_t cond;
c593cf7c 145extern unsigned minbuffer;
8e3fe3d8 146
e9b635a3
RK
147extern int16_t *dump_buffer;
148extern size_t dump_index;
149extern size_t dump_size;
150
c593cf7c 151void playrtp_oss(void), playrtp_alsa(void), playrtp_coreaudio(void);
8e3fe3d8 152
153#endif /* PLAYRTP_H */
154
155/*
156Local Variables:
157c-basic-offset:2
158comment-column:40
159fill-column:79
160indent-tabs-mode:nil
161End:
162*/