| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 |
| 11 | #include "DarwinVideoSource.h" |
| 12 |
| 13 | #import "base/RTCVideoFrame.h" |
| 14 | #import "base/RTCVideoFrameBuffer.h" |
| 15 | #import "components/video_frame_buffer/RTCCVPixelBuffer.h" |
| 16 |
| 17 | #include "api/video/i420_buffer.h" |
| 18 | #include "sdk/objc/native/src/objc_frame_buffer.h" |
| 19 |
| 20 | namespace tgcalls { |
| 21 |
| 22 | DarwinVideoTrackSource::DarwinVideoTrackSource() |
| 23 | : AdaptedVideoTrackSource(/* required resolution alignment */ 2) {} |
| 24 |
| 25 | bool DarwinVideoTrackSource::is_screencast() const { |
| 26 | return false; |
| 27 | } |
| 28 |
| 29 | absl::optional<bool> DarwinVideoTrackSource::needs_denoising() const { |
| 30 | return false; |
| 31 | } |
| 32 |
| 33 | webrtc::MediaSourceInterface::SourceState DarwinVideoTrackSource::state() const { |
| 34 | return SourceState::kLive; |
| 35 | } |
| 36 |
| 37 | bool DarwinVideoTrackSource::remote() const { |
| 38 | return false; |
| 39 | } |
| 40 |
| 41 | void DarwinVideoTrackSource::OnOutputFormatRequest(int width, int height, int fps) { |
| 42 | cricket::VideoFormat format(width, height, cricket::VideoFormat::FpsToInterval(fps), 0); |
| 43 | video_adapter()->OnOutputFormatRequest(format); |
| 44 | } |
| 45 |
| 46 | void DarwinVideoTrackSource::OnCapturedFrame(RTC_OBJC_TYPE(RTCVideoFrame) * frame) { |
| 47 | const int64_t timestamp_us = frame.timeStampNs / rtc::kNumNanosecsPerMicrosec; |
| 48 | const int64_t translated_timestamp_us = |
| 49 | timestamp_aligner_.TranslateTimestamp(timestamp_us, rtc::TimeMicros()); |
| 50 |
| 51 | int adapted_width; |
| 52 | int adapted_height; |
| 53 | int crop_width; |
| 54 | int crop_height; |
| 55 | int crop_x; |
| 56 | int crop_y; |
| 57 | if (!AdaptFrame(frame.width, |
| 58 | frame.height, |
| 59 | timestamp_us, |
| 60 | &adapted_width, |
| 61 | &adapted_height, |
| 62 | &crop_width, |
| 63 | &crop_height, |
| 64 | &crop_x, |
| 65 | &crop_y)) { |
| 66 | return; |
| 67 | } |
| 68 |
| 69 | webrtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer; |
| 70 | if (adapted_width == frame.width && adapted_height == frame.height) { |
| 71 | // No adaption - optimized path. |
| 72 | @autoreleasepool { |
| 73 | buffer = new rtc::RefCountedObject<webrtc::ObjCFrameBuffer>(frame.buffer); |
| 74 | } |
| 75 | } else if ([frame.buffer isKindOfClass:[RTC_OBJC_TYPE(RTCCVPixelBuffer) class]]) { |
| 76 | // Adapted CVPixelBuffer frame. |
| 77 | @autoreleasepool { |
| 78 | RTC_OBJC_TYPE(RTCCVPixelBuffer) *rtcPixelBuffer = |
| 79 | (RTC_OBJC_TYPE(RTCCVPixelBuffer) *)frame.buffer; |
| 80 | buffer = new rtc::RefCountedObject<webrtc::ObjCFrameBuffer>([[RTC_OBJC_TYPE(RTCCVPixelBuffer) alloc] |
| 81 | initWithPixelBuffer:rtcPixelBuffer.pixelBuffer |
| 82 | adaptedWidth:adapted_width |
| 83 | adaptedHeight:adapted_height |
| 84 | cropWidth:crop_width |
| 85 | cropHeight:crop_height |
| 86 | cropX:crop_x + rtcPixelBuffer.cropX |
| 87 | cropY:crop_y + rtcPixelBuffer.cropY]); |
| 88 | } |
| 89 | } else { |
| 90 | @autoreleasepool { |
| 91 | // Adapted I420 frame. |
| 92 | // TODO(magjed): Optimize this I420 path. |
| 93 | webrtc::scoped_refptr<webrtc::I420Buffer> i420_buffer = webrtc::I420Buffer::Create(adapted_width, adapted_height); |
| 94 | buffer = new rtc::RefCountedObject<webrtc::ObjCFrameBuffer>(frame.buffer); |
| 95 | i420_buffer->CropAndScaleFrom(*buffer->ToI420(), crop_x, crop_y, crop_width, crop_height); |
| 96 | buffer = i420_buffer; |
| 97 | } |
| 98 | } |
| 99 |
| 100 | // Applying rotation is only supported for legacy reasons and performance is |
| 101 | // not critical here. |
| 102 | webrtc::VideoRotation rotation = static_cast<webrtc::VideoRotation>(frame.rotation); |
| 103 | if (apply_rotation() && rotation != webrtc::kVideoRotation_0) { |
| 104 | buffer = webrtc::I420Buffer::Rotate(*buffer->ToI420(), rotation); |
| 105 | rotation = webrtc::kVideoRotation_0; |
| 106 | } |
| 107 |
| 108 | OnFrame(webrtc::VideoFrame::Builder() |
| 109 | .set_video_frame_buffer(buffer) |
| 110 | .set_rotation(rotation) |
| 111 | .set_timestamp_us(translated_timestamp_us) |
| 112 | .build()); |
| 113 | } |
| 114 |
| 115 | bool DarwinVideoTrackSource::OnCapturedFrame(const webrtc::VideoFrame& frame) { |
| 116 | const int64_t timestamp_us = frame.timestamp_us() / rtc::kNumNanosecsPerMicrosec; |
| 117 | const int64_t translated_timestamp_us = |
| 118 | timestamp_aligner_.TranslateTimestamp(timestamp_us, rtc::TimeMicros()); |
| 119 |
| 120 | int adapted_width; |
| 121 | int adapted_height; |
| 122 | int crop_width; |
| 123 | int crop_height; |
| 124 | int crop_x; |
| 125 | int crop_y; |
| 126 | if (!AdaptFrame(frame.width(), |
| 127 | frame.height(), |
| 128 | timestamp_us, |
| 129 | &adapted_width, |
| 130 | &adapted_height, |
| 131 | &crop_width, |
| 132 | &crop_height, |
| 133 | &crop_x, |
| 134 | &crop_y)) { |
| 135 | return false; |
| 136 | } |
| 137 |
| 138 | webrtc::scoped_refptr<webrtc::VideoFrameBuffer> buffer; |
| 139 | if (adapted_width == frame.width() && adapted_height == frame.height()) { |
| 140 | buffer = frame.video_frame_buffer(); |
| 141 | } else { |
| 142 | webrtc::scoped_refptr<webrtc::I420Buffer> i420_buffer = webrtc::I420Buffer::Create(adapted_width, adapted_height); |
| 143 | buffer = frame.video_frame_buffer(); |
| 144 | i420_buffer->CropAndScaleFrom(*buffer->ToI420(), crop_x, crop_y, crop_width, crop_height); |
| 145 | buffer = i420_buffer; |
| 146 | } |
| 147 |
| 148 | // Applying rotation is only supported for legacy reasons and performance is |
| 149 | // not critical here. |
| 150 | webrtc::VideoRotation rotation = frame.rotation(); |
| 151 | if (apply_rotation() && rotation != webrtc::kVideoRotation_0) { |
| 152 | buffer = webrtc::I420Buffer::Rotate(*buffer->ToI420(), rotation); |
| 153 | rotation = webrtc::kVideoRotation_0; |
| 154 | } |
| 155 |
| 156 | OnFrame(webrtc::VideoFrame::Builder() |
| 157 | .set_video_frame_buffer(buffer) |
| 158 | .set_rotation(rotation) |
| 159 | .set_timestamp_us(translated_timestamp_us) |
| 160 | .build()); |
| 161 |
| 162 | return true; |
| 163 | } |
| 164 |
| 165 | } // namespace webrtc |
| 166 |