decklink-device-instance.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. pixelFormat = bmdFormat8BitBGRA;
  17. } else {
  18. currentFrame.format = VIDEO_FORMAT_UYVY;
  19. pixelFormat = bmdFormat8BitYUV;
  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. const BMDDisplayMode displayMode = mode_->GetDisplayMode();
  75. const HRESULT videoResult = input->EnableVideoInput(displayMode,
  76. pixelFormat, bmdVideoInputFlagDefault);
  77. if (videoResult != S_OK) {
  78. LOG(LOG_ERROR, "Failed to enable video input");
  79. input->SetCallback(nullptr);
  80. return false;
  81. }
  82. const HRESULT audioResult = input->EnableAudioInput(
  83. bmdAudioSampleRate48kHz, bmdAudioSampleType16bitInteger,
  84. 2);
  85. if (audioResult != S_OK)
  86. LOG(LOG_WARNING, "Failed to enable audio input; continuing...");
  87. if (input->StartStreams() != S_OK) {
  88. LOG(LOG_ERROR, "Failed to start streams");
  89. input->SetCallback(nullptr);
  90. input->DisableVideoInput();
  91. input->DisableAudioInput();
  92. return false;
  93. }
  94. mode = mode_;
  95. return true;
  96. }
  97. bool DeckLinkDeviceInstance::StopCapture(void)
  98. {
  99. if (mode == nullptr || input == nullptr)
  100. return false;
  101. LOG(LOG_INFO, "Stopping capture of '%s'...",
  102. GetDevice()->GetDisplayName().c_str());
  103. input->StopStreams();
  104. input->SetCallback(nullptr);
  105. input->DisableVideoInput();
  106. input->DisableAudioInput();
  107. mode = nullptr;
  108. return true;
  109. }
  110. #define TIME_BASE 1000000000
  111. HRESULT STDMETHODCALLTYPE DeckLinkDeviceInstance::VideoInputFrameArrived(
  112. IDeckLinkVideoInputFrame *videoFrame,
  113. IDeckLinkAudioInputPacket *audioPacket)
  114. {
  115. BMDTimeValue videoTS = 0;
  116. BMDTimeValue videoDur = 0;
  117. BMDTimeValue audioTS = 0;
  118. if (videoFrame)
  119. videoFrame->GetStreamTime(&videoTS, &videoDur, TIME_BASE);
  120. if (audioPacket)
  121. audioPacket->GetPacketTime(&audioTS, TIME_BASE);
  122. if (videoFrame && videoTS >= 0)
  123. HandleVideoFrame(videoFrame, (uint64_t)videoTS);
  124. if (audioPacket && audioTS >= 0)
  125. HandleAudioPacket(audioPacket, (uint64_t)audioTS);
  126. return S_OK;
  127. }
  128. HRESULT STDMETHODCALLTYPE DeckLinkDeviceInstance::VideoInputFormatChanged(
  129. BMDVideoInputFormatChangedEvents events,
  130. IDeckLinkDisplayMode *newMode,
  131. BMDDetectedVideoInputFormatFlags detectedSignalFlags)
  132. {
  133. UNUSED_PARAMETER(events);
  134. UNUSED_PARAMETER(newMode);
  135. UNUSED_PARAMETER(detectedSignalFlags);
  136. // There is no implementation for automatic format detection, so this
  137. // method goes unused.
  138. return S_OK;
  139. }
  140. ULONG STDMETHODCALLTYPE DeckLinkDeviceInstance::AddRef(void)
  141. {
  142. return os_atomic_inc_long(&refCount);
  143. }
  144. HRESULT STDMETHODCALLTYPE DeckLinkDeviceInstance::QueryInterface(REFIID iid,
  145. LPVOID *ppv)
  146. {
  147. HRESULT result = E_NOINTERFACE;
  148. *ppv = nullptr;
  149. CFUUIDBytes unknown = CFUUIDGetUUIDBytes(IUnknownUUID);
  150. if (memcmp(&iid, &unknown, sizeof(REFIID)) == 0) {
  151. *ppv = this;
  152. AddRef();
  153. result = S_OK;
  154. } else if (memcmp(&iid, &IID_IDeckLinkNotificationCallback,
  155. sizeof(REFIID)) == 0) {
  156. *ppv = (IDeckLinkNotificationCallback *)this;
  157. AddRef();
  158. result = S_OK;
  159. }
  160. return result;
  161. }
  162. ULONG STDMETHODCALLTYPE DeckLinkDeviceInstance::Release(void)
  163. {
  164. const long newRefCount = os_atomic_dec_long(&refCount);
  165. if (newRefCount == 0) {
  166. delete this;
  167. return 0;
  168. }
  169. return newRefCount;
  170. }