decklink-device-instance.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. nextAudioTS = timestamp +
  78. ((uint64_t)frameCount * 1000000000ULL / 48000ULL) + 1;
  79. obs_source_output_audio(decklink->GetSource(), &currentPacket);
  80. }
  81. void DeckLinkDeviceInstance::HandleVideoFrame(
  82. IDeckLinkVideoInputFrame *videoFrame, const uint64_t timestamp)
  83. {
  84. if (videoFrame == nullptr)
  85. return;
  86. void *bytes;
  87. if (videoFrame->GetBytes(&bytes) != S_OK) {
  88. LOG(LOG_WARNING, "Failed to get video frame data");
  89. return;
  90. }
  91. currentFrame.data[0] = (uint8_t *)bytes;
  92. currentFrame.linesize[0] = (uint32_t)videoFrame->GetRowBytes();
  93. currentFrame.width = (uint32_t)videoFrame->GetWidth();
  94. currentFrame.height = (uint32_t)videoFrame->GetHeight();
  95. currentFrame.timestamp = timestamp;
  96. video_format_get_parameters(VIDEO_CS_601, VIDEO_RANGE_PARTIAL,
  97. currentFrame.color_matrix, currentFrame.color_range_min,
  98. currentFrame.color_range_max);
  99. obs_source_output_video(decklink->GetSource(), &currentFrame);
  100. }
  101. void DeckLinkDeviceInstance::FinalizeStream()
  102. {
  103. input->SetCallback(nullptr);
  104. if (audioRepacker != nullptr)
  105. {
  106. delete audioRepacker;
  107. audioRepacker = nullptr;
  108. }
  109. mode = nullptr;
  110. }
  111. bool DeckLinkDeviceInstance::StartCapture(DeckLinkDeviceMode *mode_)
  112. {
  113. if (mode != nullptr)
  114. return false;
  115. if (mode_ == nullptr)
  116. return false;
  117. LOG(LOG_INFO, "Starting capture...");
  118. if (!device->GetInput(&input))
  119. return false;
  120. pixelFormat = decklink->GetPixelFormat();
  121. currentFrame.format = ConvertPixelFormat(pixelFormat);
  122. const BMDDisplayMode displayMode = mode_->GetDisplayMode();
  123. const HRESULT videoResult = input->EnableVideoInput(displayMode,
  124. pixelFormat, bmdVideoInputFlagDefault);
  125. if (videoResult != S_OK) {
  126. LOG(LOG_ERROR, "Failed to enable video input");
  127. return false;
  128. }
  129. channelFormat = decklink->GetChannelFormat();
  130. currentPacket.speakers = channelFormat;
  131. if (channelFormat != SPEAKERS_UNKNOWN) {
  132. const int channel = ConvertChannelFormat(channelFormat);
  133. const HRESULT audioResult = input->EnableAudioInput(
  134. bmdAudioSampleRate48kHz, bmdAudioSampleType16bitInteger,
  135. channel);
  136. if (audioResult != S_OK)
  137. LOG(LOG_WARNING, "Failed to enable audio input; continuing...");
  138. if (!ISSTEREO(channelFormat)) {
  139. const audio_repack_mode_t repack_mode = ConvertRepackFormat(channelFormat);
  140. audioRepacker = new AudioRepacker(repack_mode);
  141. }
  142. }
  143. if (input->SetCallback(this) != S_OK) {
  144. LOG(LOG_ERROR, "Failed to set callback");
  145. FinalizeStream();
  146. return false;
  147. }
  148. if (input->StartStreams() != S_OK) {
  149. LOG(LOG_ERROR, "Failed to start streams");
  150. FinalizeStream();
  151. return false;
  152. }
  153. mode = mode_;
  154. return true;
  155. }
  156. bool DeckLinkDeviceInstance::StopCapture(void)
  157. {
  158. if (mode == nullptr || input == nullptr)
  159. return false;
  160. LOG(LOG_INFO, "Stopping capture of '%s'...",
  161. GetDevice()->GetDisplayName().c_str());
  162. input->StopStreams();
  163. FinalizeStream();
  164. return true;
  165. }
  166. #define TIME_BASE 1000000000
  167. HRESULT STDMETHODCALLTYPE DeckLinkDeviceInstance::VideoInputFrameArrived(
  168. IDeckLinkVideoInputFrame *videoFrame,
  169. IDeckLinkAudioInputPacket *audioPacket)
  170. {
  171. BMDTimeValue videoTS = 0;
  172. BMDTimeValue videoDur = 0;
  173. BMDTimeValue audioTS = 0;
  174. if (videoFrame) {
  175. videoFrame->GetStreamTime(&videoTS, &videoDur, TIME_BASE);
  176. lastVideoTS = (uint64_t)videoTS;
  177. }
  178. if (audioPacket) {
  179. BMDTimeValue newAudioTS = 0;
  180. int64_t diff;
  181. audioPacket->GetPacketTime(&newAudioTS, TIME_BASE);
  182. audioTS = newAudioTS + audioOffset;
  183. diff = (int64_t)audioTS - (int64_t)nextAudioTS;
  184. if (diff > 10000000LL) {
  185. audioOffset -= diff;
  186. audioTS = newAudioTS + audioOffset;
  187. } else if (diff < -1000000) {
  188. audioOffset = 0;
  189. audioTS = newAudioTS;
  190. }
  191. }
  192. if (videoFrame && videoTS >= 0)
  193. HandleVideoFrame(videoFrame, (uint64_t)videoTS);
  194. if (audioPacket && audioTS >= 0)
  195. HandleAudioPacket(audioPacket, (uint64_t)audioTS);
  196. return S_OK;
  197. }
  198. HRESULT STDMETHODCALLTYPE DeckLinkDeviceInstance::VideoInputFormatChanged(
  199. BMDVideoInputFormatChangedEvents events,
  200. IDeckLinkDisplayMode *newMode,
  201. BMDDetectedVideoInputFormatFlags detectedSignalFlags)
  202. {
  203. UNUSED_PARAMETER(events);
  204. UNUSED_PARAMETER(newMode);
  205. UNUSED_PARAMETER(detectedSignalFlags);
  206. // There is no implementation for automatic format detection, so this
  207. // method goes unused.
  208. return S_OK;
  209. }
  210. ULONG STDMETHODCALLTYPE DeckLinkDeviceInstance::AddRef(void)
  211. {
  212. return os_atomic_inc_long(&refCount);
  213. }
  214. HRESULT STDMETHODCALLTYPE DeckLinkDeviceInstance::QueryInterface(REFIID iid,
  215. LPVOID *ppv)
  216. {
  217. HRESULT result = E_NOINTERFACE;
  218. *ppv = nullptr;
  219. CFUUIDBytes unknown = CFUUIDGetUUIDBytes(IUnknownUUID);
  220. if (memcmp(&iid, &unknown, sizeof(REFIID)) == 0) {
  221. *ppv = this;
  222. AddRef();
  223. result = S_OK;
  224. } else if (memcmp(&iid, &IID_IDeckLinkNotificationCallback,
  225. sizeof(REFIID)) == 0) {
  226. *ppv = (IDeckLinkNotificationCallback *)this;
  227. AddRef();
  228. result = S_OK;
  229. }
  230. return result;
  231. }
  232. ULONG STDMETHODCALLTYPE DeckLinkDeviceInstance::Release(void)
  233. {
  234. const long newRefCount = os_atomic_dec_long(&refCount);
  235. if (newRefCount == 0) {
  236. delete this;
  237. return 0;
  238. }
  239. return newRefCount;
  240. }