henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +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 | /* |
| 29 | * This VideoView must be initialzed and added to a View to get |
| 30 | * either the local or remote video stream rendered. |
| 31 | * It is a view itself and it encapsulates |
| 32 | * an object of VideoRenderIosView and UIActivityIndicatorView. |
| 33 | * Both of the views will get resized as per the frame of their parent. |
| 34 | */ |
| 35 | |
| 36 | #import "VideoView.h" |
| 37 | |
| 38 | #import "RTCVideoRenderer.h" |
| 39 | #import "RTCVideoTrack.h" |
| 40 | |
| 41 | @interface VideoView () { |
| 42 | RTCVideoTrack *_track; |
| 43 | RTCVideoRenderer *_renderer; |
| 44 | } |
| 45 | |
| 46 | @property (nonatomic, weak) UIView *renderView; |
| 47 | @property (nonatomic, weak) UIActivityIndicatorView *activityView; |
| 48 | |
| 49 | @end |
| 50 | |
| 51 | @implementation VideoView |
| 52 | |
| 53 | @synthesize videoOrientation = _videoOrientation; |
| 54 | @synthesize isRemote = _isRemote; |
| 55 | @synthesize renderView = _renderView; |
| 56 | @synthesize activityView = _activityView; |
| 57 | |
| 58 | static void init(VideoView *self) { |
| 59 | UIView *renderView = [RTCVideoRenderer newRenderViewWithFrame: |
| 60 | CGRectMake(0, |
| 61 | 0, |
| 62 | self.bounds.size.width, |
| 63 | self.bounds.size.height)]; |
| 64 | [self addSubview:renderView]; |
| 65 | renderView.autoresizingMask = UIViewAutoresizingFlexibleHeight | |
| 66 | UIViewAutoresizingFlexibleWidth; |
| 67 | renderView.translatesAutoresizingMaskIntoConstraints = YES; |
| 68 | self.renderView = renderView; |
| 69 | |
| 70 | UIActivityIndicatorView *indicatorView = |
| 71 | [[UIActivityIndicatorView alloc] |
| 72 | initWithActivityIndicatorStyle: |
| 73 | UIActivityIndicatorViewStyleWhiteLarge]; |
| 74 | indicatorView.frame = self.bounds; |
| 75 | indicatorView.hidesWhenStopped = YES; |
| 76 | [self addSubview:indicatorView]; |
| 77 | indicatorView.autoresizingMask = UIViewAutoresizingFlexibleWidth | |
| 78 | UIViewAutoresizingFlexibleHeight; |
| 79 | indicatorView.translatesAutoresizingMaskIntoConstraints = YES; |
| 80 | [indicatorView startAnimating]; |
| 81 | self.activityView = indicatorView; |
| 82 | } |
| 83 | |
| 84 | - (id)initWithFrame:(CGRect)frame { |
| 85 | self = [super initWithFrame:frame]; |
| 86 | if (self) { |
| 87 | init(self); |
| 88 | } |
| 89 | return self; |
| 90 | } |
| 91 | |
| 92 | -(id)initWithCoder:(NSCoder *)aDecoder { |
| 93 | self = [super initWithCoder:aDecoder]; |
| 94 | if (self) { |
| 95 | init(self); |
| 96 | } |
| 97 | return self; |
| 98 | } |
| 99 | |
| 100 | - (UIInterfaceOrientation)videoOrientation { |
| 101 | return _videoOrientation; |
| 102 | } |
| 103 | |
| 104 | - (void)setVideoOrientation:(UIInterfaceOrientation)videoOrientation { |
| 105 | if (_videoOrientation != videoOrientation) { |
| 106 | _videoOrientation = videoOrientation; |
| 107 | |
| 108 | CGFloat angle; |
| 109 | switch (videoOrientation) { |
| 110 | case UIInterfaceOrientationPortrait: |
| 111 | angle = M_PI_2; |
| 112 | break; |
| 113 | case UIInterfaceOrientationPortraitUpsideDown: |
| 114 | angle = -M_PI_2; |
| 115 | break; |
| 116 | case UIInterfaceOrientationLandscapeLeft: |
| 117 | angle = M_PI; |
| 118 | break; |
| 119 | case UIInterfaceOrientationLandscapeRight: |
| 120 | angle = 0; |
| 121 | break; |
| 122 | } |
| 123 | // The video comes in mirrored. That is fine for the local video, |
| 124 | // but the remote video should be put back to original. |
| 125 | CGAffineTransform xform = |
| 126 | CGAffineTransformMakeScale([self isRemote] ? -1 : 1, 1); |
| 127 | xform = CGAffineTransformRotate(xform, angle); |
| 128 | [[self renderView] setTransform:xform]; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | - (void)renderVideoTrackInterface:(RTCVideoTrack *)videoTrack { |
| 133 | [self stop]; |
| 134 | |
| 135 | _track = videoTrack; |
| 136 | |
| 137 | if (_track) { |
| 138 | if (!_renderer) { |
| 139 | _renderer = [[RTCVideoRenderer alloc] |
| 140 | initWithRenderView:[self renderView]]; |
| 141 | } |
| 142 | [_track addRenderer:_renderer]; |
| 143 | [self resume]; |
| 144 | } |
| 145 | |
| 146 | [self setVideoOrientation:UIInterfaceOrientationLandscapeLeft]; |
| 147 | [self setVideoOrientation:UIInterfaceOrientationPortrait]; |
| 148 | [self setVideoOrientation:UIInterfaceOrientationLandscapeLeft]; |
| 149 | } |
| 150 | |
| 151 | -(void)pause { |
| 152 | [_renderer stop]; |
| 153 | } |
| 154 | |
| 155 | -(void)resume { |
| 156 | [self.activityView stopAnimating]; |
| 157 | [self.activityView removeFromSuperview]; |
| 158 | self.activityView = nil; |
| 159 | |
| 160 | [_renderer start]; |
| 161 | } |
| 162 | |
| 163 | - (void)stop { |
| 164 | [_track removeRenderer:_renderer]; |
| 165 | [_renderer stop]; |
| 166 | } |
| 167 | |
| 168 | @end |