AllocLayer.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #ifndef NULLSOFT_ALLOCLAYERH
  2. #define NULLSOFT_ALLOCLAYERH
  3. #include "WMHandler.h"
  4. #include "BufferPool.h"
  5. #include <cassert>
  6. class AllocLayer : public WMHandler
  7. {
  8. public:
  9. AllocLayer(IWMReader *reader)
  10. : readerAdvanced(0),
  11. listenOutput( -1),
  12. maxSize(0)
  13. {
  14. reader->QueryInterface(&readerAdvanced);
  15. }
  16. ~AllocLayer()
  17. {
  18. if (readerAdvanced)
  19. {
  20. readerAdvanced->Release();
  21. readerAdvanced = 0;
  22. }
  23. }
  24. void Listen(long output)
  25. {
  26. listenOutput = output;
  27. if (output != -1)
  28. {
  29. readerAdvanced->SetAllocateForOutput(listenOutput, TRUE);
  30. readerAdvanced->GetMaxOutputSampleSize(listenOutput, &maxSize);
  31. assert(maxSize>0);
  32. pool.SetAllocSize(maxSize);
  33. }
  34. }
  35. void Listen(long output, long numBuffers)
  36. {
  37. listenOutput = output;
  38. if (output != -1)
  39. {
  40. readerAdvanced->SetAllocateForOutput(listenOutput, TRUE);
  41. readerAdvanced->GetMaxOutputSampleSize(listenOutput, &maxSize);
  42. assert(maxSize>0);
  43. pool.SetAllocSize(maxSize);
  44. pool.PreAllocate(numBuffers);
  45. pool.limit=numBuffers;
  46. }
  47. }
  48. void FreeBuffers()
  49. {
  50. pool.FreeBuffers();
  51. }
  52. BufferPool pool;
  53. private:
  54. void AllocateOutput(long outputNum, long bufferSize, INSSBuffer *&buffer)
  55. {
  56. if (outputNum == listenOutput)
  57. {
  58. assert(maxSize >= bufferSize);
  59. buffer = pool.GetBuffer(bufferSize);
  60. }
  61. else
  62. WMHandler::AllocateOutput(outputNum, bufferSize, buffer); // let other handlers have a shot at it first.
  63. }
  64. // WMHandler
  65. long listenOutput;
  66. DWORD maxSize;
  67. IWMReaderAdvanced *readerAdvanced;
  68. };
  69. #endif