Skip to content

Commit 81815e1

Browse files
committed
Include doc type and version in properties
1 parent b9122af commit 81815e1

7 files changed

Lines changed: 77 additions & 11 deletions

File tree

examples/matroskareader.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,15 @@ int main(int argc, char *argv[])
7676
else
7777
printf("File has no attachments\n");
7878

79+
if(auto properties = dynamic_cast<const TagLib::Matroska::Properties *>(file.audioProperties())) {
80+
printf("Properties:\n");
81+
PRINT_PRETTY("Doc Type", properties->docType().toCString(false));
82+
PRINT_PRETTY("Doc Type Version", TagLib::String::number(properties->docTypeVersion()).toCString(false));
83+
PRINT_PRETTY("Codec Name", properties->codecName().toCString(true));
84+
PRINT_PRETTY("Bitrate", TagLib::String::number(properties->bitrate()).toCString(false));
85+
PRINT_PRETTY("Sample Rate", TagLib::String::number(properties->sampleRate()).toCString(false));
86+
PRINT_PRETTY("Channels", TagLib::String::number(properties->channels()).toCString(false));
87+
PRINT_PRETTY("Length [ms]", TagLib::String::number(properties->lengthInMilliseconds()).toCString(false));
88+
}
7989
return 0;
8090
}

taglib/matroska/ebml/ebmlelement.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ std::unique_ptr<EBML::Element> EBML::Element::factory(File &file)
6262
auto id = static_cast<Id>(uintId);
6363
switch(id) {
6464
RETURN_ELEMENT_FOR_CASE(Id::EBMLHeader);
65+
RETURN_ELEMENT_FOR_CASE(Id::DocType);
66+
RETURN_ELEMENT_FOR_CASE(Id::DocTypeVersion);
6567
RETURN_ELEMENT_FOR_CASE(Id::MkSegment);
6668
RETURN_ELEMENT_FOR_CASE(Id::MkInfo);
6769
RETURN_ELEMENT_FOR_CASE(Id::MkTracks);

taglib/matroska/ebml/ebmlelement.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ namespace TagLib::EBML {
3434
enum class Id : unsigned int
3535
{
3636
EBMLHeader = 0x1A45DFA3,
37+
DocType = 0x4282,
38+
DocTypeVersion = 0x4287,
3739
VoidElement = 0xEC,
3840
MkSegment = 0x18538067,
3941
MkTags = 0x1254C367,
@@ -143,7 +145,9 @@ namespace TagLib::EBML {
143145
template <Element::Id ID>
144146
struct GetElementTypeById;
145147

146-
template <> struct GetElementTypeById<Element::Id::EBMLHeader> { using type = Element; };
148+
template <> struct GetElementTypeById<Element::Id::EBMLHeader> { using type = MasterElement; };
149+
template <> struct GetElementTypeById<Element::Id::DocType> { using type = Latin1StringElement; };
150+
template <> struct GetElementTypeById<Element::Id::DocTypeVersion> { using type = UIntElement; };
147151
template <> struct GetElementTypeById<Element::Id::MkSegment> { using type = MkSegment; };
148152
template <> struct GetElementTypeById<Element::Id::MkInfo> { using type = MkInfo; };
149153
template <> struct GetElementTypeById<Element::Id::MkTracks> { using type = MkTracks; };

taglib/matroska/matroskafile.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include "matroskasegment.h"
2828
#include "ebmlutils.h"
2929
#include "ebmlelement.h"
30+
#include "ebmlstringelement.h"
31+
#include "ebmluintelement.h"
3032
#include "ebmlmksegment.h"
3133
#include "tlist.h"
3234
#include "tdebug.h"
@@ -96,7 +98,7 @@ Matroska::File::File(IOStream *stream, bool readProperties,
9698

9799
Matroska::File::~File() = default;
98100

99-
AudioProperties *Matroska::File::audioProperties() const
101+
Matroska::Properties *Matroska::File::audioProperties() const
100102
{
101103
return d->properties.get();
102104
}
@@ -271,13 +273,19 @@ void Matroska::File::read(bool readProperties, Properties::ReadStyle readStyle)
271273
offset_t fileLength = length();
272274

273275
// Find the EBML Header
274-
std::unique_ptr<EBML::Element> head(EBML::Element::factory(*this));
276+
auto head = EBML::element_cast<EBML::Element::Id::EBMLHeader>(
277+
EBML::Element::factory(*this));
275278
if(!head || head->getId() != EBML::Element::Id::EBMLHeader) {
276279
debug("Failed to find EBML head");
277280
setValid(false);
278281
return;
279282
}
280-
head->skipData(*this);
283+
if(readProperties) {
284+
head->read(*this);
285+
}
286+
else {
287+
head->skipData(*this);
288+
}
281289

282290
// Find the Matroska segment in the file
283291
std::unique_ptr<EBML::MkSegment> segment(
@@ -307,6 +315,19 @@ void Matroska::File::read(bool readProperties, Properties::ReadStyle readStyle)
307315

308316
if(readProperties) {
309317
d->properties = std::make_unique<Properties>(this);
318+
319+
for(const auto &element : *head) {
320+
auto id = element->getId();
321+
if (id == EBML::Element::Id::DocType) {
322+
d->properties->setDocType(
323+
EBML::element_cast<EBML::Element::Id::DocType>(element)->getValue());
324+
}
325+
else if (id == EBML::Element::Id::DocTypeVersion) {
326+
d->properties->setDocTypeVersion(static_cast<int>(
327+
EBML::element_cast<EBML::Element::Id::DocTypeVersion>(element)->getValue()));
328+
}
329+
}
330+
310331
segment->parseInfo(d->properties.get());
311332
segment->parseTracks(d->properties.get());
312333
if(d->tag) {

taglib/matroska/matroskafile.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ namespace TagLib::Matroska {
134134
* Returns the Matroska::Properties for this file. If no audio properties
135135
* were read then this will return a null pointer.
136136
*/
137-
AudioProperties *audioProperties() const override;
137+
Properties *audioProperties() const override;
138138

139139
/*!
140140
* Save the file.

taglib/matroska/matroskaproperties.cpp

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class Matroska::Properties::PropertiesPrivate
4141
File *file;
4242
String codecName;
4343
String title;
44+
String docType;
45+
int docTypeVersion { 0 };
4446
int length { 0 };
4547
int bitrate { -1 };
4648
int sampleRate { 0 };
@@ -88,6 +90,16 @@ int Matroska::Properties::bitsPerSample() const
8890
return d->bitsPerSample;
8991
}
9092

93+
String Matroska::Properties::docType() const
94+
{
95+
return d->docType;
96+
}
97+
98+
int Matroska::Properties::docTypeVersion() const
99+
{
100+
return d->docTypeVersion;
101+
}
102+
91103
String Matroska::Properties::codecName() const
92104
{
93105
return d->codecName;
@@ -107,11 +119,6 @@ void Matroska::Properties::setLengthInMilliseconds(int length)
107119
d->length = length;
108120
}
109121

110-
void Matroska::Properties::setBitrate(int bitrate)
111-
{
112-
d->bitrate = bitrate;
113-
}
114-
115122
void Matroska::Properties::setSampleRate(int sampleRate)
116123
{
117124
d->sampleRate = sampleRate;
@@ -127,6 +134,16 @@ void Matroska::Properties::setBitsPerSample(int bitsPerSample)
127134
d->bitsPerSample = bitsPerSample;
128135
}
129136

137+
void Matroska::Properties::setDocType(const String &docType)
138+
{
139+
d->docType = docType;
140+
}
141+
142+
void Matroska::Properties::setDocTypeVersion(int docTypeVersion)
143+
{
144+
d->docTypeVersion = docTypeVersion;
145+
}
146+
130147
void Matroska::Properties::setCodecName(const String &codecName)
131148
{
132149
d->codecName = codecName;

taglib/matroska/matroskaproperties.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ namespace TagLib::Matroska {
8080
*/
8181
int bitsPerSample() const;
8282

83+
/*!
84+
* Returns the EBML doc type, "matroska" or "webm".
85+
*/
86+
String docType() const;
87+
88+
/*!
89+
* Returns the EBML doc type version, typical values are 2 or 4.
90+
*/
91+
int docTypeVersion() const;
92+
8393
/*!
8494
* Returns the concrete codec name, for example "A_MPEG/L3"
8595
* used in the file if available, otherwise an empty string.
@@ -97,12 +107,14 @@ namespace TagLib::Matroska {
97107
class PropertiesPrivate;
98108
friend class EBML::MkInfo;
99109
friend class EBML::MkTracks;
110+
friend class File;
100111

101112
void setLengthInMilliseconds(int length);
102-
void setBitrate(int bitrate);
103113
void setSampleRate(int sampleRate);
104114
void setChannels(int channels);
105115
void setBitsPerSample(int bitsPerSample);
116+
void setDocType(const String &docType);
117+
void setDocTypeVersion(int docTypeVersion);
106118
void setCodecName(const String &codecName);
107119
void setTitle(const String &title);
108120

0 commit comments

Comments
 (0)