decklink-device-instance.hpp 3.0 KB

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