Code · 166 lines · 5682 bytes
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
166using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace MediaContainers.Matroska;

public class MatroskaTag : List<MatroskaSimpleTag>
{
	public int TargetTypeValue { get; set; } = 50;
	public string TargetType { get; set; }
	public List<ulong> TagTrackUID { get; set; }
	public List<ulong> TagEditionUID { get; set; }
	public List<ulong> TagChapterUID { get; set; }
	public List<ulong> TagAttachmentUID { get; set; }

	public void ReadFrom(EBMLMasterElement element)
	{
		if (element == null) { return; }
		if (element.Definition != MatroskaSpecification.Tag) { return; }
		foreach (var child in element.Children)
		{
			if (child.Definition == MatroskaSpecification.Targets)
			{
				foreach (var sub in child.Children)
				{
					if (sub.Definition == MatroskaSpecification.TargetTypeValue) { TargetTypeValue = (int)sub.IntValue; }
					else if (sub.Definition == MatroskaSpecification.TargetType) { TargetType = sub.StringValue; }
					else if (sub.Definition == MatroskaSpecification.TagTrackUID)
					{
						if (TagTrackUID == null) { TagTrackUID = new List<ulong>(); }
						TagTrackUID.Add(sub.UIntValue);
					}
					else if (sub.Definition == MatroskaSpecification.TagEditionUID)
					{
						if (TagEditionUID == null) { TagEditionUID = new List<ulong>(); }
						TagEditionUID.Add(sub.UIntValue);
					}
					else if (sub.Definition == MatroskaSpecification.TagChapterUID)
					{
						if (TagChapterUID == null) { TagChapterUID = new List<ulong>(); }
						TagChapterUID.Add(sub.UIntValue);
					}
					else if (sub.Definition == MatroskaSpecification.TagAttachmentUID)
					{
						if (TagAttachmentUID == null) { TagAttachmentUID = new List<ulong>(); }
						TagAttachmentUID.Add(sub.UIntValue);
					}
				}
			}
			else if (child.Definition == MatroskaSpecification.SimpleTag)
			{
				var tag = new MatroskaSimpleTag();
				tag.ReadFrom((EBMLMasterElement)child);
				Add(tag);
			}
		}
	}

	public void CopyTo(MatroskaTag tag, bool shallow = false)
	{
		tag.TargetTypeValue = TargetTypeValue;
		tag.TargetType = TargetType;
		if (shallow)
		{
			tag.Clear();
			tag.TagTrackUID = TagTrackUID;
			tag.TagEditionUID = TagEditionUID;
			tag.TagChapterUID = TagChapterUID;
			tag.TagAttachmentUID = TagAttachmentUID;
			for (int i = 0, j = Count; i < j; i++) { tag.Add(this[i]); }
		}
		else
		{
			tag.Clear();
			tag.TagTrackUID = TagTrackUID?.ToList();
			tag.TagEditionUID = TagEditionUID?.ToList();
			tag.TagChapterUID = TagChapterUID?.ToList();
			tag.TagAttachmentUID = TagAttachmentUID?.ToList();
			for (int i = 0, j = Count; i < j; i++)
			{
				var simple = new MatroskaSimpleTag();
				this[i].CopyTo(simple);
				tag.Add(simple);
			}
		}
	}

	public async ValueTask Write(EBMLWriter writer, CancellationToken cancellationToken = default)
	{
		await writer.BeginMasterElement(MatroskaSpecification.Tag, cancellationToken);
		await writer.BeginMasterElement(MatroskaSpecification.Targets, cancellationToken);
		if (TargetTypeValue != 50) { await writer.WriteUnsignedInteger(MatroskaSpecification.TargetTypeValue, (ulong)TargetTypeValue, cancellationToken); }
		if (TargetType != null) { await writer.WriteString(MatroskaSpecification.TargetType, TargetType, cancellationToken); }
		if (TagTrackUID != null)
		{
			for (int i = 0; i < TagTrackUID.Count; i++)
			{
				await writer.WriteUnsignedInteger(MatroskaSpecification.TagTrackUID, TagTrackUID[i], cancellationToken);
			}
		}
		if (TagEditionUID != null)
		{
			for (int i = 0; i < TagEditionUID.Count; i++)
			{
				await writer.WriteUnsignedInteger(MatroskaSpecification.TagEditionUID, TagEditionUID[i], cancellationToken);
			}
		}
		if (TagChapterUID != null)
		{
			for (int i = 0; i < TagChapterUID.Count; i++)
			{
				await writer.WriteUnsignedInteger(MatroskaSpecification.TagChapterUID, TagChapterUID[i], cancellationToken);
			}
		}
		if (TagAttachmentUID != null)
		{
			for (int i = 0; i < TagAttachmentUID.Count; i++)
			{
				await writer.WriteUnsignedInteger(MatroskaSpecification.TagAttachmentUID, TagAttachmentUID[i], cancellationToken);
			}
		}
		await writer.EndMasterElement(cancellationToken);
		for (int i = 0; i < Count; i++) { await this[i].Write(writer, cancellationToken); }
		await writer.EndMasterElement(cancellationToken);
	}

	public EBMLMasterElement ToElement()
	{
		var tracks = new EBMLMasterElement(MatroskaSpecification.Tag);
		var target = new EBMLMasterElement(MatroskaSpecification.Targets);
		if (TargetTypeValue != 50) { target.AddChild(new EBMLUnsignedIntegerElement(MatroskaSpecification.TargetTypeValue, (ulong)TargetTypeValue)); }
		if (TargetType != null) { target.AddChild(new EBMLStringElement(MatroskaSpecification.TargetType, TargetType)); }
		if (TagTrackUID != null)
		{
			for (int i = 0; i < TagTrackUID.Count; i++)
			{
				target.AddChild(new EBMLUnsignedIntegerElement(MatroskaSpecification.TagTrackUID, TagTrackUID[i]));
			}
		}
		if (TagEditionUID != null)
		{
			for (int i = 0; i < TagEditionUID.Count; i++)
			{
				target.AddChild(new EBMLUnsignedIntegerElement(MatroskaSpecification.TagEditionUID, TagEditionUID[i]));
			}
		}
		if (TagChapterUID != null)
		{
			for (int i = 0; i < TagChapterUID.Count; i++)
			{
				target.AddChild(new EBMLUnsignedIntegerElement(MatroskaSpecification.TagChapterUID, TagChapterUID[i]));
			}
		}
		if (TagAttachmentUID != null)
		{
			for (int i = 0; i < TagAttachmentUID.Count; i++)
			{
				target.AddChild(new EBMLUnsignedIntegerElement(MatroskaSpecification.TagAttachmentUID, TagAttachmentUID[i]));
			}
		}
		tracks.AddChild(target);
		foreach (var track in this) { tracks.AddChild(track.ToElement()); }
		return tracks;
	}
}