-
Notifications
You must be signed in to change notification settings - Fork 15
/
audio_descriptor.go
145 lines (131 loc) · 4.99 KB
/
audio_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
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
// 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 (
// AudioDescriptorTag is the splice_descriptor_tag for a audio descriptor.
AudioDescriptorTag = 0x04
)
// AudioDescriptor is an implementation of a audio_descriptor. The
// audio_descriptor() should be used when programmers and/or MVPDs do not
// support dynamic signaling (e.g., signaling of audio language changes) and
// with legacy audio formats that do not support dynamic signaling.
type AudioDescriptor struct {
XMLName xml.Name `xml:"http://www.scte.org/schemas/35 AudioDescriptor" json:"-"`
JSONType uint32 `xml:"-" json:"type"`
AudioChannels []AudioChannel `xml:"http://www.scte.org/schemas/35 AudioChannel" json:"audioChannels"`
}
// Tag returns the splice_descriptor_tag.
func (sd *AudioDescriptor) Tag() uint32 {
// ensure JSONType is set
sd.JSONType = TimeDescriptorTag
return TimeDescriptorTag
}
// decode updates this SpliceDescriptor from binary.
func (sd *AudioDescriptor) decode(b []byte) error {
r := iobit.NewReader(b)
r.Skip(8) // splice_descriptor_tag
r.Skip(8) // descriptor_length
r.Skip(32) // identifier
audioCount := int(r.Uint32(4))
r.Skip(4) // reserved
sd.AudioChannels = make([]AudioChannel, audioCount)
for i := 0; i < audioCount; i++ {
ac := AudioChannel{}
ac.ComponentTag = r.Uint32(8)
ac.ISOCode = r.String(3)
ac.BitStreamMode = r.Uint32(3)
ac.NumChannels = r.Uint32(4)
ac.FullSrvcAudio = r.Bit()
sd.AudioChannels[i] = ac
}
if err := readerError(r); err != nil {
return fmt.Errorf("audio_descriptor: %w", err)
}
return nil
}
// encode this SpliceDescriptor to binary.
func (sd *AudioDescriptor) 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, AudioDescriptorTag)
iow.PutUint32(8, uint32(length))
iow.PutUint32(32, CUEIdentifier)
iow.PutUint32(8, uint32(len(sd.AudioChannels)))
iow.PutUint32(4, Reserved)
for _, ad := range sd.AudioChannels {
iow.PutUint32(8, ad.ComponentTag)
_, _ = iow.Write([]byte(ad.ISOCode))
iow.PutUint32(3, ad.BitStreamMode)
iow.PutUint32(4, ad.NumChannels)
iow.PutBit(ad.FullSrvcAudio)
}
return buf, nil
}
// descriptorLength returns the descriptor_length
func (sd *AudioDescriptor) length() int {
length := 32 // identifier
length += 4 // audio_count
length += 4 // reserved
for i := range sd.AudioChannels {
length += sd.AudioChannels[i].length() * 8
}
return length / 8
}
// table returns the tabular description of this SpliceDescriptor.
func (sd *AudioDescriptor) table(prefix, indent string) string {
var buf bytes.Buffer
_, _ = fmt.Fprintf(&buf, prefix+"audio_descriptor() {\n")
_, _ = fmt.Fprintf(&buf, prefix+indent+"splice_descriptor_tag: %#02x\n", sd.Tag())
_, _ = fmt.Fprintf(&buf, prefix+indent+"descriptor_length: %d bytes\n", sd.length())
_, _ = fmt.Fprintf(&buf, prefix+indent+"identifier: %s\n", CUEIASCII)
_, _ = fmt.Fprintf(&buf, prefix+indent+"audio_count: %d\n", len(sd.AudioChannels))
for i, ac := range sd.AudioChannels {
_, _ = fmt.Fprintf(&buf, prefix+indent+"audio_channel[%d] {", i)
_, _ = fmt.Fprintf(&buf, prefix+indent+indent+"component_tag: %d\n", ac.ComponentTag)
_, _ = fmt.Fprintf(&buf, prefix+indent+indent+"iso_code: %s\n", ac.ISOCode)
_, _ = fmt.Fprintf(&buf, prefix+indent+indent+"bit_stream_mode: %d\n", ac.BitStreamMode)
_, _ = fmt.Fprintf(&buf, prefix+indent+indent+"num_channels: %d)\n", ac.NumChannels)
_, _ = fmt.Fprintf(&buf, prefix+indent+indent+"full_srvc_audio: %v\n", ac.FullSrvcAudio)
_, _ = fmt.Fprintf(&buf, prefix+indent+"}\n")
}
_, _ = fmt.Fprintf(&buf, prefix+"}\n")
return string(buf.Bytes())
}
// AudioChannel collects the audio PID details.
type AudioChannel struct {
ComponentTag uint32 `xml:"componentTag,attr" json:"componentTag"`
ISOCode string `xml:"ISOCode,attr" json:"isoCode"`
BitStreamMode uint32 `xml:"BitStreamMode,attr" json:"bitStreamMode"`
NumChannels uint32 `xml:"NumChannels,attr" json:"numChannels"`
FullSrvcAudio bool `xml:"FullSrvcAudio,attr" json:"fullSrvcAudio"`
}
// length returns audio_channel length.
func (ac *AudioChannel) length() int {
length := 8 // component_tag
length += 24 // iso_code
length += 3 // bit_stream_mode
length += 4 // num_channels
length++ // full_srvc_audio
return length / 8
}