VLC 4.0.0-dev
fmtp.h
Go to the documentation of this file.
1/**
2 * @file fmtp.h
3 * @brief SDP format parameters (fmtp) helpers
4 */
5/*****************************************************************************
6 * Copyright © 2022 Rémi Denis-Courmont
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 2.1
11 * of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 ****************************************************************************/
22
23#include <errno.h>
24#include <stdint.h>
25#include <stdlib.h>
26#include <string.h>
27
28static inline
29const char *vlc_sdp_fmtp_get_str(const struct vlc_sdp_pt *desc,
30 const char *name, size_t *restrict lenp)
31{
32 const char *p = desc->parameters;
33 size_t namelen = strlen(name);
34
35 while (p != NULL) {
36 p += strspn(p, " ");
37
38 if (strncmp(p, name, namelen) == 0 && p[namelen] == '=') {
39 p += namelen + 1;
40 *lenp = strcspn(p, ";");
41 return p;
42 }
43
44 p = strchr(p, ';');
45 if (p != NULL)
46 p++;
47 }
48
49 return NULL;
50}
51
52static
53int vlc_sdp_fmtp_get_ull(const struct vlc_sdp_pt *desc, const char *name,
54 unsigned long long *restrict res)
55{
56 size_t len;
57 const char *n = vlc_sdp_fmtp_get_str(desc, name, &len);
58 if (n == NULL)
59 return -ENOENT;
60 if (len == 0)
61 return -EINVAL;
62
63 char *end;
64 unsigned long long ull = strtoull(n, &end, 10);
65 if (end != n + len)
66 return -EINVAL;
67
68 *res = ull;
69 return 0;
70}
71
72static inline
73int vlc_sdp_fmtp_get_u16(const struct vlc_sdp_pt *desc, const char *name,
74 uint16_t *restrict res)
75{
76 unsigned long long ull;
77 int err = vlc_sdp_fmtp_get_ull(desc, name, &ull);
78 if (err == 0) {
79 if (ull >> 16)
80 return -ERANGE;
81
82 *res = ull;
83 }
84 return err;
85}
86
87static inline
88int vlc_sdp_fmtp_get_u8(const struct vlc_sdp_pt *desc, const char *name,
89 uint8_t *restrict res)
90{
91 unsigned long long ull;
92 int err = vlc_sdp_fmtp_get_ull(desc, name, &ull);
93 if (err == 0) {
94 if (ull >> 8)
95 return -ERANGE;
96
97 *res = ull;
98 }
99 return err;
100}
101
102#define vlc_sdp_fmtp_get(desc, name, vp) \
103 _Generic (*(vp), \
104 uint16_t: vlc_sdp_fmtp_get_u16(desc, name, (uint16_t *)(vp)), \
105 uint8_t: vlc_sdp_fmtp_get_u8(desc, name, (uint8_t *)(vp)))
106
static int vlc_sdp_fmtp_get_u16(const struct vlc_sdp_pt *desc, const char *name, uint16_t *restrict res)
Definition: fmtp.h:73
static int vlc_sdp_fmtp_get_u8(const struct vlc_sdp_pt *desc, const char *name, uint8_t *restrict res)
Definition: fmtp.h:88
static int vlc_sdp_fmtp_get_ull(const struct vlc_sdp_pt *desc, const char *name, unsigned long long *restrict res)
Definition: fmtp.h:53
static const char * vlc_sdp_fmtp_get_str(const struct vlc_sdp_pt *desc, const char *name, size_t *restrict lenp)
Definition: fmtp.h:29
#define p(t)
const char name[16]
Definition: httpd.c:1281
Payload type mapping.
Definition: rtp.h:62
const char * parameters
Format parameters from the a=fmtp line.
Definition: rtp.h:67