OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found |
| 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 "webrtc/sdk/objc/Framework/Classes/objcvideotracksource.h" |
| 12 |
| 13 #import "RTCVideoFrame+Private.h" |
| 14 |
| 15 #include "webrtc/common_video/include/corevideo_frame_buffer.h" |
| 16 |
| 17 namespace webrtc { |
| 18 |
| 19 ObjcVideoTrackSource::ObjcVideoTrackSource() {} |
| 20 |
| 21 void ObjcVideoTrackSource::OnOutputFormatRequest(int width, |
| 22 int height, |
| 23 int fps) { |
| 24 cricket::VideoFormat format(width, height, |
| 25 cricket::VideoFormat::FpsToInterval(fps), 0); |
| 26 video_adapter()->OnOutputFormatRequest(format); |
| 27 } |
| 28 |
| 29 void ObjcVideoTrackSource::OnCapturedFrame(RTCVideoFrame* frame) { |
| 30 const int64_t timestamp_us = frame.timeStampNs / rtc::kNumNanosecsPerMicrosec; |
| 31 const int64_t translated_timestamp_us = |
| 32 timestamp_aligner_.TranslateTimestamp(timestamp_us, rtc::TimeMicros()); |
| 33 |
| 34 int adapted_width; |
| 35 int adapted_height; |
| 36 int crop_width; |
| 37 int crop_height; |
| 38 int crop_x; |
| 39 int crop_y; |
| 40 if (!AdaptFrame(frame.width, frame.height, |
| 41 timestamp_us, |
| 42 &adapted_width, &adapted_height, |
| 43 &crop_width, &crop_height, |
| 44 &crop_x, &crop_y)) { |
| 45 return; |
| 46 } |
| 47 |
| 48 rtc::scoped_refptr<VideoFrameBuffer> buffer; |
| 49 if (adapted_width == frame.width && adapted_height == frame.height) { |
| 50 // No adaption - optimized path. |
| 51 buffer = frame.videoBuffer; |
| 52 } else if (frame.nativeHandle) { |
| 53 // Adapted CVPixelBuffer frame. |
| 54 buffer = new rtc::RefCountedObject<CoreVideoFrameBuffer>( |
| 55 static_cast<CVPixelBufferRef>(frame.nativeHandle), |
| 56 adapted_width, adapted_height, |
| 57 crop_width, crop_height, |
| 58 crop_x, crop_y); |
| 59 } else { |
| 60 // Adapted I420 frame. |
| 61 // TODO(magjed): Optimize this I420 path. |
| 62 rtc::scoped_refptr<I420Buffer> i420_buffer = |
| 63 I420Buffer::Create(adapted_width, adapted_height); |
| 64 i420_buffer->CropAndScaleFrom(*frame.videoBuffer, |
| 65 crop_x, crop_y, |
| 66 crop_width, crop_height); |
| 67 buffer = i420_buffer; |
| 68 } |
| 69 |
| 70 // Applying rotation is only supported for legacy reasons and performance is |
| 71 // not critical here. |
| 72 webrtc::VideoRotation rotation = |
| 73 static_cast<webrtc::VideoRotation>(frame.rotation); |
| 74 if (apply_rotation() && rotation != kVideoRotation_0) { |
| 75 buffer = I420Buffer::Rotate(buffer->NativeToI420Buffer(), rotation); |
| 76 rotation = kVideoRotation_0; |
| 77 } |
| 78 |
| 79 OnFrame(webrtc::VideoFrame(buffer, rotation, translated_timestamp_us)); |
| 80 } |
| 81 |
| 82 } // namespace webrtc |
OLD | NEW |