decklink-device-instance.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. currentFrame.format = VIDEO_FORMAT_UYVY;
  12. currentPacket.samples_per_sec = 48000;
  13. currentPacket.speakers = SPEAKERS_STEREO;
  14. currentPacket.format = AUDIO_FORMAT_16BIT;
  15. }
  16. void DeckLinkDeviceInstance::HandleAudioPacket(
  17. IDeckLinkAudioInputPacket *audioPacket,
  18. const uint64_t timestamp)
  19. {
  20. if (audioPacket == nullptr)
  21. return;
  22. void *bytes;
  23. if (audioPacket->GetBytes(&bytes) != S_OK) {
  24. LOG(LOG_WARNING, "Failed to get audio packet data");
  25. return;
  26. }
  27. currentPacket.data[0] = (uint8_t *)bytes;
  28. currentPacket.frames = (uint32_t)audioPacket->GetSampleFrameCount();
  29. currentPacket.timestamp = timestamp;
  30. obs_source_output_audio(decklink->GetSource(), &currentPacket);
  31. }
  32. void DeckLinkDeviceInstance::HandleVideoFrame(
  33. IDeckLinkVideoInputFrame *videoFrame, const uint64_t timestamp)
  34. {
  35. if (videoFrame == nullptr)
  36. return;
  37. void *bytes;
  38. if (videoFrame->GetBytes(&bytes) != S_OK) {
  39. LOG(LOG_WARNING, "Failed to get video frame data");
  40. return;
  41. }
  42. currentFrame.data[0] = (uint8_t *)bytes;
  43. currentFrame.linesize[0] = (uint32_t)videoFrame->GetRowBytes();
  44. currentFrame.width = (uint32_t)videoFrame->GetWidth();
  45. currentFrame.height = (uint32_t)videoFrame->GetHeight();
  46. currentFrame.timestamp = timestamp;
  47. video_format_get_parameters(VIDEO_CS_601, VIDEO_RANGE_PARTIAL,
  48. currentFrame.color_matrix, currentFrame.color_range_min,
  49. currentFrame.color_range_max);
  50. obs_source_output_video(decklink->GetSource(), &currentFrame);
  51. }
  52. bool DeckLinkDeviceInstance::StartCapture(DeckLinkDeviceMode *mode_)
  53. {
  54. if (mode != nullptr)
  55. return false;
  56. if (mode_ == nullptr)
  57. return false;
  58. LOG(LOG_INFO, "Starting capture...");
  59. if (!device->GetInput(&input))
  60. return false;
  61. input->SetCallback(this);
  62. const BMDDisplayMode displayMode = mode_->GetDisplayMode();
  63. const HRESULT videoResult = input->EnableVideoInput(displayMode,
  64. bmdFormat8BitYUV, bmdVideoInputFlagDefault);
  65. if (videoResult != S_OK) {
  66. LOG(LOG_ERROR, "Failed to enable video input");
  67. input->SetCallback(nullptr);
  68. return false;
  69. }
  70. const HRESULT audioResult = input->EnableAudioInput(
  71. bmdAudioSampleRate48kHz, bmdAudioSampleType16bitInteger,
  72. 2);
  73. if (audioResult != S_OK)
  74. LOG(LOG_WARNING, "Failed to enable audio input; continuing...");
  75. if (input->StartStreams() != S_OK) {
  76. LOG(LOG_ERROR, "Failed to start streams");
  77. input->SetCallback(nullptr);
  78. input->DisableVideoInput();
  79. input->DisableAudioInput();
  80. return false;
  81. }
  82. mode = mode_;
  83. return true;
  84. }
  85. bool DeckLinkDeviceInstance::StopCapture(void)
  86. {
  87. if (mode == nullptr || input == nullptr)
  88. return false;
  89. LOG(LOG_INFO, "Stopping capture of '%s'...",
  90. GetDevice()->GetDisplayName().c_str());
  91. input->StopStreams();
  92. input->SetCallback(nullptr);
  93. input->DisableVideoInput();
  94. input->DisableAudioInput();
  95. mode = nullptr;
  96. return true;
  97. }
  98. HRESULT STDMETHODCALLTYPE DeckLinkDeviceInstance::VideoInputFrameArrived(
  99. IDeckLinkVideoInputFrame *videoFrame,
  100. IDeckLinkAudioInputPacket *audioPacket)
  101. {
  102. const uint64_t timestamp = os_gettime_ns();
  103. HandleVideoFrame(videoFrame, timestamp);
  104. HandleAudioPacket(audioPacket, timestamp);
  105. return S_OK;
  106. }
  107. HRESULT STDMETHODCALLTYPE DeckLinkDeviceInstance::VideoInputFormatChanged(
  108. BMDVideoInputFormatChangedEvents events,
  109. IDeckLinkDisplayMode *newMode,
  110. BMDDetectedVideoInputFormatFlags detectedSignalFlags)
  111. {
  112. UNUSED_PARAMETER(events);
  113. UNUSED_PARAMETER(newMode);
  114. UNUSED_PARAMETER(detectedSignalFlags);
  115. // There is no implementation for automatic format detection, so this
  116. // method goes unused.
  117. return S_OK;
  118. }
  119. ULONG STDMETHODCALLTYPE DeckLinkDeviceInstance::AddRef(void)
  120. {
  121. return os_atomic_inc_long(&refCount);
  122. }
  123. HRESULT STDMETHODCALLTYPE DeckLinkDeviceInstance::QueryInterface(REFIID iid,
  124. LPVOID *ppv)
  125. {
  126. HRESULT result = E_NOINTERFACE;
  127. *ppv = nullptr;
  128. CFUUIDBytes unknown = CFUUIDGetUUIDBytes(IUnknownUUID);
  129. if (memcmp(&iid, &unknown, sizeof(REFIID)) == 0) {
  130. *ppv = this;
  131. AddRef();
  132. result = S_OK;
  133. } else if (memcmp(&iid, &IID_IDeckLinkNotificationCallback,
  134. sizeof(REFIID)) == 0) {
  135. *ppv = (IDeckLinkNotificationCallback *)this;
  136. AddRef();
  137. result = S_OK;
  138. }
  139. return result;
  140. }
  141. ULONG STDMETHODCALLTYPE DeckLinkDeviceInstance::Release(void)
  142. {
  143. const long newRefCount = os_atomic_dec_long(&refCount);
  144. if (newRefCount == 0) {
  145. delete this;
  146. return 0;
  147. }
  148. return newRefCount;
  149. }