decklink-device-instance.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #include "decklink-device-instance.hpp"
  2. #include <util/platform.h>
  3. #include <util/threading.h>
  4. #include <sstream>
  5. #define LOG(level, message, ...) blog(level, "%s: " message, \
  6. obs_source_get_name(this->decklink->GetSource()), ##__VA_ARGS__)
  7. static inline enum video_format ConvertPixelFormat(BMDPixelFormat format)
  8. {
  9. switch (format) {
  10. case bmdFormat8BitBGRA: return VIDEO_FORMAT_BGRX;
  11. default:
  12. case bmdFormat8BitYUV:;
  13. }
  14. return VIDEO_FORMAT_UYVY;
  15. }
  16. DeckLinkDeviceInstance::DeckLinkDeviceInstance(DeckLink *decklink_,
  17. DeckLinkDevice *device_) :
  18. currentFrame(), currentPacket(), decklink(decklink_), device(device_)
  19. {
  20. currentPacket.samples_per_sec = 48000;
  21. currentPacket.speakers = SPEAKERS_STEREO;
  22. currentPacket.format = AUDIO_FORMAT_16BIT;
  23. }
  24. DeckLinkDeviceInstance::~DeckLinkDeviceInstance()
  25. {
  26. }
  27. void DeckLinkDeviceInstance::HandleAudioPacket(
  28. IDeckLinkAudioInputPacket *audioPacket,
  29. const uint64_t timestamp)
  30. {
  31. if (audioPacket == nullptr)
  32. return;
  33. void *bytes;
  34. if (audioPacket->GetBytes(&bytes) != S_OK) {
  35. LOG(LOG_WARNING, "Failed to get audio packet data");
  36. return;
  37. }
  38. currentPacket.data[0] = (uint8_t *)bytes;
  39. currentPacket.frames = (uint32_t)audioPacket->GetSampleFrameCount();
  40. currentPacket.timestamp = timestamp;
  41. obs_source_output_audio(decklink->GetSource(), &currentPacket);
  42. }
  43. void DeckLinkDeviceInstance::HandleVideoFrame(
  44. IDeckLinkVideoInputFrame *videoFrame, const uint64_t timestamp)
  45. {
  46. if (videoFrame == nullptr)
  47. return;
  48. void *bytes;
  49. if (videoFrame->GetBytes(&bytes) != S_OK) {
  50. LOG(LOG_WARNING, "Failed to get video frame data");
  51. return;
  52. }
  53. currentFrame.data[0] = (uint8_t *)bytes;
  54. currentFrame.linesize[0] = (uint32_t)videoFrame->GetRowBytes();
  55. currentFrame.width = (uint32_t)videoFrame->GetWidth();
  56. currentFrame.height = (uint32_t)videoFrame->GetHeight();
  57. currentFrame.timestamp = timestamp;
  58. video_format_get_parameters(VIDEO_CS_601, VIDEO_RANGE_PARTIAL,
  59. currentFrame.color_matrix, currentFrame.color_range_min,
  60. currentFrame.color_range_max);
  61. obs_source_output_video(decklink->GetSource(), &currentFrame);
  62. }
  63. bool DeckLinkDeviceInstance::StartCapture(DeckLinkDeviceMode *mode_)
  64. {
  65. if (mode != nullptr)
  66. return false;
  67. if (mode_ == nullptr)
  68. return false;
  69. LOG(LOG_INFO, "Starting capture...");
  70. if (!device->GetInput(&input))
  71. return false;
  72. pixelFormat = decklink->GetPixelFormat();
  73. currentFrame.format = ConvertPixelFormat(pixelFormat);
  74. input->SetCallback(this);
  75. const BMDDisplayMode displayMode = mode_->GetDisplayMode();
  76. const HRESULT videoResult = input->EnableVideoInput(displayMode,
  77. pixelFormat, bmdVideoInputFlagDefault);
  78. if (videoResult != S_OK) {
  79. LOG(LOG_ERROR, "Failed to enable video input");
  80. input->SetCallback(nullptr);
  81. return false;
  82. }
  83. const HRESULT audioResult = input->EnableAudioInput(
  84. bmdAudioSampleRate48kHz, bmdAudioSampleType16bitInteger,
  85. 2);
  86. if (audioResult != S_OK)
  87. LOG(LOG_WARNING, "Failed to enable audio input; continuing...");
  88. if (input->StartStreams() != S_OK) {
  89. LOG(LOG_ERROR, "Failed to start streams");
  90. input->SetCallback(nullptr);
  91. input->DisableVideoInput();
  92. input->DisableAudioInput();
  93. return false;
  94. }
  95. mode = mode_;
  96. return true;
  97. }
  98. bool DeckLinkDeviceInstance::StopCapture(void)
  99. {
  100. if (mode == nullptr || input == nullptr)
  101. return false;
  102. LOG(LOG_INFO, "Stopping capture of '%s'...",
  103. GetDevice()->GetDisplayName().c_str());
  104. input->StopStreams();
  105. input->SetCallback(nullptr);
  106. input->DisableVideoInput();
  107. input->DisableAudioInput();
  108. mode = nullptr;
  109. return true;
  110. }
  111. #define TIME_BASE 1000000000
  112. HRESULT STDMETHODCALLTYPE DeckLinkDeviceInstance::VideoInputFrameArrived(
  113. IDeckLinkVideoInputFrame *videoFrame,
  114. IDeckLinkAudioInputPacket *audioPacket)
  115. {
  116. BMDTimeValue videoTS = 0;
  117. BMDTimeValue videoDur = 0;
  118. BMDTimeValue audioTS = 0;
  119. if (videoFrame)
  120. videoFrame->GetStreamTime(&videoTS, &videoDur, TIME_BASE);
  121. if (audioPacket)
  122. audioPacket->GetPacketTime(&audioTS, TIME_BASE);
  123. if (videoFrame && videoTS >= 0)
  124. HandleVideoFrame(videoFrame, (uint64_t)videoTS);
  125. if (audioPacket && audioTS >= 0)
  126. HandleAudioPacket(audioPacket, (uint64_t)audioTS);
  127. return S_OK;
  128. }
  129. HRESULT STDMETHODCALLTYPE DeckLinkDeviceInstance::VideoInputFormatChanged(
  130. BMDVideoInputFormatChangedEvents events,
  131. IDeckLinkDisplayMode *newMode,
  132. BMDDetectedVideoInputFormatFlags detectedSignalFlags)
  133. {
  134. UNUSED_PARAMETER(events);
  135. UNUSED_PARAMETER(newMode);
  136. UNUSED_PARAMETER(detectedSignalFlags);
  137. // There is no implementation for automatic format detection, so this
  138. // method goes unused.
  139. return S_OK;
  140. }
  141. ULONG STDMETHODCALLTYPE DeckLinkDeviceInstance::AddRef(void)
  142. {
  143. return os_atomic_inc_long(&refCount);
  144. }
  145. HRESULT STDMETHODCALLTYPE DeckLinkDeviceInstance::QueryInterface(REFIID iid,
  146. LPVOID *ppv)
  147. {
  148. HRESULT result = E_NOINTERFACE;
  149. *ppv = nullptr;
  150. CFUUIDBytes unknown = CFUUIDGetUUIDBytes(IUnknownUUID);
  151. if (memcmp(&iid, &unknown, sizeof(REFIID)) == 0) {
  152. *ppv = this;
  153. AddRef();
  154. result = S_OK;
  155. } else if (memcmp(&iid, &IID_IDeckLinkNotificationCallback,
  156. sizeof(REFIID)) == 0) {
  157. *ppv = (IDeckLinkNotificationCallback *)this;
  158. AddRef();
  159. result = S_OK;
  160. }
  161. return result;
  162. }
  163. ULONG STDMETHODCALLTYPE DeckLinkDeviceInstance::Release(void)
  164. {
  165. const long newRefCount = os_atomic_dec_long(&refCount);
  166. if (newRefCount == 0) {
  167. delete this;
  168. return 0;
  169. }
  170. return newRefCount;
  171. }