decklink-device-instance.cpp 5.5 KB

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