Tag Parser  8.0.1
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 ConversionUtilities;
11 using namespace ChronoUtilities;
12 using namespace IoUtilities;
13 
14 namespace TagParser {
15 
32 AbstractTrack::AbstractTrack(istream &inputStream, ostream &outputStream, uint64 startOffset)
33  : m_istream(&inputStream)
34  , m_ostream(&outputStream)
35  , m_reader(BinaryReader(&inputStream))
36  , m_writer(BinaryWriter(&outputStream))
37  , m_startOffset(startOffset)
38  , m_headerValid(false)
39  , m_format()
40  , m_mediaType(MediaType::Unknown)
41  , m_version(0.0)
42  , m_size(0)
43  , m_trackNumber(0)
44  , m_id(0)
45  , m_bitrate(0.0)
46  , m_maxBitrate(0.0)
47  , m_samplingFrequency(0)
48  , m_extensionSamplingFrequency(0)
49  , m_bitsPerSample(0)
50  , m_bytesPerSecond(0)
51  , m_channelCount(0)
52  , m_channelConfig(0)
53  , m_extensionChannelConfig(0)
54  , m_sampleCount(0)
55  , m_quality(0)
56  , m_depth(0)
57  , m_fps(0)
58  , m_chromaFormat(nullptr)
59  , m_interlaced(false)
60  , m_timeScale(0)
61  , m_enabled(true)
62  , m_default(false)
63  , m_forced(false)
64  , m_lacing(false)
65  , m_encrypted(false)
66  , m_usedInPresentation(true)
67  , m_usedWhenPreviewing(true)
68  , m_colorSpace(0)
69 {
70 }
71 
79 AbstractTrack::AbstractTrack(std::iostream &stream, uint64 startOffset)
80  : AbstractTrack(stream, stream, startOffset)
81 {
82 }
83 
88 {
89 }
90 
95 {
96  switch (m_format.general) {
101  return mpegChannelModeString(static_cast<MpegChannelMode>(m_channelConfig));
102  default:
103  return nullptr;
104  }
105 }
106 
111 {
113 }
114 
119 {
120  switch (m_format.general) {
123  default:
124  return nullptr;
125  }
126 }
127 
134 string AbstractTrack::label() const
135 {
136  stringstream ss;
137  ss << "ID: " << id();
138  ss << ", type: " << mediaTypeName();
139  if (!name().empty()) {
140  ss << ", name: \"" << name() << "\"";
141  }
142  if (!language().empty() && language() != "und") {
143  ss << ", language: \"" << language() << "\"";
144  }
145  return ss.str();
146 }
147 
160 {
161  // use abbreviated format
162  const char *format = m_format.shortAbbreviation();
163  if (!format || !*format) {
164  // fall back to media type name if no abbreviation available
165  format = mediaTypeName();
166  }
167 
168  // find additional info
169  const char *additionalInfo = nullptr;
170  switch (m_mediaType) {
171  case MediaType::Video:
172  if (!displaySize().isNull()) {
173  additionalInfo = displaySize().abbreviation();
174  } else if (!pixelSize().isNull()) {
175  additionalInfo = pixelSize().abbreviation();
176  }
177  break;
178  case MediaType::Audio:
179  case MediaType::Text:
180  if (channelCount()) {
181  if (!language().empty() && language() != "und") {
182  return argsToString(format, '-', channelCount(), "ch-", language());
183  } else {
184  return argsToString(format, '-', channelCount(), 'c', 'h');
185  }
186  } else if (!language().empty() && language() != "und") {
187  additionalInfo = language().data();
188  }
189  break;
190  default:;
191  }
192 
193  if (additionalInfo) {
194  return argsToString(format, '-', additionalInfo);
195  }
196  return format;
197 }
198 
212 {
213  m_headerValid = false;
214  m_istream->seekg(static_cast<streamoff>(m_startOffset), ios_base::beg);
215  try {
216  internalParseHeader(diag);
217  m_headerValid = true;
218  } catch (Failure &) {
219  throw;
220  }
221 }
222 
233 } // namespace TagParser
const std::string & language() const
Returns the language of the track if known; otherwise returns an empty string.
virtual ~AbstractTrack()
Destroys the track.
const char * channelConfigString() const
Returns a string with the channel configuration if available; otherwise returns nullptr.
const std::string name() const
Returns the track name if known; otherwise returns an empty string.
const char * abbreviation() const
Returns an abbreviation for the current instance, eg.
Definition: size.cpp:9
virtual void internalParseHeader(Diagnostics &diag)=0
This method is internally called to parse header information.
MediaType
The MediaType enum specifies the type of media data (audio, video, text, ...).
Definition: mediaformat.h:13
AbstractTrack(std::istream &inputStream, std::ostream &outputStream, uint64 startOffset)
Constructs a new track.
STL namespace.
GeneralMediaFormat general
Definition: mediaformat.h:257
Contains utility classes helping to read and write streams.
TAG_PARSER_EXPORT const char * mpegChannelModeString(MpegChannelMode channelMode)
Returns the string representation for the specified channelMode.
TAG_PARSER_EXPORT const char * channelConfigString(byte config)
Returns the string representation for the specified MPEG-4 channel config.
Definition: mp4ids.cpp:425
MediaFormat format() const
Returns the format of the track if known; otherwise returns MediaFormat::Unknown. ...
std::istream * m_istream
std::string description() const
Returns a short description about the track.
const Size & pixelSize() const
Returns the size of the encoded video frames if known; otherwise returns a zero size.
uint64 id() const
Returns the track ID if known; otherwise returns 0.
const char * shortAbbreviation() const
Returns a short abbreviation of the media format as C-style string.
const char * extensionChannelConfigString() const
Returns a string with the extension channel configuration if available; otherwise returns nullptr...
const char * mediaTypeName() const
Returns the string representation of the media type of the track.
byte extensionChannelConfig() const
Returns the extension channel configuration if available; otherwise returns nullptr.
The AbstractTrack class parses and stores technical information about video, audio and other kinds of...
Definition: abstracttrack.h:40
const Size & displaySize() const
Returns the size of the video frames to display if known; otherwise returns a zero size...
void parseHeader(Diagnostics &diag)
Parses technical information about the track from the header.
The class inherits from std::exception and serves as base class for exceptions thrown by the elements...
Definition: exceptions.h:11
Contains all classes and functions of the TagInfo library.
Definition: aaccodebook.h:9
The Diagnostics class is a container for DiagMessage.
Definition: diagnostics.h:156
uint16 channelCount() const
Returns the number of channels if known; otherwise returns 0.
std::string label() const
Returns a label for the track.