-
Notifications
You must be signed in to change notification settings - Fork 15
/
dtmf_descriptor.go
110 lines (98 loc) · 3.87 KB
/
dtmf_descriptor.go
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
// Copyright 2021 Comcast Cable Communications Management, LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
package scte35
import (
"bytes"
"encoding/xml"
"fmt"
"github.com/bamiaux/iobit"
)
const (
// DTMFDescriptorTag is the splice_descriptor_tag for a dtmf_descriptor
DTMFDescriptorTag = 0x01
)
// DTMFDescriptor is an implementation of a splice_descriptor. It provides an
// optional extension to the splice_insert() command that allows a receiver
// device to generate a legacy analog DTMF sequence based on a
// splice_info_section being received.
type DTMFDescriptor struct {
XMLName xml.Name `xml:"http://www.scte.org/schemas/35 DTMFDescriptor" json:"-"`
JSONType uint32 `xml:"-" json:"type"`
Preroll uint32 `xml:"preroll,attr" json:"preroll"`
DTMFChars string `xml:"chars,attr" json:"chars"`
}
// table returns the tabular description of this DTMFDescriptor.
func (sd *DTMFDescriptor) table(prefix, indent string) string {
var b bytes.Buffer
_, _ = fmt.Fprintf(&b, fmt.Sprintf(prefix+"dtmf_descriptor() {\n"))
_, _ = fmt.Fprintf(&b, fmt.Sprintf(prefix+indent+"splice_descriptor_tag: %#02x\n", DTMFDescriptorTag))
_, _ = fmt.Fprintf(&b, fmt.Sprintf(prefix+indent+"descriptor_length: %d bytes\n", sd.length()))
_, _ = fmt.Fprintf(&b, fmt.Sprintf(prefix+indent+"identifier: %s\n", CUEIASCII))
_, _ = fmt.Fprintf(&b, fmt.Sprintf(prefix+indent+"preroll: %.2f s\n", float32(sd.Preroll/10)))
_, _ = fmt.Fprintf(&b, fmt.Sprintf(prefix+indent+"dtmf_count: %d chars\n", len(sd.DTMFChars)))
_, _ = fmt.Fprintf(&b, fmt.Sprintf(prefix+indent+"dtmf_chars: %s\n", sd.DTMFChars))
_, _ = fmt.Fprintf(&b, fmt.Sprintf(prefix+"}\n"))
return b.String()
}
// Tag returns the splice_descriptor_tag.
func (sd *DTMFDescriptor) Tag() uint32 {
// ensure JSONType is set
sd.JSONType = DTMFDescriptorTag
return DTMFDescriptorTag
}
// decode updates this splice_descriptor from binary.
func (sd *DTMFDescriptor) decode(b []byte) error {
r := iobit.NewReader(b)
r.Skip(8) // splice_descriptor_tag
r.Skip(8) // descriptor_length
r.Skip(32) // identifier
sd.Preroll = r.Uint32(8)
dtmfCount := int(r.Uint32(3))
r.Skip(5) // reserved
sd.DTMFChars = r.String(dtmfCount)
if err := readerError(r); err != nil {
return fmt.Errorf("dtmf_descriptor: %w", err)
}
return readerError(r)
}
// encode this splice_descriptor to binary.
func (sd *DTMFDescriptor) encode() ([]byte, error) {
length := sd.length()
// add 2 bytes to contain splice_descriptor_tag & descriptor_length
buf := make([]byte, length+2)
iow := iobit.NewWriter(buf)
iow.PutUint32(8, DTMFDescriptorTag) // splice_descriptor_tag
iow.PutUint32(8, uint32(length)) // descriptor_length
iow.PutUint32(32, CUEIdentifier) // identifier
iow.PutUint32(8, sd.Preroll) // preroll
iow.PutUint32(3, uint32(len(sd.DTMFChars))) // dtmf_count
iow.PutUint32(5, Reserved) // reserved
_, err := iow.Write([]byte(sd.DTMFChars)) // dtmf_chars
if err != nil {
return buf, err
}
err = iow.Flush()
return buf, err
}
// descriptorLength returns the descriptor_length.
func (sd *DTMFDescriptor) length() int {
length := 32 // identifier
length += 8 // preroll
length += 3 // dtmf_count
length += 5 // reserved
length += len(sd.DTMFChars) * 8 // dtmf_char
return length / 8
}