henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 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.org | c693a2a | 2014-03-24 18:56:37 +0000 | [diff] [blame] | 32 | #import "RTCVideoRenderer+Internal.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 33 | |
| 34 | #if TARGET_OS_IPHONE |
| 35 | #import <UIKit/UIKit.h> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 36 | |
| 37 | #import "RTCI420Frame.h" |
| 38 | #import "RTCVideoRendererDelegate.h" |
| 39 | |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 40 | #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(). |
| 51 | class CallbackConverter : public webrtc::VideoRendererInterface { |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 52 | 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.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 90 | VideoRenderIosView* _renderView; |
| 91 | UIActivityIndicatorView* _activityIndicator; |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 92 | CallbackConverter* _converter; |
| 93 | talk_base::scoped_ptr<webrtc::VideoRenderIosImpl> _iosRenderer; |
| 94 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 95 | |
fischman@webrtc.org | 9ca93a8 | 2013-10-29 00:14:15 +0000 | [diff] [blame] | 96 | @synthesize delegate = _delegate; |
| 97 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 98 | - (id)initWithDelegate:(id<RTCVideoRendererDelegate>)delegate { |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 99 | // TODO(hughv): Create video renderer. |
| 100 | [self doesNotRecognizeSelector:_cmd]; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 101 | return self; |
| 102 | } |
| 103 | |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 104 | - (id)initWithView:(UIView*)view { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 105 | if ((self = [super init])) { |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 106 | CGRect frame = |
| 107 | CGRectMake(0, 0, view.bounds.size.width, view.bounds.size.height); |
| 108 | _renderView = [[VideoRenderIosView alloc] initWithFrame:frame]; |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 109 | _iosRenderer.reset( |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 110 | new webrtc::VideoRenderIosImpl(0, (__bridge void*)_renderView, NO)); |
| 111 | if (_iosRenderer->Init() == -1) { |
| 112 | self = nil; |
| 113 | } else { |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 114 | webrtc::VideoRenderCallback* callback = |
| 115 | _iosRenderer->AddIncomingRenderStream(0, 1, 0, 0, 1, 1); |
| 116 | _converter = new CallbackConverter(callback, 0); |
| 117 | _iosRenderer->StartRender(); |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 118 | [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.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 133 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 134 | } |
| 135 | return self; |
| 136 | } |
| 137 | |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 138 | - (void)start { |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 139 | [_activityIndicator stopAnimating]; |
| 140 | [_activityIndicator removeFromSuperview]; |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 141 | _iosRenderer->StartRender(); |
| 142 | } |
| 143 | |
| 144 | - (void)stop { |
fischman@webrtc.org | 49c5ba3 | 2014-03-31 20:22:19 +0000 | [diff] [blame] | 145 | [_activityIndicator stopAnimating]; |
| 146 | [_activityIndicator removeFromSuperview]; |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 147 | _iosRenderer->StopRender(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | @end |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 151 | |
| 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.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 169 | + (RTCVideoRenderer*)videoRendererWithFrame:(CGRect)frame { |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 170 | // 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.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 180 | - (id)initWithView:(UIView*)view { |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 181 | return nil; |
| 182 | } |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 183 | - (void)setTransform:(CGAffineTransform)transform { |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 184 | } |
| 185 | - (void)start { |
| 186 | } |
| 187 | - (void)stop { |
| 188 | } |
| 189 | |
| 190 | @end |
| 191 | @implementation RTCVideoRenderer (Internal) |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 192 | - (id)initWithVideoRenderer:(webrtc::VideoRendererInterface*)videoRenderer { |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 193 | if ((self = [super init])) { |
| 194 | // TODO(hughv): Implement. |
| 195 | } |
| 196 | return self; |
| 197 | } |
fischman@webrtc.org | 7fa1fcb | 2014-03-25 00:11:56 +0000 | [diff] [blame] | 198 | - (webrtc::VideoRendererInterface*)videoRenderer { |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 199 | // TODO(hughv): Implement. |
| 200 | return NULL; |
| 201 | } |
| 202 | @end |
| 203 | |
| 204 | #endif // TARGET_OS_IPHONE |