blob: 07041819f65b9068d5268aa92825ab4a5f116c4a [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2013, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#if !defined(__has_feature) || !__has_feature(objc_arc)
29#error "This file requires ARC support."
30#endif
31
fischman@webrtc.orgc693a2a2014-03-24 18:56:37 +000032#import "RTCVideoRenderer+Internal.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000033
34#if TARGET_OS_IPHONE
tkchin@webrtc.org1732a592014-05-19 23:26:01 +000035#import "RTCEAGLVideoView+Internal.h"
36#endif
37#import "RTCI420Frame+Internal.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000038
tkchin@webrtc.org1732a592014-05-19 23:26:01 +000039namespace webrtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040
tkchin@webrtc.org1732a592014-05-19 23:26:01 +000041class RTCVideoRendererAdapter : public VideoRendererInterface {
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000042 public:
tkchin@webrtc.org1732a592014-05-19 23:26:01 +000043 RTCVideoRendererAdapter(RTCVideoRenderer* renderer) { _renderer = renderer; }
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000044
tkchin@webrtc.org1732a592014-05-19 23:26:01 +000045 virtual void SetSize(int width, int height) OVERRIDE {
46 [_renderer.delegate renderer:_renderer
47 didSetSize:CGSizeMake(width, height)];
48 }
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000049
tkchin@webrtc.org1732a592014-05-19 23:26:01 +000050 virtual void RenderFrame(const cricket::VideoFrame* frame) OVERRIDE {
51 if (!_renderer.delegate) {
52 return;
53 }
54 RTCI420Frame* i420Frame = [[RTCI420Frame alloc] initWithVideoFrame:frame];
55 [_renderer.delegate renderer:_renderer didReceiveFrame:i420Frame];
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000056 }
57
58 private:
tkchin@webrtc.org1732a592014-05-19 23:26:01 +000059 __weak RTCVideoRenderer* _renderer;
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000060};
tkchin@webrtc.org1732a592014-05-19 23:26:01 +000061}
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000062
63@implementation RTCVideoRenderer {
tkchin@webrtc.org1732a592014-05-19 23:26:01 +000064 talk_base::scoped_ptr<webrtc::RTCVideoRendererAdapter> _adapter;
65#if TARGET_OS_IPHONE
66 RTCEAGLVideoView* _videoView;
67#endif
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000068}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069
tkchin@webrtc.org1732a592014-05-19 23:26:01 +000070- (instancetype)initWithDelegate:(id<RTCVideoRendererDelegate>)delegate {
71 if (self = [super init]) {
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000072 _delegate = delegate;
tkchin@webrtc.org1732a592014-05-19 23:26:01 +000073 _adapter.reset(new webrtc::RTCVideoRendererAdapter(self));
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000074 }
75 return self;
76}
tkchin@webrtc.org1732a592014-05-19 23:26:01 +000077
78#if TARGET_OS_IPHONE
79// TODO(tkchin): remove shim for deprecated method.
80- (instancetype)initWithView:(UIView*)view {
81 if (self = [super init]) {
82 _videoView = [[RTCEAGLVideoView alloc] initWithFrame:view.bounds];
83 _videoView.autoresizingMask =
84 UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
85 _videoView.translatesAutoresizingMaskIntoConstraints = YES;
86 [view addSubview:_videoView];
87 self.delegate = _videoView;
88 _adapter.reset(new webrtc::RTCVideoRendererAdapter(self));
89 }
90 return self;
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000091}
tkchin@webrtc.org1732a592014-05-19 23:26:01 +000092#endif
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000093
94@end
tkchin@webrtc.org1732a592014-05-19 23:26:01 +000095
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000096@implementation RTCVideoRenderer (Internal)
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000097
tkchin@webrtc.org1732a592014-05-19 23:26:01 +000098- (webrtc::VideoRendererInterface*)videoRenderer {
99 return _adapter.get();
100}
101
102@end