1 /*
2  * Apple RTP protocol handler. This file is part of Shairport.
3  * Copyright (c) James Laird 2013
4  * Copyright (c) Mike Brady 2014 -- 2019
5  * All rights reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person
8  * obtaining a copy of this software and associated documentation
9  * files (the "Software"), to deal in the Software without
10  * restriction, including without limitation the rights to use,
11  * copy, modify, merge, publish, distribute, sublicense, and/or
12  * sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  */
27 
28 #include "rtp.h"
29 #include "common.h"
30 #include "player.h"
31 #include "rtsp.h"
32 #include <arpa/inet.h>
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <inttypes.h>
36 #include <math.h>
37 #include <memory.h>
38 #include <netdb.h>
39 #include <netinet/in.h>
40 #include <pthread.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <sys/socket.h>
44 #include <sys/types.h>
45 #include <time.h>
46 #include <unistd.h>
47 
48 struct Nvll {
49   char *name;
50   double value;
51   struct Nvll *next;
52 };
53 
54 typedef struct Nvll nvll;
55 
56 uint64_t local_to_remote_time_jitter;
57 uint64_t local_to_remote_time_jitter_count;
58 
rtp_initialise(rtsp_conn_info * conn)59 void rtp_initialise(rtsp_conn_info *conn) {
60   conn->rtp_time_of_last_resend_request_error_ns = 0;
61   conn->rtp_running = 0;
62   // initialise the timer mutex
63   int rc = pthread_mutex_init(&conn->reference_time_mutex, NULL);
64   if (rc)
65     debug(1, "Error initialising reference_time_mutex.");
66 }
67 
rtp_terminate(rtsp_conn_info * conn)68 void rtp_terminate(rtsp_conn_info *conn) {
69   conn->reference_timestamp = 0;
70   // destroy the timer mutex
71   int rc = pthread_mutex_destroy(&conn->reference_time_mutex);
72   if (rc)
73     debug(1, "Error destroying reference_time_mutex variable.");
74 }
75 
local_to_remote_time_difference_now(rtsp_conn_info * conn)76 uint64_t local_to_remote_time_difference_now(rtsp_conn_info *conn) {
77   // this is an attempt to compensate for clock drift since the last time ping that was used
78   // so, if we have a non-zero clock drift, we will calculate the drift there would
79   // be from the time of the last time ping
80   uint64_t time_since_last_local_to_remote_time_difference_measurement =
81       get_absolute_time_in_ns() - conn->local_to_remote_time_difference_measurement_time;
82 
83   uint64_t result = conn->local_to_remote_time_difference;
84   if (conn->local_to_remote_time_gradient >= 1.0) {
85     result = conn->local_to_remote_time_difference +
86              (uint64_t)((conn->local_to_remote_time_gradient - 1.0) *
87                         time_since_last_local_to_remote_time_difference_measurement);
88   } else {
89     result = conn->local_to_remote_time_difference -
90              (uint64_t)((1.0 - conn->local_to_remote_time_gradient) *
91                         time_since_last_local_to_remote_time_difference_measurement);
92   }
93   return result;
94 }
95 
rtp_audio_receiver_cleanup_handler(void * arg)96 void rtp_audio_receiver_cleanup_handler(__attribute__((unused)) void *arg) {
97   debug(3, "Audio Receiver Cleanup Done.");
98 }
99 
rtp_audio_receiver(void * arg)100 void *rtp_audio_receiver(void *arg) {
101   pthread_cleanup_push(rtp_audio_receiver_cleanup_handler, arg);
102   rtsp_conn_info *conn = (rtsp_conn_info *)arg;
103 
104   int32_t last_seqno = -1;
105   uint8_t packet[2048], *pktp;
106 
107   uint64_t time_of_previous_packet_ns = 0;
108   float longest_packet_time_interval_us = 0.0;
109 
110   // mean and variance calculations from "online_variance" algorithm at
111   // https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Online_algorithm
112 
113   int32_t stat_n = 0;
114   float stat_mean = 0.0;
115   float stat_M2 = 0.0;
116 
117   int frame_count = 0;
118   ssize_t nread;
119   while (1) {
120     nread = recv(conn->audio_socket, packet, sizeof(packet), 0);
121 
122     frame_count++;
123 
124     uint64_t local_time_now_ns = get_absolute_time_in_ns();
125     if (time_of_previous_packet_ns) {
126       float time_interval_us = (local_time_now_ns - time_of_previous_packet_ns) * 0.001;
127       time_of_previous_packet_ns = local_time_now_ns;
128       if (time_interval_us > longest_packet_time_interval_us)
129         longest_packet_time_interval_us = time_interval_us;
130       stat_n += 1;
131       float stat_delta = time_interval_us - stat_mean;
132       stat_mean += stat_delta / stat_n;
133       stat_M2 += stat_delta * (time_interval_us - stat_mean);
134       if (stat_n % 2500 == 0) {
135         debug(2,
136               "Packet reception interval stats: mean, standard deviation and max for the last "
137               "2,500 packets in microseconds: %10.1f, %10.1f, %10.1f.",
138               stat_mean, sqrtf(stat_M2 / (stat_n - 1)), longest_packet_time_interval_us);
139         stat_n = 0;
140         stat_mean = 0.0;
141         stat_M2 = 0.0;
142         time_of_previous_packet_ns = 0;
143         longest_packet_time_interval_us = 0.0;
144       }
145     } else {
146       time_of_previous_packet_ns = local_time_now_ns;
147     }
148 
149     if (nread >= 0) {
150       ssize_t plen = nread;
151       uint8_t type = packet[1] & ~0x80;
152       if (type == 0x60 || type == 0x56) { // audio data / resend
153         pktp = packet;
154         if (type == 0x56) {
155           pktp += 4;
156           plen -= 4;
157         }
158         seq_t seqno = ntohs(*(uint16_t *)(pktp + 2));
159         // increment last_seqno and see if it's the same as the incoming seqno
160 
161         if (type == 0x60) { // regular audio data
162 
163           /*
164           char obf[4096];
165           char *obfp = obf;
166           int obfc;
167           for (obfc=0;obfc<plen;obfc++) {
168             snprintf(obfp, 3, "%02X", pktp[obfc]);
169             obfp+=2;
170           };
171           *obfp=0;
172           debug(1,"Audio Packet Received: \"%s\"",obf);
173           */
174 
175           if (last_seqno == -1)
176             last_seqno = seqno;
177           else {
178             last_seqno = (last_seqno + 1) & 0xffff;
179             // if (seqno != last_seqno)
180             //  debug(3, "RTP: Packets out of sequence: expected: %d, got %d.", last_seqno, seqno);
181             last_seqno = seqno; // reset warning...
182           }
183         } else {
184           debug(3, "Audio Receiver -- Retransmitted Audio Data Packet %u received.", seqno);
185         }
186 
187         uint32_t actual_timestamp = ntohl(*(uint32_t *)(pktp + 4));
188 
189         // uint32_t ssid = ntohl(*(uint32_t *)(pktp + 8));
190         // debug(1, "Audio packet SSID: %08X,%u", ssid,ssid);
191 
192         // if (packet[1]&0x10)
193         //	debug(1,"Audio packet Extension bit set.");
194 
195         pktp += 12;
196         plen -= 12;
197 
198         // check if packet contains enough content to be reasonable
199         if (plen >= 16) {
200           if ((config.diagnostic_drop_packet_fraction == 0.0) ||
201               (drand48() > config.diagnostic_drop_packet_fraction))
202             player_put_packet(seqno, actual_timestamp, pktp, plen, conn);
203           else
204             debug(3, "Dropping audio packet %u to simulate a bad connection.", seqno);
205           continue;
206         }
207         if (type == 0x56 && seqno == 0) {
208           debug(2, "resend-related request packet received, ignoring.");
209           continue;
210         }
211         debug(1, "Audio receiver -- Unknown RTP packet of type 0x%02X length %d seqno %d", type,
212               nread, seqno);
213       }
214       warn("Audio receiver -- Unknown RTP packet of type 0x%02X length %d.", type, nread);
215     } else {
216       debug(1, "Error receiving an audio packet.");
217     }
218   }
219 
220   /*
221   debug(3, "Audio receiver -- Server RTP thread interrupted. terminating.");
222   close(conn->audio_socket);
223   */
224 
225   debug(1, "Audio receiver thread \"normal\" exit -- this can't happen. Hah!");
226   pthread_cleanup_pop(0); // don't execute anything here.
227   debug(2, "Audio receiver thread exit.");
228   pthread_exit(NULL);
229 }
230 
rtp_control_handler_cleanup_handler(void * arg)231 void rtp_control_handler_cleanup_handler(__attribute__((unused)) void *arg) {
232   debug(3, "Control Receiver Cleanup Done.");
233 }
234 
rtp_control_receiver(void * arg)235 void *rtp_control_receiver(void *arg) {
236   pthread_cleanup_push(rtp_control_handler_cleanup_handler, arg);
237   rtsp_conn_info *conn = (rtsp_conn_info *)arg;
238 
239   conn->reference_timestamp = 0; // nothing valid received yet
240   uint8_t packet[2048], *pktp;
241   // struct timespec tn;
242   uint64_t remote_time_of_sync;
243   uint32_t sync_rtp_timestamp;
244   ssize_t nread;
245   while (1) {
246     nread = recv(conn->control_socket, packet, sizeof(packet), 0);
247 
248     if (nread >= 0) {
249 
250       if ((config.diagnostic_drop_packet_fraction == 0.0) ||
251           (drand48() > config.diagnostic_drop_packet_fraction)) {
252 
253         ssize_t plen = nread;
254         if (packet[1] == 0xd4) {                       // sync data
255                                                        /*
256                                                             // the following stanza is for debugging only -- normally commented out.
257                                                             {
258                                                               char obf[4096];
259                                                               char *obfp = obf;
260                                                               int obfc;
261                                                               for (obfc = 0; obfc < plen; obfc++) {
262                                                                 snprintf(obfp, 3, "%02X", packet[obfc]);
263                                                                 obfp += 2;
264                                                               };
265                                                               *obfp = 0;
266 
267 
268                                                               // get raw timestamp information
269                                                               // I think that a good way to understand these timestamps is that
270                                                               // (1) the rtlt below is the timestamp of the frame that should be playing at the
271                                                               // client-time specified in the packet if there was no delay
272                                                               // and (2) that the rt below is the timestamp of the frame that should be playing
273                                                               // at the client-time specified in the packet on this device taking account of
274                                                               // the delay
275                                                               // Thus, (3) the latency can be calculated by subtracting the second from the
276                                                               // first.
277                                                               // There must be more to it -- there something missing.
278 
279                                                               // In addition, it seems that if the value of the short represented by the second
280                                                               // pair of bytes in the packet is 7
281                                                               // then an extra time lag is expected to be added, presumably by
282                                                               // the AirPort Express.
283 
284                                                               // Best guess is that this delay is 11,025 frames.
285 
286                                                               uint32_t rtlt = nctohl(&packet[4]); // raw timestamp less latency
287                                                               uint32_t rt = nctohl(&packet[16]);  // raw timestamp
288 
289                                                               uint32_t fl = nctohs(&packet[2]); //
290 
291                                                               debug(1,"Sync Packet of %d bytes received: \"%s\", flags: %d, timestamps %u and %u,
292                                                           giving a latency of %d frames.",plen,obf,fl,rt,rtlt,rt-rtlt);
293                                                               //debug(1,"Monotonic timestamps are: %" PRId64 " and %" PRId64 "
294                                                           respectively.",monotonic_timestamp(rt, conn),monotonic_timestamp(rtlt, conn));
295                                                             }
296                                                        */
297           if (conn->local_to_remote_time_difference) { // need a time packet to be interchanged
298                                                        // first...
299             uint64_t ps, pn;
300 
301             ps = nctohl(&packet[8]);
302             ps = ps * 1000000000; // this many nanoseconds from the whole seconds
303             pn = nctohl(&packet[12]);
304             pn = pn * 1000000000;
305             pn = pn >> 32; // this many nanoseconds from the fractional part
306             remote_time_of_sync = ps + pn;
307 
308             // debug(1,"Remote Sync Time: " PRIu64 "",remote_time_of_sync);
309 
310             sync_rtp_timestamp = nctohl(&packet[16]);
311             uint32_t rtp_timestamp_less_latency = nctohl(&packet[4]);
312 
313             // debug(1,"Sync timestamp is %u.",ntohl(*((uint32_t *)&packet[16])));
314 
315             if (config.userSuppliedLatency) {
316               if (config.userSuppliedLatency != conn->latency) {
317                 debug(1, "Using the user-supplied latency: %" PRIu32 ".",
318                       config.userSuppliedLatency);
319               }
320               conn->latency = config.userSuppliedLatency;
321             } else {
322 
323               // It seems that the second pair of bytes in the packet indicate whether a fixed
324               // delay of 11,025 frames should be added -- iTunes set this field to 7 and
325               // AirPlay sets it to 4.
326 
327               // However, on older versions of AirPlay, the 11,025 frames seem to be necessary too
328 
329               // The value of 11,025 (0.25 seconds) is a guess based on the "Audio-Latency"
330               // parameter
331               // returned by an AE.
332 
333               // Sigh, it would be nice to have a published protocol...
334 
335               uint16_t flags = nctohs(&packet[2]);
336               uint32_t la = sync_rtp_timestamp - rtp_timestamp_less_latency; // note, this might
337                                                                              // loop around in
338                                                                              // modulo. Not sure if
339                                                                              // you'll get an error!
340               // debug(3, "Latency derived just from the sync packet is %" PRIu32 " frames.", la);
341 
342               if ((flags == 7) || ((conn->AirPlayVersion > 0) && (conn->AirPlayVersion <= 353)) ||
343                   ((conn->AirPlayVersion > 0) && (conn->AirPlayVersion >= 371))) {
344                 la += config.fixedLatencyOffset;
345                 // debug(3, "A fixed latency offset of %d frames has been added, giving a latency of
346                 // "
347                 //         "%" PRId64
348                 //         " frames with flags: %d and AirPlay version %d (triggers if 353 or
349                 //         less).",
350                 //      config.fixedLatencyOffset, la, flags, conn->AirPlayVersion);
351               }
352               if ((conn->maximum_latency) && (conn->maximum_latency < la))
353                 la = conn->maximum_latency;
354               if ((conn->minimum_latency) && (conn->minimum_latency > la))
355                 la = conn->minimum_latency;
356 
357               const uint32_t max_frames = ((3 * BUFFER_FRAMES * 352) / 4) - 11025;
358 
359               if (la > max_frames) {
360                 warn("An out-of-range latency request of %" PRIu32
361                      " frames was ignored. Must be %" PRIu32
362                      " frames or less (44,100 frames per second). "
363                      "Latency remains at %" PRIu32 " frames.",
364                      la, max_frames, conn->latency);
365               } else {
366 
367                 if (la != conn->latency) {
368                   conn->latency = la;
369                   debug(3,
370                         "New latency detected: %" PRIu32 ", sync latency: %" PRIu32
371                         ", minimum latency: %" PRIu32 ", maximum "
372                         "latency: %" PRIu32 ", fixed offset: %" PRIu32 ".",
373                         la, sync_rtp_timestamp - rtp_timestamp_less_latency, conn->minimum_latency,
374                         conn->maximum_latency, config.fixedLatencyOffset);
375                 }
376               }
377             }
378 
379             debug_mutex_lock(&conn->reference_time_mutex, 1000, 0);
380 
381             if (conn->initial_reference_time == 0) {
382               if (conn->packet_count_since_flush > 0) {
383                 conn->initial_reference_time = remote_time_of_sync;
384                 conn->initial_reference_timestamp = sync_rtp_timestamp;
385               }
386             } else {
387               uint64_t remote_frame_time_interval =
388                   conn->remote_reference_timestamp_time -
389                   conn->initial_reference_time; // here, this should never be zero
390               if (remote_frame_time_interval) {
391                 conn->remote_frame_rate =
392                     (1.0E9 * (conn->reference_timestamp - conn->initial_reference_timestamp)) /
393                     remote_frame_time_interval;
394               } else {
395                 conn->remote_frame_rate = 0.0; // use as a flag.
396               }
397             }
398 
399             // this is for debugging
400             uint64_t old_remote_reference_time = conn->remote_reference_timestamp_time;
401             uint32_t old_reference_timestamp = conn->reference_timestamp;
402             // int64_t old_latency_delayed_timestamp = conn->latency_delayed_timestamp;
403 
404             conn->remote_reference_timestamp_time = remote_time_of_sync;
405             // conn->reference_timestamp_time =
406             //    remote_time_of_sync - local_to_remote_time_difference_now(conn);
407             conn->reference_timestamp = sync_rtp_timestamp;
408             conn->latency_delayed_timestamp = rtp_timestamp_less_latency;
409             debug_mutex_unlock(&conn->reference_time_mutex, 0);
410 
411             conn->reference_to_previous_time_difference =
412                 remote_time_of_sync - old_remote_reference_time;
413             if (old_reference_timestamp == 0)
414               conn->reference_to_previous_frame_difference = 0;
415             else
416               conn->reference_to_previous_frame_difference =
417                   sync_rtp_timestamp - old_reference_timestamp;
418           } else {
419             debug(2, "Sync packet received before we got a timing packet back.");
420           }
421         } else if (packet[1] == 0xd6) { // resent audio data in the control path -- whaale only?
422           pktp = packet + 4;
423           plen -= 4;
424           seq_t seqno = ntohs(*(uint16_t *)(pktp + 2));
425           debug(3, "Control Receiver -- Retransmitted Audio Data Packet %u received.", seqno);
426 
427           uint32_t actual_timestamp = ntohl(*(uint32_t *)(pktp + 4));
428 
429           pktp += 12;
430           plen -= 12;
431 
432           // check if packet contains enough content to be reasonable
433           if (plen >= 16) {
434             player_put_packet(seqno, actual_timestamp, pktp, plen, conn);
435             continue;
436           } else {
437             debug(3, "Too-short retransmitted audio packet received in control port, ignored.");
438           }
439         } else
440           debug(1, "Control Receiver -- Unknown RTP packet of type 0x%02X length %d, ignored.",
441                 packet[1], nread);
442       } else {
443         debug(3, "Control Receiver -- dropping a packet to simulate a bad network.");
444       }
445     } else {
446       debug(1, "Control Receiver -- error receiving a packet.");
447     }
448   }
449   debug(1, "Control RTP thread \"normal\" exit -- this can't happen. Hah!");
450   pthread_cleanup_pop(0); // don't execute anything here.
451   debug(2, "Control RTP thread exit.");
452   pthread_exit(NULL);
453 }
454 
rtp_timing_sender_cleanup_handler(void * arg)455 void rtp_timing_sender_cleanup_handler(void *arg) {
456   rtsp_conn_info *conn = (rtsp_conn_info *)arg;
457   debug(3, "Connection %d: Timing Sender Cleanup.", conn->connection_number);
458 }
459 
rtp_timing_sender(void * arg)460 void *rtp_timing_sender(void *arg) {
461   pthread_cleanup_push(rtp_timing_sender_cleanup_handler, arg);
462   rtsp_conn_info *conn = (rtsp_conn_info *)arg;
463   struct timing_request {
464     char leader;
465     char type;
466     uint16_t seqno;
467     uint32_t filler;
468     uint64_t origin, receive, transmit;
469   };
470 
471   uint64_t request_number = 0;
472 
473   struct timing_request req; // *not* a standard RTCP NACK
474 
475   req.leader = 0x80;
476   req.type = 0xd2; // Timing request
477   req.filler = 0;
478   req.seqno = htons(7);
479 
480   conn->time_ping_count = 0;
481   while (1) {
482     // debug(1,"Send a timing request");
483 
484     if (!conn->rtp_running)
485       debug(1, "rtp_timing_sender called without active stream in RTSP conversation thread %d!",
486             conn->connection_number);
487 
488     // debug(1, "Requesting ntp timestamp exchange.");
489 
490     req.filler = 0;
491     req.origin = req.receive = req.transmit = 0;
492 
493     conn->departure_time = get_absolute_time_in_ns();
494     socklen_t msgsize = sizeof(struct sockaddr_in);
495 #ifdef AF_INET6
496     if (conn->rtp_client_timing_socket.SAFAMILY == AF_INET6) {
497       msgsize = sizeof(struct sockaddr_in6);
498     }
499 #endif
500     if ((config.diagnostic_drop_packet_fraction == 0.0) ||
501         (drand48() > config.diagnostic_drop_packet_fraction)) {
502       if (sendto(conn->timing_socket, &req, sizeof(req), 0,
503                  (struct sockaddr *)&conn->rtp_client_timing_socket, msgsize) == -1) {
504         char em[1024];
505         strerror_r(errno, em, sizeof(em));
506         debug(1, "Error %d using send-to to the timing socket: \"%s\".", errno, em);
507       }
508     } else {
509       debug(3, "Timing Sender Thread -- dropping outgoing packet to simulate bad network.");
510     }
511 
512     request_number++;
513 
514     if (request_number <= 6)
515       usleep(300000); // these are thread cancellation points
516     else
517       usleep(3000000);
518   }
519   debug(3, "rtp_timing_sender thread interrupted. This should never happen.");
520   pthread_cleanup_pop(0); // don't execute anything here.
521   pthread_exit(NULL);
522 }
523 
rtp_timing_receiver_cleanup_handler(void * arg)524 void rtp_timing_receiver_cleanup_handler(void *arg) {
525   rtsp_conn_info *conn = (rtsp_conn_info *)arg;
526   debug(3, "Timing Receiver Cleanup.");
527   // walk down the list of DACP / gradient pairs, if any
528   nvll *gradients = config.gradients;
529   if (conn->dacp_id)
530     while ((gradients) && (strcasecmp((const char *)&conn->client_ip_string, gradients->name) != 0))
531       gradients = gradients->next;
532 
533   // if gradients comes out of this non-null, it is pointing to the DACP and it's last-known
534   // gradient
535   if (gradients) {
536     gradients->value = conn->local_to_remote_time_gradient;
537     // debug(1,"Updating a drift of %.2f ppm for \"%s\".", (conn->local_to_remote_time_gradient
538     // - 1.0)*1000000, gradients->name);
539   } else {
540     nvll *new_entry = (nvll *)malloc(sizeof(nvll));
541     if (new_entry) {
542       new_entry->name = strdup((const char *)&conn->client_ip_string);
543       new_entry->value = conn->local_to_remote_time_gradient;
544       new_entry->next = config.gradients;
545       config.gradients = new_entry;
546       // debug(1,"Setting a new drift of %.2f ppm for \"%s\".", (conn->local_to_remote_time_gradient
547       // - 1.0)*1000000, new_entry->name);
548     }
549   }
550 
551   debug(3, "Cancel Timing Requester.");
552   pthread_cancel(conn->timer_requester);
553   int oldState;
554   pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldState);
555   debug(3, "Join Timing Requester.");
556   pthread_join(conn->timer_requester, NULL);
557   debug(3, "Timing Receiver Cleanup Successful.");
558   pthread_setcancelstate(oldState, NULL);
559 }
560 
rtp_timing_receiver(void * arg)561 void *rtp_timing_receiver(void *arg) {
562   pthread_cleanup_push(rtp_timing_receiver_cleanup_handler, arg);
563   rtsp_conn_info *conn = (rtsp_conn_info *)arg;
564 
565   uint8_t packet[2048];
566   ssize_t nread;
567   pthread_create(&conn->timer_requester, NULL, &rtp_timing_sender, arg);
568   //    struct timespec att;
569   uint64_t distant_receive_time, distant_transmit_time, arrival_time, return_time;
570   local_to_remote_time_jitter = 0;
571   local_to_remote_time_jitter_count = 0;
572   // uint64_t first_remote_time = 0;
573   // uint64_t first_local_time = 0;
574 
575   uint64_t first_local_to_remote_time_difference = 0;
576 
577   conn->local_to_remote_time_gradient = 1.0; // initial value.
578   // walk down the list of DACP / gradient pairs, if any
579   nvll *gradients = config.gradients;
580   while ((gradients) && (strcasecmp((const char *)&conn->client_ip_string, gradients->name) != 0))
581     gradients = gradients->next;
582 
583   // if gradients comes out of this non-null, it is pointing to the IP and it's last-known gradient
584   if (gradients) {
585     conn->local_to_remote_time_gradient = gradients->value;
586     // debug(1,"Using a stored drift of %.2f ppm for \"%s\".", (conn->local_to_remote_time_gradient
587     // - 1.0)*1000000, gradients->name);
588   }
589 
590   // calculate diffusion factor
591 
592   // at the end of the array of time pings, the diffusion factor
593   // must be diffusion_expansion_factor
594   // this, at each step, the diffusion multiplication constant must
595   // be the nth root of diffusion_expansion_factor
596   // where n is the number of elements in the array
597 
598   const double diffusion_expansion_factor = 10;
599   double log_of_multiplier = log10(diffusion_expansion_factor) / time_ping_history;
600   double multiplier = pow(10, log_of_multiplier);
601   uint64_t dispersion_factor = (uint64_t)(multiplier * 100);
602   // debug(1,"dispersion factor is %" PRIu64 ".", dispersion_factor);
603 
604   // uint64_t first_local_to_remote_time_difference_time;
605   // uint64_t l2rtd = 0;
606   int sequence_number = 0;
607 
608   // for getting mean and sd of return times
609   int32_t stat_n = 0;
610   double stat_mean = 0.0;
611   double stat_M2 = 0.0;
612 
613   while (1) {
614     nread = recv(conn->timing_socket, packet, sizeof(packet), 0);
615 
616     if (nread >= 0) {
617 
618       if ((config.diagnostic_drop_packet_fraction == 0.0) ||
619           (drand48() > config.diagnostic_drop_packet_fraction)) {
620         arrival_time = get_absolute_time_in_ns();
621 
622         // ssize_t plen = nread;
623         // debug(1,"Packet Received on Timing Port.");
624         if (packet[1] == 0xd3) { // timing reply
625 
626           return_time = arrival_time - conn->departure_time;
627           debug(3, "clock synchronisation request: return time is %8.3f milliseconds.",
628                 0.000001 * return_time);
629 
630           if (return_time < 200000000) { // must be less than 0.2 seconds
631             // distant_receive_time =
632             // ((uint64_t)ntohl(*((uint32_t*)&packet[16])))<<32+ntohl(*((uint32_t*)&packet[20]));
633 
634             uint64_t ps, pn;
635 
636             ps = nctohl(&packet[16]);
637             ps = ps * 1000000000; // this many nanoseconds from the whole seconds
638             pn = nctohl(&packet[20]);
639             pn = pn * 1000000000;
640             pn = pn >> 32; // this many nanoseconds from the fractional part
641             distant_receive_time = ps + pn;
642 
643             // distant_transmit_time =
644             // ((uint64_t)ntohl(*((uint32_t*)&packet[24])))<<32+ntohl(*((uint32_t*)&packet[28]));
645 
646             ps = nctohl(&packet[24]);
647             ps = ps * 1000000000; // this many nanoseconds from the whole seconds
648             pn = nctohl(&packet[28]);
649             pn = pn * 1000000000;
650             pn = pn >> 32; // this many nanoseconds from the fractional part
651             distant_transmit_time = ps + pn;
652 
653             uint64_t remote_processing_time = 0;
654 
655             if (distant_transmit_time >= distant_receive_time)
656               remote_processing_time = distant_transmit_time - distant_receive_time;
657             else {
658               debug(1, "Yikes: distant_transmit_time is before distant_receive_time; remote "
659                        "processing time set to zero.");
660             }
661             // debug(1,"Return trip time: %" PRIu64 " nS, remote processing time: %" PRIu64 "
662             // nS.",return_time, remote_processing_time);
663 
664             if (remote_processing_time < return_time)
665               return_time -= remote_processing_time;
666             else
667               debug(1, "Remote processing time greater than return time -- ignored.");
668 
669             int cc;
670             // debug(1, "time ping history is %d entries.", time_ping_history);
671             for (cc = time_ping_history - 1; cc > 0; cc--) {
672               conn->time_pings[cc] = conn->time_pings[cc - 1];
673               // if ((conn->time_ping_count) && (conn->time_ping_count < 10))
674               //                conn->time_pings[cc].dispersion =
675               //                  conn->time_pings[cc].dispersion * pow(2.14,
676               //                  1.0/conn->time_ping_count);
677               if (conn->time_pings[cc].dispersion > UINT64_MAX / dispersion_factor)
678                 debug(1, "dispersion factor is too large at %" PRIu64 ".");
679               else
680                 conn->time_pings[cc].dispersion =
681                     (conn->time_pings[cc].dispersion * dispersion_factor) /
682                     100; // make the dispersions 'age' by this rational factor
683             }
684             // these are used for doing a least squares calculation to get the drift
685             conn->time_pings[0].local_time = arrival_time;
686             conn->time_pings[0].remote_time = distant_transmit_time + return_time / 2;
687             conn->time_pings[0].sequence_number = sequence_number++;
688             conn->time_pings[0].chosen = 0;
689             conn->time_pings[0].dispersion = return_time;
690             if (conn->time_ping_count < time_ping_history)
691               conn->time_ping_count++;
692 
693             // here, calculate the mean and standard deviation of the return times
694 
695             // mean and variance calculations from "online_variance" algorithm at
696             // https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Online_algorithm
697 
698             stat_n += 1;
699             double stat_delta = return_time - stat_mean;
700             stat_mean += stat_delta / stat_n;
701             stat_M2 += stat_delta * (return_time - stat_mean);
702             // debug(1, "Timing packet return time stats: current, mean and standard deviation over
703             // %d packets: %.1f, %.1f, %.1f (nanoseconds).",
704             //        stat_n,return_time,stat_mean, sqrtf(stat_M2 / (stat_n - 1)));
705 
706             // here, pick the record with the least dispersion, and record that it's been chosen
707 
708             // uint64_t local_time_chosen = arrival_time;
709             // uint64_t remote_time_chosen = distant_transmit_time;
710             // now pick the timestamp with the lowest dispersion
711             uint64_t rt = conn->time_pings[0].remote_time;
712             uint64_t lt = conn->time_pings[0].local_time;
713             uint64_t tld = conn->time_pings[0].dispersion;
714             int chosen = 0;
715             for (cc = 1; cc < conn->time_ping_count; cc++)
716               if (conn->time_pings[cc].dispersion < tld) {
717                 chosen = cc;
718                 rt = conn->time_pings[cc].remote_time;
719                 lt = conn->time_pings[cc].local_time;
720                 tld = conn->time_pings[cc].dispersion;
721                 // local_time_chosen = conn->time_pings[cc].local_time;
722                 // remote_time_chosen = conn->time_pings[cc].remote_time;
723               }
724             // debug(1,"Record %d has the lowest dispersion with %0.2f us
725             // dispersion.",chosen,1.0*((tld * 1000000) >> 32));
726             conn->time_pings[chosen].chosen = 1; // record the fact that it has been used for timing
727 
728             conn->local_to_remote_time_difference =
729                 rt - lt; // make this the new local-to-remote-time-difference
730             conn->local_to_remote_time_difference_measurement_time = lt; // done at this time.
731 
732             if (first_local_to_remote_time_difference == 0) {
733               first_local_to_remote_time_difference = conn->local_to_remote_time_difference;
734               // first_local_to_remote_time_difference_time = get_absolute_time_in_fp();
735             }
736 
737             // here, let's try to use the timing pings that were selected because of their short
738             // return times to
739             // estimate a figure for drift between the local clock (x) and the remote clock (y)
740 
741             // if we plug in a local interval, we will get back what that is in remote time
742 
743             // calculate the line of best fit for relating the local time and the remote time
744             // we will calculate the slope, which is the drift
745             // see https://www.varsitytutors.com/hotmath/hotmath_help/topics/line-of-best-fit
746 
747             uint64_t y_bar = 0; // remote timestamp average
748             uint64_t x_bar = 0; // local timestamp average
749             int sample_count = 0;
750 
751             // approximate time in seconds to let the system settle down
752             const int settling_time = 60;
753             // number of points to have for calculating a valid drift
754             const int sample_point_minimum = 8;
755             for (cc = 0; cc < conn->time_ping_count; cc++)
756               if ((conn->time_pings[cc].chosen) &&
757                   (conn->time_pings[cc].sequence_number >
758                    (settling_time / 3))) { // wait for a approximate settling time
759                                            // have to scale them down so that the sum, possibly over
760                                            // every term in the array, doesn't overflow
761                 y_bar += (conn->time_pings[cc].remote_time >> time_ping_history_power_of_two);
762                 x_bar += (conn->time_pings[cc].local_time >> time_ping_history_power_of_two);
763                 sample_count++;
764               }
765             conn->local_to_remote_time_gradient_sample_count = sample_count;
766             if (sample_count > sample_point_minimum) {
767               y_bar = y_bar / sample_count;
768               x_bar = x_bar / sample_count;
769 
770               int64_t xid, yid;
771               double mtl, mbl;
772               mtl = 0;
773               mbl = 0;
774               for (cc = 0; cc < conn->time_ping_count; cc++)
775                 if ((conn->time_pings[cc].chosen) &&
776                     (conn->time_pings[cc].sequence_number > (settling_time / 3))) {
777 
778                   uint64_t slt = conn->time_pings[cc].local_time >> time_ping_history_power_of_two;
779                   if (slt > x_bar)
780                     xid = slt - x_bar;
781                   else
782                     xid = -(x_bar - slt);
783 
784                   uint64_t srt = conn->time_pings[cc].remote_time >> time_ping_history_power_of_two;
785                   if (srt > y_bar)
786                     yid = srt - y_bar;
787                   else
788                     yid = -(y_bar - srt);
789 
790                   mtl = mtl + (1.0 * xid) * yid;
791                   mbl = mbl + (1.0 * xid) * xid;
792                 }
793               if (mbl)
794                 conn->local_to_remote_time_gradient = mtl / mbl;
795               else {
796                 // conn->local_to_remote_time_gradient = 1.0;
797                 debug(1, "mbl is zero. Drift remains at %.2f ppm.",
798                       (conn->local_to_remote_time_gradient - 1.0) * 1000000);
799               }
800 
801               // scale the numbers back up
802               uint64_t ybf = y_bar << time_ping_history_power_of_two;
803               uint64_t xbf = x_bar << time_ping_history_power_of_two;
804 
805               conn->local_to_remote_time_difference =
806                   ybf - xbf; // make this the new local-to-remote-time-difference
807               conn->local_to_remote_time_difference_measurement_time = xbf;
808 
809             } else {
810               debug(3, "not enough samples to estimate drift -- remaining at %.2f ppm.",
811                     (conn->local_to_remote_time_gradient - 1.0) * 1000000);
812               // conn->local_to_remote_time_gradient = 1.0;
813             }
814             // debug(1,"local to remote time gradient is %12.2f ppm, based on %d
815             // samples.",conn->local_to_remote_time_gradient*1000000,sample_count);
816 
817           } else {
818             debug(1,
819                   "Time ping turnaround time: %" PRIu64
820                   " ns -- it looks like a timing ping was lost.",
821                   return_time);
822           }
823         } else {
824           debug(1, "Timing port -- Unknown RTP packet of type 0x%02X length %d.", packet[1], nread);
825         }
826       } else {
827         debug(3, "Timing Receiver Thread -- dropping incoming packet to simulate a bad network.");
828       }
829     } else {
830       debug(1, "Timing receiver -- error receiving a packet.");
831     }
832   }
833 
834   debug(1, "Timing Receiver RTP thread \"normal\" exit -- this can't happen. Hah!");
835   pthread_cleanup_pop(0); // don't execute anything here.
836   debug(2, "Timing Receiver RTP thread exit.");
837   pthread_exit(NULL);
838 }
839 
bind_port(int ip_family,const char * self_ip_address,uint32_t scope_id,int * sock)840 static uint16_t bind_port(int ip_family, const char *self_ip_address, uint32_t scope_id,
841                           int *sock) {
842   // look for a port in the range, if any was specified.
843   int ret = 0;
844 
845   int local_socket = socket(ip_family, SOCK_DGRAM, IPPROTO_UDP);
846   if (local_socket == -1)
847     die("Could not allocate a socket.");
848 
849   /*
850     int val = 1;
851     ret = setsockopt(local_socket, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
852     if (ret < 0) {
853       char errorstring[1024];
854       strerror_r(errno, (char *)errorstring, sizeof(errorstring));
855       debug(1, "Error %d: \"%s\". Couldn't set SO_REUSEADDR");
856     }
857   */
858 
859   SOCKADDR myaddr;
860   int tryCount = 0;
861   uint16_t desired_port;
862   do {
863     tryCount++;
864     desired_port = nextFreeUDPPort();
865     memset(&myaddr, 0, sizeof(myaddr));
866     if (ip_family == AF_INET) {
867       struct sockaddr_in *sa = (struct sockaddr_in *)&myaddr;
868       sa->sin_family = AF_INET;
869       sa->sin_port = ntohs(desired_port);
870       inet_pton(AF_INET, self_ip_address, &(sa->sin_addr));
871       ret = bind(local_socket, (struct sockaddr *)sa, sizeof(struct sockaddr_in));
872     }
873 #ifdef AF_INET6
874     if (ip_family == AF_INET6) {
875       struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)&myaddr;
876       sa6->sin6_family = AF_INET6;
877       sa6->sin6_port = ntohs(desired_port);
878       inet_pton(AF_INET6, self_ip_address, &(sa6->sin6_addr));
879       sa6->sin6_scope_id = scope_id;
880       ret = bind(local_socket, (struct sockaddr *)sa6, sizeof(struct sockaddr_in6));
881     }
882 #endif
883 
884   } while ((ret < 0) && (errno == EADDRINUSE) && (desired_port != 0) &&
885            (tryCount < config.udp_port_range));
886 
887   // debug(1,"UDP port chosen: %d.",desired_port);
888 
889   if (ret < 0) {
890     close(local_socket);
891     char errorstring[1024];
892     strerror_r(errno, (char *)errorstring, sizeof(errorstring));
893     die("error %d: \"%s\". Could not bind a UDP port! Check the udp_port_range is large enough -- "
894         "it must be "
895         "at least 3, and 10 or more is suggested -- or "
896         "check for restrictive firewall settings or a bad router! UDP base is %u, range is %u and "
897         "current suggestion is %u.",
898         errno, errorstring, config.udp_port_base, config.udp_port_range, desired_port);
899   }
900 
901   uint16_t sport;
902   SOCKADDR local;
903   socklen_t local_len = sizeof(local);
904   getsockname(local_socket, (struct sockaddr *)&local, &local_len);
905 #ifdef AF_INET6
906   if (local.SAFAMILY == AF_INET6) {
907     struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)&local;
908     sport = ntohs(sa6->sin6_port);
909   } else
910 #endif
911   {
912     struct sockaddr_in *sa = (struct sockaddr_in *)&local;
913     sport = ntohs(sa->sin_port);
914   }
915   *sock = local_socket;
916   return sport;
917 }
918 
rtp_setup(SOCKADDR * local,SOCKADDR * remote,uint16_t cport,uint16_t tport,rtsp_conn_info * conn)919 void rtp_setup(SOCKADDR *local, SOCKADDR *remote, uint16_t cport, uint16_t tport,
920                rtsp_conn_info *conn) {
921 
922   // this gets the local and remote ip numbers (and ports used for the TCD stuff)
923   // we use the local stuff to specify the address we are coming from and
924   // we use the remote stuff to specify where we're goint to
925 
926   if (conn->rtp_running)
927     warn("rtp_setup has been called with al already-active stream -- ignored. Possible duplicate "
928          "SETUP call?");
929   else {
930 
931     debug(3, "rtp_setup: cport=%d tport=%d.", cport, tport);
932 
933     // print out what we know about the client
934     void *client_addr = NULL, *self_addr = NULL;
935     // int client_port, self_port;
936     // char client_port_str[64];
937     // char self_addr_str[64];
938 
939     conn->connection_ip_family =
940         remote->SAFAMILY; // keep information about the kind of ip of the client
941 
942 #ifdef AF_INET6
943     if (conn->connection_ip_family == AF_INET6) {
944       struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)remote;
945       client_addr = &(sa6->sin6_addr);
946       // client_port = ntohs(sa6->sin6_port);
947       sa6 = (struct sockaddr_in6 *)local;
948       self_addr = &(sa6->sin6_addr);
949       // self_port = ntohs(sa6->sin6_port);
950       conn->self_scope_id = sa6->sin6_scope_id;
951     }
952 #endif
953     if (conn->connection_ip_family == AF_INET) {
954       struct sockaddr_in *sa4 = (struct sockaddr_in *)remote;
955       client_addr = &(sa4->sin_addr);
956       // client_port = ntohs(sa4->sin_port);
957       sa4 = (struct sockaddr_in *)local;
958       self_addr = &(sa4->sin_addr);
959       // self_port = ntohs(sa4->sin_port);
960     }
961 
962     inet_ntop(conn->connection_ip_family, client_addr, conn->client_ip_string,
963               sizeof(conn->client_ip_string));
964     inet_ntop(conn->connection_ip_family, self_addr, conn->self_ip_string,
965               sizeof(conn->self_ip_string));
966 
967     debug(2, "Connection %d: SETUP -- Connection from %s to self at %s.", conn->connection_number,
968           conn->client_ip_string, conn->self_ip_string);
969 
970     // set up a the record of the remote's control socket
971     struct addrinfo hints;
972     struct addrinfo *servinfo;
973 
974     memset(&conn->rtp_client_control_socket, 0, sizeof(conn->rtp_client_control_socket));
975     memset(&hints, 0, sizeof hints);
976     hints.ai_family = conn->connection_ip_family;
977     hints.ai_socktype = SOCK_DGRAM;
978     char portstr[20];
979     snprintf(portstr, 20, "%d", cport);
980     if (getaddrinfo(conn->client_ip_string, portstr, &hints, &servinfo) != 0)
981       die("Can't get address of client's control port");
982 
983 #ifdef AF_INET6
984     if (servinfo->ai_family == AF_INET6) {
985       memcpy(&conn->rtp_client_control_socket, servinfo->ai_addr, sizeof(struct sockaddr_in6));
986       // ensure the scope id matches that of remote. this is needed for link-local addresses.
987       struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)&conn->rtp_client_control_socket;
988       sa6->sin6_scope_id = conn->self_scope_id;
989     } else
990 #endif
991       memcpy(&conn->rtp_client_control_socket, servinfo->ai_addr, sizeof(struct sockaddr_in));
992     freeaddrinfo(servinfo);
993 
994     // set up a the record of the remote's timing socket
995     memset(&conn->rtp_client_timing_socket, 0, sizeof(conn->rtp_client_timing_socket));
996     memset(&hints, 0, sizeof hints);
997     hints.ai_family = conn->connection_ip_family;
998     hints.ai_socktype = SOCK_DGRAM;
999     snprintf(portstr, 20, "%d", tport);
1000     if (getaddrinfo(conn->client_ip_string, portstr, &hints, &servinfo) != 0)
1001       die("Can't get address of client's timing port");
1002 #ifdef AF_INET6
1003     if (servinfo->ai_family == AF_INET6) {
1004       memcpy(&conn->rtp_client_timing_socket, servinfo->ai_addr, sizeof(struct sockaddr_in6));
1005       // ensure the scope id matches that of remote. this is needed for link-local addresses.
1006       struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)&conn->rtp_client_timing_socket;
1007       sa6->sin6_scope_id = conn->self_scope_id;
1008     } else
1009 #endif
1010       memcpy(&conn->rtp_client_timing_socket, servinfo->ai_addr, sizeof(struct sockaddr_in));
1011     freeaddrinfo(servinfo);
1012 
1013     // now, we open three sockets -- one for the audio stream, one for the timing and one for the
1014     // control
1015     conn->remote_control_port = cport;
1016     conn->remote_timing_port = tport;
1017 
1018     conn->local_control_port = bind_port(conn->connection_ip_family, conn->self_ip_string,
1019                                          conn->self_scope_id, &conn->control_socket);
1020     conn->local_timing_port = bind_port(conn->connection_ip_family, conn->self_ip_string,
1021                                         conn->self_scope_id, &conn->timing_socket);
1022     conn->local_audio_port = bind_port(conn->connection_ip_family, conn->self_ip_string,
1023                                        conn->self_scope_id, &conn->audio_socket);
1024 
1025     debug(3, "listening for audio, control and timing on ports %d, %d, %d.", conn->local_audio_port,
1026           conn->local_control_port, conn->local_timing_port);
1027 
1028     conn->reference_timestamp = 0;
1029 
1030     conn->request_sent = 0;
1031     conn->rtp_running = 1;
1032 
1033 #ifdef CONFIG_METADATA
1034     send_ssnc_metadata('clip', conn->client_ip_string, strlen(conn->client_ip_string), 1);
1035     send_ssnc_metadata('svip', conn->self_ip_string, strlen(conn->self_ip_string), 1);
1036 #endif
1037   }
1038 }
1039 
get_reference_timestamp_stuff(uint32_t * timestamp,uint64_t * timestamp_time,uint64_t * remote_timestamp_time,rtsp_conn_info * conn)1040 void get_reference_timestamp_stuff(uint32_t *timestamp, uint64_t *timestamp_time,
1041                                    uint64_t *remote_timestamp_time, rtsp_conn_info *conn) {
1042   // types okay
1043   debug_mutex_lock(&conn->reference_time_mutex, 1000, 0);
1044   *timestamp = conn->reference_timestamp;
1045   *remote_timestamp_time = conn->remote_reference_timestamp_time;
1046   *timestamp_time =
1047       conn->remote_reference_timestamp_time - local_to_remote_time_difference_now(conn);
1048   debug_mutex_unlock(&conn->reference_time_mutex, 0);
1049 }
1050 
clear_reference_timestamp(rtsp_conn_info * conn)1051 void clear_reference_timestamp(rtsp_conn_info *conn) {
1052   debug_mutex_lock(&conn->reference_time_mutex, 1000, 1);
1053   conn->reference_timestamp = 0;
1054   conn->remote_reference_timestamp_time = 0;
1055   debug_mutex_unlock(&conn->reference_time_mutex, 3);
1056 }
1057 
have_timestamp_timing_information(rtsp_conn_info * conn)1058 int have_timestamp_timing_information(rtsp_conn_info *conn) {
1059   if (conn->reference_timestamp == 0)
1060     return 0;
1061   else
1062     return 1;
1063 }
1064 
1065 // set this to zero to use the rates supplied by the sources, which might not always be completely
1066 // right...
1067 const int use_nominal_rate = 0; // specify whether to use the nominal input rate, usually 44100 fps
1068 
sanitised_source_rate_information(uint32_t * frames,uint64_t * time,rtsp_conn_info * conn)1069 int sanitised_source_rate_information(uint32_t *frames, uint64_t *time, rtsp_conn_info *conn) {
1070   int result = 1;
1071   uint32_t fs = conn->input_rate;
1072   *frames = fs;       // default value to return
1073   *time = 1000000000; // default value to return
1074   if ((conn->initial_reference_time) && (conn->initial_reference_timestamp)) {
1075     //    uint32_t local_frames = conn->reference_timestamp - conn->initial_reference_timestamp;
1076     uint32_t local_frames =
1077         modulo_32_offset(conn->initial_reference_timestamp, conn->reference_timestamp);
1078     uint64_t local_time = conn->remote_reference_timestamp_time - conn->initial_reference_time;
1079     if ((local_frames == 0) || (local_time == 0) || (use_nominal_rate)) {
1080       result = 1;
1081     } else {
1082       double calculated_frame_rate = conn->input_rate;
1083       if (local_time)
1084         calculated_frame_rate = (1.0E9 * local_frames) / local_time;
1085       else
1086         debug(1, "sanitised_source_rate_information: local_time is zero");
1087       if ((local_time == 0) || ((calculated_frame_rate / conn->input_rate) > 1.002) ||
1088           ((calculated_frame_rate / conn->input_rate) < 0.998)) {
1089         debug(3, "input frame rate out of bounds at %.2f fps.", calculated_frame_rate);
1090         result = 1;
1091       } else {
1092         *frames = local_frames;
1093         *time = local_time;
1094         result = 0;
1095       }
1096     }
1097   }
1098   return result;
1099 }
1100 
1101 // the timestamp is a timestamp calculated at the input rate
1102 // the reference timestamps are denominated in terms of the input rate
1103 
frame_to_local_time(uint32_t timestamp,uint64_t * time,rtsp_conn_info * conn)1104 int frame_to_local_time(uint32_t timestamp, uint64_t *time, rtsp_conn_info *conn) {
1105   debug_mutex_lock(&conn->reference_time_mutex, 1000, 0);
1106   int result = 0;
1107   uint64_t time_difference;
1108   uint32_t frame_difference;
1109   result = sanitised_source_rate_information(&frame_difference, &time_difference, conn);
1110 
1111   uint64_t timestamp_interval_time;
1112   uint64_t remote_time_of_timestamp;
1113   uint32_t timestamp_interval = modulo_32_offset(conn->reference_timestamp, timestamp);
1114   if (timestamp_interval <=
1115       conn->input_rate * 3600) { // i.e. timestamp was really after the reference timestamp
1116     timestamp_interval_time = (timestamp_interval * time_difference) /
1117                               frame_difference; // this is the nominal time, based on the
1118                                                 // fps specified between current and
1119                                                 // previous sync frame.
1120     remote_time_of_timestamp = conn->remote_reference_timestamp_time +
1121                                timestamp_interval_time; // based on the reference timestamp time
1122                                                         // plus the time interval calculated based
1123                                                         // on the specified fps.
1124   } else { // i.e. timestamp was actually before the reference timestamp
1125     timestamp_interval =
1126         modulo_32_offset(timestamp, conn->reference_timestamp); // fix the calculation
1127     timestamp_interval_time = (timestamp_interval * time_difference) /
1128                               frame_difference; // this is the nominal time, based on the
1129                                                 // fps specified between current and
1130                                                 // previous sync frame.
1131     remote_time_of_timestamp = conn->remote_reference_timestamp_time -
1132                                timestamp_interval_time; // based on the reference timestamp time
1133                                                         // plus the time interval calculated based
1134                                                         // on the specified fps.
1135   }
1136   *time = remote_time_of_timestamp - local_to_remote_time_difference_now(conn);
1137   debug_mutex_unlock(&conn->reference_time_mutex, 0);
1138   return result;
1139 }
1140 
local_time_to_frame(uint64_t time,uint32_t * frame,rtsp_conn_info * conn)1141 int local_time_to_frame(uint64_t time, uint32_t *frame, rtsp_conn_info *conn) {
1142   debug_mutex_lock(&conn->reference_time_mutex, 1000, 0);
1143   int result = 0;
1144 
1145   uint64_t time_difference;
1146   uint32_t frame_difference;
1147   result = sanitised_source_rate_information(&frame_difference, &time_difference, conn);
1148 
1149   // first, get from [local] time to remote time.
1150   uint64_t remote_time = time + local_to_remote_time_difference_now(conn);
1151   // next, get the remote time interval from the remote_time to the reference time
1152   uint64_t time_interval;
1153 
1154   // here, we calculate the time interval, in terms of remote time
1155   uint64_t offset = modulo_64_offset(conn->remote_reference_timestamp_time, remote_time);
1156   int reference_time_was_earlier = (offset <= (uint64_t)3600000000000);
1157   if (reference_time_was_earlier) // if we haven't had a reference within the last hour, it'll be
1158                                   // taken as afterwards
1159     time_interval = remote_time - conn->remote_reference_timestamp_time;
1160   else
1161     time_interval = conn->remote_reference_timestamp_time - remote_time;
1162 
1163   // now, convert the remote time interval into frames using the frame rate we have observed or
1164   // which has been nominated
1165   uint32_t frame_interval = 0;
1166   if (time_difference)
1167     frame_interval = (time_interval * frame_difference) / time_difference;
1168   else
1169     debug(1, "local_time_to_frame: time_difference is zero");
1170   if (reference_time_was_earlier) {
1171     // debug(1,"Frame interval is %" PRId64 " frames.",frame_interval);
1172     *frame = (conn->reference_timestamp + frame_interval);
1173   } else {
1174     // debug(1,"Frame interval is %" PRId64 " frames.",-frame_interval);
1175     *frame = (conn->reference_timestamp - frame_interval);
1176   }
1177   debug_mutex_unlock(&conn->reference_time_mutex, 0);
1178   return result;
1179 }
1180 
rtp_request_resend(seq_t first,uint32_t count,rtsp_conn_info * conn)1181 void rtp_request_resend(seq_t first, uint32_t count, rtsp_conn_info *conn) {
1182   if (conn->rtp_running) {
1183     // if (!request_sent) {
1184     // debug(2, "requesting resend of %d packets starting at %u.", count, first);
1185     //  request_sent = 1;
1186     //}
1187 
1188     char req[8]; // *not* a standard RTCP NACK
1189     req[0] = 0x80;
1190     req[1] = (char)0x55 | (char)0x80;            // Apple 'resend'
1191     *(unsigned short *)(req + 2) = htons(1);     // our sequence number
1192     *(unsigned short *)(req + 4) = htons(first); // missed seqnum
1193     *(unsigned short *)(req + 6) = htons(count); // count
1194     socklen_t msgsize = sizeof(struct sockaddr_in);
1195 #ifdef AF_INET6
1196     if (conn->rtp_client_control_socket.SAFAMILY == AF_INET6) {
1197       msgsize = sizeof(struct sockaddr_in6);
1198     }
1199 #endif
1200     uint64_t time_of_sending_ns = get_absolute_time_in_ns();
1201     uint64_t resend_error_backoff_time = 300000000; // 0.3 seconds
1202     if ((conn->rtp_time_of_last_resend_request_error_ns == 0) ||
1203         ((time_of_sending_ns - conn->rtp_time_of_last_resend_request_error_ns) >
1204          resend_error_backoff_time)) {
1205       if ((config.diagnostic_drop_packet_fraction == 0.0) ||
1206           (drand48() > config.diagnostic_drop_packet_fraction)) {
1207         // put a time limit on the sendto
1208 
1209         struct timeval timeout;
1210         timeout.tv_sec = 0;
1211         timeout.tv_usec = 100000;
1212 
1213         if (setsockopt(conn->control_socket, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout,
1214                        sizeof(timeout)) < 0)
1215           debug(1, "Can't set timeout on resend request socket.");
1216 
1217         if (sendto(conn->control_socket, req, sizeof(req), 0,
1218                    (struct sockaddr *)&conn->rtp_client_control_socket, msgsize) == -1) {
1219           char em[1024];
1220           strerror_r(errno, em, sizeof(em));
1221           debug(2, "Error %d using sendto to request a resend: \"%s\".", errno, em);
1222           conn->rtp_time_of_last_resend_request_error_ns = time_of_sending_ns;
1223         } else {
1224           conn->rtp_time_of_last_resend_request_error_ns = 0;
1225         }
1226 
1227       } else {
1228         debug(3, "Dropping resend request packet to simulate a bad network. Backing off for 0.3 "
1229                  "second.");
1230         conn->rtp_time_of_last_resend_request_error_ns = time_of_sending_ns;
1231       }
1232     } else {
1233       debug(1,
1234             "Suppressing a resend request due to a resend sendto error in the last 0.3 seconds.");
1235     }
1236   } else {
1237     // if (!request_sent) {
1238     debug(2, "rtp_request_resend called without active stream!");
1239     //  request_sent = 1;
1240     //}
1241   }
1242 }
1243