From 215213fc2cafd0ebcd9af8c776cdc1c416014c13 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 9 May 2016 12:22:22 +0200 Subject: [PATCH] encoder/vorbis: add wrapper class for vorbis_comment --- Makefile.am | 1 + src/encoder/plugins/VorbisEncoderPlugin.cxx | 17 +++--- src/lib/xiph/VorbisComment.hxx | 58 +++++++++++++++++++++ 3 files changed, 65 insertions(+), 11 deletions(-) create mode 100644 src/lib/xiph/VorbisComment.hxx diff --git a/Makefile.am b/Makefile.am index 25706952e..4ef0c7047 100644 --- a/Makefile.am +++ b/Makefile.am @@ -585,6 +585,7 @@ if HAVE_XIPH noinst_LIBRARIES += libxiph.a libxiph_a_SOURCES = \ + src/lib/xiph/VorbisComment.hxx \ src/lib/xiph/VorbisComments.cxx src/lib/xiph/VorbisComments.hxx \ src/lib/xiph/XiphTags.cxx src/lib/xiph/XiphTags.hxx libxiph_a_CPPFLAGS = $(AM_CPPFLAGS) \ diff --git a/src/encoder/plugins/VorbisEncoderPlugin.cxx b/src/encoder/plugins/VorbisEncoderPlugin.cxx index 43e8d175b..ab097131f 100644 --- a/src/encoder/plugins/VorbisEncoderPlugin.cxx +++ b/src/encoder/plugins/VorbisEncoderPlugin.cxx @@ -20,6 +20,7 @@ #include "config.h" #include "VorbisEncoderPlugin.hxx" #include "OggEncoder.hxx" +#include "lib/xiph/VorbisComment.hxx" #include "AudioFormat.hxx" #include "config/ConfigError.hxx" #include "util/StringUtil.hxx" @@ -202,11 +203,8 @@ VorbisEncoder::HeaderOut(vorbis_comment &vc) void VorbisEncoder::SendHeader() { - vorbis_comment vc; - - vorbis_comment_init(&vc); + VorbisComment vc; HeaderOut(vc); - vorbis_comment_clear(&vc); } Encoder * @@ -260,24 +258,22 @@ VorbisEncoder::PreTag(gcc_unused Error &error) } static void -copy_tag_to_vorbis_comment(vorbis_comment *vc, const Tag &tag) +copy_tag_to_vorbis_comment(VorbisComment &vc, const Tag &tag) { for (const auto &item : tag) { char name[64]; ToUpperASCII(name, tag_item_names[item.type], sizeof(name)); - vorbis_comment_add_tag(vc, name, item.value); + vc.AddTag(name, item.value); } } bool VorbisEncoder::SendTag(const Tag &tag, gcc_unused Error &error) { - vorbis_comment comment; - /* write the vorbis_comment object */ - vorbis_comment_init(&comment); - copy_tag_to_vorbis_comment(&comment, tag); + VorbisComment comment; + copy_tag_to_vorbis_comment(comment, tag); /* reset ogg_stream_state and begin a new stream */ @@ -286,7 +282,6 @@ VorbisEncoder::SendTag(const Tag &tag, gcc_unused Error &error) /* send that vorbis_comment to the ogg_stream_state */ HeaderOut(comment); - vorbis_comment_clear(&comment); return true; } diff --git a/src/lib/xiph/VorbisComment.hxx b/src/lib/xiph/VorbisComment.hxx new file mode 100644 index 000000000..12fe1aaba --- /dev/null +++ b/src/lib/xiph/VorbisComment.hxx @@ -0,0 +1,58 @@ +/* + * Copyright 2003-2016 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_VORBIS_COMMENT_HXX +#define MPD_VORBIS_COMMENT_HXX + +#include "check.h" + +#include + +/** + * OO wrapper for a #vorbis_comment instance. + */ +class VorbisComment { + vorbis_comment vc; + +public: + VorbisComment() { + vorbis_comment_init(&vc); + } + + ~VorbisComment() { + vorbis_comment_clear(&vc); + } + + VorbisComment(const VorbisComment &) = delete; + VorbisComment &operator=(const VorbisComment &) = delete; + + operator vorbis_comment &() { + return vc; + } + + operator vorbis_comment *() { + return &vc; + } + + void AddTag(const char *tag, const char *contents) { + vorbis_comment_add_tag(&vc, tag, contents); + } +}; + +#endif