1/***
2  This file is part of PulseAudio.
3
4  Copyright 2006 Lennart Poettering
5
6  PulseAudio is free software; you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published
8  by the Free Software Foundation; either version 2.1 of the License,
9  or (at your option) any later version.
10
11  PulseAudio is distributed in the hope that it will be useful, but
12  WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  General Public License for more details.
15
16  You should have received a copy of the GNU Lesser General Public License
17  along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
18***/
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24#include "rtp.h"
25
26#include <pulsecore/core-util.h>
27
28uint8_t pa_rtp_payload_from_sample_spec(const pa_sample_spec *ss) {
29    pa_assert(ss);
30
31    if (ss->format == PA_SAMPLE_S16BE && ss->rate == 44100 && ss->channels == 2)
32        return 10;
33    if (ss->format == PA_SAMPLE_S16BE && ss->rate == 44100 && ss->channels == 1)
34        return 11;
35
36    return 127;
37}
38
39pa_sample_spec *pa_rtp_sample_spec_from_payload(uint8_t payload, pa_sample_spec *ss) {
40    pa_assert(ss);
41
42    switch (payload) {
43        case 10:
44            ss->channels = 2;
45            ss->format = PA_SAMPLE_S16BE;
46            ss->rate = 44100;
47            break;
48
49        case 11:
50            ss->channels = 1;
51            ss->format = PA_SAMPLE_S16BE;
52            ss->rate = 44100;
53            break;
54
55        case 127:
56            ss->channels = 2;
57            ss->format = PA_SAMPLE_S16LE;
58            ss->rate = 48000;
59            break;
60
61        default:
62            return NULL;
63    }
64
65    return ss;
66}
67
68pa_sample_spec *pa_rtp_sample_spec_fixup(pa_sample_spec * ss, bool enable_opus) {
69    pa_assert(ss);
70
71    if (!pa_rtp_sample_spec_valid(ss) && enable_opus)
72        ss->format = PA_SAMPLE_S16LE;
73    else if (!pa_rtp_sample_spec_valid(ss) || !enable_opus)
74        ss->format = PA_SAMPLE_S16BE;
75
76    pa_assert(pa_rtp_sample_spec_valid(ss));
77    return ss;
78}
79
80int pa_rtp_sample_spec_valid(const pa_sample_spec *ss) {
81    pa_assert(ss);
82
83    if (!pa_sample_spec_valid(ss))
84        return 0;
85
86    return ss->format == PA_SAMPLE_S16BE || ss->format == PA_SAMPLE_S16LE;
87}
88
89const char* pa_rtp_format_to_string(pa_sample_format_t f) {
90    switch (f) {
91        case PA_SAMPLE_S16BE:
92        case PA_SAMPLE_S16LE:
93            return "L16";
94        default:
95            return NULL;
96    }
97}
98
99pa_sample_format_t pa_rtp_string_to_format(const char *s, bool enable_opus) {
100    pa_assert(s);
101
102    if (pa_streq(s, "L16") && enable_opus)
103        return PA_SAMPLE_S16LE;
104    else if (pa_streq(s, "L16"))
105        return PA_SAMPLE_S16BE;
106    else
107        return PA_SAMPLE_INVALID;
108}
109