decklink-device-instance.hpp 3.2 KB

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