blob: be3d2051b938b03b54d5665f5c2b21e6ff1281a5 [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
35#import <UIKit/UIKit.h>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036
37#import "RTCI420Frame.h"
38#import "RTCVideoRendererDelegate.h"
39
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000040#import "webrtc/modules/video_render/ios/video_render_ios_impl.h"
41#import "webrtc/modules/video_render/ios/video_render_ios_view.h"
42
43#include "common_video/interface/i420_video_frame.h"
44#include "talk/app/webrtc/mediastreaminterface.h"
45#include "talk/media/base/videoframe.h"
46#include "webrtc/modules/video_render/include/video_render_defines.h"
47
48// An adapter presenting VideoRendererInterface's API and delegating to
49// a VideoRenderCallback. Suitable for feeding to
50// VideoTrackInterface::AddRenderer().
51class CallbackConverter : public webrtc::VideoRendererInterface {
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000052 public:
53 CallbackConverter(webrtc::VideoRenderCallback* callback,
54 const uint32_t streamId)
55 : callback_(callback), streamId_(streamId) {}
56
57 virtual void SetSize(int width, int height) {};
58 virtual void RenderFrame(const cricket::VideoFrame* frame) {
59 // Make this into an I420VideoFrame.
60 size_t width = frame->GetWidth();
61 size_t height = frame->GetHeight();
62
63 size_t y_plane_size = width * height;
64 size_t uv_plane_size = frame->GetChromaSize();
65
66 webrtc::I420VideoFrame i420Frame;
67 i420Frame.CreateFrame(y_plane_size,
68 frame->GetYPlane(),
69 uv_plane_size,
70 frame->GetUPlane(),
71 uv_plane_size,
72 frame->GetVPlane(),
73 width,
74 height,
75 frame->GetYPitch(),
76 frame->GetUPitch(),
77 frame->GetVPitch());
78
79 i420Frame.set_render_time_ms(frame->GetTimeStamp() / 1000000);
80
81 callback_->RenderFrame(streamId_, i420Frame);
82 }
83
84 private:
85 webrtc::VideoRenderCallback* callback_;
86 const uint32_t streamId_;
87};
88
89@implementation RTCVideoRenderer {
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000090 VideoRenderIosView* _renderView;
91 UIActivityIndicatorView* _activityIndicator;
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +000092 CallbackConverter* _converter;
93 talk_base::scoped_ptr<webrtc::VideoRenderIosImpl> _iosRenderer;
94}
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095
fischman@webrtc.org9ca93a82013-10-29 00:14:15 +000096@synthesize delegate = _delegate;
97
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098- (id)initWithDelegate:(id<RTCVideoRendererDelegate>)delegate {
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +000099 // TODO(hughv): Create video renderer.
100 [self doesNotRecognizeSelector:_cmd];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101 return self;
102}
103
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000104- (id)initWithView:(UIView*)view {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105 if ((self = [super init])) {
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000106 CGRect frame =
107 CGRectMake(0, 0, view.bounds.size.width, view.bounds.size.height);
108 _renderView = [[VideoRenderIosView alloc] initWithFrame:frame];
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000109 _iosRenderer.reset(
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000110 new webrtc::VideoRenderIosImpl(0, (__bridge void*)_renderView, NO));
111 if (_iosRenderer->Init() == -1) {
112 self = nil;
113 } else {
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000114 webrtc::VideoRenderCallback* callback =
115 _iosRenderer->AddIncomingRenderStream(0, 1, 0, 0, 1, 1);
116 _converter = new CallbackConverter(callback, 0);
117 _iosRenderer->StartRender();
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000118 [view addSubview:_renderView];
119 _renderView.autoresizingMask =
120 UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
121 _renderView.translatesAutoresizingMaskIntoConstraints = YES;
122
123 _activityIndicator = [[UIActivityIndicatorView alloc]
124 initWithActivityIndicatorStyle:
125 UIActivityIndicatorViewStyleWhiteLarge];
126 _activityIndicator.frame = view.bounds;
127 _activityIndicator.hidesWhenStopped = YES;
128 [view addSubview:_activityIndicator];
129 _activityIndicator.autoresizingMask =
130 UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
131 _activityIndicator.translatesAutoresizingMaskIntoConstraints = YES;
132 [_activityIndicator startAnimating];
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000133 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134 }
135 return self;
136}
137
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000138- (void)start {
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000139 [_activityIndicator stopAnimating];
140 [_activityIndicator removeFromSuperview];
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000141 _iosRenderer->StartRender();
142}
143
144- (void)stop {
fischman@webrtc.org49c5ba32014-03-31 20:22:19 +0000145 [_activityIndicator stopAnimating];
146 [_activityIndicator removeFromSuperview];
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000147 _iosRenderer->StopRender();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148}
149
150@end
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000151
152@implementation RTCVideoRenderer (Internal)
153
154- (webrtc::VideoRendererInterface*)videoRenderer {
155 return _converter;
156}
157
158@end
159
160#else // TARGET_OS_IPHONE
161
162// TODO(fischman): implement an OS/X RTCVideoRenderer (and add to
163// RTCPeerConnectionTest!).
164
165#import "RTCI420Frame.h"
166#import "RTCVideoRendererDelegate.h"
167@implementation RTCVideoRenderer
168@synthesize delegate = _delegate;
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000169+ (RTCVideoRenderer*)videoRendererWithFrame:(CGRect)frame {
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000170 // TODO(hughv): Implement.
171 return nil;
172}
173- (id)initWithDelegate:(id<RTCVideoRendererDelegate>)delegate {
174 if ((self = [super init])) {
175 _delegate = delegate;
176 // TODO(hughv): Create video renderer.
177 }
178 return self;
179}
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000180- (id)initWithView:(UIView*)view {
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000181 return nil;
182}
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000183- (void)setTransform:(CGAffineTransform)transform {
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000184}
185- (void)start {
186}
187- (void)stop {
188}
189
190@end
191@implementation RTCVideoRenderer (Internal)
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000192- (id)initWithVideoRenderer:(webrtc::VideoRendererInterface*)videoRenderer {
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000193 if ((self = [super init])) {
194 // TODO(hughv): Implement.
195 }
196 return self;
197}
fischman@webrtc.org7fa1fcb2014-03-25 00:11:56 +0000198- (webrtc::VideoRendererInterface*)videoRenderer {
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000199 // TODO(hughv): Implement.
200 return NULL;
201}
202@end
203
204#endif // TARGET_OS_IPHONE