main.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. ** Copyright © 2007-2014 Winamp SA
  3. **
  4. ** This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held
  5. ** liable for any damages arising from the use of this software.
  6. **
  7. ** Permission is granted to anyone to use this software for any purpose, including commercial applications, and to
  8. ** alter it and redistribute it freely, subject to the following restrictions:
  9. **
  10. ** 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software.
  11. ** If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  12. **
  13. ** 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  14. **
  15. ** 3. This notice may not be removed or altered from any source distribution.
  16. **
  17. ** Author: Ben Allison [email protected]
  18. ** Created: March 1, 2007
  19. **
  20. */
  21. #include "main.h"
  22. #include "api__in_flv.h"
  23. #include "Metadata.h"
  24. #include "../Agave/Language/api_language.h"
  25. #include <api/service/waServiceFactory.h>
  26. #include "../Winamp/wa_ipc.h"
  27. #include "../nu/Singleton.h"
  28. #include "../nu/Autochar.h"
  29. #include <shlwapi.h>
  30. #include "resource.h"
  31. #include "AlbumArt.h"
  32. #include "RawReader.h"
  33. #include <strsafe.h>
  34. #include "nswasabi/ReferenceCounted.h"
  35. #include "mkv_flac_decoder.h"
  36. api_config *AGAVE_API_CONFIG = 0;
  37. api_memmgr *WASABI_API_MEMMGR=0;
  38. AlbumArtFactory albumArtFactory;
  39. static RawMediaReaderService raw_media_reader_service;
  40. static SingletonServiceFactory<svc_raw_media_reader, RawMediaReaderService> raw_factory;
  41. static MKVDecoder mkv_service;
  42. static SingletonServiceFactory<svc_mkvdecoder, MKVDecoder> mkv_factory;
  43. // wasabi based services for localisation support
  44. api_language *WASABI_API_LNG = 0;
  45. HINSTANCE WASABI_API_LNG_HINST = 0, WASABI_API_ORIG_HINST = 0;
  46. HANDLE killswitch=0;
  47. HANDLE playThread=0;
  48. const wchar_t *winampINI=0;
  49. void Config(HWND hwndParent);
  50. void About(HWND hwndParent);
  51. wchar_t pluginName[256] = {0};
  52. int Init()
  53. {
  54. if (!IsWindow(plugin.hMainWindow))
  55. return IN_INIT_FAILURE;
  56. killswitch = CreateEvent(0, TRUE, FALSE, 0);
  57. plugin.service->service_register(&albumArtFactory);
  58. raw_factory.Register(plugin.service, &raw_media_reader_service);
  59. mkv_factory.Register(plugin.service, &mkv_service);
  60. waServiceFactory *sf = (waServiceFactory *)plugin.service->service_getServiceByGuid(AgaveConfigGUID);
  61. if (sf) AGAVE_API_CONFIG= (api_config *)sf->getInterface();
  62. sf = (waServiceFactory *)plugin.service->service_getServiceByGuid(memMgrApiServiceGuid);
  63. if (sf) WASABI_API_MEMMGR= (api_memmgr *)sf->getInterface();
  64. // loader so that we can get the localisation service api for use
  65. sf = plugin.service->service_getServiceByGuid(languageApiGUID);
  66. if (sf) WASABI_API_LNG = reinterpret_cast<api_language*>(sf->getInterface());
  67. // need to have this initialised before we try to do anything with localisation features
  68. WASABI_API_START_LANG(plugin.hDllInstance,InFlacLangGUID);
  69. StringCchPrintfW(pluginName,256,WASABI_API_LNGSTRINGW(IDS_NULLSOFT_FLAC_DECODER),PLUGIN_VER);
  70. plugin.description = (char*)pluginName;
  71. winampINI = (const wchar_t *)SendMessage(plugin.hMainWindow, WM_WA_IPC, 0, IPC_GETINIFILEW);
  72. wchar_t exts[1024] = {0};
  73. GetPrivateProfileStringW(L"in_flac", L"extensions", DEFAULT_EXTENSIONSW, exts, 1024, winampINI);
  74. plugin.FileExtensions = BuildExtensions(AutoChar(exts));
  75. config_average_bitrate = !!GetPrivateProfileIntW(L"in_flac", L"average_bitrate", 1, winampINI);
  76. plugin.UsesOutputPlug|=8;
  77. return IN_INIT_SUCCESS;
  78. }
  79. void Quit()
  80. {
  81. CloseHandle(killswitch);
  82. waServiceFactory *sf = (waServiceFactory *)plugin.service->service_getServiceByGuid(AgaveConfigGUID);
  83. if (sf) sf->releaseInterface(AGAVE_API_CONFIG);
  84. sf = (waServiceFactory *)plugin.service->service_getServiceByGuid(memMgrApiServiceGuid);
  85. if (sf) sf->releaseInterface(WASABI_API_MEMMGR);
  86. plugin.service->service_deregister(&albumArtFactory);
  87. raw_factory.Deregister(plugin.service);
  88. }
  89. void GetFileInfo(const in_char *file, in_char *title, int *length_in_ms)
  90. {
  91. if (length_in_ms)
  92. {
  93. if (!file || !*file && currentSongLength != -1000)
  94. *length_in_ms = currentSongLength;
  95. else
  96. {
  97. FLACMetadata metadata;
  98. unsigned __int64 length_in_msec;
  99. if (metadata.Open(file) && metadata.GetLengthMilliseconds(&length_in_msec))
  100. *length_in_ms = (int)length_in_msec;
  101. else
  102. *length_in_ms=-1000;
  103. }
  104. }
  105. if (title) *title=0;
  106. }
  107. int InfoBox(const in_char *file, HWND hwndParent) { return 0; }
  108. int IsOurFile(const in_char *fn)
  109. {
  110. return 0;
  111. }
  112. wchar_t *lastfn=0;
  113. HANDLE threadStarted;
  114. extern FLAC__uint64 lastoutputtime;
  115. extern volatile int bufferCount;
  116. int Play(const in_char *fn)
  117. {
  118. free(lastfn);
  119. lastfn=_wcsdup(fn);
  120. currentSongLength=-1000;
  121. plugin.is_seekable = 0;
  122. plugin.SetInfo(0,0,0,0);
  123. lastoutputtime=0;
  124. bufferCount=0;
  125. ResetEvent(killswitch);
  126. DWORD threadId;
  127. threadStarted = CreateEvent(0, TRUE, FALSE, 0);
  128. ReferenceCountedNXString filename_nx;
  129. nx_uri_t filename_uri;
  130. NXStringCreateWithUTF16(&filename_nx, fn);
  131. NXURICreateWithNXString(&filename_uri, filename_nx);
  132. playThread=CreateThread(0, 0, FLACThread, filename_uri, 0, &threadId);
  133. SetThreadPriority(playThread, AGAVE_API_CONFIG->GetInt(playbackConfigGroupGUID, L"priority", THREAD_PRIORITY_HIGHEST));
  134. WaitForSingleObject(threadStarted, INFINITE);
  135. CloseHandle(threadStarted);
  136. return 0;
  137. }
  138. int localPause=0;
  139. void Pause()
  140. {
  141. localPause=1;
  142. QueueUserAPC(APCPause, playThread, (ULONG_PTR)1);
  143. }
  144. void UnPause()
  145. {
  146. localPause=0;
  147. QueueUserAPC(APCPause, playThread, (ULONG_PTR)0);
  148. }
  149. int IsPaused()
  150. {
  151. return localPause;
  152. }
  153. void Stop()
  154. {
  155. SetEvent(killswitch);
  156. WaitForSingleObject(playThread, INFINITE);
  157. currentSongLength=-1000;
  158. plugin.outMod->Close();
  159. plugin.SAVSADeInit();
  160. free(lastfn);
  161. lastfn=0;
  162. }
  163. int GetLength()
  164. {
  165. return currentSongLength;
  166. }
  167. int GetOutputTime()
  168. {
  169. if (bufferCount)
  170. return bufferCount;
  171. if (plugin.outMod)
  172. {
  173. return (int)lastoutputtime + (plugin.outMod->GetOutputTime() - plugin.outMod->GetWrittenTime());
  174. }
  175. else
  176. return 0;
  177. }
  178. void SetOutputTime(int time_in_ms)
  179. {
  180. lastoutputtime=time_in_ms; // cheating a bit here :)
  181. QueueUserAPC(APCSeek, playThread, (ULONG_PTR)time_in_ms);
  182. }
  183. int pan = 0, volume = -666;
  184. void SetVolume(int _volume)
  185. {
  186. volume = _volume;
  187. if (plugin.outMod)
  188. plugin.outMod->SetVolume(volume);
  189. }
  190. void SetPan(int _pan)
  191. {
  192. pan = _pan;
  193. if (plugin.outMod)
  194. plugin.outMod->SetPan(pan);
  195. }
  196. void EQSet(int on, char data[10], int preamp)
  197. {}
  198. In_Module plugin =
  199. {
  200. IN_VER_RET,
  201. "nullsoft(in_flac.dll)",
  202. 0,
  203. 0,
  204. "FLAC\0FLAC Files\0",
  205. 1,
  206. 1,
  207. Config,
  208. About,
  209. Init,
  210. Quit,
  211. GetFileInfo,
  212. InfoBox,
  213. IsOurFile,
  214. Play,
  215. Pause,
  216. UnPause,
  217. IsPaused,
  218. Stop,
  219. GetLength,
  220. GetOutputTime,
  221. SetOutputTime,
  222. SetVolume,
  223. SetPan,
  224. 0,
  225. 0,
  226. 0,
  227. 0,
  228. 0,
  229. 0,
  230. 0,
  231. 0,
  232. 0,
  233. 0,
  234. 0,
  235. EQSet,
  236. 0,
  237. 0
  238. };
  239. extern "C" __declspec(dllexport) In_Module * winampGetInModule2()
  240. {
  241. return &plugin;
  242. }