Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.66 KB | None | 0 0
  1. //
  2. // RNBroadcastView.m
  3. // WowzaMedlReactNativeModule
  4. //
  5. // Created by Hugo Nagano on 11/4/16.
  6. // Copyright © 2016 Facebook. All rights reserved.
  7. //
  8.  
  9. #import "RNBroadcastView.h"
  10. #import <React/RCTBridgeModule.h>
  11. #import <React/RCTEventDispatcher.h>
  12. #import <React/UIView+React.h>
  13. #import "WMBroadcastView.h"
  14. #import "BroadcastManager.h"
  15. #import "WowzaGoCoderSDK.h"
  16.  
  17. //static NSDictionary *onLoadParamsForSource()
  18. //{
  19. // NSDictionary *dict = @{
  20. // @"width": @(source.size.width),
  21. // @"height": @(source.size.height),
  22. //
  23. // };
  24. // return @{ @"source": dict };
  25. //}
  26. @interface RNBroadcastView()<WMBroadcastViewDelegate>
  27. @property (nonatomic, copy) RCTDirectEventBlock onBroadcastStart;
  28. @property (nonatomic, copy) RCTDirectEventBlock onBroadcastFail;
  29. @property (nonatomic, copy) RCTDirectEventBlock onBroadcastStatusChange;
  30. @property (nonatomic, copy) RCTDirectEventBlock onBroadcastEventReceive;
  31. @property (nonatomic, copy) RCTDirectEventBlock onBroadcastErrorReceive;
  32. @property (nonatomic, copy) RCTDirectEventBlock onBroadcastVideoEncoded;
  33. @property (nonatomic, copy) RCTDirectEventBlock onBroadcastStop;
  34.  
  35. @property (nonatomic, strong) NSString *sdkLicenseKey;
  36. @property (nonatomic, strong) NSString *hostAddress;
  37. @property (nonatomic, assign) NSNumber *port;
  38. @property (nonatomic, strong) NSString *applicationName;
  39. @property (nonatomic, strong) NSString *broadcastName;
  40. @property (nonatomic, assign) BOOL backgroundMode;
  41. @property (nonatomic, assign) NSInteger sizePreset;
  42. @property (nonatomic, strong) NSString *username;
  43. @property (nonatomic, strong) NSString *password;
  44. @property (nonatomic, assign) BOOL broadcasting;
  45. @property (nonatomic, assign) BOOL flashOn;
  46. @property (nonatomic, assign) BOOL frontCamera;
  47. @property (nonatomic, assign) BOOL muted;
  48. @property (nonatomic, strong) WMBroadcastView *broadcast;
  49.  
  50. @end
  51. @implementation RNBroadcastView {
  52. RCTEventDispatcher *_eventDispatcher;
  53. }
  54.  
  55. - (instancetype)init {
  56. NSLog(@"init");
  57. self = [super init];
  58. if ( self ) {
  59.  
  60. }
  61. return self;
  62. }
  63.  
  64. - (instancetype)initWithCoder:(NSCoder *)aDecoder {
  65.  
  66. self = [super initWithCoder:aDecoder];
  67. if ( self ) {
  68.  
  69. }
  70. return self;
  71. }
  72. - (instancetype)initWithFrame:(CGRect)frame {
  73. self = [super initWithFrame:frame];
  74. if ( self ) {
  75.  
  76. }
  77. return self;
  78. }
  79. - (instancetype)initWithEventDispatcher:(RCTEventDispatcher *)eventDispatcher{
  80. if ((self = [super init])) {
  81. _eventDispatcher = eventDispatcher;
  82.  
  83. }
  84.  
  85. return self;
  86. }
  87. - (void)layoutMarginsDidChange{
  88. [super layoutMarginsDidChange];
  89. }
  90.  
  91. -(void)layoutSubviews{
  92. [super layoutSubviews];
  93. if(!self.broadcast){
  94. _broadcast = [BroadcastManager initializeBroadcasterWithSdkLicence:self.sdkLicenseKey
  95. andHostAddress:self.hostAddress
  96. portNumber:[self.port integerValue]
  97. applicationName:self.applicationName
  98. broadcastName:self.broadcastName
  99. username:self.username
  100. password:self.password
  101. backgroundMode:self.backgroundMode
  102. sizePreset:self.sizePreset
  103. andBroadcastView:self];
  104. self.broadcast.delegate = self;
  105. }
  106.  
  107. }
  108. -(void)didUpdateReactSubviews{
  109. [super didUpdateReactSubviews];
  110.  
  111. }
  112. -(void)didMoveToSuperview{
  113. [super didMoveToSuperview];
  114.  
  115. }
  116.  
  117. - (void)removeFromSuperview {
  118. [BroadcastManager releaseBroadcast:self.broadcast];
  119. _eventDispatcher = nil;
  120. [super removeFromSuperview];
  121. }
  122.  
  123. -(void)startBroacast {
  124. [BroadcastManager startBroadcast:self.broadcast];
  125. }
  126.  
  127. #pragma mark - Setters Props
  128. -(void)setBroadcasting:(BOOL)broadcasting{
  129. if(self.broadcast){
  130. if(broadcasting){
  131. [self startBroacast];
  132. }
  133. else{
  134. [BroadcastManager stopBroadcast:self.broadcast];
  135. }
  136. }
  137. _broadcasting = broadcasting;
  138. }
  139.  
  140. -(void)setMuted:(BOOL)muted{
  141. [BroadcastManager turnMic:muted andBroadcastView:self.broadcast];
  142. _muted = muted;
  143.  
  144. }
  145. -(void)setFlashOn:(BOOL)flashOn{
  146. [BroadcastManager switchFlash:flashOn andBroadcastView:self.broadcast];
  147. _flashOn = flashOn;
  148. }
  149. -(void)setFrontCamera:(BOOL)frontCamera{
  150. [BroadcastManager invertCamera:self.broadcast];
  151. _frontCamera = frontCamera;
  152. }
  153. -(void)setBroadcastName:(NSString *)broadcastName{
  154. [BroadcastManager changeStreamName:broadcastName andBroadcastView:self.broadcast];
  155. _broadcastName = broadcastName;
  156. }
  157. #pragma mark - WMBroadcastViewDelegate Methods
  158.  
  159. -(void)didStartBroacast{
  160. self.onBroadcastStart(@{@"event":@{@"host":self.hostAddress, @"broadcastName":self.broadcastName}});
  161. }
  162. -(void)didFailToStartBroadcast:(NSError *)error{
  163. //show error
  164. self.onBroadcastFail(@{@"error":error.localizedDescription});
  165. }
  166. -(void)broadcastStatusDidChange:(WZState)state{
  167. NSString *stateString;
  168. switch (state) {
  169. case WZStateIdle:
  170. stateString = @"idle";
  171. self.onBroadcastStop(@{@"event":@{@"host":self.hostAddress, @"broadcastName":self.broadcastName, @"status": @"stopped"}});
  172. break;
  173. case WZStateRunning:
  174. stateString = @"running";
  175. break;
  176. case WZStateStarting:
  177. stateString = @"starting";
  178. break;
  179. case WZStateStopping:
  180. stateString = @"stopping";
  181. break;
  182. default:
  183. break;
  184. }
  185.  
  186. }
  187. -(void)broadcastDidReceiveEvent:(WZEvent)event andError:(NSError *)error{
  188. if(error){
  189. self.onBroadcastErrorReceive(@{@"error":error.localizedDescription});
  190. }
  191. else{
  192. self.onBroadcastEventReceive(@{@"event": [NSNumber numberWithInteger:event]});
  193.  
  194. }
  195.  
  196. }
  197. -(void)broadcastDidReceiveError:(NSError *) error{
  198. if(error){
  199. [self performSelector:@selector(startBroacast)
  200. withObject:nil
  201. afterDelay:1.5];
  202. self.onBroadcastErrorReceive(@{@"error":error.localizedDescription});
  203. }
  204. }
  205. -(void)brodcastVideoFrameWasEncoded:(NSInteger *)durationInSeconds{
  206. self.onBroadcastVideoEncoded(@{@"seconds":[NSNumber numberWithInteger:durationInSeconds]});
  207. }
  208.  
  209. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement