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 #include "alac.h" 25 #include "audio.h" 26 27 #define time_ping_history_power_of_two 7 28 #define time_ping_history \ 29 (1 << time_ping_history_power_of_two) // 2^7 is 128. At 1 per three seconds, approximately six 30 // minutes of records 31 32 typedef struct time_ping_record { 33 uint64_t dispersion; 34 uint64_t local_time; 35 uint64_t remote_time; 36 int sequence_number; 37 int chosen; 38 } time_ping_record; 39 40 typedef uint16_t seq_t; 41 42 typedef struct audio_buffer_entry { // decoded audio packets 43 uint8_t ready; 44 uint8_t status; // flags 45 uint16_t resend_request_number; 46 signed short *data; 47 seq_t sequence_number; 48 uint64_t initialisation_time; // the time the packet was added or the time it was noticed the 49 // packet was missing 50 uint64_t resend_time; // time of last resend request or zero 51 uint32_t given_timestamp; // for debugging and checking 52 int length; // the length of the decoded data 53 } abuf_t; 54 55 typedef struct stats { // statistics for running averages 56 int64_t sync_error, correction, drift; 57 } stats_t; 58 59 // default buffer size 60 // This needs to be a power of 2 because of the way BUFIDX(seqno) works. 61 // 512 is the minimum for normal operation -- it gives 512*352/44100 or just over 4 seconds of 62 // buffers. 63 // For at least 10 seconds, you need to go to 2048. 64 // Resend requests will be spaced out evenly in the latency period, subject to a minimum interval of 65 // about 0.25 seconds. 66 // Each buffer occupies 352*4 bytes plus about, say, 64 bytes of overhead in various places, say 67 // roughly 1,500 bytes per buffer. 68 // Thus, 2048 buffers will occupy about 3 megabytes -- no big deal in a normal machine but maybe a 69 // problem in an embedded device. 70 71 #define BUFFER_FRAMES 1024 72 73 typedef enum { 74 ast_unknown, 75 ast_uncompressed, // L16/44100/2 76 ast_apple_lossless, 77 } audio_stream_type; 78 79 typedef struct { 80 int encrypted; 81 uint8_t aesiv[16], aeskey[16]; 82 int32_t fmtp[12]; 83 audio_stream_type type; 84 } stream_cfg; 85 86 typedef struct { 87 int connection_number; // for debug ID purposes, nothing else... 88 int resend_interval; // this is really just for debugging 89 char *UserAgent; // free this on teardown 90 int AirPlayVersion; // zero if not an AirPlay session. Used to help calculate latency 91 uint32_t latency; // the actual latency used for this play session 92 uint32_t minimum_latency; // set if an a=min-latency: line appears in the ANNOUNCE message; zero 93 // otherwise 94 uint32_t maximum_latency; // set if an a=max-latency: line appears in the ANNOUNCE message; zero 95 // otherwise 96 int software_mute_enabled; // if we don't have a real mute that we can use 97 int unachievable_audio_backend_latency_offset_notified; // set when a latency warning is issued 98 99 int fd; 100 int authorized; // set if a password is required and has been supplied 101 char *auth_nonce; // the session nonce, if needed 102 stream_cfg stream; 103 SOCKADDR remote, local; 104 volatile int stop; 105 volatile int running; 106 volatile uint64_t watchdog_bark_time; 107 volatile int watchdog_barks; // number of times the watchdog has timed out and done something 108 int unfixable_error_reported; // set when an unfixable error command has been executed. 109 110 time_t playstart; 111 pthread_t thread, timer_requester, rtp_audio_thread, rtp_control_thread, rtp_timing_thread, 112 player_watchdog_thread; 113 114 // buffers to delete on exit 115 signed short *tbuf; 116 int32_t *sbuf; 117 char *outbuf; 118 119 // for generating running statistics... 120 121 stats_t *statistics; 122 123 // for holding the output rate information until printed out at the end of a session 124 double frame_rate; 125 int frame_rate_status; 126 127 // for holding input rate information until printed out at the end of a session 128 129 double input_frame_rate; 130 int input_frame_rate_starting_point_is_valid; 131 132 uint64_t frames_inward_measurement_start_time; 133 uint32_t frames_inward_frames_received_at_measurement_start_time; 134 135 uint64_t frames_inward_measurement_time; 136 uint32_t frames_inward_frames_received_at_measurement_time; 137 138 // other stuff... 139 pthread_t *player_thread; 140 abuf_t audio_buffer[BUFFER_FRAMES]; 141 unsigned int max_frames_per_packet, input_num_channels, input_bit_depth, input_rate; 142 int input_bytes_per_frame, output_bytes_per_frame, output_sample_ratio; 143 int max_frame_size_change; 144 int64_t previous_random_number; 145 alac_file *decoder_info; 146 uint64_t packet_count; 147 uint64_t packet_count_since_flush; 148 int connection_state_to_output; 149 uint64_t first_packet_time_to_play; 150 int64_t time_since_play_started; // nanoseconds 151 // stats 152 uint64_t missing_packets, late_packets, too_late_packets, resend_requests; 153 int decoder_in_use; 154 // debug variables 155 int32_t last_seqno_read; 156 // mutexes and condition variables 157 pthread_cond_t flowcontrol; 158 pthread_mutex_t ab_mutex, flush_mutex, volume_control_mutex; 159 int fix_volume; 160 uint32_t timestamp_epoch, last_timestamp, 161 maximum_timestamp_interval; // timestamp_epoch of zero means not initialised, could start at 2 162 // or 1. 163 int ab_buffering, ab_synced; 164 int64_t first_packet_timestamp; 165 int flush_requested; 166 int flush_output_flushed; // true if the output device has been flushed. 167 uint32_t flush_rtp_timestamp; 168 uint64_t time_of_last_audio_packet; 169 seq_t ab_read, ab_write; 170 171 #ifdef CONFIG_MBEDTLS 172 mbedtls_aes_context dctx; 173 #endif 174 175 #ifdef CONFIG_POLARSSL 176 aes_context dctx; 177 #endif 178 179 #ifdef CONFIG_OPENSSL 180 AES_KEY aes; 181 #endif 182 183 int amountStuffed; 184 185 int32_t framesProcessedInThisEpoch; 186 int32_t framesGeneratedInThisEpoch; 187 int32_t correctionsRequestedInThisEpoch; 188 int64_t syncErrorsInThisEpoch; 189 190 // RTP stuff 191 // only one RTP session can be active at a time. 192 int rtp_running; 193 uint64_t rtp_time_of_last_resend_request_error_ns; 194 195 char client_ip_string[INET6_ADDRSTRLEN]; // the ip string pointing to the client 196 char self_ip_string[INET6_ADDRSTRLEN]; // the ip string being used by this program -- it 197 // could be one of many, so we need to know it 198 uint32_t self_scope_id; // if it's an ipv6 connection, this will be its scope 199 short connection_ip_family; // AF_INET / AF_INET6 200 201 SOCKADDR rtp_client_control_socket; // a socket pointing to the control port of the client 202 SOCKADDR rtp_client_timing_socket; // a socket pointing to the timing port of the client 203 int audio_socket; // our local [server] audio socket 204 int control_socket; // our local [server] control socket 205 int timing_socket; // local timing socket 206 207 uint16_t remote_control_port; 208 uint16_t remote_timing_port; 209 uint16_t local_audio_port; 210 uint16_t local_control_port; 211 uint16_t local_timing_port; 212 213 int64_t latency_delayed_timestamp; // this is for debugging only... 214 215 // this is what connects an rtp timestamp to the remote time 216 217 uint32_t reference_timestamp; 218 uint64_t remote_reference_timestamp_time; 219 220 // used as the initials values for calculating the rate at which the source thinks it's sending 221 // frames 222 uint32_t initial_reference_timestamp; 223 uint64_t initial_reference_time; 224 double remote_frame_rate; 225 226 // the ratio of the following should give us the operating rate, nominally 44,100 227 int64_t reference_to_previous_frame_difference; 228 uint64_t reference_to_previous_time_difference; 229 230 // debug variables 231 int request_sent; 232 233 int time_ping_count; 234 struct time_ping_record time_pings[time_ping_history]; 235 236 uint64_t departure_time; // dangerous -- this assumes that there will never be two timing 237 // request in flight at the same time 238 239 pthread_mutex_t reference_time_mutex; 240 pthread_mutex_t watchdog_mutex; 241 242 double local_to_remote_time_gradient; // if no drift, this would be exactly 1.0; likely it's 243 // slightly above or below. 244 int local_to_remote_time_gradient_sample_count; // the number of samples used to calculate the 245 // gradient 246 // add the following to the local time to get the remote time modulo 2^64 247 uint64_t local_to_remote_time_difference; // used to switch between local and remote clocks 248 uint64_t local_to_remote_time_difference_measurement_time; // when the above was calculated 249 250 int last_stuff_request; 251 252 // int64_t play_segment_reference_frame; 253 // uint64_t play_segment_reference_frame_remote_time; 254 255 int32_t buffer_occupancy; // allow it to be negative because seq_diff may be negative 256 int64_t session_corrections; 257 258 int play_number_after_flush; 259 260 // remote control stuff. The port to which to send commands is not specified, so you have to use 261 // mdns to find it. 262 // at present, only avahi can do this 263 264 char *dacp_id; // id of the client -- used to find the port to be used 265 // uint16_t dacp_port; // port on the client to send remote control messages to, else 266 // zero 267 char *dacp_active_remote; // key to send to the remote controller 268 void *dapo_private_storage; // this is used for compatibility, if dacp stuff isn't enabled. 269 270 int enable_dither; // needed for filling silences before play actually starts 271 uint64_t dac_buffer_queue_minimum_length; 272 } rtsp_conn_info; 273 274 uint32_t modulo_32_offset(uint32_t from, uint32_t to); 275 uint64_t modulo_64_offset(uint64_t from, uint64_t to); 276 277 int player_play(rtsp_conn_info *conn); 278 int player_stop(rtsp_conn_info *conn); 279 280 void player_volume(double f, rtsp_conn_info *conn); 281 void player_volume_without_notification(double f, rtsp_conn_info *conn); 282 void player_flush(uint32_t timestamp, rtsp_conn_info *conn); 283 void player_put_packet(seq_t seqno, uint32_t actual_timestamp, uint8_t *data, int len, 284 rtsp_conn_info *conn); 285 int64_t monotonic_timestamp(uint32_t timestamp, 286 rtsp_conn_info *conn); // add an epoch to the timestamp. The monotonic 287 // timestamp guaranteed to start between 2^32 2^33 288 // frames and continue up to 2^64 frames 289 // which is about 2*10^8 * 1,000 seconds at 384,000 frames per second -- about 2 trillion seconds. 290 // assumes, without checking, that successive timestamps in a series always span an interval of less 291 // than one minute. 292 293 #endif //_PLAYER_H 294