Tag Parser  6.4.0
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  uint64 currentPageOffset() const;
29  std::vector<OggPage>::size_type currentPageIndex() const;
30  void setPageIndex(std::vector<OggPage>::size_type index);
31  void setSegmentIndex(std::vector<uint32>::size_type index);
32  std::vector<uint32>::size_type currentSegmentIndex() const;
33  uint64 currentSegmentOffset() const;
34  uint64 currentCharacterOffset() const;
35  uint64 tellg() const;
36  uint32 currentSegmentSize() const;
37  void setFilter(uint32 streamSerialId);
38  void removeFilter();
39  bool areAllPagesFetched() const;
40  void read(char *buffer, std::size_t count);
41  size_t readAll(char *buffer, std::size_t max);
42  void ignore(std::size_t count = 1);
43  bool bytesRemaining(std::size_t atLeast) const;
44  bool resyncAt(uint64 offset);
45 
46  operator bool() const;
47  OggIterator &operator++();
48  OggIterator operator++(int);
49  OggIterator &operator--();
50  OggIterator operator--(int);
51 
52 private:
53  bool fetchNextPage();
54  bool matchesFilter(const OggPage &page);
55 
56  std::istream *m_stream;
57  uint64 m_startOffset;
58  uint64 m_streamSize;
59  std::vector<OggPage> m_pages;
60  std::vector<OggPage>::size_type m_page;
61  std::vector<uint32>::size_type m_segment;
62  uint64 m_offset;
63  uint32 m_bytesRead;
64  bool m_hasIdFilter;
65  uint32 m_idFilter;
66 };
67 
71 inline OggIterator::OggIterator(std::istream &stream, uint64 startOffset, uint64 streamSize) :
72  m_stream(&stream),
73  m_startOffset(startOffset),
74  m_streamSize(streamSize),
75  m_page(0),
76  m_segment(0),
77  m_offset(0),
78  m_bytesRead(0),
79  m_hasIdFilter(false),
80  m_idFilter(0)
81 {}
82 
88 inline std::istream &OggIterator::stream()
89 {
90  return *m_stream;
91 }
92 
98 inline void OggIterator::setStream(std::istream &stream)
99 {
100  m_stream = &stream;
101 }
102 
106 inline uint64 OggIterator::startOffset() const
107 {
108  return m_startOffset;
109 }
110 
114 inline uint64 OggIterator::streamSize() const
115 {
116  return m_streamSize;
117 }
118 
122 inline const std::vector<OggPage> &OggIterator::pages() const
123 {
124  return m_pages;
125 }
126 
131 inline const OggPage &OggIterator::currentPage() const
132 {
133  return m_pages[m_page];
134 }
135 
140 inline uint64 OggIterator::currentPageOffset() const
141 {
142  return m_pages[m_page].startOffset();
143 }
144 
155 inline OggIterator::operator bool() const
156 {
157  return m_page < m_pages.size() && m_segment < m_pages[m_page].segmentSizes().size();
158 }
159 
163 inline std::vector<OggPage>::size_type OggIterator::currentPageIndex() const
164 {
165  return m_page;
166 }
167 
172 inline void OggIterator::setPageIndex(std::vector<OggPage>::size_type index)
173 {
174  const OggPage &page = m_pages[m_page = index];
175  m_segment = 0;
176  m_offset = page.startOffset() + page.headerSize();
177 }
178 
184 inline void OggIterator::setSegmentIndex(std::vector<uint32>::size_type index)
185 {
186  const OggPage &page = m_pages[m_page];
187  m_offset = page.dataOffset(m_segment = index);
188 }
189 
193 inline std::vector<uint32>::size_type OggIterator::currentSegmentIndex() const
194 {
195  return m_segment;
196 }
197 
203 {
204  return m_offset;
205 }
206 
212 {
213  return m_offset + m_bytesRead;
214 }
215 
219 inline uint64 OggIterator::tellg() const
220 {
221  return currentCharacterOffset();
222 }
223 
229 inline uint32 OggIterator::currentSegmentSize() const
230 {
231  return m_pages[m_page].segmentSizes()[m_segment];
232 }
233 
242 inline void OggIterator::setFilter(uint32 streamSerialId)
243 {
244  m_hasIdFilter = true;
245  m_idFilter = streamSerialId;
246 }
247 
253 {
254  m_hasIdFilter = false;
255 }
256 
269 {
270  return (m_pages.empty() ? m_startOffset : m_pages.back().startOffset() + m_pages.back().totalSize()) >= m_streamSize;
271 }
272 
276 inline bool OggIterator::bytesRemaining(size_t atLeast) const
277 {
278  return *this && currentCharacterOffset() + atLeast <= streamSize();
279 }
280 
285 {
286  nextSegment();
287  return *this;
288 }
289 
294 {
295  OggIterator tmp = *this;
296  nextSegment();
297  return tmp;
298 }
299 
304 {
305  previousSegment();
306  return *this;
307 }
308 
313 {
314  OggIterator tmp = *this;
315  previousSegment();
316  return tmp;
317 }
318 
322 inline bool OggIterator::matchesFilter(const OggPage &page)
323 {
324  return !m_hasIdFilter || m_idFilter == page.streamSerialNumber();
325 }
326 
327 }
328 
329 #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:71
uint64 dataOffset(byte segmentIndex=0) const
Returns the data offset of the segment with the specified segmentIndex.
Definition: oggpage.h:249
bool bytesRemaining(std::size_t atLeast) const
Returns whether there are atLeast bytes remaining.
Definition: oggiterator.h:276
OggIterator & operator--()
Decrements the current position by one segment if the iterator is valid; otherwise nothing happens...
Definition: oggiterator.h:303
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:193
uint64 tellg() const
Same as currentCharacterOffset(); only provided for compliance with std::istream. ...
Definition: oggiterator.h:219
uint32 currentSegmentSize() const
Returns the size of the current segment.
Definition: oggiterator.h:229
void removeFilter()
Removes a previously set filter.
Definition: oggiterator.h:252
uint32 headerSize() const
Returns the header size in byte.
Definition: oggpage.h:220
const std::vector< OggPage > & pages() const
Returns a vector of containing the OGG pages that have been fetched yet.
Definition: oggiterator.h:122
void setPageIndex(std::vector< OggPage >::size_type index)
Sets the current page index.
Definition: oggiterator.h:172
void previousSegment()
Decreases the current position by one segment.
void setFilter(uint32 streamSerialId)
Allows to filter pages by the specified streamSerialId.
Definition: oggiterator.h:242
uint32 streamSerialNumber() const
Returns the stream serial number.
Definition: oggpage.h:156
std::istream & stream()
Returns the stream.
Definition: oggiterator.h:88
OggIterator & operator++()
Increments the current position by one segment if the iterator is valid; otherwise nothing happens...
Definition: oggiterator.h:284
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:211
void setSegmentIndex(std::vector< uint32 >::size_type index)
Sets the current segment index.
Definition: oggiterator.h:184
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:163
void nextSegment()
Increases the current position by one segment.
Definition: oggiterator.cpp:81
void setStream(std::istream &stream)
Sets the stream.
Definition: oggiterator.h:98
uint64 currentSegmentOffset() const
Returns the start offset of the current segment in the input stream if the iterator is valid; otherwi...
Definition: oggiterator.h:202
uint64 startOffset() const
Returns the start offset of the page.
Definition: oggpage.h:84
uint64 startOffset() const
Returns the start offset (which has been specified when constructing the iterator).
Definition: oggiterator.h:106
uint64 streamSize() const
Returns the stream size (which has been specified when constructing the iterator).
Definition: oggiterator.h:114
const OggPage & currentPage() const
Returns the current OGG page.
Definition: oggiterator.h:131
bool areAllPagesFetched() const
Returns an indication whether all pages have been fetched.
Definition: oggiterator.h:268
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.
uint64 currentPageOffset() const
Returns the start offset of the current OGG page.
Definition: oggiterator.h:140