WMDRMModule.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. #include "Main.h"
  2. #include "WMDRMModule.h"
  3. #include "AutoWide.h"
  4. #include "WMInformation.h"
  5. #include "AutoChar.h"
  6. #include "FileInfoDialog.h"
  7. #include "ConfigDialog.h"
  8. #include "resource.h"
  9. #include "StatusHook.h"
  10. #include "../nu/Config.h"
  11. #include "util.h"
  12. #include "WMPlaylist.h"
  13. #include "api.h"
  14. #include "output/OutPlugin.h"
  15. #include "output/AudioOut.h"
  16. #include <strsafe.h>
  17. extern Nullsoft::Utility::Config wmConfig;
  18. #define SAMPLES_PER_BLOCK 576
  19. AudioOut *out = 0;
  20. unsigned long endTime = 0;
  21. unsigned long startTime = 0;
  22. void InitOutputs(HWND hMainWindow, HMODULE hDllInstance)
  23. {}
  24. void WMDRM::AssignOutput()
  25. {
  26. out = &pluginOut;
  27. }
  28. WMDRM::WMDRM()
  29. : paused(false),
  30. clock(0), audio(0), video(0), wait(0), info(0), buffer(0), seek(0), reader(NULL), gain(0),
  31. killswitch(0), opened(false),
  32. drmProtected(false),
  33. volume(-666), pan(0),
  34. reader2(0), network(0), playing(false),
  35. startAtMilliseconds(0),
  36. dspBuffer(0),
  37. vizBuffer(0),
  38. reader1(0),
  39. flushed(false)
  40. {
  41. killswitch = CreateEvent(NULL, TRUE, TRUE, NULL);
  42. }
  43. WMDRM::~WMDRM()
  44. {
  45. DeleteObject(killswitch);
  46. delete [] dspBuffer;
  47. delete [] vizBuffer;
  48. }
  49. static int winampVersion=0;
  50. #define WINAMP_VERSION_MINOR1(winampVersion) ((winampVersion & 0x000000F0)>>4) // returns, i.e. 0x01 for 5.12 and 0x02 for 5.2...
  51. #define WINAMP_VERSION_MINOR2(winampVersion) ((winampVersion & 0x0000000F)) // returns, i.e. 0x02 for 5.12 and 0x00 for 5.2...
  52. static void MakeVersionString(QWORD *ver)
  53. {
  54. if (!winampVersion)
  55. winampVersion = (int)SendMessage(plugin.hMainWindow, WM_WA_IPC, 0, IPC_GETVERSION);
  56. LARGE_INTEGER temp;
  57. temp.HighPart = MAKELONG(WINAMP_VERSION_MINOR1(winampVersion), WINAMP_VERSION_MAJOR(winampVersion));
  58. temp.LowPart = MAKELONG(WASABI_API_APP->main_getBuildNumber(), WINAMP_VERSION_MINOR2(winampVersion));
  59. *ver = temp.QuadPart;
  60. }
  61. static void MakeUserAgentString(wchar_t str[256])
  62. {
  63. if (!winampVersion)
  64. winampVersion = (int)SendMessage(plugin.hMainWindow, WM_WA_IPC, 0, IPC_GETVERSION);
  65. StringCchPrintfW(str, 256, L"WinampASF/%01x.%02x",
  66. WINAMP_VERSION_MAJOR(winampVersion),
  67. WINAMP_VERSION_MINOR(winampVersion));
  68. }
  69. void WMDRM::InitWM()
  70. {
  71. static int triedInit = 0;
  72. if (!triedInit)
  73. {
  74. if (FAILED(WMCreateReader(0, WMT_RIGHT_PLAYBACK, &reader)) || !reader)
  75. {
  76. reader = 0;
  77. plugin.FileExtensions = "\0";
  78. return ;
  79. }
  80. if (FAILED(reader->QueryInterface(&reader1)))
  81. reader1 = 0;
  82. if (FAILED(reader->QueryInterface(&reader2)))
  83. reader2 = 0;
  84. if (FAILED(reader->QueryInterface(&network)))
  85. network = 0;
  86. if (reader1)
  87. {
  88. QWORD verStr;
  89. wchar_t userAgent[256] = {0};
  90. MakeVersionString(&verStr);
  91. MakeUserAgentString(userAgent);
  92. WM_READER_CLIENTINFO info;
  93. ZeroMemory(&info, sizeof(WM_READER_CLIENTINFO));
  94. info.cbSize = sizeof(WM_READER_CLIENTINFO);
  95. info.wszHostExe = L"winamp.exe";
  96. info.qwHostVersion = verStr;
  97. info.wszPlayerUserAgent = userAgent;
  98. info.wszBrowserWebPage = L"http://www.winamp.com";
  99. reader1->SetClientInfo(&info);
  100. }
  101. clock = new ClockLayer(reader);
  102. audio = new AudioLayer(reader);
  103. video = new VideoLayer(reader);
  104. wait = new WaitLayer(reader);
  105. info = new WMInformation(reader);
  106. buffer = new BufferLayer(reader);
  107. seek = new SeekLayer(reader, clock);
  108. gain = new GainLayer(audio, info);
  109. callback >> seek >> buffer >> clock >> video >> audio >> wait >> gain >> this;
  110. triedInit = 1;
  111. }
  112. }
  113. void WMDRM::Init()
  114. {
  115. winampVersion = (int)SendMessage(plugin.hMainWindow, WM_WA_IPC, 0, IPC_GETVERSION);
  116. InitOutputs(plugin.hMainWindow, plugin.hDllInstance);
  117. //Hook(plugin.hMainWindow);
  118. }
  119. void WMDRM::Config(HWND hwndParent)
  120. {
  121. WASABI_API_DIALOGBOXW(IDD_CONFIG, hwndParent, PreferencesDialogProc);
  122. }
  123. void WMDRM::Quit()
  124. {
  125. activePlaylist.Clear();
  126. delete setFileInfo; setFileInfo = 0;
  127. delete clock; clock = 0;
  128. delete audio; audio = 0;
  129. delete video; video = 0;
  130. delete wait; wait = 0;
  131. delete info; info = 0;
  132. delete buffer; buffer = 0;
  133. delete seek; seek = 0;
  134. if (network) network->Release(); network = 0;
  135. if (reader2) reader2->Release(); reader2 = 0;
  136. if (reader1) reader1->Release(); reader1 = 0;
  137. if (reader) reader->Release(); reader = 0;
  138. // Unhook(plugin.hMainWindow);
  139. }
  140. static void BuildTitle(WMInformation *info, const wchar_t *file, wchar_t *str, size_t len)
  141. {
  142. if (info)
  143. {
  144. wchar_t artist[256] = L"", title[256] = L"";
  145. info->GetAttribute(g_wszWMAuthor, artist, 256);
  146. info->GetAttribute(g_wszWMTitle, title, 256);
  147. if (!artist[0] && !title[0])
  148. {
  149. if (file && *file)
  150. {
  151. StringCchCopy(str, len, file);
  152. }
  153. }
  154. else if (artist[0] && title[0])
  155. StringCchPrintf(str, len, L"%s - %s", artist, title);
  156. else if (artist[0])
  157. StringCchCopy(str, len, artist);
  158. else if (title[0])
  159. StringCchCopy(str, len, title);
  160. }
  161. else if (file)
  162. StringCchCopy(str, len, file);
  163. }
  164. void WMDRM::GetFileInfo(const wchar_t *file, wchar_t *title, int *length_in_ms)
  165. {
  166. InitWM();
  167. if (length_in_ms) *length_in_ms = -1000;
  168. if (file && file[0])
  169. {
  170. bool isURL = !!PathIsURL(file);
  171. if (config_http_metadata || !isURL)
  172. {
  173. WMInformation getFileInfo(file, true);
  174. if (title)
  175. {
  176. BuildTitle(&getFileInfo, file, title, GETFILEINFO_TITLE_LENGTH);
  177. }
  178. if (length_in_ms) *length_in_ms = getFileInfo.GetLengthMilliseconds();
  179. }
  180. else
  181. {
  182. if (title)
  183. StringCchCopy(title, GETFILEINFO_TITLE_LENGTH, file);
  184. }
  185. winamp.GetStatus(title, 256, file);
  186. }
  187. else if (activePlaylist.GetFileName())
  188. {
  189. //isURL = !!wcsstr(activePlaylist.GetFileName(), L"://");
  190. if (wait && !wait->IsOpen()) // if it's not open, fill in with some default data... WMDRM::Opened() will refresh the title ...
  191. {
  192. StringCchCopy(title, GETFILEINFO_TITLE_LENGTH, activePlaylist.GetOriginalFileName());
  193. if (length_in_ms) *length_in_ms = -1000;
  194. //if (isURL)
  195. winamp.GetStatus(title, 256, activePlaylist.GetOriginalFileName());
  196. return ;
  197. }
  198. if (title)
  199. {
  200. BuildTitle(info, activePlaylist.GetOriginalFileName(), title, GETFILEINFO_TITLE_LENGTH);
  201. }
  202. if (info)
  203. if (length_in_ms) *length_in_ms = info->GetLengthMilliseconds();
  204. else
  205. if (length_in_ms) *length_in_ms = -1000;
  206. //if (isURL)
  207. winamp.GetStatus(title, 256, activePlaylist.GetOriginalFileName());
  208. }
  209. }
  210. int WMDRM::InfoBox(const wchar_t *fn, HWND hwndParent)
  211. {
  212. /* CUT> we're now using the unified file info dialogue
  213. FileInfoDialog dialog(WASABI_API_LNG_HINST, hwndParent, fn);
  214. if (dialog.WasEdited())
  215. return 0;
  216. else
  217. return 1;
  218. */
  219. return 0;
  220. }
  221. int WMDRM::IsOurFile(const in_char *fn)
  222. {
  223. // if (!reader)
  224. // return 0;
  225. if (wcsstr(fn, L".asx")) // TODO: need something WAY better than this
  226. return 1;
  227. return fileTypes.IsSupportedURL(fn);
  228. }
  229. int WMDRM::Play(const wchar_t * fn)
  230. {
  231. InitWM();
  232. if (!reader)
  233. return -1;
  234. if (network)
  235. network->SetBufferingTime((QWORD)config_buffer_time*10000LL);
  236. ResetEvent(killswitch);
  237. wait->ResetForOpen();
  238. activePlaylist.Clear();
  239. activePlaylist.playlistFilename = _wcsdup(fn);
  240. if (playlistManager->Load(fn, &activePlaylist) != PLAYLISTMANAGER_SUCCESS)
  241. activePlaylist.OnFile(fn, 0, -1, 0); // add it manually (TODO: need a better way to do this)
  242. winamp.GetVideoOutput();
  243. playing = true;
  244. startTime = winamp.GetStart() * 1000;
  245. if (!startAtMilliseconds)
  246. startAtMilliseconds = startTime;
  247. endTime = winamp.GetEnd() * 1000;
  248. clock->SetLastOutputTime(startAtMilliseconds); // normally 0, but set when metadata editor needs to stop / restart a file
  249. AssignOutput();
  250. clock->SetStartTimeMilliseconds(startAtMilliseconds); // normally 0, but set when metadata editor needs to stop / restart a file
  251. startAtMilliseconds = 0;
  252. return seek->Open(activePlaylist.GetFileName(), &callback);
  253. }
  254. void WMDRM::ReOpen()
  255. {
  256. if (opened)
  257. seek->Stop();
  258. seek->Open(activePlaylist.GetFileName(), &callback);
  259. }
  260. void WMDRM::Pause()
  261. {
  262. paused = true;
  263. if (seek)
  264. seek->Pause();
  265. }
  266. void WMDRM::UnPause()
  267. {
  268. paused = false;
  269. if (seek)
  270. seek->Unpause();
  271. }
  272. int WMDRM::IsPaused()
  273. {
  274. return (int)paused;
  275. }
  276. void WMDRM::Stop()
  277. {
  278. if (!playing)
  279. return ;
  280. playing = false;
  281. SetEvent(killswitch);
  282. if (paused)
  283. UnPause();
  284. if (seek)
  285. seek->Stop();
  286. }
  287. void WMDRM::Closed()
  288. {
  289. opened = false;
  290. WMHandler::Closed();
  291. }
  292. int WMDRM::GetLength()
  293. {
  294. if (info)
  295. return info->GetLengthMilliseconds();
  296. else
  297. return 0;
  298. }
  299. int WMDRM::GetOutputTime()
  300. {
  301. if (!opened)
  302. {
  303. //if (winamp.bufferCount)
  304. return winamp.bufferCount;
  305. //return 0;
  306. }
  307. return clock->GetOutputTime();
  308. }
  309. void WMDRM::SetOutputTime(int time_in_ms)
  310. {
  311. if (startTime || endTime)
  312. {
  313. unsigned int seektime = time_in_ms;
  314. if (endTime && seektime > endTime)
  315. seektime = endTime;
  316. if (startTime && seektime < startTime)
  317. seektime = startTime;
  318. seek->SeekTo(seektime);
  319. return ;
  320. }
  321. seek->SeekTo(time_in_ms);
  322. }
  323. void WMDRM::SetVolume(int volume)
  324. {
  325. this->volume = volume;
  326. if (out)
  327. out->SetVolume(volume);
  328. }
  329. void WMDRM::SetPan(int pan)
  330. {
  331. this->pan = pan;
  332. if (out)
  333. out->SetPan(pan);
  334. }
  335. void WMDRM::EQSet(int on, char data[10], int preamp)
  336. {}
  337. void WMDRM::BuildBuffers()
  338. {
  339. remaining.Allocate(audio->AudioSamplesToBytes(SAMPLES_PER_BLOCK));
  340. // TODO: check against old size
  341. delete [] dspBuffer;
  342. delete [] vizBuffer;
  343. dspBuffer = new unsigned char[audio->AudioSamplesToBytes(SAMPLES_PER_BLOCK) * 2];
  344. vizBuffer = new unsigned char[audio->AudioSamplesToBytes(SAMPLES_PER_BLOCK) * 2];
  345. }
  346. void WMDRM::AudioDataReceived(void *_data, unsigned long sizeBytes, DWORD timestamp)
  347. {
  348. // TODO: apply replaygain first
  349. // but if we change bitdepth, we'll have to be careful about calling audio->AudioSamplesToBytes() and similiar functions
  350. unsigned char *data = (unsigned char *)_data;
  351. if (!remaining.Empty())
  352. {
  353. if (WaitForSingleObject(killswitch, 0) == WAIT_OBJECT_0)
  354. {
  355. remaining.Flush();
  356. return ;
  357. }
  358. remaining.UpdatingWrite(data, sizeBytes);
  359. if (remaining.Full())
  360. {
  361. OutputAudioSamples(remaining.GetData(), SAMPLES_PER_BLOCK, timestamp);
  362. remaining.Flush();
  363. }
  364. }
  365. long samplesLeft = audio->AudioBytesToSamples(sizeBytes);
  366. while (samplesLeft)
  367. {
  368. if (WaitForSingleObject(killswitch, 0) == WAIT_OBJECT_0)
  369. {
  370. remaining.Flush();
  371. return ;
  372. }
  373. if (samplesLeft >= SAMPLES_PER_BLOCK)
  374. {
  375. OutputAudioSamples(data, SAMPLES_PER_BLOCK, timestamp);
  376. data += audio->AudioSamplesToBytes(SAMPLES_PER_BLOCK);
  377. samplesLeft -= SAMPLES_PER_BLOCK;
  378. }
  379. else
  380. {
  381. unsigned long bytesLeft = audio->AudioSamplesToBytes(samplesLeft);
  382. remaining.UpdatingWrite(data, bytesLeft);
  383. samplesLeft = audio->AudioBytesToSamples(bytesLeft); // should always be 0
  384. assert(samplesLeft == 0);
  385. }
  386. }
  387. }
  388. void WMDRM::QuantizedViz(void *data, long sizeBytes, DWORD timestamp)
  389. {
  390. if (drmProtected)
  391. {
  392. assert(sizeBytes == audio->Channels() *(audio->BitSize() / 8) * SAMPLES_PER_BLOCK);
  393. memset(vizBuffer, 0, sizeBytes);
  394. ptrdiff_t stride = audio->BitSize() / 8;
  395. size_t position = stride - 1;
  396. unsigned char *origData = (unsigned char *)data;
  397. for (int i = 0;i < SAMPLES_PER_BLOCK*audio->Channels();i++) // winamp hardcodes this ...
  398. {
  399. vizBuffer[position] = (origData[position] & 0xFC); // 6 bits of precision, enough for viz.
  400. position += stride;
  401. }
  402. plugin.SAAddPCMData((char *) vizBuffer, audio->Channels(), audio->ValidBits(), timestamp);
  403. plugin.VSAAddPCMData((char *) vizBuffer, audio->Channels(), audio->ValidBits(), timestamp);
  404. }
  405. else
  406. {
  407. plugin.SAAddPCMData((char *) data, audio->Channels(), audio->ValidBits(), timestamp);
  408. plugin.VSAAddPCMData((char *) data, audio->Channels(), audio->ValidBits(), timestamp);
  409. }
  410. }
  411. long WMDRM::GetPosition()
  412. {
  413. if (!opened)
  414. return 0;
  415. return out->GetWrittenTime();
  416. }
  417. void WMDRM::OutputAudioSamples(void *data, long samples)
  418. {
  419. DWORD timestamp = out->GetWrittenTime();
  420. OutputAudioSamples(data, samples, timestamp);
  421. }
  422. void WMDRM::OutputAudioSamples(void *data, long samples, DWORD &timestamp)
  423. {
  424. clock->SetLastOutputTime(timestamp);
  425. timestamp += audio->AudioSamplesToMilliseconds(samples);
  426. //clock->SetLastOutputTime(winamp.GetWrittenTime());
  427. //in theory, we could check mod->dsp_isactive(), but that opens up a potential race condition ...
  428. memcpy(dspBuffer, data, audio->AudioSamplesToBytes(samples));
  429. int dspSize = samples;
  430. if (!drmProtected)
  431. dspSize = plugin.dsp_dosamples((short *)dspBuffer, samples, audio->BitSize(), audio->Channels(), audio->SampleRate());
  432. dspSize = audio->AudioSamplesToBytes(dspSize);
  433. if (samples == SAMPLES_PER_BLOCK)
  434. QuantizedViz(dspBuffer, dspSize, timestamp);
  435. while (out->CanWrite() <= dspSize)
  436. {
  437. if (WaitForSingleObject(killswitch, 10) == WAIT_OBJECT_0)
  438. {
  439. remaining.Flush();
  440. return ;
  441. }
  442. }
  443. out->Write((char *)dspBuffer, dspSize);
  444. /*long bytesAvail = */out->CanWrite();
  445. }
  446. void WMDRM::Opened()
  447. {
  448. //winamp.ResetBuffering();
  449. drmProtected = info->IsAttribute(g_wszWMProtected);
  450. ResetEvent(killswitch);
  451. if (!audio->IsOpen())
  452. {
  453. if (video->IsOpen())
  454. {
  455. winamp.SetStatus(WASABI_API_LNGSTRINGW(IDS_REALTIME));
  456. clock->GoRealTime();
  457. plugin.is_seekable = info->IsSeekable() ? 1 : 0;
  458. winamp.SetAudioInfo(info->GetBitrate() / 1000, 0, 0);
  459. //out->SetVolume( -666); // set default volume
  460. }
  461. else
  462. {
  463. // no audio or video!!
  464. seek->Stop();
  465. First().OpenFailed();
  466. return ;
  467. }
  468. }
  469. else
  470. {
  471. BuildBuffers();
  472. plugin.is_seekable = info->IsSeekable() ? 1 : 0;
  473. winamp.SetAudioInfo(info->GetBitrate() / 1000, audio->SampleRate() / 1000, audio->Channels());
  474. out->SetVolume(volume); // set default volume
  475. out->SetPan(pan);
  476. winamp.SetVizInfo(audio->SampleRate(), audio->Channels());
  477. }
  478. opened = true;
  479. winamp.ClearStatus();
  480. reader->Start(clock->GetStartTime(), 0, 1.0f, NULL);
  481. WMHandler::Opened();
  482. }
  483. void WMDRM::Started()
  484. {
  485. ResetEvent(killswitch);
  486. winamp.ResetBuffering();
  487. winamp.ClearStatus();
  488. WMHandler::Started();
  489. }
  490. void WMDRM::EndOfFile()
  491. {
  492. if (audio->IsOpen())
  493. {
  494. if (WaitForSingleObject(killswitch, 0) != WAIT_OBJECT_0)
  495. {
  496. if (remaining.used)
  497. {
  498. OutputAudioSamples(remaining.GetData(), audio->AudioBytesToSamples(remaining.used));
  499. remaining.Flush();
  500. }
  501. out->Write(0, 0);
  502. while (out->IsPlaying())
  503. {
  504. if (WaitForSingleObject(killswitch, 10) == WAIT_OBJECT_0)
  505. {
  506. break;
  507. }
  508. }
  509. }
  510. }
  511. // TODO: if we have a playlist, start the next track instead of telling winamp to go to the next track
  512. if (playing)
  513. winamp.EndOfFile();
  514. WMHandler::EndOfFile();
  515. }
  516. void WMDRM::NewMetadata()
  517. {
  518. winamp.RefreshTitle();
  519. WMHandler::NewMetadata();
  520. }
  521. void WMDRM::Error()
  522. {
  523. // wait 200 ms for the killswitch (aka hitting stop)
  524. // this allows the user to hit "stop" and not have to continue cycling through songs if there are a whole bunch of bad/missing WMAs in the playlist
  525. if (playing && WaitForSingleObject(killswitch, 200) != WAIT_OBJECT_0)
  526. winamp.EndOfFile();
  527. }
  528. void WMDRM::OpenFailed()
  529. {
  530. if (playing && WaitForSingleObject(killswitch, 200) != WAIT_OBJECT_0) // wait 200 ms for the killswitch (see above notes)
  531. winamp.EndOfFile();
  532. }
  533. void WMDRM::Stopped()
  534. {
  535. remaining.Flush();
  536. WMHandler::Stopped();
  537. }
  538. void WMDRM::Kill()
  539. {
  540. SetEvent(killswitch);
  541. WMHandler::Kill();
  542. }
  543. void WMDRM::NewSourceFlags()
  544. {
  545. plugin.is_seekable = info->IsSeekable() ? 1 : 0;
  546. }
  547. void WMDRM::Connecting()
  548. {
  549. winamp.SetStatus(WASABI_API_LNGSTRINGW(IDS_CONNECTING));
  550. WMHandler::Connecting();
  551. }
  552. void WMDRM::Locating()
  553. {
  554. winamp.SetStatus(WASABI_API_LNGSTRINGW(IDS_LOCATING));
  555. WMHandler::Locating();
  556. }
  557. void WMDRM::AccessDenied()
  558. {
  559. winamp.SetStatus(WASABI_API_LNGSTRINGW(IDS_ACCESS_DENIED));
  560. if (playing && WaitForSingleObject(killswitch, 200) != WAIT_OBJECT_0) // wait 200 ms for the killswitch (see above notes)
  561. winamp.PressStop();
  562. }