decklink-device-instance.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #pragma once
  2. #define LOG(level, message, ...) \
  3. blog(level, "%s: " message, "decklink", ##__VA_ARGS__)
  4. #include <obs-module.h>
  5. #include <media-io/video-scaler.h>
  6. #include "decklink-device.hpp"
  7. #include "OBSVideoFrame.h"
  8. class AudioRepacker;
  9. class DecklinkBase;
  10. class DeckLinkDeviceInstance : public IDeckLinkInputCallback {
  11. protected:
  12. ComPtr<IDeckLinkConfiguration> deckLinkConfiguration;
  13. struct obs_source_frame2 currentFrame;
  14. struct obs_source_audio currentPacket;
  15. struct obs_source_cea_708 currentCaptions;
  16. DecklinkBase *decklink = nullptr;
  17. DeckLinkDevice *device = nullptr;
  18. DeckLinkDeviceMode *mode = nullptr;
  19. BMDVideoConnection videoConnection;
  20. BMDAudioConnection audioConnection;
  21. BMDDisplayMode displayMode = bmdModeNTSC;
  22. BMDPixelFormat pixelFormat = bmdFormat8BitYUV;
  23. video_colorspace colorSpace = VIDEO_CS_DEFAULT;
  24. video_colorspace activeColorSpace = VIDEO_CS_DEFAULT;
  25. video_range_type colorRange = VIDEO_RANGE_DEFAULT;
  26. ComPtr<IDeckLinkInput> input;
  27. ComPtr<IDeckLinkOutput> output;
  28. volatile long refCount = 1;
  29. int64_t audioOffset = 0;
  30. uint64_t nextAudioTS = 0;
  31. uint64_t lastVideoTS = 0;
  32. AudioRepacker *audioRepacker = nullptr;
  33. speaker_layout channelFormat = SPEAKERS_STEREO;
  34. bool swap;
  35. bool allow10Bit;
  36. OBSVideoFrame *convertFrame = nullptr;
  37. ComPtr<IDeckLinkMutableVideoFrame> decklinkOutputFrame;
  38. void FinalizeStream();
  39. void SetupVideoFormat(DeckLinkDeviceMode *mode_);
  40. void HandleAudioPacket(IDeckLinkAudioInputPacket *audioPacket,
  41. const uint64_t timestamp);
  42. void HandleVideoFrame(IDeckLinkVideoInputFrame *videoFrame,
  43. const uint64_t timestamp);
  44. public:
  45. DeckLinkDeviceInstance(DecklinkBase *decklink, DeckLinkDevice *device);
  46. virtual ~DeckLinkDeviceInstance();
  47. inline DeckLinkDevice *GetDevice() const { return device; }
  48. inline long long GetActiveModeId() const
  49. {
  50. return mode ? mode->GetId() : 0;
  51. }
  52. inline BMDPixelFormat GetActivePixelFormat() const
  53. {
  54. return pixelFormat;
  55. }
  56. inline video_colorspace GetActiveColorSpace() const
  57. {
  58. return colorSpace;
  59. }
  60. inline video_range_type GetActiveColorRange() const
  61. {
  62. return colorRange;
  63. }
  64. inline speaker_layout GetActiveChannelFormat() const
  65. {
  66. return channelFormat;
  67. }
  68. inline bool GetActiveSwapState() const { return swap; }
  69. inline BMDVideoConnection GetVideoConnection() const
  70. {
  71. return videoConnection;
  72. }
  73. inline BMDAudioConnection GetAudioConnection() const
  74. {
  75. return audioConnection;
  76. }
  77. inline DeckLinkDeviceMode *GetMode() const { return mode; }
  78. bool StartCapture(DeckLinkDeviceMode *mode, bool allow10Bit,
  79. BMDVideoConnection bmdVideoConnection,
  80. BMDAudioConnection bmdAudioConnection);
  81. bool StopCapture(void);
  82. bool StartOutput(DeckLinkDeviceMode *mode_);
  83. bool StopOutput(void);
  84. HRESULT STDMETHODCALLTYPE
  85. VideoInputFrameArrived(IDeckLinkVideoInputFrame *videoFrame,
  86. IDeckLinkAudioInputPacket *audioPacket);
  87. HRESULT STDMETHODCALLTYPE VideoInputFormatChanged(
  88. BMDVideoInputFormatChangedEvents events,
  89. IDeckLinkDisplayMode *newMode,
  90. BMDDetectedVideoInputFormatFlags detectedSignalFlags);
  91. ULONG STDMETHODCALLTYPE AddRef(void);
  92. HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv);
  93. ULONG STDMETHODCALLTYPE Release(void);
  94. void DisplayVideoFrame(video_data *frame);
  95. void WriteAudio(audio_data *frames);
  96. void HandleCaptionPacket(IDeckLinkAncillaryPacket *packet,
  97. const uint64_t timestamp);
  98. };