decklink-device-instance.cpp 6.7 KB

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