Skip to content

Commit

Permalink
Added parental rating descriptor
Browse files Browse the repository at this point in the history
  • Loading branch information
asticode committed Nov 25, 2017
1 parent 37ff39a commit 71d651c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
41 changes: 41 additions & 0 deletions descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
DescriptorTagISO639LanguageAndAudioType = 0xa
DescriptorTagMaximumBitrate = 0xe
DescriptorTagNetworkName = 0x40
DescriptorTagParentalRating = 0x55
DescriptorTagService = 0x48
DescriptorTagShortEvent = 0x4d
DescriptorTagStreamIdentifier = 0x52
Expand Down Expand Up @@ -66,6 +67,7 @@ type Descriptor struct {
Length uint8
MaximumBitrate *DescriptorMaximumBitrate
NetworkName *DescriptorNetworkName
ParentalRating *DescriptorParentalRating
Service *DescriptorService
ShortEvent *DescriptorShortEvent
StreamIdentifier *DescriptorStreamIdentifier
Expand Down Expand Up @@ -436,6 +438,43 @@ func newDescriptorNetworkName(i []byte) *DescriptorNetworkName {
return &DescriptorNetworkName{Name: i}
}

// DescriptorParentalRating represents a parental rating descriptor
// Page: 93 | Link: https://www.dvb.org/resources/public/standards/a38_dvb-si_specification.pdf
type DescriptorParentalRating struct {
Items []*DescriptorParentalRatingItem
}

// DescriptorParentalRatingItem represents a parental rating item descriptor
type DescriptorParentalRatingItem struct {
CountryCode []byte
Rating uint8
}

// MinimumAge returns the minimum age for the parental rating
func (d DescriptorParentalRatingItem) MinimumAge() int {
// Undefined or user defined ratings
if d.Rating == 0 || d.Rating > 0x10 {
return 0
}
return int(d.Rating) + 3
}

func newDescriptorParentalRating(i []byte) (d *DescriptorParentalRating) {
// Init
d = &DescriptorParentalRating{}
var offset int

// Add items
for offset < len(i) {
d.Items = append(d.Items, &DescriptorParentalRatingItem{
CountryCode: i[offset : offset+3],
Rating: uint8(i[offset+3]),
})
offset += 4
}
return
}

// DescriptorService represents a service descriptor
// Page: 96 | Chapter: 6.2.33 | Link: https://www.dvb.org/resources/public/standards/a38_dvb-si_specification.pdf
type DescriptorService struct {
Expand Down Expand Up @@ -592,6 +631,8 @@ func parseDescriptors(i []byte, offset *int) (o []*Descriptor) {
d.MaximumBitrate = newDescriptorMaximumBitrate(b)
case DescriptorTagNetworkName:
d.NetworkName = newDescriptorNetworkName(b)
case DescriptorTagParentalRating:
d.ParentalRating = newDescriptorParentalRating(b)
case DescriptorTagService:
d.Service = newDescriptorService(b)
case DescriptorTagShortEvent:
Expand Down
11 changes: 10 additions & 1 deletion descriptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func descriptorsBytes(w *astibinary.Writer) {
func TestParseDescriptor(t *testing.T) {
// Init
w := astibinary.New()
w.Write(uint16(171)) // Descriptors length
w.Write(uint16(177)) // Descriptors length
// AC3
w.Write(uint8(DescriptorTagAC3)) // Tag
w.Write(uint8(9)) // Length
Expand Down Expand Up @@ -149,6 +149,11 @@ func TestParseDescriptor(t *testing.T) {
w.Write("0001") // Item #1 content nibble level 1
w.Write("0010") // Item #1 content nibble level 2
w.Write(uint8(3)) // Item #1 user byte
// Parental rating
w.Write(uint8(DescriptorTagParentalRating)) // Tag
w.Write(uint8(4)) // Length
w.Write([]byte("cou")) // Item #1 country code
w.Write(uint8(2)) // Item #1 rating

// Assert
var offset int
Expand Down Expand Up @@ -257,4 +262,8 @@ func TestParseDescriptor(t *testing.T) {
ContentNibbleLevel2: 2,
UserByte: 3,
}}})
assert.Equal(t, *ds[14].ParentalRating, DescriptorParentalRating{Items: []*DescriptorParentalRatingItem{{
CountryCode: []byte("cou"),
Rating: 2,
}}})
}

0 comments on commit 71d651c

Please sign in to comment.