chiark / gitweb /
not 4.3 any more
[disorder] / clients / playrtp.h
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 3 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,
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  * 
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/>.
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  */
43 struct 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 */
75 union 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  */
86 static 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 */
91 static 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 */
96 static 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 */
101 static 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 */
106 static inline int lt_packet(const struct packet *a, const struct packet *b) {
107   return lt(a->timestamp, b->timestamp);
108 }
109
110 /** @brief Return true if @p p contains @p timestamp
111  *
112  * Containment implies that a sample @p timestamp exists within the packet.
113  */
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;
117
118   return (ge(timestamp, packet_start)
119           && lt(timestamp, packet_end));
120 }
121
122 /** @struct pheap 
123  * @brief Binary heap of packets ordered by timestamp */
124 HEAP_TYPE(pheap, struct packet *, lt_packet);
125
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);
130
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;
140 extern int active;
141 extern pthread_mutex_t lock;
142 extern pthread_cond_t cond;
143 extern unsigned minbuffer;
144
145 extern int16_t *dump_buffer;
146 extern size_t dump_index;
147 extern size_t dump_size;
148
149 void playrtp_oss(void), playrtp_alsa(void), playrtp_coreaudio(void);
150
151 #endif /* PLAYRTP_H */
152
153 /*
154 Local Variables:
155 c-basic-offset:2
156 comment-column:40
157 fill-column:79
158 indent-tabs-mode:nil
159 End:
160 */