Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.48 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. #pragma mark - Setters Props
  124. -(void)setBroadcasting:(BOOL)broadcasting{
  125. if(self.broadcast){
  126. if(broadcasting){
  127. [BroadcastManager startBroadcast:self.broadcast];
  128. }
  129. else{
  130. [BroadcastManager stopBroadcast:self.broadcast];
  131. }
  132. }
  133. _broadcasting = broadcasting;
  134. }
  135. -(void)setMuted:(BOOL)muted{
  136. [BroadcastManager turnMic:muted andBroadcastView:self.broadcast];
  137. _muted = muted;
  138.  
  139. }
  140. -(void)setFlashOn:(BOOL)flashOn{
  141. [BroadcastManager switchFlash:flashOn andBroadcastView:self.broadcast];
  142. _flashOn = flashOn;
  143. }
  144. -(void)setFrontCamera:(BOOL)frontCamera{
  145. [BroadcastManager invertCamera:self.broadcast];
  146. _frontCamera = frontCamera;
  147. }
  148. -(void)setBroadcastName:(NSString *)broadcastName{
  149. [BroadcastManager changeStreamName:broadcastName andBroadcastView:self.broadcast];
  150. _broadcastName = broadcastName;
  151. }
  152. #pragma mark - WMBroadcastViewDelegate Methods
  153.  
  154. -(void)didStartBroacast{
  155. self.onBroadcastStart(@{@"event":@{@"host":self.hostAddress, @"broadcastName":self.broadcastName}});
  156. }
  157. -(void)didFailToStartBroadcast:(NSError *)error{
  158. //show error
  159. self.onBroadcastFail(@{@"error":error.localizedDescription});
  160. }
  161. -(void)broadcastStatusDidChange:(WZState)state{
  162. NSString *stateString;
  163. switch (state) {
  164. case WZStateIdle:
  165. stateString = @"idle";
  166. self.onBroadcastStop(@{@"event":@{@"host":self.hostAddress, @"broadcastName":self.broadcastName, @"status": @"stopped"}});
  167. break;
  168. case WZStateRunning:
  169. stateString = @"running";
  170. break;
  171. case WZStateStarting:
  172. stateString = @"starting";
  173. break;
  174. case WZStateStopping:
  175. stateString = @"stopping";
  176. break;
  177. default:
  178. break;
  179. }
  180.  
  181. }
  182. -(void)broadcastDidReceiveEvent:(WZEvent)event andError:(NSError *)error{
  183. if(error){
  184. self.onBroadcastErrorReceive(@{@"error":error.localizedDescription});
  185. }
  186. else{
  187. self.onBroadcastEventReceive(@{@"event": [NSNumber numberWithInteger:event]});
  188.  
  189. }
  190.  
  191. }
  192. -(void)broadcastDidReceiveError:(NSError *) error{
  193. if(error){
  194. self.onBroadcastErrorReceive(@{@"error":error.localizedDescription});
  195. }
  196. }
  197. -(void)brodcastVideoFrameWasEncoded:(NSInteger *)durationInSeconds{
  198. self.onBroadcastVideoEncoded(@{@"seconds":[NSNumber numberWithInteger:durationInSeconds]});
  199. }
  200.  
  201. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement