mf_sample_queue.cxx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 "mf_sample_queue.h"
  20. #include <assert.h>
  21. MFSampleQueue::MFSampleQueue()
  22. : m_nRefCount(1)
  23. , m_nCount(0)
  24. {
  25. InitializeCriticalSection(&m_critSec);
  26. m_anchor.next = &m_anchor;
  27. m_anchor.prev = &m_anchor;
  28. }
  29. MFSampleQueue::~MFSampleQueue()
  30. {
  31. assert(m_nRefCount == 0);
  32. Clear();
  33. DeleteCriticalSection(&m_critSec);
  34. }
  35. STDMETHODIMP MFSampleQueue::QueryInterface(REFIID iid, void** ppv)
  36. {
  37. return E_NOTIMPL;
  38. }
  39. STDMETHODIMP_(ULONG) MFSampleQueue::AddRef()
  40. {
  41. return InterlockedIncrement(&m_nRefCount);
  42. }
  43. STDMETHODIMP_(ULONG) MFSampleQueue::Release()
  44. {
  45. ULONG uCount = InterlockedDecrement(&m_nRefCount);
  46. if (uCount == 0) {
  47. delete this;
  48. }
  49. // For thread safety, return a temporary variable.
  50. return uCount;
  51. }
  52. HRESULT MFSampleQueue::Queue(IMFSample* item)
  53. {
  54. if (item == NULL) {
  55. return E_POINTER;
  56. }
  57. Node *pNode = new (std::nothrow) Node(item);
  58. if (pNode == NULL) {
  59. return E_OUTOFMEMORY;
  60. }
  61. item->AddRef();
  62. EnterCriticalSection(&m_critSec);
  63. Node *pBefore = m_anchor.prev;
  64. Node *pAfter = pBefore->next;
  65. pBefore->next = pNode;
  66. pAfter->prev = pNode;
  67. pNode->prev = pBefore;
  68. pNode->next = pAfter;
  69. m_nCount++;
  70. LeaveCriticalSection(&m_critSec);
  71. return S_OK;
  72. }
  73. HRESULT MFSampleQueue::Dequeue(IMFSample**ppItem)
  74. {
  75. if (ppItem == NULL) {
  76. return E_POINTER;
  77. }
  78. EnterCriticalSection(&m_critSec);
  79. if (IsEmpty()) {
  80. LeaveCriticalSection(&m_critSec);
  81. return E_FAIL;
  82. }
  83. Node *pNode = m_anchor.next;
  84. // The next node's previous is this node's previous.
  85. pNode->next->prev = m_anchor.next->prev;
  86. // The previous node's next is this node's next.
  87. pNode->prev->next = pNode->next;
  88. *ppItem = pNode->item;
  89. delete pNode;
  90. m_nCount--;
  91. LeaveCriticalSection(&m_critSec);
  92. return S_OK;
  93. }
  94. HRESULT MFSampleQueue::Clear()
  95. {
  96. EnterCriticalSection(&m_critSec);
  97. Node *n = m_anchor.next;
  98. // Delete the nodes
  99. while (n != &m_anchor) {
  100. if (n->item) {
  101. n->item->Release();
  102. }
  103. Node *tmp = n->next;
  104. delete n;
  105. n = tmp;
  106. }
  107. // Reset the anchor to point at itself
  108. m_anchor.next = &m_anchor;
  109. m_anchor.prev = &m_anchor;
  110. m_nCount = 0;
  111. LeaveCriticalSection(&m_critSec);
  112. return S_OK;
  113. }