BufferLayer.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "BufferLayer.h"
  2. #include "Main.h"
  3. #include "resource.h"
  4. #define killEvent events[0]
  5. #define startEvent events[1]
  6. enum
  7. {
  8. KILL_EVENT = 0,
  9. START_EVENT = 1,
  10. };
  11. DWORD WINAPI BufferLayer::BufThread_stub(void *ptr)
  12. {
  13. ((BufferLayer *)ptr)->BufThread();
  14. return 0;
  15. }
  16. BufferLayer::BufferLayer(IWMReader *reader) : reader2(0), buffering(false)
  17. {
  18. if (FAILED(reader->QueryInterface(&reader2)))
  19. reader2 = 0;
  20. startEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  21. killEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  22. DWORD id;
  23. thread = CreateThread(NULL, 128*1024, BufThread_stub, (void *)this, NULL, &id);
  24. }
  25. BufferLayer::~BufferLayer()
  26. {
  27. SetEvent(killEvent);
  28. ResetEvent(startEvent);
  29. WaitForSingleObject(thread, INFINITE);
  30. if (reader2) reader2->Release(); reader2 = 0;
  31. }
  32. void BufferLayer::BufferingStarted()
  33. {
  34. winamp.SetStatus(WASABI_API_LNGSTRINGW(IDS_BUFFERING));
  35. buffering=true;
  36. SetEvent(startEvent);
  37. WMHandler::BufferingStarted();
  38. }
  39. void BufferLayer::BufferingStopped()
  40. {
  41. winamp.SetStatus(L"");
  42. buffering=false;
  43. ResetEvent(startEvent);
  44. WMHandler::BufferingStopped();
  45. }
  46. int BufferLayer::Wait()
  47. {
  48. if (WaitForSingleObject(killEvent, 0) == WAIT_OBJECT_0)
  49. return KILL_EVENT;
  50. return WaitForMultipleObjects(2, events, FALSE, INFINITE) - WAIT_OBJECT_0;
  51. }
  52. void BufferLayer::BufThread()
  53. {
  54. do
  55. {
  56. switch (Wait())
  57. {
  58. case KILL_EVENT:
  59. return ;
  60. case START_EVENT:
  61. {
  62. if (reader2)
  63. {
  64. DWORD percent;
  65. QWORD throwAway;
  66. if (SUCCEEDED(reader2->GetBufferProgress(&percent, &throwAway)))
  67. winamp.Buffering(percent, WASABI_API_LNGSTRINGW(IDS_BUFFERING));
  68. }
  69. Sleep(10);
  70. }
  71. continue;
  72. }
  73. }
  74. while (true);
  75. }
  76. void BufferLayer::OpenFailed()
  77. {
  78. ResetEvent(startEvent);
  79. WMHandler::OpenFailed();
  80. }