plugin_audio_dsp_mediabuffer.cxx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* Copyright (C) 2013 Mamadou DIOP
  2. * Copyright (C) 2013 Doubango Telecom <http://www.doubango.org>
  3. *
  4. * This file is part of Open Source Doubango Framework.
  5. *
  6. * DOUBANGO is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * DOUBANGO is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with DOUBANGO.
  18. */
  19. #include "plugin_audio_dsp_mediabuffer.h"
  20. AudioDSPMediaBuffer::AudioDSPMediaBuffer(DWORD cbMaxLength, HRESULT& hr) :
  21. m_nRefCount(1),
  22. m_cbMaxLength(cbMaxLength),
  23. m_cbLength(0),
  24. m_pbData(NULL)
  25. {
  26. m_pbData = new BYTE[cbMaxLength];
  27. if (!m_pbData) {
  28. hr = E_OUTOFMEMORY;
  29. }
  30. }
  31. AudioDSPMediaBuffer::~AudioDSPMediaBuffer()
  32. {
  33. if (m_pbData) {
  34. delete [] m_pbData;
  35. }
  36. }
  37. // Function to create a new IMediaBuffer object and return
  38. // an AddRef'd interface pointer.
  39. HRESULT AudioDSPMediaBuffer::Create(long cbMaxLen, IMediaBuffer **ppBuffer)
  40. {
  41. HRESULT hr = S_OK;
  42. AudioDSPMediaBuffer *pBuffer = NULL;
  43. if (ppBuffer == NULL) {
  44. return E_POINTER;
  45. }
  46. pBuffer = new AudioDSPMediaBuffer(cbMaxLen, hr);
  47. if (pBuffer == NULL) {
  48. hr = E_OUTOFMEMORY;
  49. }
  50. if (SUCCEEDED(hr)) {
  51. *ppBuffer = pBuffer;
  52. (*ppBuffer)->AddRef();
  53. }
  54. if (pBuffer) {
  55. pBuffer->Release();
  56. }
  57. return hr;
  58. }
  59. // IUnknown methods.
  60. STDMETHODIMP AudioDSPMediaBuffer::QueryInterface(REFIID riid, void **ppv)
  61. {
  62. if (ppv == NULL) {
  63. return E_POINTER;
  64. }
  65. else if (riid == IID_IMediaBuffer || riid == IID_IUnknown) {
  66. *ppv = static_cast<IMediaBuffer *>(this);
  67. AddRef();
  68. return S_OK;
  69. }
  70. else {
  71. *ppv = NULL;
  72. return E_NOINTERFACE;
  73. }
  74. }
  75. STDMETHODIMP_(ULONG) AudioDSPMediaBuffer::AddRef()
  76. {
  77. return InterlockedIncrement(&m_nRefCount);
  78. }
  79. STDMETHODIMP_(ULONG) AudioDSPMediaBuffer::Release()
  80. {
  81. LONG lRef = InterlockedDecrement(&m_nRefCount);
  82. if (lRef == 0) {
  83. delete this;
  84. // m_cRef is no longer valid! Return lRef.
  85. }
  86. return lRef;
  87. }
  88. // IMediaBuffer methods.
  89. STDMETHODIMP AudioDSPMediaBuffer::SetLength(DWORD cbLength)
  90. {
  91. if (cbLength > m_cbMaxLength) {
  92. return E_INVALIDARG;
  93. }
  94. m_cbLength = cbLength;
  95. return S_OK;
  96. }
  97. STDMETHODIMP AudioDSPMediaBuffer::GetMaxLength(DWORD *pcbMaxLength)
  98. {
  99. if (pcbMaxLength == NULL) {
  100. return E_POINTER;
  101. }
  102. *pcbMaxLength = m_cbMaxLength;
  103. return S_OK;
  104. }
  105. STDMETHODIMP AudioDSPMediaBuffer::GetBufferAndLength(BYTE **ppbBuffer, DWORD *pcbLength)
  106. {
  107. // Either parameter can be NULL, but not both.
  108. if (ppbBuffer == NULL && pcbLength == NULL) {
  109. return E_POINTER;
  110. }
  111. if (ppbBuffer) {
  112. *ppbBuffer = m_pbData;
  113. }
  114. if (pcbLength) {
  115. *pcbLength = m_cbLength;
  116. }
  117. return S_OK;
  118. }