| 7 |
| #import "base/RTCVideoFrameBuffer.h" |
| 8 | #import "TGRTCCVPixelBuffer.h" |
| 9 | #import "sdk/objc/native/src/objc_video_track_source.h" |
| 10 | #import "sdk/objc/native/src/objc_frame_buffer.h" |
| 11 | //#import "api/video_track_source_proxy.h" |
| 12 |
| 13 | #import "helpers/UIDevice+RTCDevice.h" |
| 14 |
| 15 | #import "helpers/AVCaptureSession+DevicePosition.h" |
| 16 | #import "helpers/RTCDispatcher+Private.h" |
| 17 | #import "base/RTCVideoFrame.h" |
| 18 |
| 19 | #include "common_video/libyuv/include/webrtc_libyuv.h" |
| 20 | #include "rtc_base/checks.h" |
| 21 | #include "rtc_base/logging.h" |
| 22 | #include "third_party/libyuv/include/libyuv.h" |
| 23 | #include "pc/video_track_source_proxy.h" |
| 24 |
| 25 | #include "DarwinVideoSource.h" |
| 26 |
| 27 | static const int64_t kNanosecondsPerSecond = 1000000000; |
| 28 |
| 29 | static tgcalls::DarwinVideoTrackSource *getObjCVideoSource(const webrtc::scoped_refptr<webrtc::VideoTrackSourceInterface> nativeSource) { |
| 30 | webrtc::VideoTrackSourceProxy *proxy_source = |
| 31 | static_cast<webrtc::VideoTrackSourceProxy *>(nativeSource.get()); |
| 32 | return static_cast<tgcalls::DarwinVideoTrackSource *>(proxy_source->internal()); |
| 33 | } |
| 34 |
| 35 | @interface CustomExternalCapturer () { |
| 36 | webrtc::scoped_refptr<webrtc::VideoTrackSourceInterface> _source; |
| 37 | } |
| 38 |
| 39 | @end |
| 40 |
| 41 | @implementation CustomExternalCapturer |
| 42 |
| 43 | - (instancetype)initWithSource:(webrtc::scoped_refptr<webrtc::VideoTrackSourceInterface>)source { |
| 44 | self = [super init]; |
| 45 | if (self != nil) { |
| 46 | _source = source; |
| 47 | } |
| 48 | return self; |
| 49 | } |
| 50 |
| 51 | - (void)dealloc { |
| 52 | } |
| 53 |
| 54 | + (void)passPixelBuffer:(CVPixelBufferRef)pixelBuffer sampleBufferReference:(CMSampleBufferRef)sampleBufferReference rotation:(RTCVideoRotation)rotation toSource:(webrtc::scoped_refptr<webrtc::VideoTrackSourceInterface>)source croppingBuffer:(std::vector<uint8_t> &)croppingBuffer { |
| 55 | TGRTCCVPixelBuffer *rtcPixelBuffer = [[TGRTCCVPixelBuffer alloc] initWithPixelBuffer:pixelBuffer]; |
| 56 | if (sampleBufferReference) { |
| 57 | [rtcPixelBuffer storeSampleBufferReference:sampleBufferReference]; |
| 58 | } |
| 59 | rtcPixelBuffer.deviceRelativeVideoRotation = -1; |
| 60 |
| 61 | int width = rtcPixelBuffer.width; |
| 62 | int height = rtcPixelBuffer.height; |
| 63 |
| 64 | width -= width % 4; |
| 65 | height -= height % 4; |
| 66 |
| 67 | if (width != rtcPixelBuffer.width || height != rtcPixelBuffer.height) { |
| 68 | CVPixelBufferRef outputPixelBufferRef = NULL; |
| 69 | OSType pixelFormat = CVPixelBufferGetPixelFormatType(rtcPixelBuffer.pixelBuffer); |
| 70 | CVPixelBufferCreate(NULL, width, height, pixelFormat, NULL, &outputPixelBufferRef); |
| 71 | if (outputPixelBufferRef) { |
| 72 | int bufferSize = [rtcPixelBuffer bufferSizeForCroppingAndScalingToWidth:width height:height]; |
| 73 | if (croppingBuffer.size() < bufferSize) { |
| 74 | croppingBuffer.resize(bufferSize); |
| 75 | } |
| 76 | if ([rtcPixelBuffer cropAndScaleTo:outputPixelBufferRef withTempBuffer:croppingBuffer.data()]) { |
| 77 | rtcPixelBuffer = [[TGRTCCVPixelBuffer alloc] initWithPixelBuffer:outputPixelBufferRef]; |
| 78 | rtcPixelBuffer.deviceRelativeVideoRotation = -1; |
| 79 | } |
| 80 | CVPixelBufferRelease(outputPixelBufferRef); |
| 81 | } |
| 82 | } |
| 83 |
| 84 | int64_t timeStampNs = CACurrentMediaTime() * kNanosecondsPerSecond; |
| 85 | RTCVideoFrame *videoFrame = [[RTCVideoFrame alloc] initWithBuffer:(id<RTCVideoFrameBuffer>)[rtcPixelBuffer toI420] rotation:rotation timeStampNs:timeStampNs]; |
| 86 |
| 87 | getObjCVideoSource(source)->OnCapturedFrame(videoFrame); |
| 88 | } |
| 89 |
| 90 | @end |
| 91 |