-
Notifications
You must be signed in to change notification settings - Fork 15
/
avail_descriptor.go
97 lines (84 loc) · 3.16 KB
/
avail_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
// 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 (
"encoding/xml"
"fmt"
"github.com/bamiaux/iobit"
)
const (
// AvailDescriptorTag is the splice_descriptor_tag for an avail_descriptor
AvailDescriptorTag = 0x00
)
// AvailDescriptor is an implementation of a splice_descriptor. It provides an
// optional extension to the splice_insert() command that allows an
// authorization identifier to be sent for an avail. Multiple copies of this
// descriptor may be included by using the loop mechanism provided. This
// identifier is intended to replicate the functionality of the cue tone system
// used in analog systems for ad insertion. This descriptor is intended only
// for use with a splice_insert() command, within a splice_info_section.
type AvailDescriptor struct {
XMLName xml.Name `xml:"http://www.scte.org/schemas/35 AvailDescriptor" json:"-"`
JSONType uint32 `xml:"-" json:"type"`
ProviderAvailID uint32 `xml:"providerAvailId,attr" json:"providerAvailId"`
}
// Tag returns the splice_descriptor_tag.
func (sd *AvailDescriptor) Tag() uint32 {
// ensure JSONType is set
sd.JSONType = AvailDescriptorTag
return AvailDescriptorTag
}
// writeTo the given table.
func (sd *AvailDescriptor) writeTo(t *table) {
tt := t.addTable()
tt.open("avail_descriptor()")
tt.addRow("splice_descriptor_tag", fmt.Sprintf("%#02x", AvailDescriptorTag))
tt.addRow("descriptor_length", sd.length())
tt.addRow("identifier", CUEIASCII)
tt.addRow("provider_avail_id", sd.ProviderAvailID)
tt.close()
}
// decode updates this splice_descriptor from binary.
func (sd *AvailDescriptor) decode(b []byte) error {
r := iobit.NewReader(b)
r.Skip(8) // splice_descriptor_tag
r.Skip(8) // descriptor_length
r.Skip(32) // identifier
sd.ProviderAvailID = r.Uint32(32)
if err := readerError(r); err != nil {
return fmt.Errorf("avail_descriptor: %w", err)
}
return nil
}
// encode this splice_descriptor to binary.
func (sd *AvailDescriptor) 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, AvailDescriptorTag) // splice_descriptor_tag
iow.PutUint32(8, uint32(length)) // descriptor_length
iow.PutUint32(32, CUEIdentifier) // identifier
iow.PutUint32(32, sd.ProviderAvailID) // provider_avail_id
err := iow.Flush()
return buf, err
}
// descriptorLength returns the descriptor_length
func (sd *AvailDescriptor) length() int {
length := 32 // identifier
length += 32 // provider_avail_id
return length / 8
}