diff --git a/src/actions.cc b/src/actions.cc index fa29759..04baef6 100644 --- a/src/actions.cc +++ b/src/actions.cc @@ -4,7 +4,7 @@ using namespace opustags; void opustags::list_tags(ogg::Decoder &dec, ITagsHandler &handler) { - ogg::Stream *s; + std::shared_ptr s; while (!handler.done()) { s = dec.read_page(); if (s == nullptr) @@ -27,7 +27,7 @@ void opustags::list_tags(ogg::Decoder &dec, ITagsHandler &handler) void opustags::edit_tags( ogg::Decoder &in, ogg::Encoder &out, ITagsHandler &handler) { - ogg::Stream *s; + std::shared_ptr s; while (true) { s = in.read_page(); if (s == nullptr) diff --git a/src/ogg.cc b/src/ogg.cc index 9bc0684..0e88718 100644 --- a/src/ogg.cc +++ b/src/ogg.cc @@ -148,17 +148,18 @@ ogg::Decoder::~Decoder() ogg_sync_clear(&sync); } -ogg::Stream *ogg::Decoder::read_page() +std::shared_ptr ogg::Decoder::read_page() { while (page_out()) { int streamno = ogg_page_serialno(¤t_page); auto i = streams.find(streamno); if (i == streams.end()) { // we could check the page number to detect new streams (pageno = 0) - i = streams.emplace(streamno, Stream(streamno)).first; + auto s = std::make_shared(streamno); + i = streams.emplace(streamno, s).first; } - if (i->second.page_in(current_page)) - return &(i->second); + if (i->second->page_in(current_page)) + return i->second; } return nullptr; // end of stream } @@ -207,9 +208,11 @@ ogg::Encoder::Encoder(std::ostream &out) ogg::Stream& ogg::Encoder::get_stream(int streamno) { auto i = streams.find(streamno); - if (i == streams.end()) - i = streams.emplace(streamno, Stream(streamno)).first; - return i->second; + if (i == streams.end()) { + auto s = std::make_shared(streamno); + i = streams.emplace(streamno, s).first; + } + return *(i->second); } void ogg::Encoder::forward(ogg::Stream &in) @@ -261,7 +264,7 @@ void ogg::Encoder::write_tags(int streamno, const Tags &tags) op.bytes = data.size(); op.packet = reinterpret_cast(const_cast(data.data())); - ogg::Stream *s = &streams.at(streamno); // assume it exists + std::shared_ptr s = streams.at(streamno); // assume it exists if (ogg_stream_packetin(&s->stream, &op) != 0) throw std::runtime_error("ogg_stream_packetin failed"); flush_stream(*s); diff --git a/src/ogg.h b/src/ogg.h index f8c3f27..b89c460 100644 --- a/src/ogg.h +++ b/src/ogg.h @@ -4,6 +4,7 @@ #include #include +#include #include namespace opustags { @@ -79,13 +80,13 @@ namespace ogg // The read page is given to Stream::page_in before this function // returns. // After the end of the file is reached, it returns NULL. - Stream *read_page(); + std::shared_ptr read_page(); std::istream &input; ogg_sync_state sync; ogg_page current_page; - std::map streams; + std::map> streams; private: bool page_out(); @@ -110,7 +111,7 @@ namespace ogg // We're gonna need some ogg_stream_state for adjusting the page // numbers and splitting large packets as it's gotta be done. - std::map streams; + std::map> streams; private: Stream& get_stream(int streamno); diff --git a/tests/ogg_test.cc b/tests/ogg_test.cc index 2473496..5b24167 100644 --- a/tests/ogg_test.cc +++ b/tests/ogg_test.cc @@ -13,7 +13,7 @@ SCENARIO("decoding a single-stream file", "[ogg]") ogg::Decoder dec(src); WHEN("reading the first page") { - ogg::Stream *s = dec.read_page(); + std::shared_ptr s = dec.read_page(); THEN("the Opus header is ready") { REQUIRE(s != nullptr); REQUIRE(s->state == ogg::HEADER_READY);