VideoThread.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include "Main.h"
  2. #include "VideoThread.h"
  3. #include "VideoLayer.h"
  4. #include "config.h"
  5. #include <windows.h>
  6. DWORD WINAPI VidThread_stub(void *ptr)
  7. {
  8. ((VideoThread *)ptr)->VidThread();
  9. return 0;
  10. }
  11. VideoThread::VideoThread() : converter(0), clock(0)
  12. {
  13. drm = false;
  14. DWORD id;
  15. thread = CreateThread(NULL, 256*1024, VidThread_stub, (void *)this, NULL, &id);
  16. SetThreadPriority(thread, AGAVE_API_CONFIG->GetInt(playbackConfigGroupGUID, L"priority", THREAD_PRIORITY_HIGHEST));
  17. }
  18. void VideoThread::Start(VideoDataConverter *_converter, WMHandler *_clock)
  19. {
  20. clock = _clock;
  21. if (converter != _converter)
  22. {
  23. if (converter)
  24. delete converter;
  25. converter = _converter;
  26. }
  27. ResetEvent(stopped);
  28. QueueUserAPC(MediaThread_StartAPC, thread, reinterpret_cast<ULONG_PTR>(this));
  29. }
  30. void VideoThread::VidThread()
  31. {
  32. while (true)
  33. {
  34. switch (WaitForSingleObjectEx(killEvent, wait, TRUE))
  35. {
  36. case WAIT_OBJECT_0:
  37. //StopAPC();
  38. return;
  39. case WAIT_TIMEOUT:
  40. {
  41. if (buffers.empty())
  42. {
  43. SetEvent(bufferFreed);
  44. continue;
  45. }
  46. MediaBuffer *buffer = buffers.front();
  47. __int64 diff;
  48. clock->TimeToSync(buffer->timestamp, diff);
  49. if (diff < VIDEO_ACCEPTABLE_JITTER)
  50. {
  51. void *data;
  52. DWORD size;
  53. buffer->buffer->GetBufferAndLength((BYTE **)&data, &size);
  54. if (buffer->drmProtected)
  55. winamp.EncryptedDrawFrame(converter->Convert(data));
  56. else
  57. winamp.DrawFrame(converter->Convert(data));
  58. try {
  59. buffer->buffer->Release();
  60. delete buffer;
  61. } catch (...) {}
  62. //buffers.pop_front();
  63. if (buffers.size())
  64. {
  65. buffers.erase(buffers.begin());
  66. }
  67. }
  68. if (buffers.size() < config_video_cache_frames)
  69. SetEvent(bufferFreed);
  70. }
  71. continue;
  72. default:
  73. continue;
  74. }
  75. }
  76. }
  77. void VideoThread::AddAPC(MediaBuffer *buffer)
  78. {
  79. if (buffers.empty())
  80. {
  81. __int64 diff;
  82. clock->TimeToSync(buffer->timestamp, diff);
  83. if (diff < VIDEO_ACCEPTABLE_JITTER)
  84. {
  85. void *data;
  86. DWORD size;
  87. buffer->buffer->GetBufferAndLength((BYTE **)&data, &size);
  88. if (buffer->drmProtected)
  89. winamp.EncryptedDrawFrame(converter->Convert(data));
  90. else
  91. winamp.DrawFrame(converter->Convert(data));
  92. buffer->buffer->Release();
  93. if (buffers.size() >= config_video_cache_frames)
  94. ResetEvent(bufferFreed);
  95. return;
  96. }
  97. }
  98. OrderedInsert(buffer);
  99. if (buffers.size() >= config_video_cache_frames)
  100. ResetEvent(bufferFreed);
  101. }
  102. struct VideoOpenParameters
  103. {
  104. int width;
  105. int height;
  106. int color_format;
  107. double aspect;
  108. int flip;
  109. bool drm;
  110. };
  111. VOID CALLBACK VideoThread::VideoThread_VideoOpenAPC(ULONG_PTR params)
  112. {
  113. VideoOpenParameters *p = (VideoOpenParameters *)params;
  114. if (p->drm)
  115. {
  116. winamp.OpenEncryptedVideo(p->width, p->height, !!p->flip, p->aspect, p->color_format);
  117. }
  118. else
  119. {
  120. winamp.OpenVideo(p->width, p->height, !!p->flip, p->aspect, p->color_format);
  121. }
  122. }
  123. void VideoThread::OpenVideo(bool drm, int width, int height, bool flip, double aspect, int fourcc)
  124. {
  125. VideoOpenParameters *p = new VideoOpenParameters;
  126. p->width = width;
  127. p->height = height;
  128. p->color_format = fourcc;
  129. p->aspect = aspect;
  130. p->flip = flip;
  131. p->drm = drm;
  132. this->drm = drm;
  133. QueueUserAPC(VideoThread_VideoOpenAPC, thread, reinterpret_cast<ULONG_PTR>(p));
  134. }
  135. VOID CALLBACK VideoThread::VideoThread_VideoCloseAPC(ULONG_PTR params)
  136. {
  137. if (params)
  138. winamp.CloseEncryptedVideo();
  139. else
  140. winamp.CloseVideo();
  141. }
  142. void VideoThread::CloseVideo(bool drm)
  143. {
  144. QueueUserAPC(VideoThread_VideoCloseAPC, thread, static_cast<ULONG_PTR>(drm));
  145. }