-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathmpd.go
1221 lines (1092 loc) · 42.4 KB
/
mpd.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package mpd
import (
"encoding/base64"
"encoding/hex"
"encoding/xml"
"errors"
"strings"
"time"
. "github.com/zencoder/go-dash/v3/helpers/ptrs"
)
// Type definition for DASH profiles
type DashProfile string
// Constants for supported DASH profiles
const (
// Live Profile
DASH_PROFILE_LIVE DashProfile = "urn:mpeg:dash:profile:isoff-live:2011"
// On Demand Profile
DASH_PROFILE_ONDEMAND DashProfile = "urn:mpeg:dash:profile:isoff-on-demand:2011"
// HbbTV Profile
DASH_PROFILE_HBBTV_1_5_LIVE DashProfile = "urn:hbbtv:dash:profile:isoff-live:2012,urn:mpeg:dash:profile:isoff-live:2011"
)
type AudioChannelConfigurationScheme string
const (
// Scheme for non-Dolby Audio
AUDIO_CHANNEL_CONFIGURATION_MPEG_DASH AudioChannelConfigurationScheme = "urn:mpeg:dash:23003:3:audio_channel_configuration:2011"
// Scheme for Dolby Audio
AUDIO_CHANNEL_CONFIGURATION_MPEG_DOLBY AudioChannelConfigurationScheme = "tag:dolby.com,2014:dash:audio_channel_configuration:2011"
)
// AccessibilityElementScheme is the scheme definition for an Accessibility element
type AccessibilityElementScheme string
// Accessibility descriptor values for Audio Description
const ACCESSIBILITY_ELEMENT_SCHEME_DESCRIPTIVE_AUDIO AccessibilityElementScheme = "urn:tva:metadata:cs:AudioPurposeCS:2007"
// Constants for some known MIME types, this is a limited list and others can be used.
const (
DASH_MIME_TYPE_VIDEO_MP4 string = "video/mp4"
DASH_MIME_TYPE_AUDIO_MP4 string = "audio/mp4"
DASH_MIME_TYPE_SUBTITLE_VTT string = "text/vtt"
DASH_MIME_TYPE_SUBTITLE_TTML string = "application/ttaf+xml"
DASH_MIME_TYPE_SUBTITLE_SRT string = "application/x-subrip"
DASH_MIME_TYPE_SUBTITLE_DFXP string = "application/ttaf+xml"
DASH_MIME_TYPE_IMAGE_JPEG string = "image/jpeg"
DASH_CONTENT_TYPE_IMAGE string = "image"
)
// Known error variables
var (
ErrNoDASHProfileSet error = errors.New("No DASH profile set")
ErrAdaptationSetNil = errors.New("Adaptation Set nil")
ErrSegmentTemplateLiveProfileOnly = errors.New("Segment template can only be used with Live Profile")
ErrSegmentTemplateNil = errors.New("Segment Template nil ")
ErrRepresentationNil = errors.New("Representation nil")
ErrAccessibilityNil = errors.New("Accessibility nil")
ErrBaseURLEmpty = errors.New("Base URL empty")
ErrSegmentBaseOnDemandProfileOnly = errors.New("Segment Base can only be used with On-Demand Profile")
ErrSegmentBaseNil = errors.New("Segment Base nil")
ErrAudioChannelConfigurationNil = errors.New("Audio Channel Configuration nil")
ErrInvalidDefaultKID = errors.New("Invalid Default KID string, should be 32 characters")
ErrPROEmpty = errors.New("PlayReady PRO empty")
ErrContentProtectionNil = errors.New("Content Protection nil")
ErrInbandEventStreamSchemeUriEmpty = errors.New("Inband Event Stream schemeIdUri Empty")
)
type MPD struct {
XMLNs *string `xml:"xmlns,attr"`
XMLNsDolby *string `xml:"xmlns:dolby,attr"`
Profiles *string `xml:"profiles,attr"`
Type *string `xml:"type,attr"`
MediaPresentationDuration *string `xml:"mediaPresentationDuration,attr"`
MinBufferTime *string `xml:"minBufferTime,attr"`
AvailabilityStartTime *string `xml:"availabilityStartTime,attr,omitempty"`
MinimumUpdatePeriod *string `xml:"minimumUpdatePeriod,attr"`
PublishTime *string `xml:"publishTime,attr"`
TimeShiftBufferDepth *string `xml:"timeShiftBufferDepth,attr"`
SuggestedPresentationDelay *Duration `xml:"suggestedPresentationDelay,attr,omitempty"`
BaseURL []string `xml:"BaseURL,omitempty"`
Location string `xml:"Location,omitempty"`
period *Period
Periods []*Period `xml:"Period,omitempty"`
UTCTiming *DescriptorType `xml:"UTCTiming,omitempty"`
}
type Period struct {
ID string `xml:"id,attr,omitempty"`
Duration Duration `xml:"duration,attr,omitempty"`
Start *Duration `xml:"start,attr,omitempty"`
BaseURL []string `xml:"BaseURL,omitempty"`
SegmentBase *SegmentBase `xml:"SegmentBase,omitempty"`
SegmentList *SegmentList `xml:"SegmentList,omitempty"`
SegmentTemplate *SegmentTemplate `xml:"SegmentTemplate,omitempty"`
AdaptationSets []*AdaptationSet `xml:"AdaptationSet,omitempty"`
EventStreams []EventStream `xml:"EventStream,omitempty"`
}
type DescriptorType struct {
SchemeIDURI *string `xml:"schemeIdUri,attr"`
Value *string `xml:"value,attr"`
ID *string `xml:"id,attr"`
}
// ISO 23009-1-2014 5.3.7
type CommonAttributesAndElements struct {
Profiles *string `xml:"profiles,attr"`
Width *string `xml:"width,attr"`
Height *string `xml:"height,attr"`
Sar *string `xml:"sar,attr"`
FrameRate *string `xml:"frameRate,attr"`
AudioSamplingRate *string `xml:"audioSamplingRate,attr"`
MimeType *string `xml:"mimeType,attr"`
SegmentProfiles *string `xml:"segmentProfiles,attr"`
Codecs *string `xml:"codecs,attr"`
MaximumSAPPeriod *string `xml:"maximumSAPPeriod,attr"`
StartWithSAP *int64 `xml:"startWithSAP,attr"`
MaxPlayoutRate *string `xml:"maxPlayoutRate,attr"`
ScanType *string `xml:"scanType,attr"`
FramePacking []DescriptorType `xml:"FramePacking,omitempty"`
AudioChannelConfiguration []DescriptorType `xml:"AudioChannelConfiguration,omitempty"`
ContentProtection []ContentProtectioner `xml:"ContentProtection,omitempty"`
EssentialProperty []DescriptorType `xml:"EssentialProperty,omitempty"`
SupplementalProperty []DescriptorType `xml:"SupplementalProperty,omitempty"`
InbandEventStream []DescriptorType `xml:"InbandEventStream,omitempty"`
}
type contentProtections []ContentProtectioner
func (as *contentProtections) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var scheme string
for _, a := range start.Attr {
if a.Name.Local == "schemeIdUri" {
scheme = a.Value
break
}
}
var target ContentProtectioner
switch scheme {
case CONTENT_PROTECTION_ROOT_SCHEME_ID_URI:
target = &CENCContentProtection{}
case CONTENT_PROTECTION_PLAYREADY_SCHEME_ID:
target = &PlayreadyContentProtection{}
case CONTENT_PROTECTION_WIDEVINE_SCHEME_ID:
target = &WidevineContentProtection{}
default:
target = &ContentProtection{}
}
if err := d.DecodeElement(target, &start); err != nil {
return err
}
*as = append(*as, target)
return nil
}
// wrappedAdaptationSet provides the default xml unmarshal
// to take care of the majority of our unmarshalling
type wrappedAdaptationSet AdaptationSet
// dtoAdaptationSet parses the items out of AdaptationSet
// that give us trouble:
// * Content Protection interface
type dtoAdaptationSet struct {
wrappedAdaptationSet
ContentProtection contentProtections `xml:"ContentProtection,omitempty"`
}
type AdaptationSet struct {
CommonAttributesAndElements
XMLName xml.Name `xml:"AdaptationSet"`
ID *string `xml:"id,attr"`
SegmentAlignment *bool `xml:"segmentAlignment,attr"`
Lang *string `xml:"lang,attr"`
Group *string `xml:"group,attr"`
PAR *string `xml:"par,attr"`
MinBandwidth *string `xml:"minBandwidth,attr"`
MaxBandwidth *string `xml:"maxBandwidth,attr"`
MinWidth *string `xml:"minWidth,attr"`
MaxWidth *string `xml:"maxWidth,attr"`
MinHeight *string `xml:"minHeight,attr"`
MaxHeight *string `xml:"maxHeight,attr"`
ContentType *string `xml:"contentType,attr"`
Roles []*Role `xml:"Role,omitempty"`
SegmentBase *SegmentBase `xml:"SegmentBase,omitempty"`
SegmentList *SegmentList `xml:"SegmentList,omitempty"`
SegmentTemplate *SegmentTemplate `xml:"SegmentTemplate,omitempty"` // Live Profile Only
Representations []*Representation `xml:"Representation,omitempty"`
AccessibilityElems []*Accessibility `xml:"Accessibility,omitempty"`
Labels []string `xml:"Label,omitempty"`
BaseURL []string `xml:"BaseURL,omitempty"`
}
func (as *AdaptationSet) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var n dtoAdaptationSet
if err := d.DecodeElement(&n, &start); err != nil {
return err
}
*as = AdaptationSet(n.wrappedAdaptationSet)
as.ContentProtection = make([]ContentProtectioner, len(n.ContentProtection))
copy(as.ContentProtection, n.ContentProtection)
return nil
}
// Constants for DRM / ContentProtection
const (
CONTENT_PROTECTION_ROOT_SCHEME_ID_URI = "urn:mpeg:dash:mp4protection:2011"
CONTENT_PROTECTION_ROOT_VALUE = "cenc"
CENC_XMLNS = "urn:mpeg:cenc:2013"
CONTENT_PROTECTION_WIDEVINE_SCHEME_ID = "urn:uuid:edef8ba9-79d6-4ace-a3c8-27dcd51d21ed"
CONTENT_PROTECTION_WIDEVINE_SCHEME_HEX = "edef8ba979d64acea3c827dcd51d21ed"
CONTENT_PROTECTION_PLAYREADY_SCHEME_ID = "urn:uuid:9a04f079-9840-4286-ab92-e65be0885f95"
CONTENT_PROTECTION_PLAYREADY_SCHEME_HEX = "9a04f07998404286ab92e65be0885f95"
CONTENT_PROTECTION_PLAYREADY_SCHEME_V10_ID = "urn:uuid:79f0049a-4098-8642-ab92-e65be0885f95"
CONTENT_PROTECTION_PLAYREADY_SCHEME_V10_HEX = "79f0049a40988642ab92e65be0885f95"
CONTENT_PROTECTION_PLAYREADY_XMLNS = "urn:microsoft:playready"
)
type ContentProtectioner interface {
ContentProtected()
}
type ContentProtection struct {
AdaptationSet *AdaptationSet `xml:"-"`
XMLName xml.Name `xml:"ContentProtection"`
SchemeIDURI *string `xml:"schemeIdUri,attr"` // Default: urn:mpeg:dash:mp4protection:2011
XMLNS *string `xml:"cenc,attr"` // Default: urn:mpeg:cenc:2013
Attrs []*xml.Attr `xml:",any,attr"`
}
type CENCContentProtection struct {
ContentProtection
DefaultKID *string `xml:"default_KID,attr"`
Value *string `xml:"value,attr"` // Default: cenc
}
type PlayreadyContentProtection struct {
ContentProtection
PlayreadyXMLNS *string `xml:"mspr,attr,omitempty"`
PRO *string `xml:"pro,omitempty"`
PSSH *string `xml:"pssh,omitempty"`
}
type WidevineContentProtection struct {
ContentProtection
PSSH *string `xml:"pssh,omitempty"`
}
type ContentProtectionMarshal struct {
AdaptationSet *AdaptationSet `xml:"-"`
XMLName xml.Name `xml:"ContentProtection"`
SchemeIDURI *string `xml:"schemeIdUri,attr"` // Default: urn:mpeg:dash:mp4protection:2011
XMLNS *string `xml:"xmlns:cenc,attr"` // Default: urn:mpeg:cenc:2013
Attrs []*xml.Attr `xml:",any,attr"`
}
type CENCContentProtectionMarshal struct {
ContentProtectionMarshal
DefaultKID *string `xml:"cenc:default_KID,attr"`
Value *string `xml:"value,attr"` // Default: cenc
}
type PlayreadyContentProtectionMarshal struct {
ContentProtectionMarshal
PlayreadyXMLNS *string `xml:"xmlns:mspr,attr,omitempty"`
PRO *string `xml:"mspr:pro,omitempty"`
PSSH *string `xml:"cenc:pssh,omitempty"`
}
type WidevineContentProtectionMarshal struct {
ContentProtectionMarshal
PSSH *string `xml:"cenc:pssh,omitempty"`
}
func (s ContentProtection) ContentProtected() {}
func (s ContentProtection) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
err := e.Encode(&ContentProtectionMarshal{
s.AdaptationSet,
s.XMLName,
s.SchemeIDURI,
s.XMLNS,
s.Attrs,
})
if err != nil {
return err
}
return nil
}
func (s CENCContentProtection) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
err := e.Encode(&CENCContentProtectionMarshal{
ContentProtectionMarshal{
s.AdaptationSet,
s.XMLName,
s.SchemeIDURI,
s.XMLNS,
s.Attrs,
},
s.DefaultKID,
s.Value,
})
if err != nil {
return err
}
return nil
}
func (s PlayreadyContentProtection) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
err := e.Encode(&PlayreadyContentProtectionMarshal{
ContentProtectionMarshal{
s.AdaptationSet,
s.XMLName,
s.SchemeIDURI,
s.XMLNS,
s.Attrs,
},
s.PlayreadyXMLNS,
s.PRO,
s.PSSH,
})
if err != nil {
return err
}
return nil
}
func (s WidevineContentProtection) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
err := e.Encode(&WidevineContentProtectionMarshal{
ContentProtectionMarshal{
s.AdaptationSet,
s.XMLName,
s.SchemeIDURI,
s.XMLNS,
s.Attrs,
},
s.PSSH,
})
if err != nil {
return err
}
return nil
}
type Role struct {
AdaptationSet *AdaptationSet `xml:"-"`
SchemeIDURI *string `xml:"schemeIdUri,attr"`
Value *string `xml:"value,attr"`
}
// Segment Template is for Live Profile Only
type SegmentTemplate struct {
AdaptationSet *AdaptationSet `xml:"-"`
SegmentTimeline *SegmentTimeline `xml:"SegmentTimeline,omitempty"`
PresentationTimeOffset *uint64 `xml:"presentationTimeOffset,attr,omitempty"`
Duration *int64 `xml:"duration,attr"`
Initialization *string `xml:"initialization,attr"`
Media *string `xml:"media,attr"`
StartNumber *int64 `xml:"startNumber,attr"`
Timescale *int64 `xml:"timescale,attr"`
}
type Representation struct {
CommonAttributesAndElements
AdaptationSet *AdaptationSet `xml:"-"`
AudioChannelConfiguration *AudioChannelConfiguration `xml:"AudioChannelConfiguration,omitempty"`
AudioSamplingRate *int64 `xml:"audioSamplingRate,attr"` // Audio
Bandwidth *int64 `xml:"bandwidth,attr"` // Audio + Video
Codecs *string `xml:"codecs,attr"` // Audio + Video
FrameRate *string `xml:"frameRate,attr,omitempty"` // Video
Height *int64 `xml:"height,attr"` // Video
ID *string `xml:"id,attr"` // Audio + Video
Width *int64 `xml:"width,attr"` // Video
BaseURL []string `xml:"BaseURL,omitempty"` // On-Demand Profile
SegmentBase *SegmentBase `xml:"SegmentBase,omitempty"` // On-Demand Profile
SegmentList *SegmentList `xml:"SegmentList,omitempty"`
SegmentTemplate *SegmentTemplate `xml:"SegmentTemplate,omitempty"`
}
type Accessibility struct {
AdaptationSet *AdaptationSet `xml:"-"`
SchemeIdUri *string `xml:"schemeIdUri,attr,omitempty"`
Value *string `xml:"value,attr,omitempty"`
}
type AudioChannelConfiguration struct {
SchemeIDURI *string `xml:"schemeIdUri,attr"`
// Value will be an int for non-Dolby Schemes, and a hexstring for Dolby Schemes, hence we make it a string
Value *string `xml:"value,attr"`
}
func (m *MPD) SetDolbyXMLNs() {
m.XMLNsDolby = Strptr("http://www.dolby.com/ns/online/DASH")
}
// Creates a new static MPD object.
// profile - DASH Profile (Live or OnDemand).
// mediaPresentationDuration - Media Presentation Duration (i.e. PT6M16S).
// minBufferTime - Min Buffer Time (i.e. PT1.97S).
// attributes - Other attributes (optional).
func NewMPD(profile DashProfile, mediaPresentationDuration, minBufferTime string, attributes ...AttrMPD) *MPD {
period := &Period{}
mpd := &MPD{
XMLNs: Strptr("urn:mpeg:dash:schema:mpd:2011"),
Profiles: Strptr((string)(profile)),
Type: Strptr("static"),
MediaPresentationDuration: Strptr(mediaPresentationDuration),
MinBufferTime: Strptr(minBufferTime),
period: period,
Periods: []*Period{period},
}
for i := range attributes {
switch attr := attributes[i].(type) {
case *attrAvailabilityStartTime:
mpd.AvailabilityStartTime = attr.GetStrptr()
}
}
return mpd
}
// Creates a new dynamic MPD object.
// profile - DASH Profile (Live or OnDemand).
// availabilityStartTime - anchor for the computation of the earliest availability time (in UTC).
// minBufferTime - Min Buffer Time (i.e. PT1.97S).
// attributes - Other attributes (optional).
func NewDynamicMPD(profile DashProfile, availabilityStartTime, minBufferTime string, attributes ...AttrMPD) *MPD {
period := &Period{}
mpd := &MPD{
XMLNs: Strptr("urn:mpeg:dash:schema:mpd:2011"),
Profiles: Strptr((string)(profile)),
Type: Strptr("dynamic"),
AvailabilityStartTime: Strptr(availabilityStartTime),
MinBufferTime: Strptr(minBufferTime),
period: period,
Periods: []*Period{period},
UTCTiming: &DescriptorType{},
}
for i := range attributes {
switch attr := attributes[i].(type) {
case *attrMinimumUpdatePeriod:
mpd.MinimumUpdatePeriod = attr.GetStrptr()
case *attrMediaPresentationDuration:
mpd.MediaPresentationDuration = attr.GetStrptr()
case *attrPublishTime:
mpd.PublishTime = attr.GetStrptr()
}
}
return mpd
}
// AddNewPeriod creates a new Period and make it the currently active one.
func (m *MPD) AddNewPeriod() *Period {
if m.period != nil && m.period.ID == "" && m.period.AdaptationSets == nil {
return m.GetCurrentPeriod()
}
period := &Period{}
m.Periods = append(m.Periods, period)
m.period = period
return period
}
// GetCurrentPeriod returns the current Period.
func (m *MPD) GetCurrentPeriod() *Period {
return m.period
}
func (period *Period) SetDuration(d time.Duration) {
period.Duration = Duration(d)
}
// Create a new Adaptation Set for thumbnails.
// mimeType - e.g. (image/jpeg)
func (m *MPD) AddNewAdaptationSetThumbnails(mimeType string) (*AdaptationSet, error) {
return m.period.AddNewAdaptationSetThumbnails(mimeType)
}
func (period *Period) AddNewAdaptationSetThumbnails(mimeType string) (*AdaptationSet, error) {
as := &AdaptationSet{
ContentType: Strptr(DASH_CONTENT_TYPE_IMAGE),
CommonAttributesAndElements: CommonAttributesAndElements{
MimeType: Strptr(mimeType),
},
}
err := period.addAdaptationSet(as)
if err != nil {
return nil, err
}
return as, nil
}
func (m *MPD) AddNewAdaptationSetThumbnailsWithID(id, mimeType string) (*AdaptationSet, error) {
return m.period.AddNewAdaptationSetThumbnailsWithID(id, mimeType)
}
func (period *Period) AddNewAdaptationSetThumbnailsWithID(id, mimeType string) (*AdaptationSet, error) {
as := &AdaptationSet{
ID: Strptr(id),
ContentType: Strptr(DASH_CONTENT_TYPE_IMAGE),
CommonAttributesAndElements: CommonAttributesAndElements{
MimeType: Strptr(mimeType),
},
}
err := period.addAdaptationSet(as)
if err != nil {
return nil, err
}
return as, nil
}
// Create a new Adaptation Set for Audio Assets.
// mimeType - MIME Type (i.e. audio/mp4).
// segmentAlignment - Segment Alignment(i.e. true).
// startWithSAP - Starts With SAP (i.e. 1).
// lang - Language (i.e. en).
func (m *MPD) AddNewAdaptationSetAudio(mimeType string, segmentAlignment bool, startWithSAP int64, lang string) (*AdaptationSet, error) {
return m.period.AddNewAdaptationSetAudio(mimeType, segmentAlignment, startWithSAP, lang)
}
// Create a new Adaptation Set for Audio Assets.
// mimeType - MIME Type (i.e. audio/mp4).
// segmentAlignment - Segment Alignment(i.e. true).
// startWithSAP - Starts With SAP (i.e. 1).
// lang - Language (i.e. en).
func (m *MPD) AddNewAdaptationSetAudioWithID(id string, mimeType string, segmentAlignment bool, startWithSAP int64, lang string) (*AdaptationSet, error) {
return m.period.AddNewAdaptationSetAudioWithID(id, mimeType, segmentAlignment, startWithSAP, lang)
}
// Create a new Adaptation Set for Audio Assets.
// mimeType - MIME Type (i.e. audio/mp4).
// segmentAlignment - Segment Alignment(i.e. true).
// startWithSAP - Starts With SAP (i.e. 1).
// lang - Language (i.e. en).
func (period *Period) AddNewAdaptationSetAudio(mimeType string, segmentAlignment bool, startWithSAP int64, lang string) (*AdaptationSet, error) {
as := &AdaptationSet{
SegmentAlignment: Boolptr(segmentAlignment),
Lang: Strptr(lang),
CommonAttributesAndElements: CommonAttributesAndElements{
MimeType: Strptr(mimeType),
StartWithSAP: Int64ptr(startWithSAP),
},
}
err := period.addAdaptationSet(as)
if err != nil {
return nil, err
}
return as, nil
}
// Create a new Adaptation Set for Audio Assets.
// mimeType - MIME Type (i.e. audio/mp4).
// segmentAlignment - Segment Alignment(i.e. true).
// startWithSAP - Starts With SAP (i.e. 1).
// lang - Language (i.e. en).
func (period *Period) AddNewAdaptationSetAudioWithID(id string, mimeType string, segmentAlignment bool, startWithSAP int64, lang string) (*AdaptationSet, error) {
as := &AdaptationSet{
ID: Strptr(id),
SegmentAlignment: Boolptr(segmentAlignment),
Lang: Strptr(lang),
CommonAttributesAndElements: CommonAttributesAndElements{
MimeType: Strptr(mimeType),
StartWithSAP: Int64ptr(startWithSAP),
},
}
err := period.addAdaptationSet(as)
if err != nil {
return nil, err
}
return as, nil
}
// Create a new Adaptation Set for Video Assets.
// mimeType - MIME Type (i.e. video/mp4).
// scanType - Scan Type (i.e.progressive).
// segmentAlignment - Segment Alignment(i.e. true).
// startWithSAP - Starts With SAP (i.e. 1).
func (m *MPD) AddNewAdaptationSetVideo(mimeType string, scanType string, segmentAlignment bool, startWithSAP int64) (*AdaptationSet, error) {
return m.period.AddNewAdaptationSetVideo(mimeType, scanType, segmentAlignment, startWithSAP)
}
// Create a new Adaptation Set for Video Assets.
// mimeType - MIME Type (i.e. video/mp4).
// scanType - Scan Type (i.e.progressive).
// segmentAlignment - Segment Alignment(i.e. true).
// startWithSAP - Starts With SAP (i.e. 1).
func (m *MPD) AddNewAdaptationSetVideoWithID(id string, mimeType string, scanType string, segmentAlignment bool, startWithSAP int64) (*AdaptationSet, error) {
return m.period.AddNewAdaptationSetVideoWithID(id, mimeType, scanType, segmentAlignment, startWithSAP)
}
// Create a new Adaptation Set for Video Assets.
// mimeType - MIME Type (i.e. video/mp4).
// scanType - Scan Type (i.e.progressive).
// segmentAlignment - Segment Alignment(i.e. true).
// startWithSAP - Starts With SAP (i.e. 1).
func (period *Period) AddNewAdaptationSetVideo(mimeType string, scanType string, segmentAlignment bool, startWithSAP int64) (*AdaptationSet, error) {
as := &AdaptationSet{
SegmentAlignment: Boolptr(segmentAlignment),
CommonAttributesAndElements: CommonAttributesAndElements{
MimeType: Strptr(mimeType),
StartWithSAP: Int64ptr(startWithSAP),
ScanType: Strptr(scanType),
},
}
err := period.addAdaptationSet(as)
if err != nil {
return nil, err
}
return as, nil
}
// Create a new Adaptation Set for Video Assets.
// mimeType - MIME Type (i.e. video/mp4).
// scanType - Scan Type (i.e.progressive).
// segmentAlignment - Segment Alignment(i.e. true).
// startWithSAP - Starts With SAP (i.e. 1).
func (period *Period) AddNewAdaptationSetVideoWithID(id string, mimeType string, scanType string, segmentAlignment bool, startWithSAP int64) (*AdaptationSet, error) {
as := &AdaptationSet{
SegmentAlignment: Boolptr(segmentAlignment),
ID: Strptr(id),
CommonAttributesAndElements: CommonAttributesAndElements{
MimeType: Strptr(mimeType),
StartWithSAP: Int64ptr(startWithSAP),
ScanType: Strptr(scanType),
},
}
err := period.addAdaptationSet(as)
if err != nil {
return nil, err
}
return as, nil
}
// Create a new Adaptation Set for Subtitle Assets.
// mimeType - MIME Type (i.e. text/vtt).
// lang - Language (i.e. en).
// label - Label for the subtitle from Studio (i.e. American)
func (m *MPD) AddNewAdaptationSetSubtitle(mimeType string, lang string, label string) (*AdaptationSet, error) {
return m.period.AddNewAdaptationSetSubtitle(mimeType, lang, label)
}
// Create a new Adaptation Set for Subtitle Assets.
// mimeType - MIME Type (i.e. text/vtt).
// lang - Language (i.e. en).
// label - Label for the subtitle from Studio (i.e. American)
func (m *MPD) AddNewAdaptationSetSubtitleWithID(id string, mimeType string, lang string, label string) (*AdaptationSet, error) {
return m.period.AddNewAdaptationSetSubtitleWithID(id, mimeType, lang, label)
}
// Create a new Adaptation Set for Subtitle Assets.
// mimeType - MIME Type (i.e. text/vtt).
// lang - Language (i.e. en).
// label - Label for the subtitle from Studio (i.e. American)
func (period *Period) AddNewAdaptationSetSubtitle(mimeType string, lang string, label string) (*AdaptationSet, error) {
as := &AdaptationSet{
Lang: Strptr(lang),
Labels: []string{label},
CommonAttributesAndElements: CommonAttributesAndElements{
MimeType: Strptr(mimeType),
},
}
err := period.addAdaptationSet(as)
if err != nil {
return nil, err
}
return as, nil
}
// Create a new Adaptation Set for Subtitle Assets.
// mimeType - MIME Type (i.e. text/vtt).
// lang - Language (i.e. en).
// label - Label for the subtitle from Studio (i.e. American)
func (period *Period) AddNewAdaptationSetSubtitleWithID(id string, mimeType string, lang string, label string) (*AdaptationSet, error) {
as := &AdaptationSet{
ID: Strptr(id),
Lang: Strptr(lang),
Labels: []string{label},
CommonAttributesAndElements: CommonAttributesAndElements{
MimeType: Strptr(mimeType),
},
}
err := period.addAdaptationSet(as)
if err != nil {
return nil, err
}
return as, nil
}
// Internal helper method for adding a AdapatationSet.
func (period *Period) addAdaptationSet(as *AdaptationSet) error {
if as == nil {
return ErrAdaptationSetNil
}
period.AdaptationSets = append(period.AdaptationSets, as)
return nil
}
// Adds a ContentProtection tag at the root level of an AdaptationSet.
// This ContentProtection tag does not include signaling for any particular DRM scheme.
// defaultKIDHex - Default Key ID as a Hex String.
//
// NOTE: this is only here for Legacy purposes. This will create an invalid UUID.
func (as *AdaptationSet) AddNewContentProtectionRootLegacyUUID(defaultKIDHex string) (*CENCContentProtection, error) {
if len(defaultKIDHex) != 32 || defaultKIDHex == "" {
return nil, ErrInvalidDefaultKID
}
// Convert the KID into the correct format
defaultKID := strings.ToLower(defaultKIDHex[0:8] + "-" + defaultKIDHex[8:12] + "-" + defaultKIDHex[12:16] + "-" + defaultKIDHex[16:32])
cp := &CENCContentProtection{
DefaultKID: Strptr(defaultKID),
Value: Strptr(CONTENT_PROTECTION_ROOT_VALUE),
}
cp.SchemeIDURI = Strptr(CONTENT_PROTECTION_ROOT_SCHEME_ID_URI)
cp.XMLNS = Strptr(CENC_XMLNS)
err := as.AddContentProtection(cp)
if err != nil {
return nil, err
}
return cp, nil
}
// Adds a ContentProtection tag at the root level of an AdaptationSet.
// This ContentProtection tag does not include signaling for any particular DRM scheme.
// defaultKIDHex - Default Key ID as a Hex String.
func (as *AdaptationSet) AddNewContentProtectionRoot(defaultKIDHex string) (*CENCContentProtection, error) {
if len(defaultKIDHex) != 32 || defaultKIDHex == "" {
return nil, ErrInvalidDefaultKID
}
// Convert the KID into the correct format
defaultKID := strings.ToLower(defaultKIDHex[0:8] + "-" + defaultKIDHex[8:12] + "-" + defaultKIDHex[12:16] + "-" + defaultKIDHex[16:20] + "-" + defaultKIDHex[20:32])
cp := &CENCContentProtection{
DefaultKID: Strptr(defaultKID),
Value: Strptr(CONTENT_PROTECTION_ROOT_VALUE),
}
cp.SchemeIDURI = Strptr(CONTENT_PROTECTION_ROOT_SCHEME_ID_URI)
cp.XMLNS = Strptr(CENC_XMLNS)
err := as.AddContentProtection(cp)
if err != nil {
return nil, err
}
return cp, nil
}
// AddNewContentProtectionSchemeWidevine adds a new content protection scheme for Widevine DRM to the adaptation set. With
// a <cenc:pssh> element that contains a Base64 encoded PSSH box
// wvHeader - binary representation of Widevine Header
// !!! Note: this function will accept any byte slice as a wvHeader value !!!
func (as *AdaptationSet) AddNewContentProtectionSchemeWidevineWithPSSH(wvHeader []byte) (*WidevineContentProtection, error) {
cp, err := NewWidevineContentProtection(wvHeader)
if err != nil {
return nil, err
}
err = as.AddContentProtection(cp)
if err != nil {
return nil, err
}
return cp, nil
}
// AddNewContentProtectionSchemeWidevine adds a new content protection scheme for Widevine DRM to the adaptation set.
func (as *AdaptationSet) AddNewContentProtectionSchemeWidevine() (*WidevineContentProtection, error) {
cp, err := NewWidevineContentProtection(nil)
if err != nil {
return nil, err
}
err = as.AddContentProtection(cp)
if err != nil {
return nil, err
}
return cp, nil
}
func NewWidevineContentProtection(wvHeader []byte) (*WidevineContentProtection, error) {
cp := &WidevineContentProtection{}
cp.SchemeIDURI = Strptr(CONTENT_PROTECTION_WIDEVINE_SCHEME_ID)
if len(wvHeader) > 0 {
cp.XMLNS = Strptr(CENC_XMLNS)
wvSystemID, err := hex.DecodeString(CONTENT_PROTECTION_WIDEVINE_SCHEME_HEX)
if err != nil {
panic(err.Error())
}
psshBox, err := MakePSSHBox(wvSystemID, wvHeader)
if err != nil {
return nil, err
}
psshB64 := base64.StdEncoding.EncodeToString(psshBox)
cp.PSSH = &psshB64
}
return cp, nil
}
// AddNewContentProtectionSchemePlayready adds a new content protection scheme for PlayReady DRM.
// pro - PlayReady Object Header, as a Base64 encoded string.
func (as *AdaptationSet) AddNewContentProtectionSchemePlayready(pro string) (*PlayreadyContentProtection, error) {
cp, err := newPlayreadyContentProtection(pro, CONTENT_PROTECTION_PLAYREADY_SCHEME_ID)
if err != nil {
return nil, err
}
err = as.AddContentProtection(cp)
if err != nil {
return nil, err
}
return cp, nil
}
// AddNewContentProtectionSchemePlayreadyV10 adds a new content protection scheme for PlayReady v1.0 DRM.
// pro - PlayReady Object Header, as a Base64 encoded string.
func (as *AdaptationSet) AddNewContentProtectionSchemePlayreadyV10(pro string) (*PlayreadyContentProtection, error) {
cp, err := newPlayreadyContentProtection(pro, CONTENT_PROTECTION_PLAYREADY_SCHEME_V10_ID)
if err != nil {
return nil, err
}
err = as.AddContentProtection(cp)
if err != nil {
return nil, err
}
return cp, nil
}
func newPlayreadyContentProtection(pro string, schemeIDURI string) (*PlayreadyContentProtection, error) {
if pro == "" {
return nil, ErrPROEmpty
}
cp := &PlayreadyContentProtection{
PlayreadyXMLNS: Strptr(CONTENT_PROTECTION_PLAYREADY_XMLNS),
PRO: Strptr(pro),
}
cp.SchemeIDURI = Strptr(schemeIDURI)
return cp, nil
}
// AddNewContentProtectionSchemePlayreadyWithPSSH adds a new content protection scheme for PlayReady DRM. The scheme
// will include both ms:pro and cenc:pssh subelements
// pro - PlayReady Object Header, as a Base64 encoded string.
func (as *AdaptationSet) AddNewContentProtectionSchemePlayreadyWithPSSH(pro string) (*PlayreadyContentProtection, error) {
cp, err := newPlayreadyContentProtection(pro, CONTENT_PROTECTION_PLAYREADY_SCHEME_ID)
if err != nil {
return nil, err
}
cp.XMLNS = Strptr(CENC_XMLNS)
prSystemID, err := hex.DecodeString(CONTENT_PROTECTION_PLAYREADY_SCHEME_HEX)
if err != nil {
panic(err.Error())
}
proBin, err := base64.StdEncoding.DecodeString(pro)
if err != nil {
return nil, err
}
psshBox, err := MakePSSHBox(prSystemID, proBin)
if err != nil {
return nil, err
}
cp.PSSH = Strptr(base64.StdEncoding.EncodeToString(psshBox))
err = as.AddContentProtection(cp)
if err != nil {
return nil, err
}
return cp, nil
}
// AddNewContentProtectionSchemePlayreadyV10WithPSSH adds a new content protection scheme for PlayReady v1.0 DRM. The scheme
// will include both ms:pro and cenc:pssh subelements
// pro - PlayReady Object Header, as a Base64 encoded string.
func (as *AdaptationSet) AddNewContentProtectionSchemePlayreadyV10WithPSSH(pro string) (*PlayreadyContentProtection, error) {
cp, err := newPlayreadyContentProtection(pro, CONTENT_PROTECTION_PLAYREADY_SCHEME_V10_ID)
if err != nil {
return nil, err
}
cp.XMLNS = Strptr(CENC_XMLNS)
prSystemID, err := hex.DecodeString(CONTENT_PROTECTION_PLAYREADY_SCHEME_V10_HEX)
if err != nil {
panic(err.Error())
}
proBin, err := base64.StdEncoding.DecodeString(pro)
if err != nil {
return nil, err
}
psshBox, err := MakePSSHBox(prSystemID, proBin)
if err != nil {
return nil, err
}
cp.PSSH = Strptr(base64.StdEncoding.EncodeToString(psshBox))
err = as.AddContentProtection(cp)
if err != nil {
return nil, err
}
return cp, nil
}
// Internal helper method for adding a ContentProtection to an AdaptationSet.
func (as *AdaptationSet) AddContentProtection(cp ContentProtectioner) error {
if cp == nil {
return ErrContentProtectionNil
}
as.ContentProtection = append(as.ContentProtection, cp)
return nil
}
// Sets up a new SegmentTemplate for an AdaptationSet.
// duration - relative to timescale (i.e. 2000).
// init - template string for init segment (i.e. $RepresentationID$/audio/en/init.mp4).
// media - template string for media segments.
// startNumber - the number to start segments from ($Number$) (i.e. 0).
// timescale - sets the timescale for duration (i.e. 1000, represents milliseconds).
func (as *AdaptationSet) SetNewSegmentTemplate(duration int64, init string, media string, startNumber int64, timescale int64) (*SegmentTemplate, error) {
st := &SegmentTemplate{
Duration: Int64ptr(duration),
Initialization: Strptr(init),
Media: Strptr(media),
StartNumber: Int64ptr(startNumber),
Timescale: Int64ptr(timescale),
}
err := as.setSegmentTemplate(st)
if err != nil {
return nil, err
}
return st, nil
}
// Internal helper method for setting the Segment Template on an AdaptationSet.
func (as *AdaptationSet) setSegmentTemplate(st *SegmentTemplate) error {
if st == nil {
return ErrSegmentTemplateNil
}
st.AdaptationSet = as
as.SegmentTemplate = st
return nil
}
// Adds a new SegmentTemplate to a thumbnail AdaptationSet
// duration - relative to timescale (i.e. 2000).
// media - template string for media segments.
// startNumber - the number to start segments from ($Number$) (i.e. 0).
// timescale - sets the timescale for duration (i.e. 1000, represents milliseconds).
func (as *AdaptationSet) SetNewSegmentTemplateThumbnails(duration int64, media string, startNumber int64, timescale int64) (*SegmentTemplate, error) {
st := &SegmentTemplate{
Duration: Int64ptr(duration),
Media: Strptr(media),
StartNumber: Int64ptr(startNumber),
Timescale: Int64ptr(timescale),
}
err := as.setSegmentTemplate(st)
if err != nil {
return nil, err
}
return st, nil
}
// Adds a new Thumbnail representation to an AdaptationSet.
// bandwidth - in Bits/s (i.e. 1518664).
// id - ID for this representation, will get used as $RepresentationID$ in template strings.
// width - width of the video (i.e. 1280).
// height - height of the video (i.e 720).
// uri -
func (as *AdaptationSet) AddNewRepresentationThumbnails(id, val, uri string, bandwidth, width, height int64) (*Representation, error) {
r := &Representation{
Bandwidth: Int64ptr(bandwidth),
ID: Strptr(id),
Width: Int64ptr(width),
Height: Int64ptr(height),
CommonAttributesAndElements: CommonAttributesAndElements{
EssentialProperty: []DescriptorType{
{
SchemeIDURI: Strptr(uri),
Value: Strptr(val),
},
},
},
}
err := as.addRepresentation(r)
if err != nil {