]> git.ipfire.org Git - thirdparty/shairport-sync.git/blob - player.h
Update check_classic_systemd_full.yml
[thirdparty/shairport-sync.git] / player.h
1 #ifndef _PLAYER_H
2 #define _PLAYER_H
3
4 #include <arpa/inet.h>
5 #include <pthread.h>
6
7 #include "config.h"
8 #include "definitions.h"
9
10 #ifdef CONFIG_MBEDTLS
11 #include <mbedtls/aes.h>
12 #include <mbedtls/havege.h>
13 #endif
14
15 #ifdef CONFIG_POLARSSL
16 #include <polarssl/aes.h>
17 #include <polarssl/havege.h>
18 #endif
19
20 #ifdef CONFIG_OPENSSL
21 #include <openssl/aes.h>
22 #endif
23
24 #ifdef CONFIG_AIRPLAY_2
25 #include "pair_ap/pair.h"
26 #include <plist/plist.h>
27 #endif
28
29 #include "alac.h"
30 #include "audio.h"
31
32 #define time_ping_history_power_of_two 7
33 // this must now be zero, otherwise bad things will happen
34 #define time_ping_history \
35 (1 << time_ping_history_power_of_two) // 2^7 is 128. At 1 per three seconds, approximately six
36 // minutes of records
37 typedef struct time_ping_record {
38 uint64_t dispersion;
39 uint64_t local_time;
40 uint64_t remote_time;
41 int sequence_number;
42 int chosen;
43 } time_ping_record;
44
45 // these are for reporting the status of the clock
46 typedef enum {
47 clock_no_anchor_info,
48 clock_ok,
49 clock_service_unavailable,
50 clock_access_error,
51 clock_data_unavailable,
52 clock_no_master,
53 clock_version_mismatch,
54 clock_not_synchronised,
55 clock_not_valid,
56 clock_not_ready,
57 } clock_status_t;
58
59 typedef uint16_t seq_t;
60
61 typedef struct audio_buffer_entry { // decoded audio packets
62 uint8_t ready;
63 uint8_t status; // flags
64 uint16_t resend_request_number;
65 signed short *data;
66 seq_t sequence_number;
67 uint64_t initialisation_time; // the time the packet was added or the time it was noticed the
68 // packet was missing
69 uint64_t resend_time; // time of last resend request or zero
70 uint32_t given_timestamp; // for debugging and checking
71 int length; // the length of the decoded data
72 } abuf_t;
73
74 typedef struct stats { // statistics for running averages
75 int64_t sync_error, correction, drift;
76 } stats_t;
77
78 // default buffer size
79 // This needs to be a power of 2 because of the way BUFIDX(seqno) works.
80 // 512 is the minimum for normal operation -- it gives 512*352/44100 or just over 4 seconds of
81 // buffers.
82 // For at least 10 seconds, you need to go to 2048.
83 // Resend requests will be spaced out evenly in the latency period, subject to a minimum interval of
84 // about 0.25 seconds.
85 // Each buffer occupies 352*4 bytes plus about, say, 64 bytes of overhead in various places, say
86 // roughly 1,500 bytes per buffer.
87 // Thus, 2048 buffers will occupy about 3 megabytes -- no big deal in a normal machine but maybe a
88 // problem in an embedded device.
89
90 #define BUFFER_FRAMES 1024
91
92 typedef enum {
93 ast_unknown,
94 ast_uncompressed, // L16/44100/2
95 ast_apple_lossless,
96 } audio_stream_type;
97
98 typedef struct {
99 int encrypted;
100 uint8_t aesiv[16], aeskey[16];
101 int32_t fmtp[12];
102 audio_stream_type type;
103 } stream_cfg;
104
105 // the following is used even when not built for AirPlay 2
106 typedef enum {
107 unspecified_stream_category = 0,
108 ptp_stream,
109 ntp_stream,
110 remote_control_stream,
111 classic_airplay_stream
112 } airplay_stream_c; // "c" for category
113
114 #ifdef CONFIG_AIRPLAY_2
115 typedef enum { ts_ntp, ts_ptp } timing_t;
116 typedef enum { ap_1, ap_2 } airplay_t;
117 typedef enum { realtime_stream, buffered_stream } airplay_stream_t;
118
119 typedef struct {
120 uint8_t *data;
121 size_t length;
122 size_t size;
123 } sized_buffer;
124
125 typedef struct {
126 struct pair_cipher_context *cipher_ctx;
127 sized_buffer encrypted_read_buffer;
128 sized_buffer plaintext_read_buffer;
129 int is_encrypted;
130 } pair_cipher_bundle; // cipher context and buffers
131
132 typedef struct {
133 struct pair_setup_context *setup_ctx;
134 struct pair_verify_context *verify_ctx;
135 pair_cipher_bundle control_cipher_bundle;
136 } ap2_pairing;
137
138 // flush requests are stored in order of flushFromSeq
139 // on the basis that block numbers are monotonic modulo 2^24
140 typedef struct flush_request_t {
141 int flushNow; // if true, the flushFrom stuff is invalid
142 uint32_t flushFromSeq;
143 uint32_t flushFromTS;
144 uint32_t flushUntilSeq;
145 uint32_t flushUntilTS;
146 struct flush_request_t *next;
147 } flush_request_t;
148
149 #endif
150
151 typedef struct {
152 int connection_number; // for debug ID purposes, nothing else...
153 int resend_interval; // this is really just for debugging
154 int rtsp_link_is_idle; // if true, this indicates if the client asleep
155 char *UserAgent; // free this on teardown
156 int AirPlayVersion; // zero if not an AirPlay session. Used to help calculate latency
157 int latency_warning_issued;
158 uint32_t latency; // the actual latency used for this play session
159 uint32_t minimum_latency; // set if an a=min-latency: line appears in the ANNOUNCE message; zero
160 // otherwise
161 uint32_t maximum_latency; // set if an a=max-latency: line appears in the ANNOUNCE message; zero
162 // otherwise
163 int software_mute_enabled; // if we don't have a real mute that we can use
164 int fd;
165 int authorized; // set if a password is required and has been supplied
166 char *auth_nonce; // the session nonce, if needed
167 stream_cfg stream;
168 SOCKADDR remote, local;
169 volatile int stop;
170 volatile int running;
171 volatile uint64_t watchdog_bark_time;
172 volatile int watchdog_barks; // number of times the watchdog has timed out and done something
173
174 uint64_t playstart;
175 uint64_t connection_start_time; // the time the device is selected, which could be a long time
176 // before a play
177 pthread_t thread, timer_requester, rtp_audio_thread, rtp_control_thread, rtp_timing_thread,
178 player_watchdog_thread;
179
180 // buffers to delete on exit
181 int32_t *tbuf;
182 int32_t *sbuf;
183 char *outbuf;
184
185 // for generating running statistics...
186
187 stats_t *statistics;
188
189 // for holding the output rate information until printed out at the end of a session
190 double raw_frame_rate;
191 double corrected_frame_rate;
192 int frame_rate_valid;
193
194 // for holding input rate information until printed out at the end of a session
195
196 double input_frame_rate;
197 int input_frame_rate_starting_point_is_valid;
198
199 uint64_t frames_inward_measurement_start_time;
200 uint32_t frames_inward_frames_received_at_measurement_start_time;
201
202 uint64_t frames_inward_measurement_time;
203 uint32_t frames_inward_frames_received_at_measurement_time;
204
205 // other stuff...
206 pthread_t *player_thread;
207 abuf_t audio_buffer[BUFFER_FRAMES];
208 unsigned int max_frames_per_packet, input_num_channels, input_bit_depth, input_rate;
209 int input_bytes_per_frame, output_bytes_per_frame, output_sample_ratio;
210 int max_frame_size_change;
211 int64_t previous_random_number;
212 alac_file *decoder_info;
213 uint64_t packet_count;
214 uint64_t packet_count_since_flush;
215 int connection_state_to_output;
216 uint64_t first_packet_time_to_play;
217 int64_t time_since_play_started; // nanoseconds
218 // stats
219 uint64_t missing_packets, late_packets, too_late_packets, resend_requests;
220 int decoder_in_use;
221 // debug variables
222 int32_t last_seqno_read;
223 // mutexes and condition variables
224 pthread_cond_t flowcontrol;
225 pthread_mutex_t ab_mutex, flush_mutex, volume_control_mutex, player_create_delete_mutex;
226
227 int fix_volume;
228 double own_airplay_volume;
229 int own_airplay_volume_set;
230
231 uint32_t timestamp_epoch, last_timestamp,
232 maximum_timestamp_interval; // timestamp_epoch of zero means not initialised, could start at 2
233 // or 1.
234 int ab_buffering, ab_synced;
235 int64_t first_packet_timestamp;
236 int flush_requested;
237 int flush_output_flushed; // true if the output device has been flushed.
238 uint32_t flush_rtp_timestamp;
239 uint64_t time_of_last_audio_packet;
240 seq_t ab_read, ab_write;
241
242 #ifdef CONFIG_MBEDTLS
243 mbedtls_aes_context dctx;
244 #endif
245
246 #ifdef CONFIG_POLARSSL
247 aes_context dctx;
248 #endif
249
250 int amountStuffed;
251
252 int32_t framesProcessedInThisEpoch;
253 int32_t framesGeneratedInThisEpoch;
254 int32_t correctionsRequestedInThisEpoch;
255 int64_t syncErrorsInThisEpoch;
256
257 // RTP stuff
258 // only one RTP session can be active at a time.
259 int rtp_running;
260 uint64_t rtp_time_of_last_resend_request_error_ns;
261
262 char client_ip_string[INET6_ADDRSTRLEN]; // the ip string of the client
263 uint16_t client_rtsp_port;
264 char self_ip_string[INET6_ADDRSTRLEN]; // the ip string being used by this program -- it
265 uint16_t self_rtsp_port; // could be one of many, so we need to know it
266
267 uint32_t self_scope_id; // if it's an ipv6 connection, this will be its scope
268 short connection_ip_family; // AF_INET / AF_INET6
269
270 SOCKADDR rtp_client_control_socket; // a socket pointing to the control port of the client
271 SOCKADDR rtp_client_timing_socket; // a socket pointing to the timing port of the client
272 int audio_socket; // our local [server] audio socket
273 int control_socket; // our local [server] control socket
274 int timing_socket; // local timing socket
275
276 uint16_t remote_control_port;
277 uint16_t remote_timing_port;
278 uint16_t local_audio_port;
279 uint16_t local_control_port;
280 uint16_t local_timing_port;
281
282 int64_t latency_delayed_timestamp; // this is for debugging only...
283
284 // this is what connects an rtp timestamp to the remote time
285
286 int udp_clock_is_initialised;
287 int udp_clock_sender_is_initialised;
288
289 int anchor_remote_info_is_valid;
290
291 // these can be modified if the master clock changes over time
292 uint64_t anchor_clock;
293 uint64_t anchor_time; // this is the time according to the clock
294 uint32_t anchor_rtptime;
295
296 // these are used to identify when the master clock becomes equal to the
297 // actual anchor clock information, so it can be used to avoid accumulating errors
298 uint64_t actual_anchor_clock;
299 uint64_t actual_anchor_time;
300 uint32_t actual_anchor_rtptime;
301
302 clock_status_t clock_status;
303
304 airplay_stream_c
305 airplay_stream_category; // is it a remote control stream or a normal "full service" stream?
306 // (will be unspecified if not build for AirPlay 2)
307
308 #ifdef CONFIG_AIRPLAY_2
309 char *airplay_gid; // UUID in the Bonjour advertisement -- if NULL, the group UUID is the same as
310 // the pi UUID
311 airplay_t airplay_type; // are we using AirPlay 1 or AirPlay 2 protocol on this connection?
312 airplay_stream_t airplay_stream_type; // is it realtime audio or buffered audio...
313 timing_t timing_type; // are we using NTP or PTP on this connection?
314
315 pthread_t *rtp_event_thread;
316 pthread_t *rtp_data_thread;
317 pthread_t rtp_ap2_control_thread;
318 pthread_t rtp_realtime_audio_thread;
319 pthread_t rtp_buffered_audio_thread;
320
321 int last_anchor_info_is_valid;
322 uint32_t last_anchor_rtptime;
323 uint64_t last_anchor_local_time;
324 uint64_t last_anchor_time_of_update;
325 uint64_t last_anchor_validity_start_time;
326
327 ssize_t ap2_audio_buffer_size;
328 ssize_t ap2_audio_buffer_minimum_size;
329 flush_request_t *flush_requests; // if non-null, there are flush requests, mutex protected
330 int ap2_flush_requested;
331 int ap2_flush_from_valid;
332 uint32_t ap2_flush_from_rtp_timestamp;
333 uint32_t ap2_flush_from_sequence_number;
334 uint32_t ap2_flush_until_rtp_timestamp;
335 uint32_t ap2_flush_until_sequence_number;
336 int ap2_rate; // protect with flush mutex, 0 means don't play, 1 means play
337 int ap2_play_enabled; // protect with flush mutex
338
339 ap2_pairing ap2_pairing_context;
340
341 int event_socket;
342 int data_socket;
343 SOCKADDR ap2_remote_control_socket_addr; // a socket pointing to the control port of the client
344 socklen_t ap2_remote_control_socket_addr_length;
345 int ap2_control_socket;
346 int realtime_audio_socket;
347 int buffered_audio_socket;
348
349 uint16_t local_data_port;
350 uint16_t local_event_port;
351 uint16_t local_ap2_control_port;
352 uint16_t local_realtime_audio_port;
353 uint16_t local_buffered_audio_port;
354
355 uint64_t audio_format;
356 uint64_t compression;
357 unsigned char *session_key; // needs to be free'd at the end
358 uint64_t frames_packet;
359 uint64_t type;
360 uint64_t networkTimeTimelineID; // the clock ID used by the player
361 uint8_t groupContainsGroupLeader; // information coming from the SETUP
362 #endif
363
364 // used as the initials values for calculating the rate at which the source thinks it's sending
365 // frames
366 uint32_t initial_reference_timestamp;
367 uint64_t initial_reference_time;
368 double remote_frame_rate;
369
370 // the ratio of the following should give us the operating rate, nominally 44,100
371 int64_t reference_to_previous_frame_difference;
372 uint64_t reference_to_previous_time_difference;
373
374 // debug variables
375 int request_sent;
376
377 int time_ping_count;
378 struct time_ping_record time_pings[time_ping_history];
379
380 uint64_t departure_time; // dangerous -- this assumes that there will never be two timing
381 // request in flight at the same time
382
383 pthread_mutex_t reference_time_mutex;
384 pthread_mutex_t watchdog_mutex;
385
386 double local_to_remote_time_gradient; // if no drift, this would be exactly 1.0; likely it's
387 // slightly above or below.
388 int local_to_remote_time_gradient_sample_count; // the number of samples used to calculate the
389 // gradient
390 // add the following to the local time to get the remote time modulo 2^64
391 uint64_t local_to_remote_time_difference; // used to switch between local and remote clocks
392 uint64_t local_to_remote_time_difference_measurement_time; // when the above was calculated
393
394 int last_stuff_request;
395
396 // int64_t play_segment_reference_frame;
397 // uint64_t play_segment_reference_frame_remote_time;
398
399 int32_t buffer_occupancy; // allow it to be negative because seq_diff may be negative
400 int64_t session_corrections;
401
402 int play_number_after_flush;
403
404 // remote control stuff. The port to which to send commands is not specified, so you have to use
405 // mdns to find it.
406 // at present, only avahi can do this
407
408 char *dacp_id; // id of the client -- used to find the port to be used
409 // uint16_t dacp_port; // port on the client to send remote control messages to, else
410 // zero
411 char *dacp_active_remote; // key to send to the remote controller
412 void *dapo_private_storage; // this is used for compatibility, if dacp stuff isn't enabled.
413
414 int enable_dither; // needed for filling silences before play actually starts
415 uint64_t dac_buffer_queue_minimum_length;
416 } rtsp_conn_info;
417
418 extern pthread_mutex_t principal_conn_lock;
419 extern int statistics_row; // will be reset to zero when debug level changes or statistics enabled
420
421 void reset_buffer(rtsp_conn_info *conn);
422
423 void get_audio_buffer_size_and_occupancy(unsigned int *size, unsigned int *occupancy,
424 rtsp_conn_info *conn);
425
426 int32_t modulo_32_offset(uint32_t from, uint32_t to);
427
428 void ab_resync(rtsp_conn_info *conn);
429
430 int player_prepare_to_play(rtsp_conn_info *conn);
431 int player_play(rtsp_conn_info *conn);
432 int player_stop(rtsp_conn_info *conn);
433
434 void player_volume(double f, rtsp_conn_info *conn);
435 void player_volume_without_notification(double f, rtsp_conn_info *conn);
436 void player_flush(uint32_t timestamp, rtsp_conn_info *conn);
437 // void player_full_flush(rtsp_conn_info *conn);
438 void player_put_packet(int original_format, seq_t seqno, uint32_t actual_timestamp, uint8_t *data,
439 int len, rtsp_conn_info *conn);
440 int64_t monotonic_timestamp(uint32_t timestamp,
441 rtsp_conn_info *conn); // add an epoch to the timestamp. The monotonic
442 // timestamp guaranteed to start between 2^32 2^33
443 // frames and continue up to 2^64 frames
444 // which is about 2*10^8 * 1,000 seconds at 384,000 frames per second -- about 2 trillion seconds.
445 // assumes, without checking, that successive timestamps in a series always span an interval of less
446 // than one minute.
447
448 double suggested_volume(rtsp_conn_info *conn); // volume suggested for the connection
449
450 #endif //_PLAYER_H