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 | |
| 32 | #import "RTCVideoRenderer+internal.h" |
| 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 { |
| 52 | |
| 53 | public: |
| 54 | CallbackConverter(webrtc::VideoRenderCallback* callback, |
| 55 | const uint32_t streamId) |
| 56 | : callback_(callback), streamId_(streamId) {} |
| 57 | |
| 58 | virtual void SetSize(int width, int height) {}; |
| 59 | virtual void RenderFrame(const cricket::VideoFrame* frame) { |
| 60 | // Make this into an I420VideoFrame. |
| 61 | size_t width = frame->GetWidth(); |
| 62 | size_t height = frame->GetHeight(); |
| 63 | |
| 64 | size_t y_plane_size = width * height; |
| 65 | size_t uv_plane_size = frame->GetChromaSize(); |
| 66 | |
| 67 | webrtc::I420VideoFrame i420Frame; |
| 68 | i420Frame.CreateFrame(y_plane_size, |
| 69 | frame->GetYPlane(), |
| 70 | uv_plane_size, |
| 71 | frame->GetUPlane(), |
| 72 | uv_plane_size, |
| 73 | frame->GetVPlane(), |
| 74 | width, |
| 75 | height, |
| 76 | frame->GetYPitch(), |
| 77 | frame->GetUPitch(), |
| 78 | frame->GetVPitch()); |
| 79 | |
| 80 | i420Frame.set_render_time_ms(frame->GetTimeStamp() / 1000000); |
| 81 | |
| 82 | callback_->RenderFrame(streamId_, i420Frame); |
| 83 | } |
| 84 | |
| 85 | private: |
| 86 | webrtc::VideoRenderCallback* callback_; |
| 87 | const uint32_t streamId_; |
| 88 | }; |
| 89 | |
| 90 | @implementation RTCVideoRenderer { |
| 91 | CallbackConverter* _converter; |
| 92 | talk_base::scoped_ptr<webrtc::VideoRenderIosImpl> _iosRenderer; |
| 93 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 94 | |
fischman@webrtc.org | 9ca93a8 | 2013-10-29 00:14:15 +0000 | [diff] [blame] | 95 | @synthesize delegate = _delegate; |
| 96 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 97 | + (RTCVideoRenderer *)videoRenderGUIWithFrame:(CGRect)frame { |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame^] | 98 | return [[RTCVideoRenderer alloc] |
| 99 | initWithRenderView:[RTCVideoRenderer newRenderViewWithFrame:frame]]; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | - (id)initWithDelegate:(id<RTCVideoRendererDelegate>)delegate { |
| 103 | if ((self = [super init])) { |
| 104 | _delegate = delegate; |
| 105 | // TODO (hughv): Create video renderer. |
| 106 | } |
| 107 | return self; |
| 108 | } |
| 109 | |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame^] | 110 | + (UIView*)newRenderViewWithFrame:(CGRect)frame { |
| 111 | VideoRenderIosView* newView = |
| 112 | [[VideoRenderIosView alloc] initWithFrame:frame]; |
| 113 | return newView; |
| 114 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 115 | |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame^] | 116 | - (id)initWithRenderView:(UIView*)view { |
| 117 | NSAssert([view isKindOfClass:[VideoRenderIosView class]], |
| 118 | @"The view must be of kind 'VideoRenderIosView'"); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 119 | if ((self = [super init])) { |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame^] | 120 | VideoRenderIosView* renderView = (VideoRenderIosView*)view; |
| 121 | _iosRenderer.reset( |
| 122 | new webrtc::VideoRenderIosImpl(0, (__bridge void*)renderView, NO)); |
| 123 | if (_iosRenderer->Init() != -1) { |
| 124 | webrtc::VideoRenderCallback* callback = |
| 125 | _iosRenderer->AddIncomingRenderStream(0, 1, 0, 0, 1, 1); |
| 126 | _converter = new CallbackConverter(callback, 0); |
| 127 | _iosRenderer->StartRender(); |
| 128 | } else { |
| 129 | self = nil; |
| 130 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 131 | } |
| 132 | return self; |
| 133 | } |
| 134 | |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame^] | 135 | - (void)start { |
| 136 | _iosRenderer->StartRender(); |
| 137 | } |
| 138 | |
| 139 | - (void)stop { |
| 140 | _iosRenderer->StopRender(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @end |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame^] | 144 | |
| 145 | @implementation RTCVideoRenderer (Internal) |
| 146 | |
| 147 | - (webrtc::VideoRendererInterface*)videoRenderer { |
| 148 | return _converter; |
| 149 | } |
| 150 | |
| 151 | @end |
| 152 | |
| 153 | #else // TARGET_OS_IPHONE |
| 154 | |
| 155 | // TODO(fischman): implement an OS/X RTCVideoRenderer (and add to |
| 156 | // RTCPeerConnectionTest!). |
| 157 | |
| 158 | #import "RTCI420Frame.h" |
| 159 | #import "RTCVideoRendererDelegate.h" |
| 160 | @implementation RTCVideoRenderer |
| 161 | @synthesize delegate = _delegate; |
| 162 | + (RTCVideoRenderer*)videoRenderGUIWithFrame:(CGRect)frame { |
| 163 | // TODO(hughv): Implement. |
| 164 | return nil; |
| 165 | } |
| 166 | - (id)initWithDelegate:(id<RTCVideoRendererDelegate>)delegate { |
| 167 | if ((self = [super init])) { |
| 168 | _delegate = delegate; |
| 169 | // TODO(hughv): Create video renderer. |
| 170 | } |
| 171 | return self; |
| 172 | } |
| 173 | |
| 174 | + (UIView*)newRenderViewWithFrame:(CGRect)frame { |
| 175 | return nil; |
| 176 | } |
| 177 | - (id)initWithRenderView:(UIView*)renderView { |
| 178 | return nil; |
| 179 | } |
| 180 | - (void)start { |
| 181 | } |
| 182 | - (void)stop { |
| 183 | } |
| 184 | |
| 185 | @end |
| 186 | @implementation RTCVideoRenderer (Internal) |
| 187 | - (id)initWithVideoRenderer:(webrtc::VideoRendererInterface *)videoRenderer { |
| 188 | if ((self = [super init])) { |
| 189 | // TODO(hughv): Implement. |
| 190 | } |
| 191 | return self; |
| 192 | } |
| 193 | - (webrtc::VideoRendererInterface *)videoRenderer { |
| 194 | // TODO(hughv): Implement. |
| 195 | return NULL; |
| 196 | } |
| 197 | @end |
| 198 | |
| 199 | #endif // TARGET_OS_IPHONE |