1/*
2 *  Copyright 2015 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#import "RTCVideoRendererAdapter+Private.h"
12#import "base/RTCVideoFrame.h"
13
14#include <memory>
15
16#include "sdk/objc/native/api/video_frame.h"
17
18namespace webrtc {
19
20class VideoRendererAdapter
21    : public rtc::VideoSinkInterface<webrtc::VideoFrame> {
22 public:
23  VideoRendererAdapter(RTCVideoRendererAdapter* adapter) {
24    adapter_ = adapter;
25    size_ = CGSizeZero;
26  }
27
28  void OnFrame(const webrtc::VideoFrame& nativeVideoFrame) override {
29    RTC_OBJC_TYPE(RTCVideoFrame)* videoFrame = NativeToObjCVideoFrame(nativeVideoFrame);
30
31    CGSize current_size = (videoFrame.rotation % 180 == 0)
32                              ? CGSizeMake(videoFrame.width, videoFrame.height)
33                              : CGSizeMake(videoFrame.height, videoFrame.width);
34
35    if (!CGSizeEqualToSize(size_, current_size)) {
36      size_ = current_size;
37      [adapter_.videoRenderer setSize:size_];
38    }
39    [adapter_.videoRenderer renderFrame:videoFrame];
40  }
41
42 private:
43  __weak RTCVideoRendererAdapter *adapter_;
44  CGSize size_;
45};
46}
47
48@implementation RTCVideoRendererAdapter {
49  std::unique_ptr<webrtc::VideoRendererAdapter> _adapter;
50}
51
52@synthesize videoRenderer = _videoRenderer;
53
54- (instancetype)initWithNativeRenderer:(id<RTC_OBJC_TYPE(RTCVideoRenderer)>)videoRenderer {
55  NSParameterAssert(videoRenderer);
56  if (self = [super init]) {
57    _videoRenderer = videoRenderer;
58    _adapter.reset(new webrtc::VideoRendererAdapter(self));
59  }
60  return self;
61}
62
63- (rtc::VideoSinkInterface<webrtc::VideoFrame> *)nativeVideoRenderer {
64  return _adapter.get();
65}
66
67@end
68