aboutsummaryrefslogtreecommitdiff
path: root/plugins/rtp/src/codec_util.vala
blob: 6bd465c1b308122397170ef634c58479076eae1a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
using Gee;
using Xmpp;
using Xmpp.Xep;

public class Dino.Plugins.Rtp.CodecUtil {
    private Set<string> supported_elements = new HashSet<string>();
    private Set<string> unsupported_elements = new HashSet<string>();

    public static Gst.Caps get_caps(string media, JingleRtp.PayloadType payload_type) {
        Gst.Caps caps = new Gst.Caps.simple("application/x-rtp",
                "media", typeof(string), media,
                "payload", typeof(int), payload_type.id);
        //"channels", typeof(int), payloadType.channels,
                //"max-ptime", typeof(int), payloadType.maxptime);
        unowned Gst.Structure s = caps.get_structure(0);
        if (payload_type.clockrate != 0) {
            s.set("clock-rate", typeof(int), payload_type.clockrate);
        }
        if (payload_type.name != null) {
            s.set("encoding-name", typeof(string), payload_type.name.up());
        }
        return caps;
    }

    public static string? get_codec_from_payload(string media, JingleRtp.PayloadType payload_type) {
        if (payload_type.name != null) return payload_type.name.down();
        if (media == "audio") {
            switch (payload_type.id) {
                case 0:
                    return "pcmu";
                case 8:
                    return "pcma";
            }
        }
        return null;
    }

    public static string? get_media_type_from_payload(string media, JingleRtp.PayloadType payload_type) {
        return get_media_type(media, get_codec_from_payload(media, payload_type));
    }

    public static string? get_media_type(string media, string? codec) {
        if (codec == null) return null;
        if (media == "audio") {
            switch (codec) {
                case "pcma":
                    return "audio/x-alaw";
                case "pcmu":
                    return "audio/x-mulaw";
            }
        }
        return @"$media/x-$codec";
    }

    public static string? get_rtp_pay_element_name_from_payload(string media, JingleRtp.PayloadType payload_type) {
        return get_pay_candidate(media, get_codec_from_payload(media, payload_type));
    }

    public static string? get_pay_candidate(string media, string? codec) {
        if (codec == null) return null;
        return @"rtp$(codec)pay";
    }

    public static string? get_rtp_depay_element_name_from_payload(string media, JingleRtp.PayloadType payload_type) {
        return get_depay_candidate(media, get_codec_from_payload(media, payload_type));
    }

    public static string? get_depay_candidate(string media, string? codec) {
        if (codec == null) return null;
        return @"rtp$(codec)depay";
    }

    public static string[] get_encode_candidates(string media, string? codec) {
        if (codec == null) return new string[0];
        if (media == "audio") {
            switch (codec) {
                case "opus":
                    return new string[] {"opusenc"};
                case "speex":
                    return new string[] {"speexenc"};
                case "pcma":
                    return new string[] {"alawenc"};
                case "pcmu":
                    return new string[] {"mulawenc"};
            }
        } else if (media == "video") {
            switch (codec) {
                case "h264":
                    return new string[] {/*"msdkh264enc", */"vaapih264enc", "x264enc"};
                case "vp9":
                    return new string[] {/*"msdkvp9enc", */"vaapivp9enc" /*, "vp9enc" */};
                case "vp8":
                    return new string[] {/*"msdkvp8enc", */"vaapivp8enc", "vp8enc"};
            }
        }
        return new string[0];
    }

    public static string[] get_decode_candidates(string media, string? codec) {
        if (codec == null) return new string[0];
        if (media == "audio") {
            switch (codec) {
                case "opus":
                    return new string[] {"opusdec"};
                case "speex":
                    return new string[] {"speexdec"};
                case "pcma":
                    return new string[] {"alawdec"};
                case "pcmu":
                    return new string[] {"mulawdec"};
            }
        } else if (media == "video") {
            switch (codec) {
                case "h264":
                    return new string[] {/*"msdkh264dec", */"vaapih264dec"};
                case "vp9":
                    return new string[] {/*"msdkvp9dec", */"vaapivp9dec", "vp9dec"};
                case "vp8":
                    return new string[] {/*"msdkvp8dec", */"vaapivp8dec", "vp8dec"};
            }
        }
        return new string[0];
    }

    public static string? get_encode_prefix(string media, string codec, string encode) {
        if (encode == "msdkh264enc") return "video/x-raw,format=NV12 ! ";
        if (encode == "vaapih264enc") return "video/x-raw,format=NV12 ! ";
        return null;
    }

    public static string? get_encode_suffix(string media, string codec, string encode) {
        // H264
        const string h264_suffix = " ! video/x-h264,profile=constrained-baseline ! h264parse";
        if (encode == "msdkh264enc") return @" bitrate=256 rate-control=vbr target-usage=7$h264_suffix";
        if (encode == "vaapih264enc") return @" bitrate=256 quality-level=7 tune=low-power$h264_suffix";
        if (encode == "x264enc") return @" byte-stream=1 bitrate=256 profile=baseline speed-preset=ultrafast tune=zerolatency$h264_suffix";
        if (media == "video" && codec == "h264") return h264_suffix;

        // VP8
        if (encode == "msdkvp8enc") return " bitrate=256 rate-control=vbr target-usage=7";
        if (encode == "vaapivp8enc") return " bitrate=256 rate-control=vbr quality-level=7";
        if (encode == "vp8enc") return " target-bitrate=256000 deadline=1 error-resilient=1";

        // OPUS
        if (encode == "opusenc") return " audio-type=voice";

        return null;
    }

    public static string? get_decode_prefix(string media, string codec, string decode) {
        return null;
    }

    public bool is_element_supported(string element_name) {
        if (unsupported_elements.contains(element_name)) return false;
        if (supported_elements.contains(element_name)) return true;
        var test_element = Gst.ElementFactory.make(element_name, @"test-$element_name");
        if (test_element != null) {
            supported_elements.add(element_name);
            return true;
        } else {
            debug("%s is not supported on this platform", element_name);
            unsupported_elements.add(element_name);
            return false;
        }
    }

    public string? get_encode_element_name(string media, string? codec) {
        foreach (string candidate in get_encode_candidates(media, codec)) {
            if (is_element_supported(candidate)) return candidate;
        }
        return null;
    }

    public string? get_pay_element_name(string media, string? codec) {
        string candidate = get_pay_candidate(media, codec);
        if (is_element_supported(candidate)) return candidate;
        return null;
    }

    public string? get_decode_element_name(string media, string? codec) {
        foreach (string candidate in get_decode_candidates(media, codec)) {
            if (is_element_supported(candidate)) return candidate;
        }
        return null;
    }

    public string? get_depay_element_name(string media, string? codec) {
        string candidate = get_depay_candidate(media, codec);
        if (is_element_supported(candidate)) return candidate;
        return null;
    }

    public void mark_element_unsupported(string element_name) {
        unsupported_elements.add(element_name);
    }

    public string? get_decode_bin_description(string media, string? codec, string? element_name = null, string? name = null) {
        if (codec == null) return null;
        string base_name = name ?? @"encode-$codec-$(Random.next_int())";
        string depay = get_depay_element_name(media, codec);
        string decode = element_name ?? get_decode_element_name(media, codec);
        if (depay == null || decode == null) return null;
        string decode_prefix = get_decode_prefix(media, codec, decode) ?? "";
        string resample = media == "audio" ? @" ! audioresample name=$base_name-resample" : "";
        return @"$depay name=$base_name-rtp-depay ! $decode_prefix$decode name=$base_name-decode ! $(media)convert name=$base_name-convert$resample";
    }

    public Gst.Element? get_decode_bin(string media, JingleRtp.PayloadType payload_type, string? name = null) {
        string? codec = get_codec_from_payload(media, payload_type);
        string base_name = name ?? @"encode-$codec-$(Random.next_int())";
        string? desc = get_decode_bin_description(media, codec, null, base_name);
        if (desc == null) return null;
        debug("Pipeline to decode %s %s: %s", media, codec, desc);
        Gst.Element bin = Gst.parse_bin_from_description(desc, true);
        bin.name = name;
        return bin;
    }

    public string? get_encode_bin_description(string media, string? codec, string? element_name = null, uint pt = 96, string? name = null) {
        if (codec == null) return null;
        string base_name = name ?? @"encode-$codec-$(Random.next_int())";
        string pay = get_pay_element_name(media, codec);
        string encode = element_name ?? get_encode_element_name(media, codec);
        if (pay == null || encode == null) return null;
        string encode_prefix = get_encode_prefix(media, codec, encode) ?? "";
        string encode_suffix = get_encode_suffix(media, codec, encode) ?? "";
        string resample = media == "audio" ? @" ! audioresample name=$base_name-resample" : "";
        return @"$(media)convert name=$base_name-convert$resample ! $encode_prefix$encode$encode_suffix ! $pay pt=$pt name=$base_name-rtp-pay";
    }

    public Gst.Element? get_encode_bin(string media, JingleRtp.PayloadType payload_type, string? name = null) {
        string? codec = get_codec_from_payload(media, payload_type);
        string base_name = name ?? @"encode-$codec-$(Random.next_int())";
        string? desc = get_encode_bin_description(media, codec, null, payload_type.id, base_name);
        if (desc == null) return null;
        debug("Pipeline to encode %s %s: %s", media, codec, desc);
        Gst.Element bin = Gst.parse_bin_from_description(desc, true);
        bin.name = name;
        return bin;
    }

}