8 #include "definitions.h"
11 #include <mbedtls/aes.h>
12 #include <mbedtls/havege.h>
15 #ifdef CONFIG_POLARSSL
16 #include <polarssl/aes.h>
17 #include <polarssl/havege.h>
21 #include <openssl/aes.h>
24 #ifdef CONFIG_AIRPLAY_2
25 #include "pair_ap/pair.h"
26 #include <plist/plist.h>
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
37 typedef struct time_ping_record
{
45 // these are for reporting the status of the clock
49 clock_service_unavailable
,
51 clock_data_unavailable
,
53 clock_version_mismatch
,
54 clock_not_synchronised
,
59 typedef uint16_t seq_t
;
61 typedef struct audio_buffer_entry
{ // decoded audio packets
63 uint8_t status
; // flags
64 uint16_t resend_request_number
;
66 seq_t sequence_number
;
67 uint64_t initialisation_time
; // the time the packet was added or the time it was noticed the
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
74 typedef struct stats
{ // statistics for running averages
75 int64_t sync_error
, correction
, drift
;
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
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.
90 #define BUFFER_FRAMES 1024
94 ast_uncompressed
, // L16/44100/2
100 uint8_t aesiv
[16], aeskey
[16];
102 audio_stream_type type
;
105 // the following is used even when not built for AirPlay 2
107 unspecified_stream_category
= 0,
110 remote_control_stream
,
111 classic_airplay_stream
112 } airplay_stream_c
; // "c" for category
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
;
126 struct pair_cipher_context
*cipher_ctx
;
127 sized_buffer encrypted_read_buffer
;
128 sized_buffer plaintext_read_buffer
;
130 } pair_cipher_bundle
; // cipher context and buffers
133 struct pair_setup_context
*setup_ctx
;
134 struct pair_verify_context
*verify_ctx
;
135 pair_cipher_bundle control_cipher_bundle
;
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
;
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
161 uint32_t maximum_latency
; // set if an a=max-latency: line appears in the ANNOUNCE message; zero
163 int software_mute_enabled
; // if we don't have a real mute that we can use
165 int authorized
; // set if a password is required and has been supplied
166 char *auth_nonce
; // the session nonce, if needed
168 SOCKADDR remote
, local
;
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
175 uint64_t connection_start_time
; // the time the device is selected, which could be a long time
177 pthread_t thread
, timer_requester
, rtp_audio_thread
, rtp_control_thread
, rtp_timing_thread
,
178 player_watchdog_thread
;
180 // buffers to delete on exit
185 // for generating running statistics...
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
;
194 // for holding input rate information until printed out at the end of a session
196 double input_frame_rate
;
197 int input_frame_rate_starting_point_is_valid
;
199 uint64_t frames_inward_measurement_start_time
;
200 uint32_t frames_inward_frames_received_at_measurement_start_time
;
202 uint64_t frames_inward_measurement_time
;
203 uint32_t frames_inward_frames_received_at_measurement_time
;
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
219 uint64_t missing_packets
, late_packets
, too_late_packets
, resend_requests
;
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
;
228 double own_airplay_volume
;
229 int own_airplay_volume_set
;
231 uint32_t timestamp_epoch
, last_timestamp
,
232 maximum_timestamp_interval
; // timestamp_epoch of zero means not initialised, could start at 2
234 int ab_buffering
, ab_synced
;
235 int64_t first_packet_timestamp
;
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
;
242 #ifdef CONFIG_MBEDTLS
243 mbedtls_aes_context dctx
;
246 #ifdef CONFIG_POLARSSL
252 int32_t framesProcessedInThisEpoch
;
253 int32_t framesGeneratedInThisEpoch
;
254 int32_t correctionsRequestedInThisEpoch
;
255 int64_t syncErrorsInThisEpoch
;
258 // only one RTP session can be active at a time.
260 uint64_t rtp_time_of_last_resend_request_error_ns
;
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
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
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
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
;
282 int64_t latency_delayed_timestamp
; // this is for debugging only...
284 // this is what connects an rtp timestamp to the remote time
286 int udp_clock_is_initialised
;
287 int udp_clock_sender_is_initialised
;
289 int anchor_remote_info_is_valid
;
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
;
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
;
302 clock_status_t clock_status
;
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)
308 #ifdef CONFIG_AIRPLAY_2
309 char *airplay_gid
; // UUID in the Bonjour advertisement -- if NULL, the group UUID is the same as
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?
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
;
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
;
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
339 ap2_pairing ap2_pairing_context
;
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
;
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
;
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
;
360 uint64_t networkTimeTimelineID
; // the clock ID used by the player
361 uint8_t groupContainsGroupLeader
; // information coming from the SETUP
364 // used as the initials values for calculating the rate at which the source thinks it's sending
366 uint32_t initial_reference_timestamp
;
367 uint64_t initial_reference_time
;
368 double remote_frame_rate
;
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
;
378 struct time_ping_record time_pings
[time_ping_history
];
380 uint64_t departure_time
; // dangerous -- this assumes that there will never be two timing
381 // request in flight at the same time
383 pthread_mutex_t reference_time_mutex
;
384 pthread_mutex_t watchdog_mutex
;
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
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
394 int last_stuff_request
;
396 // int64_t play_segment_reference_frame;
397 // uint64_t play_segment_reference_frame_remote_time;
399 int32_t buffer_occupancy
; // allow it to be negative because seq_diff may be negative
400 int64_t session_corrections
;
402 int play_number_after_flush
;
404 // remote control stuff. The port to which to send commands is not specified, so you have to use
406 // at present, only avahi can do this
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
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.
414 int enable_dither
; // needed for filling silences before play actually starts
415 uint64_t dac_buffer_queue_minimum_length
;
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
421 void reset_buffer(rtsp_conn_info
*conn
);
423 void get_audio_buffer_size_and_occupancy(unsigned int *size
, unsigned int *occupancy
,
424 rtsp_conn_info
*conn
);
426 int32_t modulo_32_offset(uint32_t from
, uint32_t to
);
428 void ab_resync(rtsp_conn_info
*conn
);
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
);
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
448 double suggested_volume(rtsp_conn_info
*conn
); // volume suggested for the connection