Skip to content

Commit

Permalink
Update package to comply with SCTE 35 2022B (#11)
Browse files Browse the repository at this point in the history
* Add SCR SegmentationUPID

* Add `preRollMilliSeconds` XML attribute

* Deprecate SegmentationUpid.Format

* Refactor rendering splice_info_section table
  • Loading branch information
blahspam committed Jan 5, 2023
1 parent 5452ffe commit c36c3dc
Show file tree
Hide file tree
Showing 25 changed files with 431 additions and 854 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16
go-version: 1.18

- name: Build
run: go build -v ./...
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
.idea
.vscode
.vscode
go.work*
20 changes: 9 additions & 11 deletions cmd/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func encodeCommand() *cobra.Command {
} else {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
input = input + scanner.Text()
input += scanner.Text()
}
err = scanner.Err()
if err != nil {
Expand All @@ -58,23 +58,21 @@ func encodeCommand() *cobra.Command {
}
}

if strings.HasPrefix(strings.TrimSpace(input), "<") {
switch {
case strings.HasPrefix(strings.TrimSpace(input), "<"):
err = xml.Unmarshal([]byte(input), &sis)
} else if strings.HasPrefix(strings.TrimSpace(input), "{") {
case strings.HasPrefix(strings.TrimSpace(input), "{"):
err = json.Unmarshal([]byte(input), &sis)
} else {
default:
err = fmt.Errorf("unrecognized or empty input")
}

if err == nil {
// print encoded signal
_, _ = fmt.Fprintf(os.Stdout, "Base64: %s\n", sis.Base64())
_, _ = fmt.Fprintf(os.Stdout, "Hex : %s\n", sis.Hex())
} else {
// print error
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "Error: %s\n", err)
return
}

_, _ = fmt.Fprintf(os.Stdout, "Base64: %s\n", sis.Base64())
_, _ = fmt.Fprintf(os.Stdout, "Hex : %s\n", sis.Hex())
},
}
return cmd
Expand Down
14 changes: 12 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
module github.com/Comcast/scte35-go

go 1.16
go 1.18

require (
github.com/bamiaux/iobit v0.0.0-20170418073505-498159a04883
github.com/spf13/cobra v1.2.1
github.com/spf13/cobra v1.6.1
github.com/stretchr/testify v1.7.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
563 changes: 9 additions & 554 deletions go.sum

Large diffs are not rendered by default.

36 changes: 18 additions & 18 deletions pkg/scte35/audio_descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
package scte35

import (
"bytes"
"encoding/xml"
"fmt"
"strconv"

"github.com/bamiaux/iobit"
)
Expand Down Expand Up @@ -104,25 +104,25 @@ func (sd *AudioDescriptor) length() int {
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))
// writeTo the given table.
func (sd *AudioDescriptor) writeTo(t *table) {
tt := t.addTable()
tt.open("audio_descriptor()")
tt.addRow("splice_descriptor_tag", fmt.Sprintf("%#02x", sd.Tag()))
tt.addRow("descriptor_length", sd.length())
tt.addRow("identifier", CUEIASCII)
tt.addRow("audio_count", 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")
at := tt.addTable()
at.open("audio_channel[" + strconv.Itoa(i) + "]")
at.addRow("component_tag", ac.ComponentTag)
at.addRow("iso_code", ac.ISOCode)
at.addRow("bit_stream_mode", ac.BitStreamMode)
at.addRow("num_channels", ac.NumChannels)
at.addRow("full_srvc_audio", ac.FullSrvcAudio)
at.close()
}
_, _ = fmt.Fprintf(&buf, prefix+"}\n")
return string(buf.Bytes())
tt.close()
}

// AudioChannel collects the audio PID details.
Expand Down
20 changes: 9 additions & 11 deletions pkg/scte35/avail_descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package scte35

import (
"bytes"
"encoding/xml"
"fmt"

Expand Down Expand Up @@ -49,16 +48,15 @@ func (sd *AvailDescriptor) Tag() uint32 {
return AvailDescriptorTag
}

// table returns the avail_descriptor details in tabular format.
func (sd *AvailDescriptor) table(prefix, indent string) string {
var b bytes.Buffer
_, _ = fmt.Fprintf(&b, prefix+"avail_descriptor() {\n")
_, _ = fmt.Fprintf(&b, prefix+indent+"splice_descriptor_tag: %#02x\n", AvailDescriptorTag)
_, _ = fmt.Fprintf(&b, prefix+indent+"descriptor_length: %d bytes\n", sd.length())
_, _ = fmt.Fprintf(&b, prefix+indent+"identifier: %s\n", CUEIASCII)
_, _ = fmt.Fprintf(&b, prefix+indent+"provider_avail_id: %d\n", sd.ProviderAvailID)
_, _ = fmt.Fprintf(&b, prefix+"}\n")
return b.String()
// 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.
Expand Down
8 changes: 5 additions & 3 deletions pkg/scte35/bandwidth_reservation.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ func (cmd *BandwidthReservation) length() int {
return 0
}

// table returns the tabular description of this splice_command.
func (cmd *BandwidthReservation) table(prefix, _ string) string {
return prefix+"bandwidth_reservation() {}\n"
// writeTo the given table.
func (cmd *BandwidthReservation) writeTo(t *table) {
tt := t.addTable()
tt.open("bandwidth_reservation()")
tt.close()
}
2 changes: 1 addition & 1 deletion pkg/scte35/delivery_restrictions.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type DeliveryRestrictions struct {
DeviceRestrictions uint32 `xml:"deviceRestrictions,attr" json:"deviceRestrictions"`
}

// deviceRestrictionsName returns the human readable name for the
// deviceRestrictionsName returns the human-readable name for the
// device_restrictions.
func (dr *DeliveryRestrictions) deviceRestrictionsName() string {
switch dr.DeviceRestrictions {
Expand Down
24 changes: 11 additions & 13 deletions pkg/scte35/dtmf_descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package scte35

import (
"bytes"
"encoding/xml"
"fmt"

Expand All @@ -40,18 +39,17 @@ type DTMFDescriptor struct {
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()
// writeTo the given table.
func (sd *DTMFDescriptor) writeTo(t *table) {
tt := t.addTable()
tt.open("dtmf_descriptor()")
tt.addRow("splice_descriptor_tag", fmt.Sprintf("%#02x", DTMFDescriptorTag))
tt.addRow("descriptor_length", sd.length())
tt.addRow("identifier", CUEIASCII)
tt.addRow("preroll", float32(sd.Preroll/10))
tt.addRow("dtmf_count", len(sd.DTMFChars))
tt.addRow("dtmf_chars", sd.DTMFChars)
tt.close()
}

// Tag returns the splice_descriptor_tag.
Expand Down
16 changes: 7 additions & 9 deletions pkg/scte35/private_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package scte35

import (
"bytes"
"encoding/binary"
"encoding/xml"
"fmt"
Expand Down Expand Up @@ -93,12 +92,11 @@ func (cmd *PrivateCommand) length() int {
return length / 8
}

// table returns the tabular description of this PrivateCommand.
func (cmd *PrivateCommand) table(prefix, indent string) string {
var b bytes.Buffer
_, _ = fmt.Fprintln(&b, prefix+"private_command() {\n")
_, _ = fmt.Fprintln(&b, prefix+indent+"identifier: %s\n", cmd.IdentifierString())
_, _ = fmt.Fprintln(&b, prefix+indent+"private_byte: %#0x\n", cmd.PrivateBytes)
_, _ = fmt.Fprintln(&b, prefix+"}\n")
return b.String()
// writeTo the given table.
func (cmd *PrivateCommand) writeTo(t *table) {
tt := t.addTable()
tt.open("private_command()")
tt.addRow("identifier", cmd.IdentifierString())
tt.addRow("private_byte", fmt.Sprintf("%#0x", cmd.PrivateBytes))
tt.close()
}
18 changes: 8 additions & 10 deletions pkg/scte35/private_descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package scte35

import (
"bytes"
"encoding/binary"
"encoding/xml"
"fmt"
Expand Down Expand Up @@ -92,13 +91,12 @@ func (sd *PrivateDescriptor) length() int {
}

// table returns the tabular description of this PrivateDescriptor.
func (sd *PrivateDescriptor) table(prefix, indent string) string {
var b bytes.Buffer
_, _ = fmt.Fprintf(&b, fmt.Sprintf(prefix+"private_descriptor() {\n"))
_, _ = fmt.Fprintf(&b, fmt.Sprintf(prefix+indent+"splice_descriptor_tag: %#02x\n", sd.Tag()))
_, _ = fmt.Fprintf(&b, fmt.Sprintf(prefix+indent+"descriptor_length: %d bytes\n", sd.length()))
_, _ = fmt.Fprintf(&b, fmt.Sprintf(prefix+indent+"identifier: %s\n", sd.IdentifierString()))
_, _ = fmt.Fprintf(&b, fmt.Sprintf(prefix+indent+"private_bytes: %#0x\n", sd.PrivateBytes))
_, _ = fmt.Fprintf(&b, fmt.Sprintf(prefix+"}\n"))
return b.String()
func (sd *PrivateDescriptor) writeTo(t *table) {
tt := t.addTable()
tt.open("private_descriptor()")
tt.addRow("splice_descriptor_tag", fmt.Sprintf("%#02x", sd.Tag()))
tt.addRow("descriptor_length", sd.length())
tt.addRow("identifier", sd.IdentifierString())
tt.addRow("private_bytes", fmt.Sprintf("%#0x", sd.PrivateBytes))
tt.close()
}
4 changes: 2 additions & 2 deletions pkg/scte35/scte35.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var Logger = log.New(io.Discard, "SCTE35 ", log.Ldate|log.Ltime|log.Llongfile)

// DecodeBase64 is a convenience function for decoding a base-64 string into
// a SpliceInfoSection. If an error occurs, the returned SpliceInfoSection
// will contains the results of decoding up until the error condition
// will contain the results of decoding up until the error condition
// was encountered.
func DecodeBase64(s string) (*SpliceInfoSection, error) {
sis := &SpliceInfoSection{}
Expand Down Expand Up @@ -145,7 +145,7 @@ func readerError(r iobit.Reader) error {
if r.LeftBits() > 0 {
return ErrBufferUnderflow
}
if r.Error() == iobit.ErrOverflow {
if errors.Is(r.Error(), iobit.ErrOverflow) {
return ErrBufferOverflow
}
return nil
Expand Down
33 changes: 13 additions & 20 deletions pkg/scte35/scte35_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,8 @@ func TestDecodeBase64(t *testing.T) {
},
SegmentationUPIDs: []scte35.SegmentationUPID{
{
Type: 8,
Format: "text",
Value: "791755781",
Type: 8,
Value: "791755781",
},
},
SegmentationTypeID: 53,
Expand All @@ -386,9 +385,8 @@ func TestDecodeBase64(t *testing.T) {
},
SegmentationUPIDs: []scte35.SegmentationUPID{
{
Type: 8,
Format: "text",
Value: "791755998",
Type: 8,
Value: "791755998",
},
},
SegmentationTypeID: 35,
Expand Down Expand Up @@ -436,19 +434,16 @@ func TestDecodeBase64(t *testing.T) {
&scte35.SegmentationDescriptor{
SegmentationUPIDs: []scte35.SegmentationUPID{
{
Type: scte35.SegmentationUPIDTypeEIDR,
Format: "text",
Value: "10.5239/8BE5-E3F6-0000-0000-0000",
Type: scte35.SegmentationUPIDTypeEIDR,
Value: "10.5239/8BE5-E3F6-0000-0000-0000",
},
{
Type: scte35.SegmentationUPIDTypeEIDR,
Format: "text",
Value: "10.5239/8BE5-E3F6-0000-0000-0000",
Type: scte35.SegmentationUPIDTypeEIDR,
Value: "10.5239/8BE5-E3F6-0000-0000-0000",
},
{
Type: scte35.SegmentationUPIDTypeADI,
Format: "text",
Value: "SIGNAL:Ly9EMGxKR0hFZUtpMHdCUVZnRUFnZz1",
Type: scte35.SegmentationUPIDTypeADI,
Value: "SIGNAL:Ly9EMGxKR0hFZUtpMHdCUVZnRUFnZz1",
},
},
SegmentationEventID: 2,
Expand All @@ -472,7 +467,6 @@ func TestDecodeBase64(t *testing.T) {
{
Type: scte35.SegmentationUPIDTypeMPU,
FormatIdentifier: uint32ptr(1145656131),
Format: "base-64",
Value: "WU1XRjA0NTIwMDBI",
},
},
Expand Down Expand Up @@ -506,9 +500,8 @@ func TestDecodeBase64(t *testing.T) {
&scte35.SegmentationDescriptor{
SegmentationUPIDs: []scte35.SegmentationUPID{
{
Type: scte35.SegmentationUPIDTypeURI,
Format: "text",
Value: "urn:nbcuni.com:brc:499866434",
Type: scte35.SegmentationUPIDTypeURI,
Value: "urn:nbcuni.com:brc:499866434",
},
},
SegmentationDuration: uint64ptr(1347087),
Expand Down Expand Up @@ -585,7 +578,7 @@ func TestDecodeBase64(t *testing.T) {
}

// uncomment this to verify the output as text
// scte35.Log.Printf("\n%s", sis.Details())
// scte35.Logger.Printf("\n%s", sis.Table("", "\t"))
})
}
}
Expand Down

0 comments on commit c36c3dc

Please sign in to comment.