Line 0
Link Here
|
|
|
1 |
/*MT* |
2 |
|
3 |
MediaTomb - http://www.mediatomb.cc/ |
4 |
|
5 |
flac_handler.cc - this file is part of MediaTomb. |
6 |
|
7 |
Copyright (C) 2005 Gena Batyan <bgeradz@mediatomb.cc>, |
8 |
Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc> |
9 |
|
10 |
Copyright (C) 2006-2009 Gena Batyan <bgeradz@mediatomb.cc>, |
11 |
Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>, |
12 |
Leonhard Wimmer <leo@mediatomb.cc> |
13 |
|
14 |
MediaTomb is free software; you can redistribute it and/or modify |
15 |
it under the terms of the GNU General Public License version 2 |
16 |
as published by the Free Software Foundation. |
17 |
|
18 |
MediaTomb is distributed in the hope that it will be useful, |
19 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
20 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
21 |
GNU General Public License for more details. |
22 |
|
23 |
You should have received a copy of the GNU General Public License |
24 |
version 2 along with MediaTomb; if not, write to the Free Software |
25 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. |
26 |
|
27 |
$Id: flac_handler.cc 2032 2009-12-21 22:58:57Z lww $ |
28 |
*/ |
29 |
|
30 |
/// \file flac_handler.cc |
31 |
/// \brief Implementeation of the FlacHandler class. |
32 |
|
33 |
#ifdef HAVE_CONFIG_H |
34 |
#include "autoconfig.h" |
35 |
#endif |
36 |
|
37 |
#ifdef HAVE_FLAC |
38 |
|
39 |
#include <FLAC/all.h> |
40 |
|
41 |
#include "flac_handler.h" |
42 |
#include "string_converter.h" |
43 |
#include "config_manager.h" |
44 |
#include "common.h" |
45 |
#include "tools.h" |
46 |
#include "mem_io_handler.h" |
47 |
|
48 |
#include "content_manager.h" |
49 |
|
50 |
using namespace zmm; |
51 |
|
52 |
FlacHandler::FlacHandler() : MetadataHandler() |
53 |
{ |
54 |
} |
55 |
|
56 |
static void addField(metadata_fields_t field, const FLAC__StreamMetadata* tags, Ref<CdsItem> item) |
57 |
{ |
58 |
String value; |
59 |
int i; |
60 |
|
61 |
Ref<StringConverter> sc = StringConverter::i2i(); // sure is sure |
62 |
|
63 |
switch (field) |
64 |
{ |
65 |
case M_TITLE: |
66 |
i = FLAC__metadata_object_vorbiscomment_find_entry_from(tags, /*offset=*/0, "TITLE"); |
67 |
break; |
68 |
case M_ARTIST: |
69 |
i = FLAC__metadata_object_vorbiscomment_find_entry_from(tags, /*offset=*/0, "ARTIST"); |
70 |
break; |
71 |
case M_ALBUM: |
72 |
i = FLAC__metadata_object_vorbiscomment_find_entry_from(tags, /*offset=*/0, "ALBUM"); |
73 |
break; |
74 |
case M_DATE: |
75 |
i = FLAC__metadata_object_vorbiscomment_find_entry_from(tags, /*offset=*/0, "DATE"); |
76 |
break; |
77 |
case M_GENRE: |
78 |
i = FLAC__metadata_object_vorbiscomment_find_entry_from(tags, /*offset=*/0, "GENRE"); |
79 |
break; |
80 |
case M_DESCRIPTION: |
81 |
i = FLAC__metadata_object_vorbiscomment_find_entry_from(tags, /*offset=*/0, "DESCRIPTION"); |
82 |
break; |
83 |
case M_TRACKNUMBER: |
84 |
i = FLAC__metadata_object_vorbiscomment_find_entry_from(tags, /*offset=*/0, "TRACKNUMBER"); |
85 |
break; |
86 |
default: |
87 |
return; |
88 |
} |
89 |
|
90 |
if( 0 <= i ) |
91 |
value = strchr((const char *)tags->data.vorbis_comment.comments[i].entry, '=') + 1; |
92 |
else |
93 |
return; |
94 |
|
95 |
value = trim_string(value); |
96 |
|
97 |
if (string_ok(value)) |
98 |
{ |
99 |
item->setMetadata(MT_KEYS[field].upnp, sc->convert(value)); |
100 |
log_debug("Setting metadata on item: %d, %s\n", field, sc->convert(value).c_str()); |
101 |
} |
102 |
} |
103 |
|
104 |
void FlacHandler::fillMetadata(Ref<CdsItem> item) |
105 |
{ |
106 |
FLAC__StreamMetadata* tags = NULL; |
107 |
FLAC__StreamMetadata streaminfo; |
108 |
Ref<StringConverter> sc = StringConverter::i2i(); // sure is sure |
109 |
|
110 |
if( !FLAC__metadata_get_tags(item->getLocation().c_str(), &tags) ) |
111 |
return; |
112 |
|
113 |
if( FLAC__METADATA_TYPE_VORBIS_COMMENT == tags->type ) |
114 |
{ |
115 |
for (int i = 0; i < M_MAX; i++) |
116 |
addField((metadata_fields_t) i, tags, item); |
117 |
} |
118 |
|
119 |
FLAC__metadata_object_delete(tags); |
120 |
tags = NULL; |
121 |
|
122 |
if( !FLAC__metadata_get_streaminfo(item->getLocation().c_str(), &streaminfo) ) |
123 |
return; |
124 |
|
125 |
if( FLAC__METADATA_TYPE_STREAMINFO == streaminfo.type ) |
126 |
{ |
127 |
// note: UPnP requires bytes/second |
128 |
item->getResource(0)->addAttribute(MetadataHandler::getResAttrName(R_BITRATE), String::from((unsigned)((streaminfo.data.stream_info.bits_per_sample * streaminfo.data.stream_info.sample_rate) / 8))); |
129 |
// note: UPnP requires HMS |
130 |
item->getResource(0)->addAttribute(MetadataHandler::getResAttrName(R_DURATION), secondsToHMS((unsigned)(streaminfo.data.stream_info.total_samples / streaminfo.data.stream_info.sample_rate))); |
131 |
item->getResource(0)->addAttribute(MetadataHandler::getResAttrName(R_SAMPLEFREQUENCY), String::from(streaminfo.data.stream_info.sample_rate)); |
132 |
item->getResource(0)->addAttribute(MetadataHandler::getResAttrName(R_NRAUDIOCHANNELS), String::from(streaminfo.data.stream_info.channels)); |
133 |
} |
134 |
|
135 |
if( !FLAC__metadata_get_picture(item->getLocation().c_str(), |
136 |
&tags, |
137 |
FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER, |
138 |
NULL, |
139 |
NULL, |
140 |
(unsigned)-1, |
141 |
(unsigned)-1, |
142 |
(unsigned)-1, |
143 |
(unsigned)-1 ) ) |
144 |
return; |
145 |
|
146 |
if( FLAC__METADATA_TYPE_PICTURE == tags->type ) |
147 |
{ |
148 |
String art_mimetype = tags->data.picture.mime_type; |
149 |
log_debug("Mime type : %s\n", sc->convert(art_mimetype).c_str()); |
150 |
|
151 |
// saw that simply "PNG" was used with some mp3's, so mimetype setting |
152 |
// was probably invalid |
153 |
if (!string_ok(art_mimetype) || (art_mimetype.index('/') == -1)) |
154 |
{ |
155 |
#ifdef HAVE_MAGIC |
156 |
art_mimetype = ContentManager::getInstance()->getMimeTypeFromBuffer((void *)tags->data.picture.data, tags->data.picture.data_length); |
157 |
if (!string_ok(art_mimetype)) |
158 |
#endif |
159 |
art_mimetype = _(MIMETYPE_DEFAULT); |
160 |
|
161 |
log_debug("Mime type via magic: %s\n", sc->convert(art_mimetype).c_str()); |
162 |
} |
163 |
|
164 |
// if we could not determine the mimetype, then there is no |
165 |
// point to add the resource - it's probably garbage |
166 |
if (art_mimetype != _(MIMETYPE_DEFAULT)) |
167 |
{ |
168 |
Ref<CdsResource> resource(new CdsResource(CH_FLAC)); |
169 |
resource->addAttribute(MetadataHandler::getResAttrName(R_PROTOCOLINFO), renderProtocolInfo(art_mimetype)); |
170 |
resource->addParameter(_(RESOURCE_CONTENT_TYPE), _(ID3_ALBUM_ART)); |
171 |
item->addResource(resource); |
172 |
} |
173 |
} |
174 |
|
175 |
FLAC__metadata_object_delete(tags); |
176 |
} |
177 |
|
178 |
Ref<IOHandler> FlacHandler::serveContent(Ref<CdsItem> item, int resNum, off_t *data_size) |
179 |
{ |
180 |
FLAC__StreamMetadata* picture = NULL; |
181 |
|
182 |
if( !FLAC__metadata_get_picture(item->getLocation().c_str(), |
183 |
&picture, |
184 |
FLAC__STREAM_METADATA_PICTURE_TYPE_FRONT_COVER, |
185 |
NULL, |
186 |
NULL, |
187 |
(unsigned)-1, |
188 |
(unsigned)-1, |
189 |
(unsigned)-1, |
190 |
(unsigned)-1 ) ) |
191 |
throw _Exception(_("FlacHandler: could not exctract cover from: ") + item->getLocation()); |
192 |
|
193 |
if( FLAC__METADATA_TYPE_PICTURE != picture->type ) |
194 |
throw _Exception(_("TagHandler: resource has no album information")); |
195 |
|
196 |
Ref<IOHandler> h(new MemIOHandler((void *)picture->data.picture.data, picture->data.picture.data_length)); |
197 |
*data_size = picture->data.picture.data_length; |
198 |
|
199 |
FLAC__metadata_object_delete(picture); |
200 |
|
201 |
return h; |
202 |
} |
203 |
|
204 |
#endif // HAVE_FLAC |