Tag Parser  10.0.0
C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
abstracttrack.cpp
Go to the documentation of this file.
1 #include "./abstracttrack.h"
2 #include "./exceptions.h"
3 #include "./mediaformat.h"
4 
5 #include "./mp4/mp4ids.h"
6 
8 
9 using namespace std;
10 using namespace CppUtilities;
11 
12 namespace TagParser {
13 
30 AbstractTrack::AbstractTrack(istream &inputStream, ostream &outputStream, std::uint64_t startOffset)
31  : m_istream(&inputStream)
32  , m_ostream(&outputStream)
33  , m_reader(BinaryReader(&inputStream))
34  , m_writer(BinaryWriter(&outputStream))
35  , m_startOffset(startOffset)
37  , m_format()
38  , m_mediaType(MediaType::Unknown)
39  , m_version(0.0)
40  , m_size(0)
41  , m_trackNumber(0)
42  , m_id(0)
43  , m_bitrate(0.0)
44  , m_maxBitrate(0.0)
45  , m_samplingFrequency(0)
46  , m_extensionSamplingFrequency(0)
47  , m_bitsPerSample(0)
48  , m_bytesPerSecond(0)
49  , m_channelCount(0)
50  , m_channelConfig(0)
51  , m_extensionChannelConfig(0)
52  , m_sampleCount(0)
53  , m_quality(0)
54  , m_depth(0)
55  , m_fps(0)
56  , m_timeScale(0)
57  , m_colorSpace(0)
58 {
59 }
60 
68 AbstractTrack::AbstractTrack(std::iostream &stream, std::uint64_t startOffset)
69  : AbstractTrack(stream, stream, startOffset)
70 {
71 }
72 
77 {
78 }
79 
83 std::string_view AbstractTrack::channelConfigString() const
84 {
85  switch (m_format.general) {
91  default:
92  return std::string_view();
93  }
94 }
95 
100 {
102 }
103 
108 {
109  switch (m_format.general) {
112  default:
113  return std::string_view();
114  }
115 }
116 
123 string AbstractTrack::label() const
124 {
125  stringstream ss;
126  ss << "ID: " << id();
127  ss << ", type: " << mediaTypeName();
128  if (!name().empty()) {
129  ss << ", name: \"" << name() << "\"";
130  }
131  if (const auto &language = locale().fullOrSomeAbbreviatedName(); !language.empty()) {
132  ss << ", language: " << language << "";
133  }
134  return ss.str();
135 }
136 
138 string AbstractTrack::makeDescription(bool verbose) const
139 {
140  // use abbreviated format
141  const auto format = MediaFormat(m_format.general, verbose ? m_format.sub : 0, verbose ? m_format.extension : 0);
143  if (formatName.empty()) {
144  // fall back to media type name if no abbreviation available
146  }
147 
148  // find additional info and level
149  auto additionalInfoRef = std::string_view();
150  auto level = std::string();
151  switch (m_mediaType) {
152  case MediaType::Video:
153  if (!displaySize().isNull()) {
154  additionalInfoRef = displaySize().abbreviation();
155  } else if (!pixelSize().isNull()) {
156  additionalInfoRef = pixelSize().abbreviation();
157  }
158  if (verbose) {
159  switch (format.general) {
163  if (version() != 0.0) {
164  level = "@L" + numberToString(version());
165  }
166  break;
167  default:;
168  }
169  }
170  break;
171  case MediaType::Audio:
172  case MediaType::Text:
173  if (channelCount()) {
174  if (const auto &localeName = locale().someAbbreviatedName(); !localeName.empty()) {
175  return argsToString(formatName, '-', channelCount(), "ch-", localeName);
176  } else {
177  return argsToString(formatName, '-', channelCount(), 'c', 'h');
178  }
179  } else if (const auto &localeName = locale().someAbbreviatedName(); !localeName.empty()) {
180  additionalInfoRef = localeName;
181  }
182  break;
183  default:;
184  }
185 
186  if (!additionalInfoRef.empty()) {
187  return argsToString(formatName, level, '-', additionalInfoRef);
188  }
189  return argsToString(formatName, level);
190 }
192 
205 {
206  return makeDescription(true);
207 }
208 
219 {
220  return makeDescription(false);
221 }
222 
236 {
238  m_istream->seekg(static_cast<streamoff>(m_startOffset), ios_base::beg);
239  try {
240  internalParseHeader(diag, progress);
242  } catch (const Failure &) {
243  throw;
244  }
245 }
246 
257 } // namespace TagParser
The AbortableProgressFeedback class provides feedback about an ongoing operation via callbacks.
The AbstractTrack class parses and stores technical information about video, audio and other kinds of...
Definition: abstracttrack.h:65
std::uint64_t id() const
Returns the track ID if known; otherwise returns 0.
std::string label() const
Returns a label for the track.
std::uint8_t m_extensionChannelConfig
const Locale & locale() const
Returns the locale of the track if known; otherwise returns an empty locale.
std::string_view extensionChannelConfigString() const
Returns a string with the extension channel configuration if available; otherwise returns nullptr.
std::string_view formatName() const
Returns the format of the track as C-style string if known; otherwise returns the format abbreviation...
const Size & displaySize() const
Returns the size of the video frames to display if known; otherwise returns a zero size.
double version() const
Returns the version/level of the track if known; otherwise returns 0.
const Size & pixelSize() const
Returns the size of the encoded video frames if known; otherwise returns a zero size.
std::uint8_t extensionChannelConfig() const
Returns the extension channel configuration if available; otherwise returns nullptr.
AbstractTrack(std::istream &inputStream, std::ostream &outputStream, std::uint64_t startOffset)
Constructs a new track.
std::uint8_t m_channelConfig
std::string shortDescription() const
Returns a short description about the track.
std::string_view mediaTypeName() const
Returns the string representation of the media type of the track.
std::uint64_t m_startOffset
void parseHeader(Diagnostics &diag, AbortableProgressFeedback &progress)
Parses technical information about the track from the header.
std::uint16_t channelCount() const
Returns the number of channels if known; otherwise returns 0.
std::string_view channelConfigString() const
Returns a string with the channel configuration if available; otherwise returns nullptr.
virtual void internalParseHeader(Diagnostics &diag, AbortableProgressFeedback &progress)=0
This method is internally called to parse header information.
const std::string name() const
Returns the track name if known; otherwise returns an empty string.
std::istream * m_istream
std::string description() const
Returns a description about the track.
MediaFormat format() const
Returns the format of the track if known; otherwise returns MediaFormat::Unknown.
virtual ~AbstractTrack()
Destroys the track.
The Diagnostics class is a container for DiagMessage.
Definition: diagnostics.h:156
The class inherits from std::exception and serves as base class for exceptions thrown by the elements...
Definition: exceptions.h:11
The MediaFormat class specifies the format of media data.
Definition: mediaformat.h:246
std::string_view shortAbbreviation() const
Returns a short abbreviation of the media format as C-style string.
unsigned char extension
Definition: mediaformat.h:261
GeneralMediaFormat general
Definition: mediaformat.h:259
std::string_view abbreviation() const
Returns an abbreviation for the current instance, eg.
Definition: size.cpp:17
constexpr TAG_PARSER_EXPORT std::string_view language()
TAG_PARSER_EXPORT std::string_view channelConfigString(std::uint8_t config)
Returns the string representation for the specified MPEG-4 channel config.
Definition: mp4ids.cpp:433
Contains all classes and functions of the TagInfo library.
Definition: aaccodebook.h:10
TAG_PARSER_EXPORT std::string_view mpegChannelModeString(MpegChannelMode channelMode)
Returns the string representation for the specified channelMode.
MediaType
The MediaType enum specifies the type of media data (audio, video, text, ...).
Definition: mediaformat.h:14
MpegChannelMode
Specifies the channel mode.
TrackFlags
The TrackFlags enum specifies miscellaneous boolean properties of a track.
Definition: abstracttrack.h:46