react-native-webrtc 124.0.4 → 124.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. package/.nvmrc +1 -0
  2. package/android/build.gradle +3 -2
  3. package/android/consumer-rules.pro +2 -0
  4. package/android/src/main/java/com/oney/WebRTCModule/AbstractVideoCaptureController.java +30 -3
  5. package/android/src/main/java/com/oney/WebRTCModule/CameraCaptureController.java +180 -73
  6. package/android/src/main/java/com/oney/WebRTCModule/GetUserMediaImpl.java +53 -24
  7. package/android/src/main/java/com/oney/WebRTCModule/MediaProjectionNotification.java +11 -19
  8. package/android/src/main/java/com/oney/WebRTCModule/MediaProjectionService.java +0 -2
  9. package/android/src/main/java/com/oney/WebRTCModule/PeerConnectionObserver.java +4 -2
  10. package/android/src/main/java/com/oney/WebRTCModule/RTCVideoViewManager.java +25 -0
  11. package/android/src/main/java/com/oney/WebRTCModule/ScreenCaptureController.java +6 -4
  12. package/android/src/main/java/com/oney/WebRTCModule/WebRTCModule.java +20 -14
  13. package/android/src/main/java/com/oney/WebRTCModule/WebRTCView.java +37 -0
  14. package/android/src/main/java/com/oney/WebRTCModule/videoEffects/VideoEffectProcessor.java +15 -9
  15. package/android/src/main/java/org/webrtc/Camera1Helper.java +0 -1
  16. package/android/src/main/java/org/webrtc/Camera2Helper.java +4 -3
  17. package/ios/RCTWebRTC/CaptureController.h +3 -0
  18. package/ios/RCTWebRTC/CaptureController.m +12 -0
  19. package/ios/RCTWebRTC/I420Converter.h +22 -0
  20. package/ios/RCTWebRTC/I420Converter.m +164 -0
  21. package/ios/RCTWebRTC/PIPController.h +24 -0
  22. package/ios/RCTWebRTC/PIPController.m +234 -0
  23. package/ios/RCTWebRTC/RTCVideoViewManager.h +23 -0
  24. package/ios/RCTWebRTC/RTCVideoViewManager.m +131 -28
  25. package/ios/RCTWebRTC/SampleBufferVideoCallView.h +12 -0
  26. package/ios/RCTWebRTC/SampleBufferVideoCallView.m +178 -0
  27. package/ios/RCTWebRTC/ScreenCaptureController.m +4 -0
  28. package/ios/RCTWebRTC/VideoCaptureController.h +3 -0
  29. package/ios/RCTWebRTC/VideoCaptureController.m +95 -30
  30. package/ios/RCTWebRTC/WebRTCModule+Permissions.m +4 -8
  31. package/ios/RCTWebRTC/WebRTCModule+RTCAudioSession.m +20 -0
  32. package/ios/RCTWebRTC/WebRTCModule+RTCDataChannel.m +31 -40
  33. package/ios/RCTWebRTC/WebRTCModule+RTCMediaStream.h +5 -0
  34. package/ios/RCTWebRTC/WebRTCModule+RTCMediaStream.m +93 -28
  35. package/ios/RCTWebRTC/WebRTCModule+RTCPeerConnection.m +26 -55
  36. package/ios/RCTWebRTC/WebRTCModule+Transceivers.m +11 -28
  37. package/ios/RCTWebRTC/WebRTCModuleOptions.h +1 -0
  38. package/ios/RCTWebRTC/videoEffects/ProcessorProvider.h +9 -0
  39. package/ios/RCTWebRTC/videoEffects/ProcessorProvider.m +23 -0
  40. package/ios/RCTWebRTC/videoEffects/VideoEffectProcessor.h +13 -0
  41. package/ios/RCTWebRTC/videoEffects/VideoEffectProcessor.m +23 -0
  42. package/ios/RCTWebRTC/videoEffects/VideoFrameProcessor.h +8 -0
  43. package/ios/RCTWebRTC.xcodeproj/project.pbxproj +4 -0
  44. package/lib/commonjs/Constraints.js +2 -0
  45. package/lib/commonjs/Constraints.js.map +1 -0
  46. package/lib/commonjs/MediaDevices.js.map +1 -1
  47. package/lib/commonjs/MediaStreamTrack.js +31 -5
  48. package/lib/commonjs/MediaStreamTrack.js.map +1 -1
  49. package/lib/commonjs/RTCAudioSession.js +33 -0
  50. package/lib/commonjs/RTCAudioSession.js.map +1 -0
  51. package/lib/commonjs/RTCPIPView.js +38 -0
  52. package/lib/commonjs/RTCPIPView.js.map +1 -0
  53. package/lib/commonjs/RTCPeerConnection.js +2 -4
  54. package/lib/commonjs/RTCPeerConnection.js.map +1 -1
  55. package/lib/commonjs/RTCUtil.js +2 -3
  56. package/lib/commonjs/RTCUtil.js.map +1 -1
  57. package/lib/commonjs/RTCView.js.map +1 -1
  58. package/lib/commonjs/getUserMedia.js.map +1 -1
  59. package/lib/commonjs/index.js +28 -0
  60. package/lib/commonjs/index.js.map +1 -1
  61. package/lib/module/Constraints.js +2 -0
  62. package/lib/module/Constraints.js.map +1 -0
  63. package/lib/module/MediaDevices.js.map +1 -1
  64. package/lib/module/MediaStreamTrack.js +32 -6
  65. package/lib/module/MediaStreamTrack.js.map +1 -1
  66. package/lib/module/RTCAudioSession.js +26 -0
  67. package/lib/module/RTCAudioSession.js.map +1 -0
  68. package/lib/module/RTCPIPView.js +26 -0
  69. package/lib/module/RTCPIPView.js.map +1 -0
  70. package/lib/module/RTCPeerConnection.js +2 -4
  71. package/lib/module/RTCPeerConnection.js.map +1 -1
  72. package/lib/module/RTCUtil.js +2 -3
  73. package/lib/module/RTCUtil.js.map +1 -1
  74. package/lib/module/RTCView.js.map +1 -1
  75. package/lib/module/getUserMedia.js.map +1 -1
  76. package/lib/module/index.js +3 -1
  77. package/lib/module/index.js.map +1 -1
  78. package/lib/typescript/Constraints.d.ts +19 -0
  79. package/lib/typescript/MediaDevices.d.ts +2 -1
  80. package/lib/typescript/MediaStreamTrack.d.ts +27 -5
  81. package/lib/typescript/RTCAudioSession.d.ts +10 -0
  82. package/lib/typescript/RTCPIPView.d.ts +15 -0
  83. package/lib/typescript/RTCPeerConnection.d.ts +2 -1
  84. package/lib/typescript/RTCUtil.d.ts +7 -1
  85. package/lib/typescript/RTCView.d.ts +58 -1
  86. package/lib/typescript/getUserMedia.d.ts +4 -4
  87. package/lib/typescript/index.d.ts +5 -3
  88. package/package.json +2 -1
  89. package/src/.eslintrc.cjs +1 -1
  90. package/src/Constraints.ts +21 -0
  91. package/src/MediaDevices.ts +2 -2
  92. package/src/MediaStreamTrack.ts +47 -9
  93. package/src/RTCAudioSession.ts +25 -0
  94. package/src/RTCPIPView.tsx +47 -0
  95. package/src/RTCPeerConnection.ts +4 -5
  96. package/src/RTCUtil.ts +10 -3
  97. package/src/RTCView.ts +60 -2
  98. package/src/getUserMedia.ts +4 -3
  99. package/src/index.ts +12 -3
  100. package/tools/release.sh +1 -1
  101. package/tsconfig.json +3 -1
  102. package/ios/RCTWebRTC.xcodeproj/xcuserdata/saghul.xcuserdatad/xcschemes/xcschememanagement.plist +0 -14
  103. package/ios/RCTWebRTC.xcworkspace/xcuserdata/saghul.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  104. package/tools/build-webrtc.py +0 -344
@@ -12,7 +12,6 @@ import android.util.Log;
12
12
 
13
13
  import java.util.Random;
14
14
 
15
-
16
15
  /**
17
16
  * This class implements an Android {@link Service}, a foreground one specifically, and it's
18
17
  * responsible for presenting an ongoing notification when a conference is in progress.
@@ -70,7 +69,6 @@ public class MediaProjectionService extends Service {
70
69
 
71
70
  @Override
72
71
  public int onStartCommand(Intent intent, int flags, int startId) {
73
-
74
72
  Notification notification = MediaProjectionNotification.buildMediaProjectionNotification(this);
75
73
 
76
74
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
@@ -250,7 +250,8 @@ class PeerConnectionObserver implements PeerConnection.Observer {
250
250
  return;
251
251
  }
252
252
 
253
- peerConnection.getStats(targetReceiver, rtcStatsReport -> promise.resolve(StringUtils.statsToJSON(rtcStatsReport)));
253
+ peerConnection.getStats(
254
+ targetReceiver, rtcStatsReport -> promise.resolve(StringUtils.statsToJSON(rtcStatsReport)));
254
255
  }
255
256
 
256
257
  public void senderGetStats(String senderId, Promise promise) {
@@ -268,7 +269,8 @@ class PeerConnectionObserver implements PeerConnection.Observer {
268
269
  return;
269
270
  }
270
271
 
271
- peerConnection.getStats(targetSender, rtcStatsReport -> promise.resolve(StringUtils.statsToJSON(rtcStatsReport)));
272
+ peerConnection.getStats(
273
+ targetSender, rtcStatsReport -> promise.resolve(StringUtils.statsToJSON(rtcStatsReport)));
272
274
  }
273
275
 
274
276
  @Override
@@ -1,8 +1,13 @@
1
1
  package com.oney.WebRTCModule;
2
2
 
3
+ import com.facebook.react.bridge.ReadableMap;
3
4
  import com.facebook.react.uimanager.SimpleViewManager;
4
5
  import com.facebook.react.uimanager.ThemedReactContext;
5
6
  import com.facebook.react.uimanager.annotations.ReactProp;
7
+ import com.facebook.react.uimanager.events.RCTEventEmitter;
8
+
9
+ import java.util.HashMap;
10
+ import java.util.Map;
6
11
 
7
12
  public class RTCVideoViewManager extends SimpleViewManager<WebRTCView> {
8
13
  private static final String REACT_CLASS = "RTCVideoView";
@@ -70,4 +75,24 @@ public class RTCVideoViewManager extends SimpleViewManager<WebRTCView> {
70
75
  public void setZOrder(WebRTCView view, int zOrder) {
71
76
  view.setZOrder(zOrder);
72
77
  }
78
+
79
+ /**
80
+ * Sets the callback for when video dimensions change.
81
+ *
82
+ * @param view The {@code WebRTCView} on which the callback is to be set.
83
+ * @param onDimensionsChange The callback to be called when video dimensions change.
84
+ */
85
+ @ReactProp(name = "onDimensionsChange")
86
+ public void setOnDimensionsChange(WebRTCView view, boolean onDimensionsChange) {
87
+ view.setOnDimensionsChange(onDimensionsChange);
88
+ }
89
+
90
+ @Override
91
+ public Map<String, Object> getExportedCustomDirectEventTypeConstants() {
92
+ Map<String, Object> eventTypeConstants = new HashMap<>();
93
+ Map<String, String> dimensionsChangeEvent = new HashMap<>();
94
+ dimensionsChangeEvent.put("registrationName", "onDimensionsChange");
95
+ eventTypeConstants.put("onDimensionsChange", dimensionsChangeEvent);
96
+ return eventTypeConstants;
97
+ }
73
98
  }
@@ -25,10 +25,7 @@ public class ScreenCaptureController extends AbstractVideoCaptureController {
25
25
 
26
26
  private final Context context;
27
27
 
28
- public ScreenCaptureController(Context context,
29
- int width,
30
- int height,
31
- Intent mediaProjectionPermissionResultData) {
28
+ public ScreenCaptureController(Context context, int width, int height, Intent mediaProjectionPermissionResultData) {
32
29
  super(width, height, DEFAULT_FPS);
33
30
 
34
31
  this.mediaProjectionPermissionResultData = mediaProjectionPermissionResultData;
@@ -60,6 +57,11 @@ public class ScreenCaptureController extends AbstractVideoCaptureController {
60
57
  }
61
58
  }
62
59
 
60
+ @Override
61
+ public String getDeviceId() {
62
+ return "screen-capture";
63
+ }
64
+
63
65
  @Override
64
66
  public void dispose() {
65
67
  MediaProjectionService.abort(context);
@@ -67,10 +67,10 @@ public class WebRTCModule extends ReactContextBaseJavaModule {
67
67
  String fieldTrials = options.fieldTrials;
68
68
 
69
69
  PeerConnectionFactory.initialize(PeerConnectionFactory.InitializationOptions.builder(reactContext)
70
- .setFieldTrials(fieldTrials)
71
- .setNativeLibraryLoader(new LibraryLoader())
72
- .setInjectableLogger(injectableLogger, loggingSeverity)
73
- .createInitializationOptions());
70
+ .setFieldTrials(fieldTrials)
71
+ .setNativeLibraryLoader(new LibraryLoader())
72
+ .setInjectableLogger(injectableLogger, loggingSeverity)
73
+ .createInitializationOptions());
74
74
 
75
75
  if (injectableLogger == null && loggingSeverity != null) {
76
76
  Logging.enableLogToDebugOutput(loggingSeverity);
@@ -706,7 +706,7 @@ public class WebRTCModule extends ReactContextBaseJavaModule {
706
706
  }
707
707
 
708
708
  @ReactMethod(isBlockingSynchronousMethod = true)
709
- public void transceiverSetCodecPreferences(int id, String senderId, ReadableArray codecPreferences) {
709
+ public boolean transceiverSetCodecPreferences(int id, String senderId, ReadableArray codecPreferences) {
710
710
  ThreadUtils.runOnExecutor(() -> {
711
711
  WritableMap identifier = Arguments.createMap();
712
712
  WritableMap params = Arguments.createMap();
@@ -765,6 +765,7 @@ public class WebRTCModule extends ReactContextBaseJavaModule {
765
765
  Log.d(TAG, "transceiverSetCodecPreferences(): " + e.getMessage());
766
766
  }
767
767
  });
768
+ return true;
768
769
  }
769
770
 
770
771
  @ReactMethod
@@ -882,11 +883,13 @@ public class WebRTCModule extends ReactContextBaseJavaModule {
882
883
  }
883
884
 
884
885
  @ReactMethod
885
- public void mediaStreamTrackSwitchCamera(String id) {
886
+ public void mediaStreamTrackApplyConstraints(String id, ReadableMap constraints, Promise promise) {
886
887
  ThreadUtils.runOnExecutor(() -> {
887
888
  MediaStreamTrack track = getLocalTrack(id);
888
889
  if (track != null) {
889
- getUserMediaImpl.switchCamera(id);
890
+ getUserMediaImpl.applyConstraints(id, constraints, promise);
891
+ } else {
892
+ promise.reject(new Exception("mediaStreamTrackApplyConstraints() could not find track " + id));
890
893
  }
891
894
  });
892
895
  }
@@ -938,8 +941,8 @@ public class WebRTCModule extends ReactContextBaseJavaModule {
938
941
  }
939
942
 
940
943
  @ReactMethod
941
- public void mediaStreamTrackSetVideoEffect(String id, String name) {
942
- ThreadUtils.runOnExecutor(() -> { getUserMediaImpl.setVideoEffect(id, name); });
944
+ public void mediaStreamTrackSetVideoEffects(String id, ReadableArray names) {
945
+ ThreadUtils.runOnExecutor(() -> { getUserMediaImpl.setVideoEffects(id, names); });
943
946
  }
944
947
 
945
948
  @ReactMethod
@@ -1275,11 +1278,14 @@ public class WebRTCModule extends ReactContextBaseJavaModule {
1275
1278
  return;
1276
1279
  }
1277
1280
 
1278
- IceCandidate candidate = new IceCandidate(
1279
- candidateMap.hasKey("sdpMid") && !candidateMap.isNull("sdpMid") ? candidateMap.getString("sdpMid") : "",
1280
- candidateMap.hasKey("sdpMLineIndex") && !candidateMap.isNull("sdpMLineIndex") ? candidateMap.getInt("sdpMLineIndex") : 0,
1281
- candidateMap.getString("candidate"));
1282
-
1281
+ IceCandidate candidate = new IceCandidate(candidateMap.hasKey("sdpMid") && !candidateMap.isNull("sdpMid")
1282
+ ? candidateMap.getString("sdpMid")
1283
+ : "",
1284
+ candidateMap.hasKey("sdpMLineIndex") && !candidateMap.isNull("sdpMLineIndex")
1285
+ ? candidateMap.getInt("sdpMLineIndex")
1286
+ : 0,
1287
+ candidateMap.getString("candidate"));
1288
+
1283
1289
  peerConnection.addIceCandidate(candidate, new AddIceObserver() {
1284
1290
  @Override
1285
1291
  public void onAddSuccess() {
@@ -10,7 +10,12 @@ import android.view.ViewGroup;
10
10
 
11
11
  import androidx.core.view.ViewCompat;
12
12
 
13
+ import com.facebook.react.bridge.Arguments;
14
+ import com.facebook.react.bridge.Callback;
13
15
  import com.facebook.react.bridge.ReactContext;
16
+ import com.facebook.react.bridge.ReadableMap;
17
+ import com.facebook.react.bridge.WritableMap;
18
+ import com.facebook.react.uimanager.events.RCTEventEmitter;
14
19
 
15
20
  import org.webrtc.EglBase;
16
21
  import org.webrtc.Logging;
@@ -144,6 +149,11 @@ public class WebRTCView extends ViewGroup {
144
149
  */
145
150
  private VideoTrack videoTrack;
146
151
 
152
+ /**
153
+ * The callback to be called when video dimensions change.
154
+ */
155
+ private boolean onDimensionsChangeEnabled = false;
156
+
147
157
  public WebRTCView(Context context) {
148
158
  super(context);
149
159
 
@@ -257,6 +267,24 @@ public class WebRTCView extends ViewGroup {
257
267
  // The onFrameResolutionChanged method call executes on the
258
268
  // surfaceViewRenderer's render Thread.
259
269
  post(requestSurfaceViewRendererLayoutRunnable);
270
+
271
+ // Call the onDimensionsChange callback if it's enabled
272
+ if (onDimensionsChangeEnabled) {
273
+ post(() -> {
274
+ try {
275
+ ReactContext reactContext = (ReactContext) getContext();
276
+ WritableMap params = Arguments.createMap();
277
+ params.putInt("width", videoWidth);
278
+ params.putInt("height", videoHeight);
279
+
280
+ // Send the event through React Native's event system
281
+ reactContext.getJSModule(RCTEventEmitter.class)
282
+ .receiveEvent(getId(), "onDimensionsChange", params);
283
+ } catch (Exception e) {
284
+ Log.e(TAG, "Error calling onDimensionsChange callback", e);
285
+ }
286
+ });
287
+ }
260
288
  }
261
289
  }
262
290
 
@@ -543,4 +571,13 @@ public class WebRTCView extends ViewGroup {
543
571
  rendererAttached = true;
544
572
  }
545
573
  }
574
+
575
+ /**
576
+ * Sets whether the onDimensionsChange callback should be called.
577
+ *
578
+ * @param enabled Whether the callback should be enabled.
579
+ */
580
+ public void setOnDimensionsChange(boolean enabled) {
581
+ this.onDimensionsChangeEnabled = enabled;
582
+ }
546
583
  }
@@ -5,6 +5,8 @@ import org.webrtc.VideoFrame;
5
5
  import org.webrtc.VideoProcessor;
6
6
  import org.webrtc.VideoSink;
7
7
 
8
+ import java.util.List;
9
+
8
10
  /**
9
11
  * Lightweight abstraction for an object that can receive video frames, process and add effects in
10
12
  * them, and pass them on to another object.
@@ -12,11 +14,11 @@ import org.webrtc.VideoSink;
12
14
  public class VideoEffectProcessor implements VideoProcessor {
13
15
  private VideoSink mSink;
14
16
  final private SurfaceTextureHelper textureHelper;
15
- final private VideoFrameProcessor videoFrameProcessor;
17
+ final private List<VideoFrameProcessor> videoFrameProcessors;
16
18
 
17
- public VideoEffectProcessor(VideoFrameProcessor processor, SurfaceTextureHelper textureHelper) {
19
+ public VideoEffectProcessor(List<VideoFrameProcessor> processors, SurfaceTextureHelper textureHelper) {
18
20
  this.textureHelper = textureHelper;
19
- this.videoFrameProcessor = processor;
21
+ this.videoFrameProcessors = processors;
20
22
  }
21
23
 
22
24
  @Override
@@ -39,13 +41,17 @@ public class VideoEffectProcessor implements VideoProcessor {
39
41
  @Override
40
42
  public void onFrameCaptured(VideoFrame frame) {
41
43
  frame.retain();
42
- VideoFrame outputFrame = videoFrameProcessor.process(frame, textureHelper);
43
-
44
- if (outputFrame == null) {
45
- mSink.onFrame(frame);
46
- frame.release();
47
- return;
44
+ VideoFrame outputFrame = frame;
45
+ for (VideoFrameProcessor processor : this.videoFrameProcessors) {
46
+ outputFrame = processor.process(outputFrame, textureHelper);
47
+
48
+ if (outputFrame == null) {
49
+ mSink.onFrame(frame);
50
+ frame.release();
51
+ return;
52
+ }
48
53
  }
54
+
49
55
  mSink.onFrame(outputFrame);
50
56
  outputFrame.release();
51
57
  frame.release();
@@ -30,7 +30,6 @@ import java.util.List;
30
30
  */
31
31
 
32
32
  public class Camera1Helper {
33
-
34
33
  public static int getCameraId(String deviceName) {
35
34
  return Camera1Enumerator.getCameraIndex(deviceName);
36
35
  }
@@ -30,13 +30,14 @@ import java.util.List;
30
30
  * by [CameraManager.getCameraIdList].
31
31
  */
32
32
  public class Camera2Helper {
33
-
34
33
  @Nullable
35
- public static List<CameraEnumerationAndroid.CaptureFormat> getSupportedFormats(CameraManager cameraManager, @Nullable String cameraId) {
34
+ public static List<CameraEnumerationAndroid.CaptureFormat> getSupportedFormats(
35
+ CameraManager cameraManager, @Nullable String cameraId) {
36
36
  return Camera2Enumerator.getSupportedFormats(cameraManager, cameraId);
37
37
  }
38
38
 
39
- public static Size findClosestCaptureFormat(CameraManager cameraManager, @Nullable String cameraId, int width, int height) {
39
+ public static Size findClosestCaptureFormat(
40
+ CameraManager cameraManager, @Nullable String cameraId, int width, int height) {
40
41
  List<CameraEnumerationAndroid.CaptureFormat> formats = getSupportedFormats(cameraManager, cameraId);
41
42
 
42
43
  List<Size> sizes = new ArrayList<>();
@@ -6,9 +6,12 @@ NS_ASSUME_NONNULL_BEGIN
6
6
  @interface CaptureController : NSObject
7
7
 
8
8
  @property(nonatomic, strong) id<CapturerEventsDelegate> eventsDelegate;
9
+ @property(nonatomic, copy, nullable) NSString *deviceId;
9
10
 
10
11
  - (void)startCapture;
11
12
  - (void)stopCapture;
13
+ - (NSDictionary *)getSettings;
14
+ - (void)applyConstraints:(NSDictionary *)constraints error:(NSError **)outError;
12
15
 
13
16
  @end
14
17
 
@@ -12,6 +12,18 @@
12
12
  // subclasses needs to override
13
13
  }
14
14
 
15
+ - (NSDictionary *)getSettings {
16
+ // subclasses needs to override
17
+ return @{@"deviceId" : self.deviceId};
18
+ }
19
+
20
+ - (void)applyConstraints:(NSDictionary *)constraints error:(NSError **)outError {
21
+ *outError =
22
+ [NSError errorWithDomain:@"react-native-webrtc"
23
+ code:0
24
+ userInfo:@{NSLocalizedDescriptionKey : @"This video track does not support applyConstraints."}];
25
+ }
26
+
15
27
  @end
16
28
 
17
29
  #endif
@@ -0,0 +1,22 @@
1
+ //
2
+ // I420Converter.h
3
+ // VideoSampleCaptureRender
4
+ //
5
+ // Adapted from:
6
+ // https://github.com/twilio/video-ios-affectiva/blob/ed2e864324c40ad25e5a06cc2b05298b03caed09/EmoCall/I420Converter.h
7
+ // Created by Boisy Pitre on 5/21/16.
8
+ // Copyright © 2016 Twilio. All rights reserved.
9
+ //
10
+
11
+ #import <Accelerate/Accelerate.h>
12
+ #import <UIKit/UIKit.h>
13
+ #import <WebRTC/WebRTC.h>
14
+
15
+ @interface I420Converter : NSObject
16
+
17
+ - (vImage_Error)prepareForAccelerateConversion;
18
+ - (void)unprepareForAccelerateConversion;
19
+ - (CVPixelBufferRef)convertI420ToPixelBuffer:(RTCI420Buffer *)buffer;
20
+ - (void)createPixelBufferPoolWithWidth:(size_t)width height:(size_t)height;
21
+
22
+ @end
@@ -0,0 +1,164 @@
1
+ //
2
+ // I420Converter.m
3
+ // VideoSampleCaptureRender
4
+ //
5
+ // Adapted from:
6
+ // https://github.com/twilio/video-ios-affectiva/blob/ed2e864324c40ad25e5a06cc2b05298b03caed09/EmoCall/I420Converter.m
7
+ // Created by Boisy Pitre on 5/21/16.
8
+ // Copyright © 2016 Twilio. All rights reserved.
9
+ //
10
+
11
+ #import "I420Converter.h"
12
+
13
+ @interface I420Converter ()
14
+
15
+ @property(nonatomic, assign) vImage_YpCbCrToARGB *conversionInfo;
16
+ @property(nonatomic, assign) CVPixelBufferPoolRef pixelBufferPool;
17
+ @property(nonatomic, assign) size_t poolWidth;
18
+ @property(nonatomic, assign) size_t poolHeight;
19
+ @end
20
+
21
+ @implementation I420Converter
22
+
23
+ - (vImage_Error)prepareForAccelerateConversion {
24
+ // Setup the YpCbCr to ARGB conversion.
25
+ if (_conversionInfo != NULL) {
26
+ return kvImageNoError;
27
+ }
28
+
29
+ // I420 uses limited range.
30
+ vImage_YpCbCrPixelRange pixelRange = {16, 128, 235, 240, 255, 0, 255, 0};
31
+ vImage_YpCbCrToARGB *outInfo = malloc(sizeof(vImage_YpCbCrToARGB));
32
+ vImageYpCbCrType inType = kvImage420Yp8_Cb8_Cr8;
33
+ vImageARGBType outType = kvImageARGB8888;
34
+
35
+ vImage_Error error = vImageConvert_YpCbCrToARGB_GenerateConversion(kvImage_YpCbCrToARGBMatrix_ITU_R_601_4,
36
+ &pixelRange,
37
+ outInfo,
38
+ inType,
39
+ outType,
40
+ kvImagePrintDiagnosticsToConsole);
41
+
42
+ _conversionInfo = outInfo;
43
+
44
+ return error;
45
+ }
46
+
47
+ - (void)unprepareForAccelerateConversion {
48
+ if (_conversionInfo != NULL) {
49
+ free(_conversionInfo);
50
+ _conversionInfo = NULL;
51
+ }
52
+ if (_pixelBufferPool != NULL) {
53
+ CVPixelBufferPoolRelease(_pixelBufferPool);
54
+ _pixelBufferPool = NULL;
55
+ }
56
+ }
57
+
58
+ - (void)createPixelBufferPoolWithWidth:(size_t)width height:(size_t)height {
59
+ if (_pixelBufferPool != NULL) {
60
+ CVPixelBufferPoolRelease(_pixelBufferPool);
61
+ }
62
+
63
+ _poolWidth = width;
64
+ _poolHeight = height;
65
+ NSDictionary *pixelBufferAttributes = @{
66
+ (id)kCVPixelBufferPixelFormatTypeKey : @(kCVPixelFormatType_32BGRA),
67
+ (id)kCVPixelBufferWidthKey : @(width),
68
+ (id)kCVPixelBufferHeightKey : @(height),
69
+ (id)kCVPixelBufferIOSurfacePropertiesKey : @{}
70
+ };
71
+
72
+ CVReturn ret = CVPixelBufferPoolCreate(
73
+ kCFAllocatorDefault, NULL, (__bridge CFDictionaryRef)pixelBufferAttributes, &_pixelBufferPool);
74
+
75
+ if (ret != kCVReturnSuccess) {
76
+ NSLog(@"Error creating pixel buffer pool: %d", ret);
77
+ _pixelBufferPool = NULL;
78
+ }
79
+ }
80
+
81
+ - (CVPixelBufferRef)convertI420ToPixelBuffer:(RTCI420Buffer *)buffer {
82
+ if (_conversionInfo == nil) {
83
+ NSLog(@"%@: not prepared", NSStringFromSelector(_cmd));
84
+ return NULL;
85
+ }
86
+
87
+ if (_pixelBufferPool == NULL || _poolWidth != buffer.width || _poolHeight != buffer.height) {
88
+ [self createPixelBufferPoolWithWidth:buffer.width height:buffer.height];
89
+ }
90
+
91
+ CVPixelBufferRef pixelBuffer = NULL;
92
+ CVReturn status = CVPixelBufferPoolCreatePixelBuffer(kCFAllocatorDefault, _pixelBufferPool, &pixelBuffer);
93
+
94
+ if (status != kCVReturnSuccess) {
95
+ return NULL;
96
+ }
97
+
98
+ vImage_Error err = [self convertFrameVImageYUV:buffer toBuffer:pixelBuffer];
99
+
100
+ if (err != kvImageNoError) {
101
+ NSLog(@"%@: error during conversion: %ld", NSStringFromSelector(_cmd), err);
102
+ CVPixelBufferRelease(pixelBuffer);
103
+ return NULL;
104
+ }
105
+
106
+ return pixelBuffer;
107
+ }
108
+
109
+ - (vImage_Error)convertFrameVImageYUV:(RTCI420Buffer *)buffer toBuffer:(CVPixelBufferRef)pixelBufferRef {
110
+ if (pixelBufferRef == NULL) {
111
+ return kvImageInvalidParameter;
112
+ }
113
+
114
+ // Compute info for I420 source.
115
+ vImagePixelCount width = buffer.width;
116
+ vImagePixelCount height = buffer.height;
117
+ vImagePixelCount subsampledWidth = buffer.chromaWidth;
118
+ vImagePixelCount subsampledHeight = buffer.chromaHeight;
119
+
120
+ const uint8_t *yPlane = buffer.dataY;
121
+ const uint8_t *uPlane = buffer.dataU;
122
+ const uint8_t *vPlane = buffer.dataV;
123
+ size_t yStride = (size_t)buffer.strideY;
124
+ size_t uStride = (size_t)buffer.strideU;
125
+ size_t vStride = (size_t)buffer.strideV;
126
+
127
+ // Create vImage buffers to represent each of the Y, U, and V planes
128
+ vImage_Buffer yPlaneBuffer = {.data = (void *)yPlane, .height = height, .width = width, .rowBytes = yStride};
129
+ vImage_Buffer uPlaneBuffer = {
130
+ .data = (void *)uPlane, .height = subsampledHeight, .width = subsampledWidth, .rowBytes = uStride};
131
+ vImage_Buffer vPlaneBuffer = {
132
+ .data = (void *)vPlane, .height = subsampledHeight, .width = subsampledWidth, .rowBytes = vStride};
133
+
134
+ // Create a vImage buffer for the destination pixel buffer.
135
+ CVPixelBufferLockBaseAddress(pixelBufferRef, 0);
136
+
137
+ void *pixelBufferData = CVPixelBufferGetBaseAddress(pixelBufferRef);
138
+ size_t rowBytes = CVPixelBufferGetBytesPerRow(pixelBufferRef);
139
+ vImage_Buffer destinationImageBuffer = {
140
+ .data = pixelBufferData, .height = height, .width = width, .rowBytes = rowBytes};
141
+
142
+ // Do the conversion.
143
+
144
+ uint8_t permuteMap[4] = {3, 2, 1, 0}; // BGRA
145
+ vImage_Error convertError = vImageConvert_420Yp8_Cb8_Cr8ToARGB8888(&yPlaneBuffer,
146
+ &uPlaneBuffer,
147
+ &vPlaneBuffer,
148
+ &destinationImageBuffer,
149
+ self.conversionInfo,
150
+ permuteMap,
151
+ 255,
152
+ kvImageNoFlags);
153
+
154
+ CVPixelBufferUnlockBaseAddress(pixelBufferRef, 0);
155
+
156
+ return convertError;
157
+ }
158
+
159
+ - (void)dealloc {
160
+ [self unprepareForAccelerateConversion];
161
+ CVPixelBufferPoolRelease(_pixelBufferPool);
162
+ }
163
+
164
+ @end
@@ -0,0 +1,24 @@
1
+ #import <AVKit/AVKit.h>
2
+ #import <UIKit/UIKit.h>
3
+ #import <WebRTC/RTCVideoTrack.h>
4
+
5
+ #import "RTCVideoViewManager.h"
6
+
7
+ API_AVAILABLE(ios(15.0))
8
+ @interface PIPController : NSObject<AVPictureInPictureControllerDelegate>
9
+
10
+ @property(nonatomic, weak) UIView *sourceView;
11
+ @property(nonatomic, strong) RTCVideoTrack *videoTrack;
12
+
13
+ @property(nonatomic, assign) BOOL startAutomatically;
14
+ @property(nonatomic, assign) BOOL stopAutomatically;
15
+ @property(nonatomic, assign) CGSize preferredSize;
16
+
17
+ - (instancetype)initWithSourceView:(UIView *)sourceView;
18
+ - (void)togglePIP;
19
+ - (void)startPIP;
20
+ - (void)stopPIP;
21
+ - (void)insertFallbackView:(UIView *)subview;
22
+ - (void)setObjectFit:(RTCVideoViewObjectFit)fit;
23
+
24
+ @end