shiguredo_mpd 2026.1.0-canary.0

MPD parser for MPEG-DASH
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
//! MPD 構造体から XML 文字列へのシリアライズ

use std::io::Cursor;

use xml::writer::{EmitterConfig, EventWriter, XmlEvent};

use crate::duration::format_duration;
use crate::types::*;

/// MPD 構造体を XML 文字列にシリアライズする
pub fn write(mpd: &Mpd) -> String {
    let mut buf = Cursor::new(Vec::new());
    let mut w = EmitterConfig::new()
        .perform_indent(true)
        .indent_string("  ")
        .write_document_declaration(true)
        .create_writer(&mut buf);

    write_mpd(&mut w, mpd);

    String::from_utf8(buf.into_inner()).expect("MPD XML must be valid UTF-8")
}

/// MPD ルート要素を書き出す
fn write_mpd(w: &mut EventWriter<&mut Cursor<Vec<u8>>>, mpd: &Mpd) {
    let ns = "urn:mpeg:dash:schema:mpd:2011";
    let type_str = match mpd.presentation_type {
        PresentationType::Static => "static",
        PresentationType::Dynamic => "dynamic",
    };

    // XLink 属性が存在する場合は xlink 名前空間を宣言する
    let has_xlink = mpd.periods.iter().any(|p| {
        p.xlink_href.is_some()
            || p.adaptation_sets.iter().any(|as_| {
                as_.segment_list
                    .as_ref()
                    .is_some_and(|sl| sl.xlink_href.is_some())
                    || as_.representations.iter().any(|r| {
                        r.segment_list
                            .as_ref()
                            .is_some_and(|sl| sl.xlink_href.is_some())
                    })
            })
            || p.segment_list
                .as_ref()
                .is_some_and(|sl| sl.xlink_href.is_some())
    });

    let mut el = XmlEvent::start_element("MPD")
        .default_ns(ns)
        .attr("type", type_str)
        .attr("profiles", &mpd.profiles);
    if has_xlink {
        el = el.ns("xlink", "http://www.w3.org/1999/xlink");
    }

    let id_str;
    if let Some(ref id) = mpd.id {
        id_str = id.clone();
        el = el.attr("id", &id_str);
    }

    let min_buffer_time = format_duration(mpd.min_buffer_time);
    el = el.attr("minBufferTime", &min_buffer_time);

    // 可変属性を一時変数に保持する
    let duration_str;
    let update_str;
    let shift_str;
    let delay_str;
    let max_seg_str;
    let max_subseg_str;
    if let Some(d) = mpd.media_presentation_duration {
        duration_str = format_duration(d);
        el = el.attr("mediaPresentationDuration", &duration_str);
    }
    if let Some(d) = mpd.minimum_update_period {
        update_str = format_duration(d);
        el = el.attr("minimumUpdatePeriod", &update_str);
    }
    if let Some(ref s) = mpd.availability_start_time {
        el = el.attr("availabilityStartTime", s);
    }
    if let Some(ref s) = mpd.availability_end_time {
        el = el.attr("availabilityEndTime", s);
    }
    if let Some(d) = mpd.time_shift_buffer_depth {
        shift_str = format_duration(d);
        el = el.attr("timeShiftBufferDepth", &shift_str);
    }
    if let Some(d) = mpd.suggested_presentation_delay {
        delay_str = format_duration(d);
        el = el.attr("suggestedPresentationDelay", &delay_str);
    }
    if let Some(ref s) = mpd.publish_time {
        el = el.attr("publishTime", s);
    }
    if let Some(d) = mpd.max_segment_duration {
        max_seg_str = format_duration(d);
        el = el.attr("maxSegmentDuration", &max_seg_str);
    }
    if let Some(d) = mpd.max_subsegment_duration {
        max_subseg_str = format_duration(d);
        el = el.attr("maxSubsegmentDuration", &max_subseg_str);
    }

    w.write(el).expect("failed to write MPD element");

    for base_url in &mpd.base_urls {
        write_base_url(w, base_url);
    }
    for utc in &mpd.utc_timings {
        write_utc_timing(w, utc);
    }
    for loc in &mpd.locations {
        write_location(w, loc);
    }
    for sd in &mpd.service_descriptions {
        write_service_description(w, sd);
    }
    if let Some(ref cs) = mpd.content_steering {
        write_content_steering(w, cs);
    }
    for pl in &mpd.patch_locations {
        write_patch_location(w, pl);
    }
    for ep in &mpd.essential_properties {
        write_descriptor(w, "EssentialProperty", ep);
    }
    for sp in &mpd.supplemental_properties {
        write_descriptor(w, "SupplementalProperty", sp);
    }
    for m in &mpd.metrics {
        write_metrics(w, m);
    }
    for period in &mpd.periods {
        write_period(w, period);
    }

    w.write(XmlEvent::end_element())
        .expect("failed to close MPD element");
}

/// Period 要素を書き出す
fn write_period(w: &mut EventWriter<&mut Cursor<Vec<u8>>>, period: &Period) {
    let start_str;
    let dur_str;

    let mut el = XmlEvent::start_element("Period");
    if let Some(ref id) = period.id {
        el = el.attr("id", id);
    }
    if let Some(s) = period.start {
        start_str = format_duration(s);
        el = el.attr("start", &start_str);
    }
    if let Some(d) = period.duration {
        dur_str = format_duration(d);
        el = el.attr("duration", &dur_str);
    }
    if let Some(ref href) = period.xlink_href {
        el = el.attr("xlink:href", href);
    }
    if let Some(ref actuate) = period.xlink_actuate {
        el = el.attr("xlink:actuate", actuate);
    }
    w.write(el).expect("failed to write Period element");

    for base_url in &period.base_urls {
        write_base_url(w, base_url);
    }
    for sp in &period.supplemental_properties {
        write_descriptor(w, "SupplementalProperty", sp);
    }
    for ep in &period.essential_properties {
        write_descriptor(w, "EssentialProperty", ep);
    }
    if let Some(ref ai) = period.asset_identifier {
        write_descriptor(w, "AssetIdentifier", ai);
    }
    for es in &period.event_streams {
        write_event_stream(w, es);
    }
    for ps in &period.preselections {
        write_preselection(w, ps);
    }
    if let Some(ref sb) = period.segment_base {
        write_segment_base(w, sb);
    }
    if let Some(ref sl) = period.segment_list {
        write_segment_list(w, sl);
    }
    if let Some(ref st) = period.segment_template {
        write_segment_template(w, st);
    }
    for subset in &period.subsets {
        write_subset(w, subset);
    }
    for as_ in &period.adaptation_sets {
        write_adaptation_set(w, as_);
    }

    w.write(XmlEvent::end_element())
        .expect("failed to close Period element");
}

/// AdaptationSet 要素を書き出す
fn write_adaptation_set(w: &mut EventWriter<&mut Cursor<Vec<u8>>>, as_: &AdaptationSet) {
    let id_str;
    let width_str;
    let height_str;

    let mut el = XmlEvent::start_element("AdaptationSet");
    if let Some(id) = as_.id {
        id_str = id.to_string();
        el = el.attr("id", &id_str);
    }
    if let Some(ref mt) = as_.mime_type {
        el = el.attr("mimeType", mt);
    }
    if let Some(ref c) = as_.codecs {
        el = el.attr("codecs", c);
    }
    if let Some(ct) = as_.content_type {
        let ct_str = match ct {
            ContentType::Video => "video",
            ContentType::Audio => "audio",
            ContentType::Text => "text",
            ContentType::Image => "image",
            ContentType::Muxed => "muxed",
        };
        el = el.attr("contentType", ct_str);
    }
    if let Some(ref lang) = as_.lang {
        el = el.attr("lang", lang);
    }
    if let Some(w_val) = as_.width {
        width_str = w_val.to_string();
        el = el.attr("width", &width_str);
    }
    if let Some(h_val) = as_.height {
        height_str = h_val.to_string();
        el = el.attr("height", &height_str);
    }
    if let Some(ref fr) = as_.frame_rate {
        el = el.attr("frameRate", fr);
    }
    let mnw_str;
    if let Some(mnw) = as_.min_width {
        mnw_str = mnw.to_string();
        el = el.attr("minWidth", &mnw_str);
    }
    let mnh_str;
    if let Some(mnh) = as_.min_height {
        mnh_str = mnh.to_string();
        el = el.attr("minHeight", &mnh_str);
    }
    if let Some(ref mnfr) = as_.min_frame_rate {
        el = el.attr("minFrameRate", mnfr);
    }
    let mnbw_str;
    if let Some(mnbw) = as_.min_bandwidth {
        mnbw_str = mnbw.to_string();
        el = el.attr("minBandwidth", &mnbw_str);
    }
    let mxbw_str;
    if let Some(mxbw) = as_.max_bandwidth {
        mxbw_str = mxbw.to_string();
        el = el.attr("maxBandwidth", &mxbw_str);
    }
    let mw_str;
    if let Some(mw) = as_.max_width {
        mw_str = mw.to_string();
        el = el.attr("maxWidth", &mw_str);
    }
    let mh_str;
    if let Some(mh) = as_.max_height {
        mh_str = mh.to_string();
        el = el.attr("maxHeight", &mh_str);
    }
    if let Some(ref mfr) = as_.max_frame_rate {
        el = el.attr("maxFrameRate", mfr);
    }
    if let Some(ref p) = as_.par {
        el = el.attr("par", p);
    }
    let asr_as_str;
    if let Some(asr) = as_.audio_sampling_rate {
        asr_as_str = asr.to_string();
        el = el.attr("audioSamplingRate", &asr_as_str);
    }
    if let Some(ref s) = as_.sar {
        el = el.attr("sar", s);
    }
    if let Some(ref p) = as_.profiles {
        el = el.attr("profiles", p);
    }
    if let Some(ref st) = as_.scan_type {
        el = el.attr("scanType", st);
    }
    let sap_str;
    if let Some(sap) = as_.start_with_sap {
        sap_str = sap.to_string();
        el = el.attr("startWithSAP", &sap_str);
    }
    let mpr_str;
    if let Some(mpr) = as_.max_playout_rate {
        mpr_str = mpr.to_string();
        el = el.attr("maxPlayoutRate", &mpr_str);
    }
    let sp_str;
    if let Some(sp) = as_.selection_priority {
        sp_str = sp.to_string();
        el = el.attr("selectionPriority", &sp_str);
    }
    if let Some(ref sc) = as_.supplemental_codecs {
        el = el.attr("scte214:supplementalCodecs", sc);
    }
    if let Some(cd) = as_.coding_dependency {
        el = el.attr("codingDependency", if cd { "true" } else { "false" });
    }
    let msap_str;
    if let Some(msap) = as_.maximum_sap_period {
        msap_str = format_duration(msap);
        el = el.attr("maximumSAPPeriod", &msap_str);
    }
    if let Some(ref sgp) = as_.segment_profiles {
        el = el.attr("segmentProfiles", sgp);
    }
    if as_.segment_alignment {
        el = el.attr("segmentAlignment", "true");
    }
    if as_.subsegment_alignment {
        el = el.attr("subsegmentAlignment", "true");
    }
    if let Some(bs) = as_.bitstream_switching {
        el = el.attr("bitstreamSwitching", if bs { "true" } else { "false" });
    }
    w.write(el).expect("failed to write AdaptationSet element");

    for base_url in &as_.base_urls {
        write_base_url(w, base_url);
    }
    for role in &as_.roles {
        write_descriptor(w, "Role", role);
    }
    for acc in &as_.accessibilities {
        write_descriptor(w, "Accessibility", acc);
    }
    for acc in &as_.audio_channel_configurations {
        write_descriptor(w, "AudioChannelConfiguration", acc);
    }
    for label in &as_.labels {
        let mut el = XmlEvent::start_element("Label");
        if let Some(ref lang) = label.lang {
            el = el.attr("lang", lang);
        }
        w.write(el).expect("failed to write Label element");
        w.write(XmlEvent::characters(&label.text))
            .expect("failed to write Label content");
        w.write(XmlEvent::end_element())
            .expect("failed to close Label element");
    }
    for label in &as_.group_labels {
        let mut el = XmlEvent::start_element("GroupLabel");
        if let Some(ref lang) = label.lang {
            el = el.attr("lang", lang);
        }
        w.write(el).expect("failed to write GroupLabel element");
        w.write(XmlEvent::characters(&label.text))
            .expect("failed to write GroupLabel content");
        w.write(XmlEvent::end_element())
            .expect("failed to close GroupLabel element");
    }
    for ep in &as_.essential_properties {
        write_descriptor(w, "EssentialProperty", ep);
    }
    for sp in &as_.supplemental_properties {
        write_descriptor(w, "SupplementalProperty", sp);
    }
    for vp in &as_.viewpoints {
        write_descriptor(w, "Viewpoint", vp);
    }
    for fp in &as_.frame_packings {
        write_descriptor(w, "FramePacking", fp);
    }
    for ies in &as_.inband_event_streams {
        write_descriptor(w, "InbandEventStream", ies);
    }
    for prt in &as_.producer_reference_times {
        write_producer_reference_time(w, prt);
    }
    for cc in &as_.content_components {
        write_content_component(w, cc);
    }
    for ssp in &as_.segment_sequence_properties {
        write_segment_sequence_properties(w, ssp);
    }
    for es in &as_.event_streams {
        write_event_stream(w, es);
    }
    for cp in &as_.content_protections {
        write_content_protection(w, cp);
    }
    if let Some(ref sb) = as_.segment_base {
        write_segment_base(w, sb);
    }
    if let Some(ref sl) = as_.segment_list {
        write_segment_list(w, sl);
    }
    if let Some(ref st) = as_.segment_template {
        write_segment_template(w, st);
    }
    for rep in &as_.representations {
        write_representation(w, rep);
    }

    w.write(XmlEvent::end_element())
        .expect("failed to close AdaptationSet element");
}

/// Representation 要素を書き出す
fn write_representation(w: &mut EventWriter<&mut Cursor<Vec<u8>>>, rep: &Representation) {
    let bw_str = rep.bandwidth.to_string();
    let width_str;
    let height_str;
    let asr_str;

    let mut el = XmlEvent::start_element("Representation")
        .attr("id", &rep.id)
        .attr("bandwidth", &bw_str);

    if let Some(ref mt) = rep.mime_type {
        el = el.attr("mimeType", mt);
    }
    if let Some(ref c) = rep.codecs {
        el = el.attr("codecs", c);
    }
    if let Some(w_val) = rep.width {
        width_str = w_val.to_string();
        el = el.attr("width", &width_str);
    }
    if let Some(h_val) = rep.height {
        height_str = h_val.to_string();
        el = el.attr("height", &height_str);
    }
    if let Some(ref fr) = rep.frame_rate {
        el = el.attr("frameRate", fr);
    }
    if let Some(asr) = rep.audio_sampling_rate {
        asr_str = asr.to_string();
        el = el.attr("audioSamplingRate", &asr_str);
    }
    if let Some(ref s) = rep.sar {
        el = el.attr("sar", s);
    }
    let qr_str;
    if let Some(qr) = rep.quality_ranking {
        qr_str = qr.to_string();
        el = el.attr("qualityRanking", &qr_str);
    }
    if let Some(ref di) = rep.dependency_id {
        el = el.attr("dependencyId", di);
    }
    let rep_mpr_str;
    if let Some(mpr) = rep.max_playout_rate {
        rep_mpr_str = mpr.to_string();
        el = el.attr("maxPlayoutRate", &rep_mpr_str);
    }
    if let Some(ref st) = rep.scan_type {
        el = el.attr("scanType", st);
    }
    let rep_sap_str;
    if let Some(sap) = rep.start_with_sap {
        rep_sap_str = sap.to_string();
        el = el.attr("startWithSAP", &rep_sap_str);
    }
    if let Some(ref p) = rep.profiles {
        el = el.attr("profiles", p);
    }
    if let Some(ref sc) = rep.supplemental_codecs {
        el = el.attr("scte214:supplementalCodecs", sc);
    }
    if let Some(cd) = rep.coding_dependency {
        el = el.attr("codingDependency", if cd { "true" } else { "false" });
    }
    if let Some(ref cpd) = rep.codec_private_data {
        el = el.attr("codecPrivateData", cpd);
    }
    if let Some(ref mss) = rep.media_stream_structure_id {
        el = el.attr("mediaStreamStructureId", mss);
    }
    let rep_msap_str;
    if let Some(msap) = rep.maximum_sap_period {
        rep_msap_str = format_duration(msap);
        el = el.attr("maximumSAPPeriod", &rep_msap_str);
    }
    if let Some(ref sgp) = rep.segment_profiles {
        el = el.attr("segmentProfiles", sgp);
    }
    w.write(el).expect("failed to write Representation element");

    for base_url in &rep.base_urls {
        write_base_url(w, base_url);
    }
    for acc in &rep.audio_channel_configurations {
        write_descriptor(w, "AudioChannelConfiguration", acc);
    }
    for ep in &rep.essential_properties {
        write_descriptor(w, "EssentialProperty", ep);
    }
    for sp in &rep.supplemental_properties {
        write_descriptor(w, "SupplementalProperty", sp);
    }
    for fp in &rep.frame_packings {
        write_descriptor(w, "FramePacking", fp);
    }
    for ies in &rep.inband_event_streams {
        write_descriptor(w, "InbandEventStream", ies);
    }
    for prt in &rep.producer_reference_times {
        write_producer_reference_time(w, prt);
    }
    for ssp in &rep.segment_sequence_properties {
        write_segment_sequence_properties(w, ssp);
    }
    for sr in &rep.sub_representations {
        write_sub_representation(w, sr);
    }
    for cp in &rep.content_protections {
        write_content_protection(w, cp);
    }
    if let Some(ref sb) = rep.segment_base {
        write_segment_base(w, sb);
    }
    if let Some(ref sl) = rep.segment_list {
        write_segment_list(w, sl);
    }
    if let Some(ref st) = rep.segment_template {
        write_segment_template(w, st);
    }

    w.write(XmlEvent::end_element())
        .expect("failed to close Representation element");
}

/// SegmentBase 要素を書き出す
fn write_segment_base(w: &mut EventWriter<&mut Cursor<Vec<u8>>>, sb: &SegmentBase) {
    let ts_str = sb.timescale.to_string();
    let pto_str;

    let ato_str;
    let mut el = XmlEvent::start_element("SegmentBase").attr("timescale", &ts_str);
    if let Some(ref ir) = sb.index_range {
        el = el.attr("indexRange", ir);
    }
    if let Some(ire) = sb.index_range_exact {
        el = el.attr("indexRangeExact", if ire { "true" } else { "false" });
    }
    if sb.presentation_time_offset != 0 {
        pto_str = sb.presentation_time_offset.to_string();
        el = el.attr("presentationTimeOffset", &pto_str);
    }
    if let Some(ato) = sb.availability_time_offset {
        ato_str = ato.to_string();
        el = el.attr("availabilityTimeOffset", &ato_str);
    }
    if let Some(atc) = sb.availability_time_complete {
        el = el.attr(
            "availabilityTimeComplete",
            if atc { "true" } else { "false" },
        );
    }
    w.write(el).expect("failed to write SegmentBase element");

    if sb.initialization_source_url.is_some() || sb.initialization_range.is_some() {
        let mut el = XmlEvent::start_element("Initialization");
        if let Some(ref source_url) = sb.initialization_source_url {
            el = el.attr("sourceURL", source_url);
        }
        if let Some(ref range) = sb.initialization_range {
            el = el.attr("range", range);
        }
        w.write(el).expect("failed to write Initialization element");
        w.write(XmlEvent::end_element())
            .expect("failed to close Initialization element");
    }

    if sb.representation_index_source_url.is_some() || sb.representation_index_range.is_some() {
        let mut el = XmlEvent::start_element("RepresentationIndex");
        if let Some(ref source_url) = sb.representation_index_source_url {
            el = el.attr("sourceURL", source_url);
        }
        if let Some(ref range) = sb.representation_index_range {
            el = el.attr("range", range);
        }
        w.write(el)
            .expect("failed to write RepresentationIndex element");
        w.write(XmlEvent::end_element())
            .expect("failed to close RepresentationIndex element");
    }

    if sb.bitstream_switching_source_url.is_some() || sb.bitstream_switching_range.is_some() {
        let mut el = XmlEvent::start_element("BitstreamSwitching");
        if let Some(ref source_url) = sb.bitstream_switching_source_url {
            el = el.attr("sourceURL", source_url);
        }
        if let Some(ref range) = sb.bitstream_switching_range {
            el = el.attr("range", range);
        }
        w.write(el)
            .expect("failed to write BitstreamSwitching element");
        w.write(XmlEvent::end_element())
            .expect("failed to close BitstreamSwitching element");
    }

    w.write(XmlEvent::end_element())
        .expect("failed to close SegmentBase element");
}

/// SegmentList 要素を書き出す
fn write_segment_list(w: &mut EventWriter<&mut Cursor<Vec<u8>>>, sl: &SegmentList) {
    let ts_str = sl.timescale.to_string();
    let sn_str = sl.start_number.to_string();
    let dur_str;
    let pto_str;

    let en_str;
    let ato_str;
    let mut el = XmlEvent::start_element("SegmentList")
        .attr("timescale", &ts_str)
        .attr("startNumber", &sn_str);
    if let Some(ref href) = sl.xlink_href {
        el = el.attr("xlink:href", href);
    }
    if let Some(ref actuate) = sl.xlink_actuate {
        el = el.attr("xlink:actuate", actuate);
    }
    if let Some(en) = sl.end_number {
        en_str = en.to_string();
        el = el.attr("endNumber", &en_str);
    }
    if let Some(d) = sl.duration {
        dur_str = d.to_string();
        el = el.attr("duration", &dur_str);
    }
    if sl.presentation_time_offset != 0 {
        pto_str = sl.presentation_time_offset.to_string();
        el = el.attr("presentationTimeOffset", &pto_str);
    }
    if let Some(ato) = sl.availability_time_offset {
        ato_str = ato.to_string();
        el = el.attr("availabilityTimeOffset", &ato_str);
    }
    if let Some(atc) = sl.availability_time_complete {
        el = el.attr(
            "availabilityTimeComplete",
            if atc { "true" } else { "false" },
        );
    }
    w.write(el).expect("failed to write SegmentList element");

    if sl.initialization_source_url.is_some() || sl.initialization_range.is_some() {
        let mut el = XmlEvent::start_element("Initialization");
        if let Some(ref source_url) = sl.initialization_source_url {
            el = el.attr("sourceURL", source_url);
        }
        if let Some(ref range) = sl.initialization_range {
            el = el.attr("range", range);
        }
        w.write(el).expect("failed to write Initialization element");
        w.write(XmlEvent::end_element())
            .expect("failed to close Initialization element");
    }

    if sl.representation_index_source_url.is_some() || sl.representation_index_range.is_some() {
        let mut el = XmlEvent::start_element("RepresentationIndex");
        if let Some(ref source_url) = sl.representation_index_source_url {
            el = el.attr("sourceURL", source_url);
        }
        if let Some(ref range) = sl.representation_index_range {
            el = el.attr("range", range);
        }
        w.write(el)
            .expect("failed to write RepresentationIndex element");
        w.write(XmlEvent::end_element())
            .expect("failed to close RepresentationIndex element");
    }

    if sl.bitstream_switching_source_url.is_some() || sl.bitstream_switching_range.is_some() {
        let mut el = XmlEvent::start_element("BitstreamSwitching");
        if let Some(ref source_url) = sl.bitstream_switching_source_url {
            el = el.attr("sourceURL", source_url);
        }
        if let Some(ref range) = sl.bitstream_switching_range {
            el = el.attr("range", range);
        }
        w.write(el)
            .expect("failed to write BitstreamSwitching element");
        w.write(XmlEvent::end_element())
            .expect("failed to close BitstreamSwitching element");
    }

    if let Some(ref timeline) = sl.segment_timeline {
        write_segment_timeline(w, timeline);
    }

    for seg_url in &sl.segment_urls {
        let mut el = XmlEvent::start_element("SegmentURL");
        if let Some(ref media) = seg_url.media {
            el = el.attr("media", media);
        }
        if let Some(ref mr) = seg_url.media_range {
            el = el.attr("mediaRange", mr);
        }
        if let Some(ref ir) = seg_url.index_range {
            el = el.attr("indexRange", ir);
        }
        w.write(el).expect("failed to write SegmentURL element");
        w.write(XmlEvent::end_element())
            .expect("failed to close SegmentURL element");
    }

    w.write(XmlEvent::end_element())
        .expect("failed to close SegmentList element");
}

/// SegmentTemplate 要素を書き出す
fn write_segment_template(w: &mut EventWriter<&mut Cursor<Vec<u8>>>, st: &SegmentTemplate) {
    let ts_str = st.timescale.to_string();
    let sn_str = st.start_number.to_string();
    let dur_str;
    let pto_str;

    let en_str;
    let ato_str;
    let mut el = XmlEvent::start_element("SegmentTemplate")
        .attr("timescale", &ts_str)
        .attr("startNumber", &sn_str);

    if let Some(ref media) = st.media {
        el = el.attr("media", media);
    }
    if let Some(ref init) = st.initialization {
        el = el.attr("initialization", init);
    }
    if let Some(ref idx) = st.index {
        el = el.attr("index", idx);
    }
    if let Some(d) = st.duration {
        dur_str = d.to_string();
        el = el.attr("duration", &dur_str);
    }
    if let Some(en) = st.end_number {
        en_str = en.to_string();
        el = el.attr("endNumber", &en_str);
    }
    if st.presentation_time_offset != 0 {
        pto_str = st.presentation_time_offset.to_string();
        el = el.attr("presentationTimeOffset", &pto_str);
    }
    if let Some(ato) = st.availability_time_offset {
        ato_str = ato.to_string();
        el = el.attr("availabilityTimeOffset", &ato_str);
    }
    if let Some(atc) = st.availability_time_complete {
        el = el.attr(
            "availabilityTimeComplete",
            if atc { "true" } else { "false" },
        );
    }
    w.write(el)
        .expect("failed to write SegmentTemplate element");

    if st.bitstream_switching_source_url.is_some() || st.bitstream_switching_range.is_some() {
        let mut el = XmlEvent::start_element("BitstreamSwitching");
        if let Some(ref source_url) = st.bitstream_switching_source_url {
            el = el.attr("sourceURL", source_url);
        }
        if let Some(ref range) = st.bitstream_switching_range {
            el = el.attr("range", range);
        }
        w.write(el)
            .expect("failed to write BitstreamSwitching element");
        w.write(XmlEvent::end_element())
            .expect("failed to close BitstreamSwitching element");
    }

    if let Some(ref timeline) = st.segment_timeline {
        write_segment_timeline(w, timeline);
    }

    w.write(XmlEvent::end_element())
        .expect("failed to close SegmentTemplate element");
}

/// SegmentTimeline 要素を書き出す
fn write_segment_timeline(w: &mut EventWriter<&mut Cursor<Vec<u8>>>, entries: &[TimelineEntry]) {
    w.write(XmlEvent::start_element("SegmentTimeline"))
        .expect("failed to write SegmentTimeline element");

    for entry in entries {
        let d_str = entry.d.to_string();
        let t_str;
        let r_str;
        let k_str;

        let mut el = XmlEvent::start_element("S").attr("d", &d_str);
        if let Some(t) = entry.t {
            t_str = t.to_string();
            el = el.attr("t", &t_str);
        }
        if entry.r != 0 {
            r_str = entry.r.to_string();
            el = el.attr("r", &r_str);
        }
        if let Some(k) = entry.k {
            k_str = k.to_string();
            el = el.attr("k", &k_str);
        }
        w.write(el).expect("failed to write S element");
        w.write(XmlEvent::end_element())
            .expect("failed to close S element");
    }

    w.write(XmlEvent::end_element())
        .expect("failed to close SegmentTimeline element");
}

/// ContentProtection 要素を書き出す
fn write_content_protection(w: &mut EventWriter<&mut Cursor<Vec<u8>>>, cp: &ContentProtection) {
    let mut el =
        XmlEvent::start_element("ContentProtection").attr("schemeIdUri", &cp.scheme_id_uri);
    if let Some(ref v) = cp.value {
        el = el.attr("value", v);
    }
    if let Some(ref kid) = cp.default_kid {
        el = el.attr("cenc:default_KID", kid);
    }
    if let Some(ref r) = cp.robustness {
        el = el.attr("robustness", r);
    }
    if let Some(ref r) = cp.ref_ {
        el = el.attr("ref", r);
    }
    if let Some(ref ri) = cp.ref_id {
        el = el.attr("refId", ri);
    }
    w.write(el)
        .expect("failed to write ContentProtection element");

    if let Some(ref pssh) = cp.pssh {
        w.write(XmlEvent::start_element("cenc:pssh"))
            .expect("failed to write cenc:pssh element");
        w.write(XmlEvent::characters(pssh))
            .expect("failed to write pssh content");
        w.write(XmlEvent::end_element())
            .expect("failed to close cenc:pssh element");
    }
    if let Some(ref laurl) = cp.laurl {
        w.write(XmlEvent::start_element("Laurl"))
            .expect("failed to write Laurl element");
        w.write(XmlEvent::characters(laurl))
            .expect("failed to write Laurl content");
        w.write(XmlEvent::end_element())
            .expect("failed to close Laurl element");
    }
    if let Some(ref pro) = cp.pro {
        w.write(XmlEvent::start_element("pro"))
            .expect("failed to write pro element");
        w.write(XmlEvent::characters(pro))
            .expect("failed to write pro content");
        w.write(XmlEvent::end_element())
            .expect("failed to close pro element");
    }
    for certurl in &cp.certurls {
        let mut el = XmlEvent::start_element("Certurl");
        if let Some(ref ct) = certurl.cert_type {
            el = el.attr("certType", ct);
        }
        w.write(el).expect("failed to write Certurl element");
        w.write(XmlEvent::characters(&certurl.url))
            .expect("failed to write Certurl content");
        w.write(XmlEvent::end_element())
            .expect("failed to close Certurl element");
    }

    w.write(XmlEvent::end_element())
        .expect("failed to close ContentProtection element");
}

/// Descriptor 要素を書き出す
fn write_descriptor(
    w: &mut EventWriter<&mut Cursor<Vec<u8>>>,
    element_name: &str,
    desc: &Descriptor,
) {
    let mut el = XmlEvent::start_element(element_name).attr("schemeIdUri", &desc.scheme_id_uri);
    if let Some(ref v) = desc.value {
        el = el.attr("value", v);
    }
    if let Some(ref id) = desc.id {
        el = el.attr("id", id);
    }
    if let Some(ref url) = desc.dvb_url {
        el = el.attr("dvb:url", url);
    }
    if let Some(ref mt) = desc.dvb_mime_type {
        el = el.attr("dvb:mimeType", mt);
    }
    if let Some(ref ff) = desc.dvb_font_family {
        el = el.attr("dvb:fontFamily", ff);
    }
    w.write(el)
        .unwrap_or_else(|_| panic!("failed to write {element_name} element"));
    w.write(XmlEvent::end_element())
        .unwrap_or_else(|_| panic!("failed to close {element_name} element"));
}

/// UTCTiming 要素を書き出す
fn write_utc_timing(w: &mut EventWriter<&mut Cursor<Vec<u8>>>, utc: &UtcTiming) {
    let mut el = XmlEvent::start_element("UTCTiming").attr("schemeIdUri", &utc.scheme_id_uri);
    if let Some(ref v) = utc.value {
        el = el.attr("value", v);
    }
    if let Some(ref id) = utc.id {
        el = el.attr("id", id);
    }
    w.write(el).expect("failed to write UTCTiming element");
    w.write(XmlEvent::end_element())
        .expect("failed to close UTCTiming element");
}

/// Location 要素を書き出す
fn write_location(w: &mut EventWriter<&mut Cursor<Vec<u8>>>, loc: &Location) {
    let mut el = XmlEvent::start_element("Location");
    if let Some(ref sl) = loc.service_location {
        el = el.attr("serviceLocation", sl);
    }
    w.write(el).expect("failed to write Location element");
    w.write(XmlEvent::characters(&loc.url))
        .expect("failed to write Location content");
    w.write(XmlEvent::end_element())
        .expect("failed to close Location element");
}

/// ServiceDescription 要素を書き出す
fn write_service_description(w: &mut EventWriter<&mut Cursor<Vec<u8>>>, sd: &ServiceDescription) {
    let id_str;
    let mut el = XmlEvent::start_element("ServiceDescription");
    if let Some(id) = sd.id {
        id_str = id.to_string();
        el = el.attr("id", &id_str);
    }
    w.write(el)
        .expect("failed to write ServiceDescription element");

    if let Some(ref scope) = sd.scope {
        write_descriptor(w, "Scope", scope);
    }

    if let Some(ref lat) = sd.latency {
        let target_str;
        let min_str;
        let max_str;
        let ref_id_str;
        let mut el = XmlEvent::start_element("Latency");
        if let Some(t) = lat.target {
            target_str = t.to_string();
            el = el.attr("target", &target_str);
        }
        if let Some(mn) = lat.min {
            min_str = mn.to_string();
            el = el.attr("min", &min_str);
        }
        if let Some(mx) = lat.max {
            max_str = mx.to_string();
            el = el.attr("max", &max_str);
        }
        if let Some(ri) = lat.reference_id {
            ref_id_str = ri.to_string();
            el = el.attr("referenceId", &ref_id_str);
        }
        w.write(el).expect("failed to write Latency element");
        w.write(XmlEvent::end_element())
            .expect("failed to close Latency element");
    }

    if let Some(ref pr) = sd.playback_rate {
        let min_str;
        let max_str;
        let mut el = XmlEvent::start_element("PlaybackRate");
        if let Some(mn) = pr.min {
            min_str = mn.to_string();
            el = el.attr("min", &min_str);
        }
        if let Some(mx) = pr.max {
            max_str = mx.to_string();
            el = el.attr("max", &max_str);
        }
        w.write(el).expect("failed to write PlaybackRate element");
        w.write(XmlEvent::end_element())
            .expect("failed to close PlaybackRate element");
    }

    if let Some(ref oq) = sd.operating_quality {
        write_operating_quality(w, oq);
    }
    if let Some(ref ob) = sd.operating_bandwidth {
        write_operating_bandwidth(w, ob);
    }
    if let Some(ref cdr) = sd.client_data_reporting {
        write_client_data_reporting(w, cdr);
    }

    w.write(XmlEvent::end_element())
        .expect("failed to close ServiceDescription element");
}

/// ContentSteering 要素を書き出す
fn write_content_steering(w: &mut EventWriter<&mut Cursor<Vec<u8>>>, cs: &ContentSteering) {
    let mut el = XmlEvent::start_element("ContentSteering");
    if let Some(ref dsl) = cs.default_service_location {
        el = el.attr("defaultServiceLocation", dsl);
    }
    if let Some(qbs) = cs.query_before_start {
        el = el.attr("queryBeforeStart", if qbs { "true" } else { "false" });
    }
    if let Some(cr) = cs.client_requirement {
        el = el.attr("clientRequirement", if cr { "true" } else { "false" });
    }
    w.write(el)
        .expect("failed to write ContentSteering element");
    w.write(XmlEvent::characters(&cs.server_url))
        .expect("failed to write ContentSteering content");
    w.write(XmlEvent::end_element())
        .expect("failed to close ContentSteering element");
}

/// EventStream 要素を書き出す
fn write_event_stream(w: &mut EventWriter<&mut Cursor<Vec<u8>>>, es: &EventStream) {
    let ts_str = es.timescale.to_string();
    let mut el = XmlEvent::start_element("EventStream").attr("schemeIdUri", &es.scheme_id_uri);
    if let Some(ref v) = es.value {
        el = el.attr("value", v);
    }
    el = el.attr("timescale", &ts_str);
    let pto_str;
    if es.presentation_time_offset != 0 {
        pto_str = es.presentation_time_offset.to_string();
        el = el.attr("presentationTimeOffset", &pto_str);
    }
    w.write(el).expect("failed to write EventStream element");

    for event in &es.events {
        let pt_str = event.presentation_time.to_string();
        let dur_str;
        let mut el = XmlEvent::start_element("Event").attr("presentationTime", &pt_str);
        if let Some(d) = event.duration {
            dur_str = d.to_string();
            el = el.attr("duration", &dur_str);
        }
        if let Some(ref id) = event.id {
            el = el.attr("id", id);
        }
        if let Some(ref md) = event.message_data {
            el = el.attr("messageData", md);
        }
        w.write(el).expect("failed to write Event element");
        if let Some(ref content) = event.content {
            w.write(XmlEvent::characters(content))
                .expect("failed to write Event content");
        }
        if let Some(ref sb) = event.signal_binary {
            w.write(XmlEvent::start_element("Signal"))
                .expect("failed to write Signal element");
            w.write(XmlEvent::start_element("Binary"))
                .expect("failed to write Binary element");
            w.write(XmlEvent::characters(sb))
                .expect("failed to write Binary content");
            w.write(XmlEvent::end_element())
                .expect("failed to close Binary element");
            w.write(XmlEvent::end_element())
                .expect("failed to close Signal element");
        }
        w.write(XmlEvent::end_element())
            .expect("failed to close Event element");
    }

    w.write(XmlEvent::end_element())
        .expect("failed to close EventStream element");
}

/// ProducerReferenceTime 要素を書き出す
fn write_producer_reference_time(
    w: &mut EventWriter<&mut Cursor<Vec<u8>>>,
    prt: &ProducerReferenceTime,
) {
    let id_str = prt.id.to_string();
    let pt_str = prt.presentation_time.to_string();
    let mut el = XmlEvent::start_element("ProducerReferenceTime")
        .attr("id", &id_str)
        .attr("wallClockTime", &prt.wall_clock_time)
        .attr("presentationTime", &pt_str);
    if let Some(inband) = prt.inband {
        el = el.attr("inband", if inband { "true" } else { "false" });
    }
    if let Some(ref t) = prt.type_ {
        el = el.attr("type", t);
    }
    if let Some(ref as_) = prt.application_scheme {
        el = el.attr("applicationScheme", as_);
    }
    w.write(el)
        .expect("failed to write ProducerReferenceTime element");

    if let Some(ref utc) = prt.utc_timing {
        write_utc_timing(w, utc);
    }

    w.write(XmlEvent::end_element())
        .expect("failed to close ProducerReferenceTime element");
}

/// SubRepresentation 要素を書き出す
fn write_sub_representation(w: &mut EventWriter<&mut Cursor<Vec<u8>>>, sr: &SubRepresentation) {
    let level_str;
    let bw_str;
    let width_str;
    let height_str;
    let asr_str;
    let sap_str;
    let mpr_str;
    let msap_str;
    let mut el = XmlEvent::start_element("SubRepresentation");
    if let Some(l) = sr.level {
        level_str = l.to_string();
        el = el.attr("level", &level_str);
    }
    if let Some(bw) = sr.bandwidth {
        bw_str = bw.to_string();
        el = el.attr("bandwidth", &bw_str);
    }
    if let Some(ref cc) = sr.content_component {
        el = el.attr("contentComponent", cc);
    }
    if let Some(ref c) = sr.codecs {
        el = el.attr("codecs", c);
    }
    if let Some(ref dl) = sr.dependency_level {
        el = el.attr("dependencyLevel", dl);
    }
    if let Some(w_val) = sr.width {
        width_str = w_val.to_string();
        el = el.attr("width", &width_str);
    }
    if let Some(h_val) = sr.height {
        height_str = h_val.to_string();
        el = el.attr("height", &height_str);
    }
    if let Some(ref mt) = sr.mime_type {
        el = el.attr("mimeType", mt);
    }
    if let Some(ref fr) = sr.frame_rate {
        el = el.attr("frameRate", fr);
    }
    if let Some(asr) = sr.audio_sampling_rate {
        asr_str = asr.to_string();
        el = el.attr("audioSamplingRate", &asr_str);
    }
    if let Some(ref s) = sr.sar {
        el = el.attr("sar", s);
    }
    if let Some(ref st) = sr.scan_type {
        el = el.attr("scanType", st);
    }
    if let Some(ref p) = sr.profiles {
        el = el.attr("profiles", p);
    }
    if let Some(sap) = sr.start_with_sap {
        sap_str = sap.to_string();
        el = el.attr("startWithSAP", &sap_str);
    }
    if let Some(mpr) = sr.max_playout_rate {
        mpr_str = mpr.to_string();
        el = el.attr("maxPlayoutRate", &mpr_str);
    }
    if let Some(cd) = sr.coding_dependency {
        el = el.attr("codingDependency", if cd { "true" } else { "false" });
    }
    if let Some(msap) = sr.maximum_sap_period {
        msap_str = format_duration(msap);
        el = el.attr("maximumSAPPeriod", &msap_str);
    }
    if let Some(ref sgp) = sr.segment_profiles {
        el = el.attr("segmentProfiles", sgp);
    }
    w.write(el)
        .expect("failed to write SubRepresentation element");

    for cp in &sr.content_protections {
        write_content_protection(w, cp);
    }
    for acc in &sr.audio_channel_configurations {
        write_descriptor(w, "AudioChannelConfiguration", acc);
    }
    for fp in &sr.frame_packings {
        write_descriptor(w, "FramePacking", fp);
    }
    for ies in &sr.inband_event_streams {
        write_descriptor(w, "InbandEventStream", ies);
    }
    for ep in &sr.essential_properties {
        write_descriptor(w, "EssentialProperty", ep);
    }
    for sp in &sr.supplemental_properties {
        write_descriptor(w, "SupplementalProperty", sp);
    }
    for ssp in &sr.segment_sequence_properties {
        write_segment_sequence_properties(w, ssp);
    }

    w.write(XmlEvent::end_element())
        .expect("failed to close SubRepresentation element");
}

/// Preselection 要素を書き出す
fn write_preselection(w: &mut EventWriter<&mut Cursor<Vec<u8>>>, ps: &Preselection) {
    let order_str;
    let mut el = XmlEvent::start_element("Preselection");
    if let Some(ref id) = ps.id {
        el = el.attr("id", id);
    }
    if let Some(ref pc) = ps.preselection_components {
        el = el.attr("preselectionComponents", pc);
    }
    if let Some(ref lang) = ps.lang {
        el = el.attr("lang", lang);
    }
    if let Some(ref tag) = ps.tag {
        el = el.attr("tag", tag);
    }
    if let Some(o) = ps.order {
        order_str = o.to_string();
        el = el.attr("order", &order_str);
    }
    if let Some(ref ct) = ps.content_type {
        el = el.attr("contentType", ct);
    }
    if let Some(ref mt) = ps.mime_type {
        el = el.attr("mimeType", mt);
    }
    if let Some(ref c) = ps.codecs {
        el = el.attr("codecs", c);
    }
    w.write(el).expect("failed to write Preselection element");

    for acc in &ps.accessibilities {
        write_descriptor(w, "Accessibility", acc);
    }
    for role in &ps.roles {
        write_descriptor(w, "Role", role);
    }
    for vp in &ps.viewpoints {
        write_descriptor(w, "Viewpoint", vp);
    }
    for label in &ps.labels {
        let mut el = XmlEvent::start_element("Label");
        if let Some(ref lang) = label.lang {
            el = el.attr("lang", lang);
        }
        w.write(el).expect("failed to write Label element");
        w.write(XmlEvent::characters(&label.text))
            .expect("failed to write Label content");
        w.write(XmlEvent::end_element())
            .expect("failed to close Label element");
    }
    for ep in &ps.essential_properties {
        write_descriptor(w, "EssentialProperty", ep);
    }
    for sp in &ps.supplemental_properties {
        write_descriptor(w, "SupplementalProperty", sp);
    }

    w.write(XmlEvent::end_element())
        .expect("failed to close Preselection element");
}

/// BaseURL 要素を書き出す
fn write_base_url(w: &mut EventWriter<&mut Cursor<Vec<u8>>>, base_url: &BaseUrl) {
    let ato_str;
    let dp_str;
    let dw_str;
    let mut el = XmlEvent::start_element("BaseURL");
    if let Some(ref sl) = base_url.service_location {
        el = el.attr("serviceLocation", sl);
    }
    if let Some(ato) = base_url.availability_time_offset {
        ato_str = ato.to_string();
        el = el.attr("availabilityTimeOffset", &ato_str);
    }
    if let Some(atc) = base_url.availability_time_complete {
        el = el.attr(
            "availabilityTimeComplete",
            if atc { "true" } else { "false" },
        );
    }
    if let Some(ref br) = base_url.byte_range {
        el = el.attr("byteRange", br);
    }
    if let Some(dp) = base_url.dvb_priority {
        dp_str = dp.to_string();
        el = el.attr("dvb:priority", &dp_str);
    }
    if let Some(dw) = base_url.dvb_weight {
        dw_str = dw.to_string();
        el = el.attr("dvb:weight", &dw_str);
    }
    w.write(el).expect("failed to write BaseURL element");
    w.write(XmlEvent::characters(&base_url.url))
        .expect("failed to write BaseURL content");
    w.write(XmlEvent::end_element())
        .expect("failed to close BaseURL element");
}

/// PatchLocation 要素を書き出す
fn write_patch_location(w: &mut EventWriter<&mut Cursor<Vec<u8>>>, pl: &PatchLocation) {
    let ttl_str;
    let mut el = XmlEvent::start_element("PatchLocation");
    if let Some(ref sl) = pl.service_location {
        el = el.attr("serviceLocation", sl);
    }
    if let Some(ttl) = pl.ttl {
        ttl_str = ttl.to_string();
        el = el.attr("ttl", &ttl_str);
    }
    w.write(el).expect("failed to write PatchLocation element");
    w.write(XmlEvent::characters(&pl.url))
        .expect("failed to write PatchLocation content");
    w.write(XmlEvent::end_element())
        .expect("failed to close PatchLocation element");
}

/// ContentComponent 要素を書き出す
fn write_content_component(w: &mut EventWriter<&mut Cursor<Vec<u8>>>, cc: &ContentComponent) {
    let mut el = XmlEvent::start_element("ContentComponent");
    if let Some(ref id) = cc.id {
        el = el.attr("id", id);
    }
    if let Some(ref ct) = cc.content_type {
        el = el.attr("contentType", ct);
    }
    if let Some(ref lang) = cc.lang {
        el = el.attr("lang", lang);
    }
    w.write(el)
        .expect("failed to write ContentComponent element");

    for acc in &cc.accessibilities {
        write_descriptor(w, "Accessibility", acc);
    }
    for role in &cc.roles {
        write_descriptor(w, "Role", role);
    }

    w.write(XmlEvent::end_element())
        .expect("failed to close ContentComponent element");
}

/// OperatingQuality 要素を書き出す
fn write_operating_quality(w: &mut EventWriter<&mut Cursor<Vec<u8>>>, oq: &OperatingQuality) {
    let min_str;
    let max_str;
    let target_str;
    let mqd_str;
    let mut el = XmlEvent::start_element("OperatingQuality");
    if let Some(ref mt) = oq.media_type {
        el = el.attr("mediaType", mt);
    }
    if let Some(mn) = oq.min {
        min_str = mn.to_string();
        el = el.attr("min", &min_str);
    }
    if let Some(mx) = oq.max {
        max_str = mx.to_string();
        el = el.attr("max", &max_str);
    }
    if let Some(t) = oq.target {
        target_str = t.to_string();
        el = el.attr("target", &target_str);
    }
    if let Some(ref t) = oq.type_ {
        el = el.attr("type", t);
    }
    if let Some(mqd) = oq.max_quality_difference {
        mqd_str = mqd.to_string();
        el = el.attr("maxQualityDifference", &mqd_str);
    }
    w.write(el)
        .expect("failed to write OperatingQuality element");
    w.write(XmlEvent::end_element())
        .expect("failed to close OperatingQuality element");
}

/// OperatingBandwidth 要素を書き出す
fn write_operating_bandwidth(w: &mut EventWriter<&mut Cursor<Vec<u8>>>, ob: &OperatingBandwidth) {
    let min_str;
    let max_str;
    let target_str;
    let mut el = XmlEvent::start_element("OperatingBandwidth");
    if let Some(ref mt) = ob.media_type {
        el = el.attr("mediaType", mt);
    }
    if let Some(mn) = ob.min {
        min_str = mn.to_string();
        el = el.attr("min", &min_str);
    }
    if let Some(mx) = ob.max {
        max_str = mx.to_string();
        el = el.attr("max", &max_str);
    }
    if let Some(t) = ob.target {
        target_str = t.to_string();
        el = el.attr("target", &target_str);
    }
    w.write(el)
        .expect("failed to write OperatingBandwidth element");
    w.write(XmlEvent::end_element())
        .expect("failed to close OperatingBandwidth element");
}

/// ClientDataReporting 要素を書き出す
fn write_client_data_reporting(
    w: &mut EventWriter<&mut Cursor<Vec<u8>>>,
    cdr: &ClientDataReporting,
) {
    let mut el = XmlEvent::start_element("ClientDataReporting");
    if let Some(ref sl) = cdr.service_locations {
        el = el.attr("serviceLocations", sl);
    }
    if let Some(ref as_) = cdr.adaptation_sets {
        el = el.attr("adaptationSets", as_);
    }
    w.write(el)
        .expect("failed to write ClientDataReporting element");

    if let Some(ref cmcd) = cdr.cmcd_parameters {
        let mut el =
            XmlEvent::start_element("CMCDParameters").attr("schemeIdUri", &cmcd.scheme_id_uri);
        if let Some(ref v) = cmcd.value {
            el = el.attr("value", v);
        }
        if let Some(ref id) = cmcd.id {
            el = el.attr("id", id);
        }
        if let Some(ref ver) = cmcd.version {
            el = el.attr("version", ver);
        }
        if let Some(ref sid) = cmcd.session_id {
            el = el.attr("sessionID", sid);
        }
        if let Some(ref cid) = cmcd.content_id {
            el = el.attr("contentID", cid);
        }
        if let Some(ref m) = cmcd.mode {
            el = el.attr("mode", m);
        }
        if let Some(ref k) = cmcd.keys {
            el = el.attr("keys", k);
        }
        if let Some(ref ir) = cmcd.include_in_requests {
            el = el.attr("includeInRequests", ir);
        }
        w.write(el).expect("failed to write CMCDParameters element");
        w.write(XmlEvent::end_element())
            .expect("failed to close CMCDParameters element");
    }

    w.write(XmlEvent::end_element())
        .expect("failed to close ClientDataReporting element");
}

/// SegmentSequenceProperties 要素を書き出す
fn write_segment_sequence_properties(
    w: &mut EventWriter<&mut Cursor<Vec<u8>>>,
    ssp: &SegmentSequenceProperties,
) {
    let cadence_str;
    let sap_type_str;
    let mut el = XmlEvent::start_element("SegmentSequenceProperties");
    if let Some(c) = ssp.cadence {
        cadence_str = c.to_string();
        el = el.attr("cadence", &cadence_str);
    }
    if let Some(st) = ssp.sap_type {
        sap_type_str = st.to_string();
        el = el.attr("sapType", &sap_type_str);
    }
    if let Some(e) = ssp.event {
        el = el.attr("event", if e { "true" } else { "false" });
    }
    if let Some(ref a) = ssp.alignment {
        el = el.attr("alignment", a);
    }
    w.write(el)
        .expect("failed to write SegmentSequenceProperties element");
    w.write(XmlEvent::end_element())
        .expect("failed to close SegmentSequenceProperties element");
}

/// Metrics 要素を書き出す
fn write_metrics(w: &mut EventWriter<&mut Cursor<Vec<u8>>>, m: &Metrics) {
    let el = XmlEvent::start_element("Metrics").attr("metrics", &m.metrics);
    w.write(el).expect("failed to write Metrics element");

    for reporting in &m.reportings {
        write_descriptor(w, "Reporting", reporting);
    }
    for range in &m.ranges {
        let starttime_str;
        let duration_str;
        let mut el = XmlEvent::start_element("Range");
        if let Some(st) = range.starttime {
            starttime_str = format_duration(st);
            el = el.attr("starttime", &starttime_str);
        }
        if let Some(d) = range.duration {
            duration_str = format_duration(d);
            el = el.attr("duration", &duration_str);
        }
        w.write(el).expect("failed to write Range element");
        w.write(XmlEvent::end_element())
            .expect("failed to close Range element");
    }

    w.write(XmlEvent::end_element())
        .expect("failed to close Metrics element");
}

/// Subset 要素を書き出す
fn write_subset(w: &mut EventWriter<&mut Cursor<Vec<u8>>>, subset: &Subset) {
    let mut el = XmlEvent::start_element("Subset").attr("contains", &subset.contains);
    if let Some(ref id) = subset.id {
        el = el.attr("id", id);
    }
    w.write(el).expect("failed to write Subset element");
    w.write(XmlEvent::end_element())
        .expect("failed to close Subset element");
}