File: fmtp.go

package info (click to toggle)
golang-github-pion-webrtc.v3 3.1.56-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,392 kB
  • sloc: javascript: 595; sh: 28; makefile: 5
file content (92 lines) | stat: -rw-r--r-- 1,914 bytes parent folder | download | duplicates (2)
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
// Package fmtp implements per codec parsing of fmtp lines
package fmtp

import (
	"strings"
)

// FMTP interface for implementing custom
// FMTP parsers based on MimeType
type FMTP interface {
	// MimeType returns the MimeType associated with
	// the fmtp
	MimeType() string
	// Match compares two fmtp descriptions for
	// compatibility based on the MimeType
	Match(f FMTP) bool
	// Parameter returns a value for the associated key
	// if contained in the parsed fmtp string
	Parameter(key string) (string, bool)
}

// Parse parses an fmtp string based on the MimeType
func Parse(mimetype, line string) FMTP {
	var f FMTP

	parameters := make(map[string]string)

	for _, p := range strings.Split(line, ";") {
		pp := strings.SplitN(strings.TrimSpace(p), "=", 2)
		key := strings.ToLower(pp[0])
		var value string
		if len(pp) > 1 {
			value = pp[1]
		}
		parameters[key] = value
	}

	switch {
	case strings.EqualFold(mimetype, "video/h264"):
		f = &h264FMTP{
			parameters: parameters,
		}
	default:
		f = &genericFMTP{
			mimeType:   mimetype,
			parameters: parameters,
		}
	}

	return f
}

type genericFMTP struct {
	mimeType   string
	parameters map[string]string
}

func (g *genericFMTP) MimeType() string {
	return g.mimeType
}

// Match returns true if g and b are compatible fmtp descriptions
// The generic implementation is used for MimeTypes that are not defined
func (g *genericFMTP) Match(b FMTP) bool {
	c, ok := b.(*genericFMTP)
	if !ok {
		return false
	}

	if !strings.EqualFold(g.mimeType, c.MimeType()) {
		return false
	}

	for k, v := range g.parameters {
		if vb, ok := c.parameters[k]; ok && !strings.EqualFold(vb, v) {
			return false
		}
	}

	for k, v := range c.parameters {
		if va, ok := g.parameters[k]; ok && !strings.EqualFold(va, v) {
			return false
		}
	}

	return true
}

func (g *genericFMTP) Parameter(key string) (string, bool) {
	v, ok := g.parameters[key]
	return v, ok
}