| 1 | // |
| 2 | // libtgvoip is free and unencumbered public domain software. |
| 3 | // For more information, see http://unlicense.org or the UNLICENSE file |
| 4 | // you should have received with this source code distribution. |
| 5 | // |
| 6 | |
| 7 | #ifndef LIBTGVOIP_VIDEOTOOLBOXENCODERSOURCE |
| 8 | #define LIBTGVOIP_VIDEOTOOLBOXENCODERSOURCE |
| 9 | |
| 10 | #include "../../video/VideoSource.h" |
| 11 | #include <CoreMedia/CoreMedia.h> |
| 12 | #include <VideoToolbox/VideoToolbox.h> |
| 13 | #include <vector> |
| 14 | |
| 15 | #ifdef __OBJC__ |
| 16 | @class TGVVideoSource; |
| 17 | #else |
| 18 | typedef struct objc_object TGVVideoSource; |
| 19 | #endif |
| 20 | |
| 21 | namespace tgvoip |
| 22 | { |
| 23 | namespace video |
| 24 | { |
| 25 | class VideoToolboxEncoderSource : public VideoSource |
| 26 | { |
| 27 | public: |
| 28 | VideoToolboxEncoderSource(TGVVideoSource* parent); |
| 29 | virtual ~VideoToolboxEncoderSource(); |
| 30 | virtual void Start() override; |
| 31 | virtual void Stop() override; |
| 32 | virtual void Reset(std::uint32_t codec, int maxResolution) override; |
| 33 | virtual void RequestKeyFrame() override; |
| 34 | virtual void SetBitrate(std::uint32_t bitrate) override; |
| 35 | void EncodeFrame(CMSampleBufferRef frame); |
| 36 | void SetStreamPaused(bool paused); |
| 37 | static std::vector<std::uint32_t> GetAvailableEncoders(); |
| 38 | static bool SupportsFullHD(); |
| 39 | |
| 40 | private: |
| 41 | void EncoderCallback(OSStatus status, CMSampleBufferRef buffer, VTEncodeInfoFlags flags); |
| 42 | void SetEncoderBitrateAndLimit(std::uint32_t bitrate); |
| 43 | bool needUpdateStreamParams = true; |
| 44 | std::uint32_t codec = 0; |
| 45 | VTCompressionSessionRef session = NULL; |
| 46 | bool keyframeRequested = false; |
| 47 | std::uint32_t bitrateChangeRequested = 0; |
| 48 | std::uint32_t lastBitrate = 512 * 1024; |
| 49 | unsigned int lastFrameRate = 0; |
| 50 | TGVVideoSource* objcObject; |
| 51 | }; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | #endif /* LIBTGVOIP_VIDEOTOOLBOXENCODERSOURCE */ |
| 56 | |