magjed | abb84b8 | 2017-03-28 01:56:41 -0700 | [diff] [blame] | 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 | |
magjed | d41631a | 2017-05-03 07:10:10 -0700 | [diff] [blame] | 15 | #include "webrtc/sdk/objc/Framework/Classes/corevideo_frame_buffer.h" |
magjed | abb84b8 | 2017-03-28 01:56:41 -0700 | [diff] [blame] | 16 | |
| 17 | namespace webrtc { |
| 18 | |
| 19 | ObjcVideoTrackSource::ObjcVideoTrackSource() {} |
| 20 | |
| 21 | void ObjcVideoTrackSource::OnOutputFormatRequest(int width, int height, int fps) { |
| 22 | cricket::VideoFormat format(width, height, cricket::VideoFormat::FpsToInterval(fps), 0); |
| 23 | video_adapter()->OnOutputFormatRequest(format); |
| 24 | } |
| 25 | |
| 26 | void ObjcVideoTrackSource::OnCapturedFrame(RTCVideoFrame* frame) { |
| 27 | const int64_t timestamp_us = frame.timeStampNs / rtc::kNumNanosecsPerMicrosec; |
| 28 | const int64_t translated_timestamp_us = |
| 29 | timestamp_aligner_.TranslateTimestamp(timestamp_us, rtc::TimeMicros()); |
| 30 | |
| 31 | int adapted_width; |
| 32 | int adapted_height; |
| 33 | int crop_width; |
| 34 | int crop_height; |
| 35 | int crop_x; |
| 36 | int crop_y; |
| 37 | if (!AdaptFrame(frame.width, frame.height, timestamp_us, &adapted_width, &adapted_height, |
| 38 | &crop_width, &crop_height, &crop_x, &crop_y)) { |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | rtc::scoped_refptr<VideoFrameBuffer> buffer; |
| 43 | if (adapted_width == frame.width && adapted_height == frame.height) { |
| 44 | // No adaption - optimized path. |
| 45 | buffer = frame.videoBuffer; |
| 46 | } else if (frame.nativeHandle) { |
| 47 | // Adapted CVPixelBuffer frame. |
| 48 | buffer = new rtc::RefCountedObject<CoreVideoFrameBuffer>( |
| 49 | static_cast<CVPixelBufferRef>(frame.nativeHandle), adapted_width, adapted_height, |
| 50 | crop_width, crop_height, crop_x, crop_y); |
| 51 | } else { |
| 52 | // Adapted I420 frame. |
| 53 | // TODO(magjed): Optimize this I420 path. |
| 54 | rtc::scoped_refptr<I420Buffer> i420_buffer = I420Buffer::Create(adapted_width, adapted_height); |
| 55 | i420_buffer->CropAndScaleFrom(*frame.videoBuffer, crop_x, crop_y, crop_width, crop_height); |
| 56 | buffer = i420_buffer; |
| 57 | } |
| 58 | |
| 59 | // Applying rotation is only supported for legacy reasons and performance is |
| 60 | // not critical here. |
| 61 | webrtc::VideoRotation rotation = static_cast<webrtc::VideoRotation>(frame.rotation); |
| 62 | if (apply_rotation() && rotation != kVideoRotation_0) { |
nisse | 6e6a485 | 2017-05-02 06:56:07 -0700 | [diff] [blame] | 63 | buffer = I420Buffer::Rotate(buffer->NativeToI420Buffer(), rotation); |
magjed | abb84b8 | 2017-03-28 01:56:41 -0700 | [diff] [blame] | 64 | rotation = kVideoRotation_0; |
| 65 | } |
| 66 | |
| 67 | OnFrame(webrtc::VideoFrame(buffer, rotation, translated_timestamp_us)); |
| 68 | } |
| 69 | |
| 70 | } // namespace webrtc |