aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Input/in_mp3/ExtendedInfo.cpp
blob: a3affdebafdd3bfe442c12e46c2cce3710875f38 (plain) (blame)
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
#include "main.h"
#include "Metadata.h"
#include "../Winamp/wa_ipc.h"
#include "../nu/ns_wc.h"
#include "uvox_3901.h"
#include "uvox_3902.h"
#include "Stopper.h"
#include <shlwapi.h>
#include "../Agave/Language/api_language.h"
#include <strsafe.h>

extern CGioFile *g_playing_file;
static FILETIME ftLastWriteTime;

// is used to determine if the last write time of the file has changed when
// asked to get the metadata for the same cached file so we can update things
BOOL HasFileTimeChanged(const wchar_t *fn)
{
	WIN32_FILE_ATTRIBUTE_DATA fileData = {0};
	if (GetFileAttributesExW(fn, GetFileExInfoStandard, &fileData) == TRUE)
	{
		if(CompareFileTime(&ftLastWriteTime, &fileData.ftLastWriteTime))
		{
			ftLastWriteTime = fileData.ftLastWriteTime;
			return TRUE;
		}
	}
	return FALSE;
}

void UpdateFileTimeChanged(const wchar_t *fn)
{
	WIN32_FILE_ATTRIBUTE_DATA fileData;
	if (GetFileAttributesExW(fn, GetFileExInfoStandard, &fileData) == TRUE)
	{
		ftLastWriteTime = fileData.ftLastWriteTime;
	}
}

Metadata *m_ext_set_mp3info = NULL;
Metadata *m_ext_get_mp3info = NULL;
extern "C" __declspec(dllexport)
int winampGetExtendedFileInfoW(const wchar_t *fn, const char *data, wchar_t *dest, size_t destlen)
{
	if (!_stricmp(data, "type"))
	{
		dest[0] = '0';
		dest[1] = 0;
		return 1;
	}
	if (!_stricmp(data, "rateable"))
	{
		dest[0] = '1';
		dest[1] = 0;
		return 1;
	}
	else if (!_stricmp(data, "family"))
	{
		if (!fn || !fn[0]) return 0;
		int len = lstrlenW(fn);
		if (len < 4 || L'.' != fn[len - 4])  return 0;
		const wchar_t *p = &fn[len - 3];
		if (!_wcsicmp(p, L"MP3")) { WASABI_API_LNGSTRINGW_BUF(IDS_FAMILY_STRING_MP3, dest, destlen); return 1; }
		if (!_wcsicmp(p, L"MP2")) { WASABI_API_LNGSTRINGW_BUF(IDS_FAMILY_STRING_MP2, dest, destlen); return 1; }
		if (!_wcsicmp(p, L"MP1")) { WASABI_API_LNGSTRINGW_BUF(IDS_FAMILY_STRING_MP1, dest, destlen); return 1; }
		if (!_wcsicmp(p, L"AAC")) { WASABI_API_LNGSTRINGW_BUF(IDS_FAMILY_STRING_MPEG2_AAC, dest, destlen); return 1; }
		if (!_wcsicmp(p, L"VLB")) { WASABI_API_LNGSTRINGW_BUF(IDS_FAMILY_STRING_DOLBY, dest, destlen); return 1; }
		return 0;
	}
	else if (!_stricmp(data, "mime"))
	{
		if (!fn || !fn[0]) return 0;
		int len = lstrlenW(fn);
		if (len < 4 || L'.' != fn[len - 4])  return 0;
		const wchar_t *p = &fn[len - 3];
		if (!_wcsicmp(p, L"MP3")) { StringCchCopyW(dest, destlen, L"audio/mpeg"); return 1; }
		if (!_wcsicmp(p, L"MP2")) { StringCchCopyW(dest, destlen, L"audio/mpeg"); return 1; }
		if (!_wcsicmp(p, L"MP1")) { StringCchCopyW(dest, destlen, L"audio/mpeg"); return 1; }
		if (!_wcsicmp(p, L"AAC")) { StringCchCopyW(dest, destlen, L"audio/aac"); return 1; }
		if (!_wcsicmp(p, L"VLB")) { StringCchCopyW(dest, destlen, L"audio/vlb"); return 1; }
		return 0;
	}
	else if (!_strnicmp(data, "uvox/", 5))
	{
		EnterCriticalSection(&streamInfoLock);
		if (g_playing_file)
		{
			if (g_playing_file->uvox_3901) // check again now that we've acquired the lock
			{
				Ultravox3901 uvox_metadata;
				if (uvox_metadata.Parse(g_playing_file->uvox_3901) != API_XML_FAILURE)
				{
					LeaveCriticalSection(&streamInfoLock);
					return uvox_metadata.GetExtendedData(data, dest, (int)destlen);
				}
			}
			else if (g_playing_file->uvox_3902)
			{
				Ultravox3902 uvox_metadata;
				if (uvox_metadata.Parse(g_playing_file->uvox_3902) != API_XML_FAILURE)
				{
					LeaveCriticalSection(&streamInfoLock);
					return uvox_metadata.GetExtendedData(data, dest, (int)destlen);
				}
			}
		}
		LeaveCriticalSection(&streamInfoLock);
		return 0;
	}
	else if (_stricmp(data, "0x3901") == 0)
	{
		EnterCriticalSection(&streamInfoLock);
		if (g_playing_file && g_playing_file->uvox_3901) // check again now that we've acquired the lock
		{
			if (dest == NULL)   // It's empty, he's looking for the size of the 0x3901
			{
				int size = MultiByteToWideChar(CP_UTF8, 0, g_playing_file->uvox_3901, -1, 0, 0);
				LeaveCriticalSection(&streamInfoLock);
				return size;
			}
			else
			{
				MultiByteToWideCharSZ(CP_UTF8, 0, g_playing_file->uvox_3901, -1, dest, (int)destlen);
				LeaveCriticalSection(&streamInfoLock);
				return 1;
			}
		}
		LeaveCriticalSection(&streamInfoLock);
		return 0;
	}
	else if (!_stricmp(data, "streamtype"))
	{
		if (lstrcmpW(lastfn, fn))
			return 0;

		if (g_playing_file)
		{
			EnterCriticalSection(&streamInfoLock);
			if (g_playing_file) // check again now that we've acquired the lock
			{
				StringCchPrintfW(dest, destlen, L"%d", g_playing_file->IsStream());
				LeaveCriticalSection(&streamInfoLock);
				return 1;
			}
			LeaveCriticalSection(&streamInfoLock);
		}

		return 0;
	}
	else if (!_stricmp(data, "streammetadata"))
	{
		if (lstrcmpW(lastfn, fn))
			return 0;

		if (g_playing_file)
		{
			uint32_t len=0;
			EnterCriticalSection(&streamInfoLock);
			if (g_playing_file && g_playing_file->GetID3v2(&len) && len > 0) // check again now that we've acquired the lock
			{
				lstrcpynW(dest, L"1", (int)destlen);
				LeaveCriticalSection(&streamInfoLock);
				return 1;	// always return 1 to ensure we can do title lookups
			}
			LeaveCriticalSection(&streamInfoLock);
		}
		return 0;
	}
	else if (!_stricmp(data, "streamtitle"))
	{
		EnterCriticalSection(&streamInfoLock);
		if (g_playing_file) // check again now that we've acquired the lock
			ConvertTryUTF8(g_playing_file->stream_current_title, dest, destlen);
		else
			dest[0]=0;
		LeaveCriticalSection(&streamInfoLock);
		return 1;
	}
	else if (!_stricmp(data, "streamname"))
	{
		EnterCriticalSection(&streamInfoLock);
		if (g_playing_file) // check again now that we've acquired the lock
			ConvertTryUTF8(g_playing_file->stream_name, dest, destlen);
		else
			dest[0]=0;
		LeaveCriticalSection(&streamInfoLock);
		return 1;
	}
	else if (!_stricmp(data, "streamurl"))
	{
		EnterCriticalSection(&streamInfoLock);
		if (g_playing_file) // check again now that we've acquired the lock
			ConvertTryUTF8(g_playing_file->stream_url, dest, destlen);
		else
			dest[0]=0;
		LeaveCriticalSection(&streamInfoLock);
		return 1;
	}	
	else if (!_stricmp(data, "streamgenre"))
	{
		EnterCriticalSection(&streamInfoLock);
		if (g_playing_file) // check again now that we've acquired the lock
			ConvertTryUTF8(g_playing_file->stream_genre, dest, destlen);
		else
			dest[0]=0;
		LeaveCriticalSection(&streamInfoLock);
		return 1;
	}
	else if (!_stricmp(data, "streaminformation"))
 	{
 		EnterCriticalSection(&streamInfoLock);
 		if (g_playing_file)
 			g_playing_file->GetStreamInfo(dest, destlen);
 		else
 			dest[0]=0;
 		LeaveCriticalSection(&streamInfoLock);
 		return 1;
 	}

	if (!fn || !fn[0]) return 0;

	if (!_wcsnicmp(fn, L"uvox://", 7))
		return 0;

	if (g_playing_file && PathIsURL(fn) && !lstrcmpW(lastfn, fn))
	{
		EnterCriticalSection(&streamInfoLock);
		uint32_t len = 0;
		if (g_playing_file && g_playing_file->GetID3v2(&len) && len > 0) // check again now that we've acquired the lock
		{
			Metadata meta(g_playing_file, fn);
			int ret = meta.GetExtendedData(data, dest, (int)destlen);
			LeaveCriticalSection(&streamInfoLock);
			return ret;
		}
		LeaveCriticalSection(&streamInfoLock);
	}

	if (PathIsURL(fn))
		return 0;

	if (m_ext_get_mp3info && (!m_ext_get_mp3info->IsMe(fn) || HasFileTimeChanged(fn)))
	{
		m_ext_get_mp3info->Release();
		m_ext_get_mp3info=0;
	}

	if (!m_ext_get_mp3info)
	{
		m_ext_get_mp3info = new Metadata;
		m_ext_get_mp3info->Open(fn);
	}

	return m_ext_get_mp3info->GetExtendedData(data, dest, (int)destlen);
}

extern "C"
	__declspec(dllexport) int winampSetExtendedFileInfoW(const wchar_t *fn, const char *data, const wchar_t *val)
{
	if (!m_ext_set_mp3info || (m_ext_set_mp3info && !m_ext_set_mp3info->IsMe(fn)))
	{
		if(m_ext_set_mp3info) m_ext_set_mp3info->Release();
		m_ext_set_mp3info = new Metadata;
		m_ext_set_mp3info->Open(fn);
	}
	return m_ext_set_mp3info->SetExtendedData(data, val);
}

extern "C"
	__declspec(dllexport) int winampWriteExtendedFileInfo()
{
	// flush our read cache too :)
	if(m_ext_get_mp3info) m_ext_get_mp3info->Release();
	m_ext_get_mp3info = NULL;

	if (!m_ext_set_mp3info) return 0;

	Stopper stopper;
	if (m_ext_set_mp3info->IsMe(lastfn))
		stopper.Stop();

	// just in-case something changed
	if (!m_ext_set_mp3info) return 0;

	int ret = m_ext_set_mp3info->Save();
	stopper.Play();
	m_ext_set_mp3info->Release();
	m_ext_set_mp3info = NULL;

	// update last modified so we're not re-queried on our own updates
	UpdateFileTimeChanged(lastfn);

	return !ret;
}

extern "C" __declspec(dllexport)
	int winampClearExtendedFileInfoW(const wchar_t *fn)
{
	Metadata meta;
	if (meta.Open(fn)==METADATA_SUCCESS)
	{
		meta.id3v2.Clear();
		Stopper stopper;
		if (meta.IsMe(lastfn))
			stopper.Stop();
		meta.Save();
		stopper.Play();

		// update last modified so we're not re-queried on our own updates
		UpdateFileTimeChanged(fn);

		return 1;
	}
	return 0;
}