-
Notifications
You must be signed in to change notification settings - Fork 1
/
TagLibHandler.cpp
429 lines (379 loc) · 10.8 KB
/
TagLibHandler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#define NOMINMAX
#include <string>
#include <sstream>
#include <initguid.h>
#include <mmdeviceapi.h>
#include <shobjidl.h> // IInitializeWithStream, IDestinationStreamFactory
#include <propsys.h> // Property System APIs and interfaces
#include <propkey.h> // System PROPERTYKEY definitions
#include <propvarutil.h> // PROPVARIANT and VARIANT helper APIs
#include <tag.h>
#include <fileref.h>
#include "exttag.h"
//
// Releases the specified pointer if not NULL
//
#define SAFE_RELEASE(p) \
if (p) \
{ \
(p)->Release(); \
(p) = NULL; \
}
// DLL lifetime management functions
void DllAddRef();
void DllRelease();
const PROPERTYKEY keys[] = {
PKEY_Music_AlbumTitle, PKEY_Music_Artist,
PKEY_Music_TrackNumber, PKEY_Music_Genre,
PKEY_Title, PKEY_Media_Year, PKEY_Audio_ChannelCount,
PKEY_Media_Duration, PKEY_Audio_EncodingBitrate,
PKEY_Audio_SampleRate, PKEY_Rating, PKEY_Music_AlbumArtist,
PKEY_Music_Composer, PKEY_Music_Conductor,
PKEY_Media_Publisher, PKEY_Media_SubTitle,
PKEY_Media_Producer, PKEY_Music_Mood, PKEY_Copyright,
PKEY_Music_PartOfSet,
PKEY_Keywords, PKEY_Comment, PKEY_Media_DateReleased
};
// Debug property handler class definition
class CTagLibPropertyStore :
public IPropertyStore,
public IPropertyStoreCapabilities,
public IInitializeWithStream
{
public:
static HRESULT CreateInstance(REFIID riid, void **ppv);
// IUnknown
IFACEMETHODIMP QueryInterface(REFIID riid, void **ppv)
{
static const QITAB qit[] = {
QITABENT(CTagLibPropertyStore, IPropertyStore),
QITABENT(CTagLibPropertyStore, IPropertyStoreCapabilities),
QITABENT(CTagLibPropertyStore, IInitializeWithStream),
{ 0 },
};
return QISearch(this, qit, riid, ppv);
}
IFACEMETHODIMP_(ULONG) AddRef()
{
DllAddRef();
return InterlockedIncrement(&_cRef);
}
IFACEMETHODIMP_(ULONG) Release()
{
DllRelease();
ULONG cRef = InterlockedDecrement(&_cRef);
if (!cRef)
{
delete this;
}
return cRef;
}
// IPropertyStore
IFACEMETHODIMP GetCount(__out DWORD *pcProps);
IFACEMETHODIMP GetAt(DWORD iProp, __out PROPERTYKEY *pkey);
IFACEMETHODIMP GetValue(REFPROPERTYKEY key, __out PROPVARIANT *pPropVar);
IFACEMETHODIMP SetValue(REFPROPERTYKEY key, REFPROPVARIANT propVar);
IFACEMETHODIMP Commit();
// IPropertyStoreCapabilities
IFACEMETHODIMP IsPropertyWritable(REFPROPERTYKEY key);
// IInitializeWithStream
IFACEMETHODIMP Initialize(IStream *pStream, DWORD grfMode);
protected:
CTagLibPropertyStore() : _cRef(1), _pStream(NULL), _grfMode(0)
{
DllAddRef();
if (FAILED(PSCreateMemoryPropertyStore(IID_PPV_ARGS(&_pCache))))
OutputDebugStr(L"TaglibHandler: PSCreateMemoryPropertyStore failed");
PROPVARIANT pv = {};
pv.vt = VT_EMPTY;
for (size_t i = 0; i < ARRAYSIZE(keys); ++i)
_pCache->SetValue(keys[i], pv);
}
~CTagLibPropertyStore()
{
SAFE_RELEASE(_pStream);
SAFE_RELEASE(_pCache);
}
IStream* _pStream; // data stream passed in to Initialize, and saved to on Commit
IPropertyStoreCache* _pCache;
DWORD _grfMode; // STGM mode passed to Initialize
TagLib::FileRef taglibfile;
private:
long _cRef;
};
bool operator==(REFPROPERTYKEY left, REFPROPERTYKEY right)
{
return IsEqualPropertyKey(left, right);
}
struct Dbstr
{
const BSTR val;
Dbstr(const std::wstring &str) : val(SysAllocString(str.c_str()))
{
OutputDebugStr(L"bstr()");
}
~Dbstr()
{
OutputDebugStr(L"~bstr");
SysFreeString(val);
}
operator BSTR()
{
OutputDebugStr(L"op bstr");
return val;
}
};
#define TRY_BSTR(func) \
try \
{ \
InitPropVariantFromString(func(taglibfile).c_str(), pPropVar);\
} \
catch (std::domain_error &) \
{ \
pPropVar->vt = VT_EMPTY; \
}
HRESULT CTagLibPropertyStore::GetValue(REFPROPERTYKEY key, PROPVARIANT *pPropVar)
{
try
{
const TagLib::AudioProperties *ap = taglibfile.audioProperties();
const TagLib::Tag *tag = taglibfile.tag();
// If the tag is empty, treat it as if it doesn't exist.
if (tag && tag->isEmpty())
tag = NULL;
if (ap && key == PKEY_Audio_ChannelCount)
{
pPropVar->uintVal = ap->channels();
pPropVar->vt = VT_UI4;
}
else if (ap && key == PKEY_Media_Duration)
{
pPropVar->ulVal = ap->length()*10000000;
pPropVar->vt = VT_UI8;
}
else if (ap && key == PKEY_Audio_EncodingBitrate)
{
pPropVar->uintVal = ap->bitrate()*1024;
pPropVar->vt = VT_UI4;
}
else if (ap && key == PKEY_Audio_SampleRate)
{
pPropVar->uintVal = ap->sampleRate();
pPropVar->vt = VT_UI4;
}
else if (tag && key == PKEY_Music_AlbumArtist)
TRY_BSTR(albumArtist)
else if (tag && key == PKEY_Music_AlbumTitle)
InitPropVariantFromString(tag->album().toWString().c_str(), pPropVar);
else if (tag && key == PKEY_Music_Artist)
InitPropVariantFromString(tag->artist().toWString().c_str(), pPropVar);
else if (tag && key == PKEY_Music_Genre)
InitPropVariantFromString(tag->genre().toWString().c_str(), pPropVar);
else if (tag && key == PKEY_Title)
InitPropVariantFromString(tag->title().toWString().c_str(), pPropVar);
else if (tag && key == PKEY_Music_TrackNumber)
{
if (tag->track() == 0)
pPropVar->vt = VT_EMPTY;
else
{
pPropVar->uintVal = tag->track();
pPropVar->vt = VT_UI4;
}
}
else if (tag && key == PKEY_Media_Year)
{
if (tag->year() == 0)
pPropVar->vt = VT_EMPTY;
else
{
pPropVar->uintVal = tag->year();
pPropVar->vt = VT_UI4;
}
}
else if (tag && key == PKEY_Rating)
{
pPropVar->uintVal = rating(taglibfile);
pPropVar->vt = VT_UI4;
}
else if (tag && key == PKEY_Keywords)
{
const wstrvec_t keys = keywords(taglibfile);
if (!keys.empty())
{
pPropVar->vt = VT_ARRAY | VT_BSTR;
SAFEARRAYBOUND aDim[1] = {};
aDim[0].cElements = keys.size();
pPropVar->parray = SafeArrayCreate(VT_BSTR, 1, aDim);
if (!pPropVar->parray)
return E_OUTOFMEMORY;
long aLong[1] = {};
for (wstrvec_t::const_iterator it = keys.begin(); it != keys.end(); ++it)
{
SafeArrayPutElement(pPropVar->parray, aLong, Dbstr(*it));
++aLong[0];
}
}
}
else if (tag && key == PKEY_Comment)
InitPropVariantFromString(tag->comment().toWString().c_str(), pPropVar);
else if (tag && key == PKEY_Media_DateReleased)
{
SYSTEMTIME date = releasedate(taglibfile);
// Attempt to recover in case of failure.:
// GetDateFormat, at least in my locale, fails if at least the day, month and year aren't set:
if (date.wMonth != 0 && date.wDay != 0)
{
std::vector<WCHAR> buf;
#define GDF(x) GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &date, NULL, x, static_cast<int>(buf.size()))
buf.resize(GDF(NULL));
if (GDF(&buf.at(0)))
{
pPropVar->bstrVal = SysAllocString(&buf.at(0));
pPropVar->vt = VT_BSTR;
}
}
else
if (int year = tag->year())
{
std::wstringstream ss; ss << year;
InitPropVariantFromString(ss.str().c_str(), pPropVar);
}
}
else if (tag && key == PKEY_Music_Composer)
TRY_BSTR(composer)
else if (tag && key == PKEY_Music_Conductor)
TRY_BSTR(conductor)
else if (tag && key == PKEY_Media_SubTitle)
TRY_BSTR(subtitle)
else if (tag && key == PKEY_Media_Publisher)
TRY_BSTR(label)
else if (tag && key == PKEY_Media_Producer)
TRY_BSTR(producer)
else if (tag && key == PKEY_Music_Mood)
TRY_BSTR(mood)
else if (tag && key == PKEY_Copyright)
TRY_BSTR(copyright)
else if (tag && key == PKEY_Music_PartOfSet)
TRY_BSTR(partofset)
else
return S_FALSE;
return S_OK;
}
catch (std::exception &e)
{
OutputDebugStringA(e.what());
pPropVar->vt = VT_EMPTY;
return ERROR_INTERNAL_ERROR;
}
catch (...)
{
// This has only ever been reached when taglib gives us a non-null but invalid tag;
// not reproducable. Worse, the exception is only when wstring's constructor has
// caught it, there's a chance of a complete crash here, otoh, with a non-abort(),
// the app/system may deal gracefully.
OutputDebugString(L"TaglibHandler encountered unexpected exception in GetValue");
pPropVar->vt = VT_EMPTY;
return ERROR_INTERNAL_ERROR;
}
}
HRESULT CTagLibPropertyStore::CreateInstance(REFIID riid, void **ppv)
{
HRESULT hr = E_OUTOFMEMORY;
CTagLibPropertyStore *pNew = new CTagLibPropertyStore();
if (pNew)
{
hr = pNew->QueryInterface(riid, ppv);
SAFE_RELEASE(pNew);
}
return hr;
}
HRESULT CTagLibPropertyStore_CreateInstance(REFIID riid, void **ppv)
{
return CTagLibPropertyStore::CreateInstance(riid, ppv);
}
HRESULT CTagLibPropertyStore::GetCount(__out DWORD *pcProps)
{
*pcProps = ARRAYSIZE(keys);
return S_OK;
}
HRESULT CTagLibPropertyStore::GetAt(DWORD iProp, __out PROPERTYKEY *pkey)
{
return _pCache->GetAt(iProp, pkey);
}
// SetValue just updates the internal value cache
HRESULT CTagLibPropertyStore::SetValue(REFPROPERTYKEY, REFPROPVARIANT)
{
return STG_E_ACCESSDENIED; // Ok, but read-only.
}
// Commit writes the internal value cache back out to the stream passed to Initialize
HRESULT CTagLibPropertyStore::Commit()
{
return S_OK; // Ok, we wrote all we could (nothing).
}
// Indicates whether the users should be able to edit values for the given property key
// S_OK | S_FALSE
HRESULT CTagLibPropertyStore::IsPropertyWritable(REFPROPERTYKEY)
{
return S_FALSE;
}
struct IStreamAccessor : public TagLib::FileAccessor
{
IStreamAccessor(IStream *stream) : stream(stream) {}
IStream *stream;
bool isOpen() const
{
return true;
}
size_t fread(void *pv, size_t s1, size_t s2) const
{
ULONG read = 0;
stream->Read(pv, s1*s2, &read);
return read;
}
size_t fwrite(const void *,size_t,size_t)
{
return 0;
}
int fseek(long distance, int direction)
{
LARGE_INTEGER dist;
dist.QuadPart = distance;
return FAILED(stream->Seek(dist, direction, NULL)); // 0 on success.
}
void clearError()
{
// Uh oh.
}
long tell() const
{
ULARGE_INTEGER newpos;
LARGE_INTEGER dist = {};
stream->Seek(dist, STREAM_SEEK_CUR, &newpos);
return newpos.QuadPart;
}
int truncate(long length)
{
return -1; // error
}
TagLib::FileNameHandle name() const
{
STATSTG a;
if (FAILED(stream->Stat(&a, STATFLAG_DEFAULT)))
return TagLib::FileName("");
return TagLib::FileName(a.pwcsName);
}
bool readOnly() const
{
return true;
}
};
// Initialize populates the internal value cache with data from the specified stream
// S_OK | E_UNEXPECTED | ERROR_READ_FAULT | ERROR_FILE_CORRUPT | ERROR_INTERNAL_ERROR
HRESULT CTagLibPropertyStore::Initialize(IStream *pStream, DWORD grfMode)
{
taglibfile = TagLib::FileRef(new IStreamAccessor(pStream));
if (taglibfile.isNull())
return ERROR_FILE_CORRUPT;
return S_OK;
}