Tag Parser  6.2.2
C++ library for reading and writing MP4 (iTunes), ID3, Vorbis, Opus, FLAC and Matroska tags
oggiterator.h
Go to the documentation of this file.
1 #ifndef MEDIA_OGGITERATOR_H
2 #define MEDIA_OGGITERATOR_H
3 
4 #include "./oggpage.h"
5 
6 #include <iosfwd>
7 #include <vector>
8 
9 namespace Media {
10 
12 {
13 public:
14  OggIterator(std::istream &stream, uint64 startOffset, uint64 streamSize);
15 
16  void clear(std::istream &stream, uint64 startOffset, uint64 streamSize);
17  std::istream &stream();
18  void setStream(std::istream &stream);
19  uint64 startOffset() const;
20  uint64 streamSize() const;
21  void reset();
22  void nextPage();
23  void nextSegment();
24  void previousPage();
25  void previousSegment();
26  const std::vector<OggPage> &pages() const;
27  const OggPage &currentPage() const;
28  std::vector<OggPage>::size_type currentPageIndex() const;
29  void setPageIndex(std::vector<OggPage>::size_type index);
30  void setSegmentIndex(std::vector<uint32>::size_type index);
31  std::vector<uint32>::size_type currentSegmentIndex() const;
32  uint64 currentSegmentOffset() const;
33  uint64 currentCharacterOffset() const;
34  uint64 tellg() const;
35  uint32 currentSegmentSize() const;
36  void setFilter(uint32 streamSerialId);
37  void removeFilter();
38  bool areAllPagesFetched() const;
39  void read(char *buffer, std::size_t count);
40  size_t readAll(char *buffer, std::size_t max);
41  void ignore(std::size_t count = 1);
42  bool bytesRemaining(std::size_t atLeast) const;
43 
44  operator bool() const;
45  OggIterator &operator++();
46  OggIterator operator++(int);
47  OggIterator &operator--();
48  OggIterator operator--(int);
49 
50 private:
51  bool fetchNextPage();
52  bool matchesFilter(const OggPage &page);
53 
54  std::istream *m_stream;
55  uint64 m_startOffset;
56  uint64 m_streamSize;
57  std::vector<OggPage> m_pages;
58  std::vector<OggPage>::size_type m_page;
59  std::vector<uint32>::size_type m_segment;
60  uint64 m_offset;
61  uint32 m_bytesRead;
62  bool m_hasIdFilter;
63  uint32 m_idFilter;
64 };
65 
69 inline OggIterator::OggIterator(std::istream &stream, uint64 startOffset, uint64 streamSize) :
70  m_stream(&stream),
71  m_startOffset(startOffset),
72  m_streamSize(streamSize),
73  m_page(0),
74  m_segment(0),
75  m_offset(0),
76  m_bytesRead(0),
77  m_hasIdFilter(false),
78  m_idFilter(0)
79 {}
80 
86 inline std::istream &OggIterator::stream()
87 {
88  return *m_stream;
89 }
90 
96 inline void OggIterator::setStream(std::istream &stream)
97 {
98  m_stream = &stream;
99 }
100 
104 inline uint64 OggIterator::startOffset() const
105 {
106  return m_startOffset;
107 }
108 
112 inline uint64 OggIterator::streamSize() const
113 {
114  return m_streamSize;
115 }
116 
120 inline const std::vector<OggPage> &OggIterator::pages() const
121 {
122  return m_pages;
123 }
124 
129 inline const OggPage &OggIterator::currentPage() const
130 {
131  return m_pages[m_page];
132 }
133 
144 inline OggIterator::operator bool() const
145 {
146  return m_page < m_pages.size() && m_segment < m_pages[m_page].segmentSizes().size();
147 }
148 
152 inline std::vector<OggPage>::size_type OggIterator::currentPageIndex() const
153 {
154  return m_page;
155 }
156 
162 inline void OggIterator::setPageIndex(std::vector<OggPage>::size_type index)
163 {
164  const OggPage &page = m_pages[m_page = index];
165  m_segment = 0;
166  m_offset = page.startOffset() + page.headerSize();
167 }
168 
174 inline void OggIterator::setSegmentIndex(std::vector<uint32>::size_type index)
175 {
176  const OggPage &page = m_pages[m_page];
177  m_offset = page.dataOffset(m_segment = index);
178 }
179 
183 inline std::vector<uint32>::size_type OggIterator::currentSegmentIndex() const
184 {
185  return m_segment;
186 }
187 
193 {
194  return m_offset;
195 }
196 
202 {
203  return m_offset + m_bytesRead;
204 }
205 
209 inline uint64 OggIterator::tellg() const
210 {
211  return currentCharacterOffset();
212 }
213 
219 inline uint32 OggIterator::currentSegmentSize() const
220 {
221  return m_pages[m_page].segmentSizes()[m_segment];
222 }
223 
232 inline void OggIterator::setFilter(uint32 streamSerialId)
233 {
234  m_hasIdFilter = true;
235  m_idFilter = streamSerialId;
236 }
237 
243 {
244  m_hasIdFilter = false;
245 }
246 
255 {
256  return (m_pages.empty() ? m_startOffset : m_pages.back().startOffset() + m_pages.back().totalSize()) >= m_streamSize;
257 }
258 
262 inline bool OggIterator::bytesRemaining(size_t atLeast) const
263 {
264  return *this && currentCharacterOffset() + atLeast <= streamSize();
265 }
266 
271 {
272  nextSegment();
273  return *this;
274 }
275 
280 {
281  OggIterator tmp = *this;
282  nextSegment();
283  return tmp;
284 }
285 
290 {
291  previousSegment();
292  return *this;
293 }
294 
299 {
300  OggIterator tmp = *this;
301  previousSegment();
302  return tmp;
303 }
304 
308 inline bool OggIterator::matchesFilter(const OggPage &page)
309 {
310  return !m_hasIdFilter || m_idFilter == page.streamSerialNumber();
311 }
312 
313 }
314 
315 #endif // MEDIA_OGGITERATOR_H
OggIterator(std::istream &stream, uint64 startOffset, uint64 streamSize)
Constructs a new iterator for the specified stream of streamSize bytes at the specified startOffset...
Definition: oggiterator.h:69
uint64 dataOffset(byte segmentIndex=0) const
Returns the data offset of the segment with the specified segmentIndex.
Definition: oggpage.h:240
bool bytesRemaining(std::size_t atLeast) const
Returns whether there are atLeast bytes remaining.
Definition: oggiterator.h:262
OggIterator & operator--()
Decrements the current position by one segment if the iterator is valid; otherwise nothing happens...
Definition: oggiterator.h:289
std::vector< uint32 >::size_type currentSegmentIndex() const
Returns the index of the current segment (in the current page) if the iterator is valid; otherwise an...
Definition: oggiterator.h:183
uint64 tellg() const
Same as currentCharacterOffset(); only provided for compliance with std::istream. ...
Definition: oggiterator.h:209
uint32 currentSegmentSize() const
Returns the size of the current segment.
Definition: oggiterator.h:219
void removeFilter()
Removes a previously set filter.
Definition: oggiterator.h:242
uint32 headerSize() const
Returns the header size in byte.
Definition: oggpage.h:219
const std::vector< OggPage > & pages() const
Returns a vector of containing the OGG pages that have been fetched yet.
Definition: oggiterator.h:120
void setPageIndex(std::vector< OggPage >::size_type index)
Sets the current page index.
Definition: oggiterator.h:162
void previousSegment()
Decreases the current position by one segment.
void setFilter(uint32 streamSerialId)
Allows to filter pages by the specified streamSerialId.
Definition: oggiterator.h:232
uint32 streamSerialNumber() const
Returns the stream serial number.
Definition: oggpage.h:155
std::istream & stream()
Returns the stream.
Definition: oggiterator.h:86
OggIterator & operator++()
Increments the current position by one segment if the iterator is valid; otherwise nothing happens...
Definition: oggiterator.h:270
The OggPage class is used to parse OGG pages.
Definition: oggpage.h:14
uint64 currentCharacterOffset() const
Returns the offset of the current character in the input stream if the iterator is valid; otherwise a...
Definition: oggiterator.h:201
void setSegmentIndex(std::vector< uint32 >::size_type index)
Sets the current segment index.
Definition: oggiterator.h:174
std::vector< OggPage >::size_type currentPageIndex() const
Returns the index of the current page if the iterator is valid; otherwise an undefined index is retur...
Definition: oggiterator.h:152
void nextSegment()
Increases the current position by one segment.
Definition: oggiterator.cpp:77
void setStream(std::istream &stream)
Sets the stream.
Definition: oggiterator.h:96
uint64 currentSegmentOffset() const
Returns the start offset of the current segment in the input stream if the iterator is valid; otherwi...
Definition: oggiterator.h:192
uint64 startOffset() const
Returns the start offset of the page.
Definition: oggpage.h:83
uint64 startOffset() const
Returns the start offset (which has been specified when constructing the iterator).
Definition: oggiterator.h:104
uint64 streamSize() const
Returns the stream size (which has been specified when constructing the iterator).
Definition: oggiterator.h:112
const OggPage & currentPage() const
Returns the current OGG page.
Definition: oggiterator.h:129
bool areAllPagesFetched() const
Returns an indication whether all pages have been fetched.
Definition: oggiterator.h:254
The OggIterator class helps iterating through all segments of an OGG bitstream.
Definition: oggiterator.h:11
Contains all classes and functions of the TagInfo library.
Definition: exceptions.h:9
#define TAG_PARSER_EXPORT
Marks the symbol to be exported by the tagparser library.