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