| 7 | #include "media/base/media_constants.h" |
| 8 | #include "TGRTCDefaultVideoEncoderFactory.h" |
| 9 | #include "TGRTCDefaultVideoDecoderFactory.h" |
| 10 | #include "sdk/objc/native/api/video_encoder_factory.h" |
| 11 | #include "sdk/objc/native/api/video_decoder_factory.h" |
| 12 | #include "pc/video_track_source_proxy.h" |
| 13 | #import "base/RTCLogging.h" |
| 14 | #include "AudioDeviceModuleIOS.h" |
| 15 | #include "AudioDeviceModuleMacos.h" |
| 16 | #include "DarwinVideoSource.h" |
| 17 | #include "objc_video_encoder_factory.h" |
| 18 | #include "objc_video_decoder_factory.h" |
| 19 | #include "sdk/objc/native/src/objc_frame_buffer.h" |
| 20 | #import "sdk/objc/components/video_frame_buffer/RTCCVPixelBuffer.h" |
| 21 |
| 22 | #import "DarwinFFMpeg.h" |
| 23 |
| 24 | #ifdef WEBRTC_IOS |
| 25 | #include "platform/darwin/iOS/RTCAudioSession.h" |
| 26 | #include "platform/darwin/iOS/RTCAudioSessionConfiguration.h" |
| 27 | #import <UIKit/UIKit.h> |
| 28 | #endif // WEBRTC_IOS |
| 29 |
| 30 | #import <AVFoundation/AVFoundation.h> |
| 31 |
| 32 | #include <sys/sysctl.h> |
| 33 |
| 34 | namespace tgcalls { |
| 35 |
| 36 | std::unique_ptr<webrtc::VideoDecoderFactory> CustomObjCToNativeVideoDecoderFactory( |
| 37 | id<RTC_OBJC_TYPE(RTCVideoDecoderFactory)> objc_video_decoder_factory) { |
| 38 | return std::make_unique<webrtc::CustomObjCVideoDecoderFactory>(objc_video_decoder_factory); |
| 39 | } |
| 40 |
| 41 | static DarwinVideoTrackSource *getObjCVideoSource(const webrtc::scoped_refptr<webrtc::VideoTrackSourceInterface> nativeSource) { |
| 42 | webrtc::VideoTrackSourceProxy *proxy_source = |
| 43 | static_cast<webrtc::VideoTrackSourceProxy *>(nativeSource.get()); |
| 44 | return static_cast<DarwinVideoTrackSource *>(proxy_source->internal()); |
| 45 | } |
| 46 |
| 47 | [[maybe_unused]] static NSString *getPlatformInfo() { |
| 48 | const char *typeSpecifier = "hw.machine"; |
| 49 |
| 50 | size_t size; |
| 51 | sysctlbyname(typeSpecifier, NULL, &size, NULL, 0); |
| 52 |
| 53 | char *answer = (char *)malloc(size); |
| 54 | sysctlbyname(typeSpecifier, answer, &size, NULL, 0); |
| 55 |
| 56 | NSString *results = [NSString stringWithCString:answer encoding:NSUTF8StringEncoding]; |
| 57 |
| 58 | free(answer); |
| 59 | return results; |
| 60 | } |
| 61 |
| 62 | std::unique_ptr<rtc::NetworkMonitorFactory> DarwinInterface::createNetworkMonitorFactory() { |
| 63 | return webrtc::CreateNetworkMonitorFactory(); |
| 64 | } |
| 65 |
| 66 | void DarwinInterface::configurePlatformAudio(int numChanels) { |
| 67 | } |
| 68 |
| 69 | std::unique_ptr<webrtc::VideoEncoderFactory> DarwinInterface::makeVideoEncoderFactory(bool preferHardwareEncoding, bool isScreencast) { |
| 70 | auto nativeFactory = std::make_unique<webrtc::CustomObjCVideoEncoderFactory>([[TGRTCDefaultVideoEncoderFactory alloc] initWithPreferHardwareH264:preferHardwareEncoding preferX264:false]); |
| 71 | if (!preferHardwareEncoding) { |
| 72 | auto nativeHardwareFactory = std::make_unique<webrtc::CustomObjCVideoEncoderFactory>([[TGRTCDefaultVideoEncoderFactory alloc] initWithPreferHardwareH264:true preferX264:false]); |
| 73 | return std::make_unique<webrtc::SimulcastVideoEncoderFactory>(std::move(nativeFactory), std::move(nativeHardwareFactory)); |
| 74 | } |
| 75 | return nativeFactory; |
| 76 | } |
| 77 |
| 78 | std::unique_ptr<webrtc::VideoDecoderFactory> DarwinInterface::makeVideoDecoderFactory() { |
| 79 | return CustomObjCToNativeVideoDecoderFactory([[TGRTCDefaultVideoDecoderFactory alloc] init]); |
| 80 | } |
| 81 |
| 82 | bool DarwinInterface::supportsEncoding(const std::string &codecName) { |
| 83 | if (false) { |
| 84 | } |
| 85 | #ifndef WEBRTC_DISABLE_H265 |
| 86 | else if (codecName == cricket::kH265CodecName) { |
| 87 | #ifdef WEBRTC_IOS |
| 88 | if (@available(iOS 11.0, *)) { |
| 89 | return [[AVAssetExportSession allExportPresets] containsObject:AVAssetExportPresetHEVCHighestQuality]; |
| 90 | } |
| 91 | #elif defined WEBRTC_MAC // WEBRTC_IOS |
| 92 |
| 93 | #ifdef __x86_64__ |
| 94 | return NO; |
| 95 | #else |
| 96 | return YES; |
| 97 | #endif |
| 98 | #endif // WEBRTC_IOS || WEBRTC_MAC |
| 99 | } |
| 100 | #endif |
| 101 | else if (codecName == cricket::kH264CodecName) { |
| 102 | #ifdef __x86_64__ |
| 103 | return YES; |
| 104 | #else |
| 105 | return NO; |
| 106 | #endif |
| 107 | } else if (codecName == cricket::kVp8CodecName) { |
| 108 | return true; |
| 109 | } else if (codecName == cricket::kVp9CodecName) { |
| 110 | return true; |
| 111 | } |
| 112 | return false; |
| 113 | } |
| 114 |
| 115 | webrtc::scoped_refptr<webrtc::VideoTrackSourceInterface> DarwinInterface::makeVideoSource(rtc::Thread *signalingThread, rtc::Thread *workerThread) { |
| 116 | webrtc::scoped_refptr<tgcalls::DarwinVideoTrackSource> objCVideoTrackSource(new rtc::RefCountedObject<tgcalls::DarwinVideoTrackSource>()); |
| 117 | return webrtc::VideoTrackSourceProxy::Create(signalingThread, workerThread, objCVideoTrackSource); |
| 118 | } |
| 119 |
| 120 | void DarwinInterface::adaptVideoSource(webrtc::scoped_refptr<webrtc::VideoTrackSourceInterface> videoSource, int width, int height, int fps) { |
| 121 | getObjCVideoSource(videoSource)->OnOutputFormatRequest(width, height, fps); |
| 122 | } |
| 123 |
| 124 | std::unique_ptr<VideoCapturerInterface> DarwinInterface::makeVideoCapturer(webrtc::scoped_refptr<webrtc::VideoTrackSourceInterface> source, std::string deviceId, std::function<void(VideoState)> stateUpdated, std::function<void(PlatformCaptureInfo)> captureInfoUpdated, std::shared_ptr<PlatformContext> platformContext, std::pair<int, int> &outResolution) { |
| 125 | return std::make_unique<VideoCapturerInterfaceImpl>(source, deviceId, stateUpdated, captureInfoUpdated, outResolution); |
| 126 | } |
| 127 |
| 128 | webrtc::scoped_refptr<WrappedAudioDeviceModule> DarwinInterface::wrapAudioDeviceModule(webrtc::scoped_refptr<webrtc::AudioDeviceModule> module) { |
| 129 | #ifdef WEBRTC_IOS |
| 130 | return rtc::make_ref_counted<AudioDeviceModuleIOS>(module); |
| 131 | #else |
| 132 | return rtc::make_ref_counted<AudioDeviceModuleMacos>(module); |
| 133 | #endif |
| 134 | } |
| 135 |
| 136 | void DarwinInterface::setupVideoDecoding(AVCodecContext *codecContext) { |
| 137 | return setupDarwinVideoDecoding(codecContext); |
| 138 | } |
| 139 |
| 140 | webrtc::scoped_refptr<webrtc::VideoFrameBuffer> DarwinInterface::createPlatformFrameFromData(AVFrame const *frame) { |
| 141 | return createDarwinPlatformFrameFromData(frame); |
| 142 | } |
| 143 |
| 144 | std::unique_ptr<PlatformInterface> CreatePlatformInterface() { |
| 145 | return std::make_unique<DarwinInterface>(); |
| 146 | } |
| 147 |
| 148 | DarwinVideoFrame::DarwinVideoFrame(CVPixelBufferRef pixelBuffer) { |
| 149 | _pixelBuffer = CVPixelBufferRetain(pixelBuffer); |
| 150 | } |
| 151 |
| 152 | DarwinVideoFrame::~DarwinVideoFrame() { |
| 153 | if (_pixelBuffer) { |
| 154 | CVPixelBufferRelease(_pixelBuffer); |
| 155 | } |
| 156 | } |
| 157 |
| 158 | } // namespace tgcalls |
| 159 |