mf_sample_grabber.cxx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_grabber.h"
  20. #include "tinymedia/tmedia_producer.h"
  21. #include "tsk_memory.h"
  22. #include "tsk_debug.h"
  23. #include <assert.h>
  24. // Create a new instance of the object.
  25. HRESULT SampleGrabberCB::CreateInstance(const struct tmedia_producer_s* pcWrappedProducer, SampleGrabberCB **ppCB)
  26. {
  27. assert(pcWrappedProducer);
  28. *ppCB = new (std::nothrow) SampleGrabberCB(pcWrappedProducer);
  29. if (ppCB == NULL) {
  30. return E_OUTOFMEMORY;
  31. }
  32. return S_OK;
  33. }
  34. STDMETHODIMP SampleGrabberCB::QueryInterface(REFIID riid, void** ppv)
  35. {
  36. static const QITAB qit[] = {
  37. QITABENT(SampleGrabberCB, IMFSampleGrabberSinkCallback),
  38. QITABENT(SampleGrabberCB, IMFClockStateSink),
  39. { 0 }
  40. };
  41. return QISearch(this, qit, riid, ppv);
  42. }
  43. STDMETHODIMP_(ULONG) SampleGrabberCB::AddRef()
  44. {
  45. return InterlockedIncrement(&m_cRef);
  46. }
  47. STDMETHODIMP_(ULONG) SampleGrabberCB::Release()
  48. {
  49. ULONG cRef = InterlockedDecrement(&m_cRef);
  50. if (cRef == 0) {
  51. delete this;
  52. }
  53. return cRef;
  54. }
  55. // IMFClockStateSink methods.
  56. // In these example, the IMFClockStateSink methods do not perform any actions.
  57. // You can use these methods to track the state of the sample grabber sink.
  58. STDMETHODIMP SampleGrabberCB::OnClockStart(MFTIME hnsSystemTime, LONGLONG llClockStartOffset)
  59. {
  60. TSK_DEBUG_INFO("SampleGrabberCB::OnClockStart(%lld, %lld)", hnsSystemTime, llClockStartOffset);
  61. return S_OK;
  62. }
  63. STDMETHODIMP SampleGrabberCB::OnClockStop(MFTIME hnsSystemTime)
  64. {
  65. TSK_DEBUG_INFO("SampleGrabberCB::OnClockStop(%lld)", hnsSystemTime);
  66. return S_OK;
  67. }
  68. STDMETHODIMP SampleGrabberCB::OnClockPause(MFTIME hnsSystemTime)
  69. {
  70. TSK_DEBUG_INFO("SampleGrabberCB::OnClockPause(%lld)", hnsSystemTime);
  71. return S_OK;
  72. }
  73. STDMETHODIMP SampleGrabberCB::OnClockRestart(MFTIME hnsSystemTime)
  74. {
  75. TSK_DEBUG_INFO("SampleGrabberCB::OnClockRestart(%lld)", hnsSystemTime);
  76. return S_OK;
  77. }
  78. STDMETHODIMP SampleGrabberCB::OnClockSetRate(MFTIME hnsSystemTime, float flRate)
  79. {
  80. TSK_DEBUG_INFO("SampleGrabberCB::OnClockSetRate(%lld, %f)", hnsSystemTime, flRate);
  81. return S_OK;
  82. }
  83. // IMFSampleGrabberSink methods.
  84. STDMETHODIMP SampleGrabberCB::OnSetPresentationClock(IMFPresentationClock* pClock)
  85. {
  86. TSK_DEBUG_INFO("SampleGrabberCB::OnSetPresentationClock");
  87. return S_OK;
  88. }
  89. STDMETHODIMP SampleGrabberCB::OnProcessSample(
  90. REFGUID guidMajorMediaType, DWORD dwSampleFlags,
  91. LONGLONG llSampleTime, LONGLONG llSampleDuration, const BYTE * pSampleBuffer,
  92. DWORD dwSampleSize)
  93. {
  94. if (m_pWrappedProducer && TMEDIA_PRODUCER(m_pWrappedProducer)->enc_cb.callback) {
  95. #if 1
  96. if (m_bMuted) {
  97. // Send zeros. Do not skip sending data to avoid NAT issues and session deconnection.
  98. // Some TelePresence systems disconnect the session when the remote peer stops sending video data.
  99. memset((void*)pSampleBuffer, 0, dwSampleSize);
  100. }
  101. #endif
  102. TMEDIA_PRODUCER(m_pWrappedProducer)->enc_cb.callback(TMEDIA_PRODUCER(m_pWrappedProducer)->enc_cb.callback_data, pSampleBuffer, dwSampleSize);
  103. }
  104. return S_OK;
  105. }
  106. STDMETHODIMP SampleGrabberCB::OnShutdown()
  107. {
  108. TSK_DEBUG_INFO("SampleGrabberCB::OnShutdown");
  109. return S_OK;
  110. }