You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JitsiMeetView.m 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright @ 2018-present 8x8, Inc.
  3. * Copyright @ 2017-2018 Atlassian Pty Ltd
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. #include <mach/mach_time.h>
  18. #import "ExternalAPI.h"
  19. #import "JitsiMeet+Private.h"
  20. #import "JitsiMeetConferenceOptions+Private.h"
  21. #import "JitsiMeetView+Private.h"
  22. #import "ReactUtils.h"
  23. #import "RNRootView.h"
  24. /**
  25. * Backwards compatibility: turn the boolean prop into a feature flag.
  26. */
  27. static NSString *const PiPEnabledFeatureFlag = @"pip.enabled";
  28. @implementation JitsiMeetView {
  29. /**
  30. * The unique identifier of this `JitsiMeetView` within the process for the
  31. * purposes of `ExternalAPI`. The name scope was inspired by postis which we
  32. * use on Web for the similar purposes of the iframe-based external API.
  33. */
  34. NSString *externalAPIScope;
  35. /**
  36. * React Native view where the entire content will be rendered.
  37. */
  38. RNRootView *rootView;
  39. }
  40. /**
  41. * The `JitsiMeetView`s associated with their `ExternalAPI` scopes (i.e. unique
  42. * identifiers within the process).
  43. */
  44. static NSMapTable<NSString *, JitsiMeetView *> *views;
  45. /**
  46. * This gets called automagically when the program starts.
  47. */
  48. __attribute__((constructor))
  49. static void initializeViewsMap() {
  50. views = [NSMapTable strongToWeakObjectsMapTable];
  51. }
  52. #pragma mark Initializers
  53. - (instancetype)init {
  54. self = [super init];
  55. if (self) {
  56. [self initWithXXX];
  57. }
  58. return self;
  59. }
  60. - (instancetype)initWithCoder:(NSCoder *)coder {
  61. self = [super initWithCoder:coder];
  62. if (self) {
  63. [self initWithXXX];
  64. }
  65. return self;
  66. }
  67. - (instancetype)initWithFrame:(CGRect)frame {
  68. self = [super initWithFrame:frame];
  69. if (self) {
  70. [self initWithXXX];
  71. }
  72. return self;
  73. }
  74. /**
  75. * Internal initialization:
  76. *
  77. * - sets the background color
  78. * - initializes the external API scope
  79. */
  80. - (void)initWithXXX {
  81. // Hook this JitsiMeetView into ExternalAPI.
  82. externalAPIScope = [NSUUID UUID].UUIDString;
  83. [views setObject:self forKey:externalAPIScope];
  84. // Set a background color which is in accord with the JavaScript and Android
  85. // parts of the application and causes less perceived visual flicker than
  86. // the default background color.
  87. self.backgroundColor
  88. = [UIColor colorWithRed:.07f green:.07f blue:.07f alpha:1];
  89. }
  90. #pragma mark API
  91. - (void)join:(JitsiMeetConferenceOptions *)options {
  92. [self setProps:options == nil ? @{} : [options asProps]];
  93. }
  94. - (void)leave {
  95. [self setProps:@{}];
  96. }
  97. - (void)hangUp {
  98. ExternalAPI *externalAPI = [[JitsiMeet sharedInstance] getExternalAPI];
  99. [externalAPI sendHangUp];
  100. }
  101. - (void)setAudioMuted:(BOOL)muted {
  102. ExternalAPI *externalAPI = [[JitsiMeet sharedInstance] getExternalAPI];
  103. [externalAPI sendSetAudioMuted:muted];
  104. }
  105. - (void)sendEndpointTextMessage:(NSString * _Nonnull)message :(NSString * _Nullable)to {
  106. ExternalAPI *externalAPI = [[JitsiMeet sharedInstance] getExternalAPI];
  107. [externalAPI sendEndpointTextMessage:message :to];
  108. }
  109. - (void)toggleScreenShare:(BOOL)enabled {
  110. ExternalAPI *externalAPI = [[JitsiMeet sharedInstance] getExternalAPI];
  111. [externalAPI toggleScreenShare:enabled];
  112. }
  113. - (void)retrieveParticipantsInfo:(void (^ _Nonnull)(NSArray * _Nullable))completionHandler {
  114. ExternalAPI *externalAPI = [[JitsiMeet sharedInstance] getExternalAPI];
  115. [externalAPI retrieveParticipantsInfo:completionHandler];
  116. }
  117. - (void)openChat:(NSString*)to {
  118. ExternalAPI *externalAPI = [[JitsiMeet sharedInstance] getExternalAPI];
  119. [externalAPI openChat:to];
  120. }
  121. - (void)closeChat {
  122. ExternalAPI *externalAPI = [[JitsiMeet sharedInstance] getExternalAPI];
  123. [externalAPI closeChat];
  124. }
  125. - (void)sendChatMessage:(NSString * _Nonnull)message :(NSString * _Nullable)to {
  126. ExternalAPI *externalAPI = [[JitsiMeet sharedInstance] getExternalAPI];
  127. [externalAPI sendChatMessage:message :to];
  128. }
  129. - (void)setVideoMuted:(BOOL)muted {
  130. ExternalAPI *externalAPI = [[JitsiMeet sharedInstance] getExternalAPI];
  131. [externalAPI sendSetVideoMuted:muted];
  132. }
  133. #pragma mark Private methods
  134. /**
  135. * Passes the given props to the React Native application. The props which we pass
  136. * are a combination of 3 different sources:
  137. *
  138. * - JitsiMeet.defaultConferenceOptions
  139. * - This function's parameters
  140. * - Some extras which are added by this function
  141. */
  142. - (void)setProps:(NSDictionary *_Nonnull)newProps {
  143. NSMutableDictionary *props = mergeProps([[JitsiMeet sharedInstance] getDefaultProps], newProps);
  144. // Set the PiP flag if it wasn't manually set.
  145. NSMutableDictionary *featureFlags = props[@"flags"];
  146. if (featureFlags[PiPEnabledFeatureFlag] == nil) {
  147. featureFlags[PiPEnabledFeatureFlag]
  148. = [NSNumber numberWithBool:
  149. self.delegate && [self.delegate respondsToSelector:@selector(enterPictureInPicture:)]];
  150. }
  151. props[@"externalAPIScope"] = externalAPIScope;
  152. // This method is supposed to be imperative i.e. a second
  153. // invocation with one and the same URL is expected to join the respective
  154. // conference again if the first invocation was followed by leaving the
  155. // conference. However, React and, respectively,
  156. // appProperties/initialProperties are declarative expressions i.e. one and
  157. // the same URL will not trigger an automatic re-render in the JavaScript
  158. // source code. The workaround implemented below introduces imperativeness
  159. // in React Component props by defining a unique value per invocation.
  160. props[@"timestamp"] = @(mach_absolute_time());
  161. if (rootView) {
  162. // Update props with the new URL.
  163. rootView.appProperties = props;
  164. } else {
  165. RCTBridge *bridge = [[JitsiMeet sharedInstance] getReactBridge];
  166. rootView
  167. = [[RNRootView alloc] initWithBridge:bridge
  168. moduleName:@"App"
  169. initialProperties:props];
  170. rootView.backgroundColor = self.backgroundColor;
  171. // Add rootView as a subview which completely covers this one.
  172. [rootView setFrame:[self bounds]];
  173. rootView.autoresizingMask
  174. = UIViewAutoresizingFlexibleWidth
  175. | UIViewAutoresizingFlexibleHeight;
  176. [self addSubview:rootView];
  177. }
  178. }
  179. + (BOOL)setPropsInViews:(NSDictionary *_Nonnull)newProps {
  180. BOOL handled = NO;
  181. if (views) {
  182. for (NSString *externalAPIScope in views) {
  183. JitsiMeetView *view
  184. = [self viewForExternalAPIScope:externalAPIScope];
  185. if (view) {
  186. [view setProps:newProps];
  187. handled = YES;
  188. }
  189. }
  190. }
  191. return handled;
  192. }
  193. + (instancetype)viewForExternalAPIScope:(NSString *)externalAPIScope {
  194. return [views objectForKey:externalAPIScope];
  195. }
  196. @end