chiark / gitweb /
Merge playlist support.
[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    * - @ref SILENT - packet is entirely silent
62    */
63   unsigned flags;
64 /** @brief idle bit set in RTP packet*/
65 #define IDLE 0x0001
66
67 /** @brief RTP packet is entirely silent */
68 #define SILENT 0x0002
69
70   /** @brief Raw sample data
71    *
72    * Only the first @p nsamples samples are defined; the rest is uninitialized
73    * data.
74    */
75   uint16_t samples_raw[MAXSAMPLES];
76 };
77
78 /** @brief Structure of free packet list */
79 union free_packet {
80   struct packet p;
81   union free_packet *next;
82 };
83
84 /** @brief Return true iff \f$a < b\f$ in sequence-space arithmetic
85  *
86  * Specifically it returns true if \f$(a-b) mod 2^{32} < 2^{31}\f$.
87  *
88  * See also lt_packet().
89  */
90 static inline int lt(uint32_t a, uint32_t b) {
91   return (uint32_t)(a - b) & 0x80000000;
92 }
93
94 /** @brief Return true iff a >= b in sequence-space arithmetic */
95 static inline int ge(uint32_t a, uint32_t b) {
96   return !lt(a, b);
97 }
98
99 /** @brief Return true iff a > b in sequence-space arithmetic */
100 static inline int gt(uint32_t a, uint32_t b) {
101   return lt(b, a);
102 }
103
104 /** @brief Return true iff a <= b in sequence-space arithmetic */
105 static inline int le(uint32_t a, uint32_t b) {
106   return !lt(b, a);
107 }
108
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);
112 }
113
114 /** @brief Return true if @p p contains @p timestamp
115  *
116  * Containment implies that a sample @p timestamp exists within the packet.
117  */
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;
121
122   return (ge(timestamp, packet_start)
123           && lt(timestamp, packet_end));
124 }
125
126 /** @struct pheap 
127  * @brief Binary heap of packets ordered by timestamp */
128 HEAP_TYPE(pheap, struct packet *, lt_packet);
129
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);
134
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;
143 extern int active;
144 extern pthread_mutex_t lock;
145 extern pthread_cond_t cond;
146 extern unsigned minbuffer;
147
148 extern int16_t *dump_buffer;
149 extern size_t dump_index;
150 extern size_t dump_size;
151
152 void playrtp_oss(void), playrtp_alsa(void), playrtp_coreaudio(void);
153
154 #endif /* PLAYRTP_H */
155
156 /*
157 Local Variables:
158 c-basic-offset:2
159 comment-column:40
160 fill-column:79
161 indent-tabs-mode:nil
162 End:
163 */