1 /*
2  * Copyright 2003-2021 The Music Player Daemon Project
3  * http://www.musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef MPD_OGG_STREAM_STATE_HXX
21 #define MPD_OGG_STREAM_STATE_HXX
22 
23 #include <ogg/ogg.h>
24 
25 #include <cassert>
26 #include <cstdint>
27 
28 #include <string.h>
29 
30 class OggStreamState {
31 	ogg_stream_state state;
32 
33 public:
OggStreamState(int serialno)34 	explicit OggStreamState(int serialno) noexcept {
35 		ogg_stream_init(&state, serialno);
36 	}
37 
38 	/**
39 	 * Initialize a decoding #ogg_stream_state with the first
40 	 * page.
41 	 */
OggStreamState(ogg_page & page)42 	explicit OggStreamState(ogg_page &page) noexcept {
43 		ogg_stream_init(&state, ogg_page_serialno(&page));
44 		PageIn(page);
45 	}
46 
~OggStreamState()47 	~OggStreamState() noexcept {
48 		ogg_stream_clear(&state);
49 	}
50 
operator ogg_stream_state&()51 	operator ogg_stream_state &() noexcept {
52 		return state;
53 	}
54 
Reinitialize(int serialno)55 	void Reinitialize(int serialno) noexcept {
56 		ogg_stream_reset_serialno(&state, serialno);
57 	}
58 
GetSerialNo() const59 	long GetSerialNo() const noexcept {
60 		return state.serialno;
61 	}
62 
Reset()63 	void Reset() noexcept {
64 		ogg_stream_reset(&state);
65 	}
66 
67 	/* encoding */
68 
PacketIn(const ogg_packet & packet)69 	void PacketIn(const ogg_packet &packet) noexcept {
70 		ogg_stream_packetin(&state,
71 				    const_cast<ogg_packet *>(&packet));
72 	}
73 
PageOut(ogg_page & page)74 	bool PageOut(ogg_page &page) noexcept {
75 		return ogg_stream_pageout(&state, &page) != 0;
76 	}
77 
Flush(ogg_page & page)78 	bool Flush(ogg_page &page) noexcept {
79 		return ogg_stream_flush(&state, &page) != 0;
80 	}
81 
82 	/* decoding */
83 
PageIn(ogg_page & page)84 	bool PageIn(ogg_page &page) noexcept {
85 		return ogg_stream_pagein(&state, &page) == 0;
86 	}
87 
PacketOut(ogg_packet & packet)88 	int PacketOut(ogg_packet &packet) noexcept {
89 		return ogg_stream_packetout(&state, &packet);
90 	}
91 };
92 
93 #endif
94