Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

90 rindas
5.6KB

  1. #include "rtp-ext.h"
  2. #include "rtp-util.h"
  3. #include <inttypes.h>
  4. #include <stdint.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <assert.h>
  8. #include <stdio.h>
  9. // https://webrtc.googlesource.com/src/+/refs/heads/main/docs/native-code/rtp-hdrext/abs-capture-time/
  10. /*
  11. RTP header extension format
  12. Data layout overview
  13. Data layout of the shortened version of abs-capture-time with a 1-byte header + 8 bytes of data:
  14. 0 1 2 3
  15. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  16. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  17. | ID | len=7 | absolute capture timestamp (bit 0-23) |
  18. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  19. | absolute capture timestamp (bit 24-55) |
  20. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  21. | ... (56-63) |
  22. +-+-+-+-+-+-+-+-+
  23. Data layout of the extended version of abs-capture-time with a 1-byte header + 16 bytes of data:
  24. 0 1 2 3
  25. 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  26. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  27. | ID | len=15| absolute capture timestamp (bit 0-23) |
  28. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  29. | absolute capture timestamp (bit 24-55) |
  30. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  31. | ... (56-63) | estimated capture clock offset (bit 0-23) |
  32. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  33. | estimated capture clock offset (bit 24-55) |
  34. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  35. | ... (56-63) |
  36. +-+-+-+-+-+-+-+-+
  37. Data layout details
  38. Absolute capture timestamp
  39. Absolute capture timestamp is the NTP timestamp of when the first frame in a packet was originally captured. This timestamp MUST be based on the same clock as the clock used to generate NTP timestamps for RTCP sender reports on the capture system.
  40. It's not always possible to do an NTP clock readout at the exact moment of when a media frame is captured. A capture system MAY postpone the readout until a more convenient time. A capture system SHOULD have known delays (e.g. from hardware buffers) subtracted from the readout to make the final timestamp as close to the actual capture time as possible.
  41. This field is encoded as a 64-bit unsigned fixed-point number with the high 32 bits for the timestamp in seconds and low 32 bits for the fractional part. This is also known as the UQ32.32 format and is what the RTP specification defines as the canonical format to represent NTP timestamps.
  42. Estimated capture clock offset
  43. Estimated capture clock offset is the sender¡®s estimate of the offset between its own NTP clock and the capture system¡¯s NTP clock. The sender is here defined as the system that owns the NTP clock used to generate the NTP timestamps for the RTCP sender reports on this stream. The sender system is typically either the capture system or a mixer.
  44. This field is encoded as a 64-bit two¡¯s complement signed fixed-point number with the high 32 bits for the seconds and low 32 bits for the fractional part. It¡¯s intended to make it easy for a receiver, that knows how to estimate the sender system¡¯s NTP clock, to also estimate the capture system¡¯s NTP clock:
  45. Capture NTP Clock = Sender NTP Clock + Capture Clock Offset
  46. Further details
  47. Capture system
  48. A receiver MUST treat the first CSRC in the CSRC list of a received packet as if it belongs to the capture system. If the CSRC list is empty, then the receiver MUST treat the SSRC as if it belongs to the capture system. Mixers SHOULD put the most prominent CSRC as the first CSRC in a packet¡¯s CSRC list.
  49. Intermediate systems
  50. An intermediate system (e.g. mixer) MAY adjust these timestamps as needed. It MAY also choose to rewrite the timestamps completely, using its own NTP clock as reference clock, if it wants to present itself as a capture system for A/V-sync purposes.
  51. Timestamp interpolation
  52. A sender SHOULD save bandwidth by not sending abs-capture-time with every RTP packet. It SHOULD still send them at regular intervals (e.g. every second) to help mitigate the impact of clock drift and packet loss. Mixers SHOULD always send abs-capture-time with the first RTP packet after changing capture system.
  53. A receiver SHOULD memorize the capture system (i.e. CSRC/SSRC), capture timestamp, and RTP timestamp of the most recently received abs-capture-time packet on each received stream. It can then use that information, in combination with RTP timestamps of packets without abs-capture-time, to extrapolate missing capture timestamps.
  54. Timestamp interpolation works fine as long as there¡¯s reasonably low NTP/RTP clock drift. This is not always true. Senders that detect ¡°jumps¡± between its NTP and RTP clock mappings SHOULD send abs-capture-time with the first RTP packet after such a thing happening.
  55. */
  56. int rtp_ext_absolute_capture_time_parse(const uint8_t* data, int bytes, struct rtp_ext_absolute_capture_time_t* ext)
  57. {
  58. assert(bytes == 8 || bytes == 16);
  59. if (bytes != 8 && bytes != 16)
  60. return -1;
  61. memset(ext, 0, sizeof(*ext));
  62. ext->timestamp = rtp_read_uint64(data);
  63. ext->offset = bytes >= 16 ? rtp_read_uint64(data + 8) : 0;
  64. return 0;
  65. }
  66. int rtp_ext_absolute_capture_time_write(uint8_t* data, int bytes, const struct rtp_ext_absolute_capture_time_t* ext)
  67. {
  68. if (bytes < 8)
  69. return -1;
  70. rtp_write_uint64(data, ext->timestamp);
  71. if (bytes >= 16)
  72. rtp_write_uint64(data + 8, ext->offset);
  73. return bytes >= 16 ? 16 : 8;
  74. }