comparison libgsmhr1/rtp_in.c @ 492:cc3a831712a4

libgsmhr1: implement arbitrary RTP input
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 15 Jun 2024 06:52:37 +0000
parents
children
comparison
equal deleted inserted replaced
491:45bf34451dd7 492:cc3a831712a4
1 /*
2 * The function implemented in this module takes whatever HRv1 payload format
3 * came in from RTP (can be RFC 5993 with or without TW-TS-002 extensions,
4 * can be bare TS 101 318, can be zero-length or missing payload) and turns it
5 * into canonical TW-TS-002 format, be it for storage or for immediate
6 * further processing.
7 */
8
9 #include <stdint.h>
10 #include <string.h>
11 #include "tw_gsmhr.h"
12
13 int gsmhr_rtp_in_preen(const uint8_t *rtp_in, unsigned rtp_in_len,
14 uint8_t *canon_pl)
15 {
16 int ft;
17
18 switch (rtp_in_len) {
19 case 0:
20 /* BFI-no-data, but not an invalid RTP input per se */
21 canon_pl[0] = 0x70;
22 return 0;
23 case 1:
24 /*
25 * This length is valid only if the payload is in
26 * RFC 5993 or TW-TS-002 format with FT=1 or FT=7.
27 */
28 if (rtp_in[0] & 0x80)
29 goto bad_rtp_input;
30 ft = rtp_in[0] >> 4;
31 if (ft == 1 || ft == 7) {
32 canon_pl[0] = rtp_in[0];
33 return 0;
34 } else
35 goto bad_rtp_input;
36 case GSMHR_FRAME_LEN_RPF:
37 /*
38 * The length is that of a TS 101 318 payload.
39 * No further checks can be done: every possible
40 * bit pattern is a valid payload in this format.
41 * But we do need to check for a perfect SID
42 * (the only kind of SID this format allows)
43 * and mark it accordingly in the output.
44 */
45 canon_pl[0] = gsmhr_ts101318_is_perfect_sid(rtp_in) << 4;
46 memcpy(canon_pl + 1, rtp_in, GSMHR_FRAME_LEN_RPF);
47 return 0;
48 case GSMHR_FRAME_LEN_5993:
49 /*
50 * This length is valid only if the payload is in
51 * RFC 5993 or TW-TS-002 format with FT=0, FT=2 or FT=6.
52 */
53 if (rtp_in[0] & 0x80)
54 goto bad_rtp_input;
55 ft = rtp_in[0] >> 4;
56 if (ft == 0 || ft == 2 || ft == 6) {
57 memcpy(canon_pl, rtp_in, GSMHR_FRAME_LEN_5993);
58 return 0;
59 } else
60 goto bad_rtp_input;
61 default:
62 bad_rtp_input:
63 /*
64 * Treat it like BFI-no-data, and tell the caller
65 * that we received an invalid RTP payload.
66 */
67 canon_pl[0] = 0x70;
68 return -1;
69 }
70 }