react-native-suuqencode 0.1.0 → 0.1.2

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.
@@ -16,7 +16,7 @@ Pod::Spec.new do |s|
16
16
  s.source_files = "ios/**/*.{h,m,mm,cpp}"
17
17
  s.private_header_files = "ios/**/*.h"
18
18
  s.frameworks = "VideoToolbox", "CoreMedia", "CoreFoundation"
19
-
19
+ s.dependency "SuuqeDMABuf"
20
20
 
21
21
  install_modules_dependencies(s)
22
22
  end
package/ios/Suuqencode.mm CHANGED
@@ -1,10 +1,13 @@
1
1
  #import "Suuqencode.h"
2
+ #import <SuuqeDMABuf/DMABuf.h>
2
3
 
3
- @interface Suuqencode()
4
+ @interface Suuqencode ()
4
5
 
5
- @property (nonatomic) VTCompressionSessionRef compressionSession;
6
- @property (nonatomic) dispatch_queue_t encodeQueue;
7
- @property (nonatomic) int frameCount;
6
+ @property(nonatomic) VTCompressionSessionRef compressionSession;
7
+ @property(nonatomic) dispatch_queue_t encodeQueue;
8
+ @property(nonatomic) int frameCount;
9
+
10
+ - (void)sendEncodedData:(NSData *)data;
8
11
 
9
12
  @end
10
13
 
@@ -12,113 +15,132 @@
12
15
 
13
16
  RCT_EXPORT_MODULE()
14
17
 
15
- - (instancetype)init
16
- {
17
- self = [super init];
18
- if (self) {
19
- _encodeQueue = dispatch_queue_create("com.suuqencode.encodequeue", DISPATCH_QUEUE_SERIAL);
20
- }
21
- return self;
22
- }
23
-
24
- RCT_EXPORT_METHOD(encode:(NSString *)base64Bitmap width:(int)width height:(int)height)
25
- {
26
- dispatch_async(_encodeQueue, ^{
27
- if (!self.compressionSession) {
28
- [self setupCompressionSessionWithWidth:width height:height];
29
- }
30
-
31
- NSData *bitmapData = [[NSData alloc] initWithBase64EncodedString:base64Bitmap options:0];
32
- if (!bitmapData) {
33
- NSLog(@"Invalid base64 bitmap string");
34
- return;
35
- }
36
-
37
- CVPixelBufferRef pixelBuffer = NULL;
38
- CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, width, height, kCVPixelFormatType_32ARGB, nil, &pixelBuffer);
39
- if (status != kCVReturnSuccess) {
40
- NSLog(@"Failed to create CVPixelBuffer");
41
- return;
42
- }
43
-
44
- CVPixelBufferLockBaseAddress(pixelBuffer, 0);
45
- void *pixelData = CVPixelBufferGetBaseAddress(pixelBuffer);
46
- memcpy(pixelData, [bitmapData bytes], [bitmapData length]);
47
- CVPixelBufferUnlockBaseAddress(pixelBuffer, 0);
48
-
49
- CMTime presentationTimeStamp = CMTimeMake(self.frameCount++, 30);
50
- VTEncodeInfoFlags flags;
51
-
52
- VTCompressionSessionEncodeFrame(self.compressionSession, pixelBuffer, presentationTimeStamp, kCMTimeInvalid, NULL, NULL, &flags);
53
- CVPixelBufferRelease(pixelBuffer);
54
- });
55
- }
56
-
57
- - (void)setupCompressionSessionWithWidth:(int)width height:(int)height {
58
- VTCompressionSessionCreate(NULL, width, height, kCMVideoCodecType_H264, NULL, NULL, NULL, compressionOutputCallback, (__bridge void *)(self), &_compressionSession);
59
-
60
- VTSessionSetProperty(_compressionSession, kVTCompressionPropertyKey_RealTime, kCFBooleanTrue);
61
- VTSessionSetProperty(_compressionSession, kVTCompressionPropertyKey_ProfileLevel, kVTProfileLevel_H264_Baseline_AutoLevel);
62
- VTSessionSetProperty(_compressionSession, kVTCompressionPropertyKey_AverageBitRate, (__bridge CFTypeRef)@(width * height * 10));
63
- VTSessionSetProperty(_compressionSession, kVTCompressionPropertyKey_MaxKeyFrameInterval, (__bridge CFTypeRef)@(20));
64
-
65
- VTCompressionSessionPrepareToEncodeFrames(_compressionSession);
18
+ - (instancetype)init {
19
+ self = [super init];
20
+ if (self) {
21
+ _encodeQueue = dispatch_queue_create("com.suuqencode.encodequeue",
22
+ DISPATCH_QUEUE_SERIAL);
23
+ }
24
+ return self;
66
25
  }
67
26
 
68
- void compressionOutputCallback(void *outputCallbackRefCon, void *sourceFrameRefCon, OSStatus status, VTEncodeInfoFlags infoFlags, CMSampleBufferRef sampleBuffer) {
69
- if (status != noErr) {
70
- NSLog(@"Error encoding frame: %d", (int)status);
71
- return;
72
- }
27
+ RCT_EXPORT_METHOD(startEncode) {
28
+ [DMABuf setFrameChangeCallback:^{
29
+ void *buf = [DMABuf buf];
30
+ int width = [DMABuf width];
31
+ int height = [DMABuf height];
73
32
 
74
- if (!CMSampleBufferDataIsReady(sampleBuffer)) {
75
- return;
33
+ if (!buf) {
34
+ return;
76
35
  }
77
36
 
78
- Suuqencode *encoder = (__bridge Suuqencode *)outputCallbackRefCon;
37
+ dispatch_async(self.encodeQueue, ^{
38
+ if (!self.compressionSession) {
39
+ [self setupCompressionSessionWithWidth:width height:height];
40
+ }
79
41
 
80
- bool isKeyFrame = !CFDictionaryContainsKey( (CFDictionaryRef)CFArrayGetValueAtIndex(CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, true), 0), (const void *)kCMSampleAttachmentKey_NotSync);
42
+ CVPixelBufferRef pixelBuffer = NULL;
43
+ CVReturn status = CVPixelBufferCreateWithBytes(
44
+ kCFAllocatorDefault, width, height, kCVPixelFormatType_32BGRA, buf,
45
+ width * 4, NULL, NULL, NULL, &pixelBuffer);
81
46
 
82
- if (isKeyFrame)
83
- {
84
- CMFormatDescriptionRef format = CMSampleBufferGetFormatDescription(sampleBuffer);
85
- const uint8_t *sparameterSet;
86
- size_t sparameterSetSize, sparameterSetCount;
87
- CMVideoFormatDescriptionGetH264ParameterSetAtIndex(format, 0, &sparameterSet, &sparameterSetSize, &sparameterSetCount, 0 );
88
-
89
- const uint8_t *pparameterSet;
90
- size_t pparameterSetSize, pparameterSetCount;
91
- CMVideoFormatDescriptionGetH264ParameterSetAtIndex(format, 1, &pparameterSet, &pparameterSetSize, &pparameterSetCount, 0 );
47
+ if (status != kCVReturnSuccess) {
48
+ NSLog(@"Failed to create CVPixelBuffer");
49
+ return;
50
+ }
92
51
 
93
- NSData *sps = [NSData dataWithBytes:sparameterSet length:sparameterSetSize];
94
- NSData *pps = [NSData dataWithBytes:pparameterSet length:pparameterSetSize];
52
+ CMTime presentationTimeStamp = CMTimeMake(self.frameCount++, 30);
53
+ VTEncodeInfoFlags flags;
95
54
 
96
- [encoder sendEncodedData:sps];
97
- [encoder sendEncodedData:pps];
98
- }
55
+ VTCompressionSessionEncodeFrame(self.compressionSession, pixelBuffer,
56
+ presentationTimeStamp, kCMTimeInvalid,
57
+ NULL, NULL, &flags);
58
+ CVPixelBufferRelease(pixelBuffer);
59
+ });
60
+ }];
61
+ }
99
62
 
100
- CMBlockBufferRef dataBuffer = CMSampleBufferGetDataBuffer(sampleBuffer);
101
- size_t length, totalLength;
102
- char *dataPointer;
103
- CMBlockBufferGetDataPointer(dataBuffer, 0, &length, &totalLength, &dataPointer);
63
+ - (void)setupCompressionSessionWithWidth:(int)width height:(int)height {
64
+ VTCompressionSessionCreate(NULL, width, height, kCMVideoCodecType_H264, NULL,
65
+ NULL, NULL, compressionOutputCallback,
66
+ (__bridge void *)(self), &_compressionSession);
67
+
68
+ VTSessionSetProperty(_compressionSession, kVTCompressionPropertyKey_RealTime,
69
+ kCFBooleanTrue);
70
+ VTSessionSetProperty(_compressionSession,
71
+ kVTCompressionPropertyKey_ProfileLevel,
72
+ kVTProfileLevel_H264_Baseline_AutoLevel);
73
+ VTSessionSetProperty(_compressionSession,
74
+ kVTCompressionPropertyKey_AverageBitRate,
75
+ (__bridge CFTypeRef) @(width * height * 10));
76
+ VTSessionSetProperty(_compressionSession,
77
+ kVTCompressionPropertyKey_MaxKeyFrameInterval,
78
+ (__bridge CFTypeRef) @(20));
79
+
80
+ VTCompressionSessionPrepareToEncodeFrames(_compressionSession);
81
+ }
104
82
 
105
- NSData *naluData = [NSData dataWithBytes:dataPointer length:length];
106
- [encoder sendEncodedData:naluData];
83
+ void compressionOutputCallback(void *outputCallbackRefCon,
84
+ void *sourceFrameRefCon, OSStatus status,
85
+ VTEncodeInfoFlags infoFlags,
86
+ CMSampleBufferRef sampleBuffer) {
87
+ if (status != noErr) {
88
+ NSLog(@"Error encoding frame: %d", (int)status);
89
+ return;
90
+ }
91
+
92
+ if (!CMSampleBufferDataIsReady(sampleBuffer)) {
93
+ return;
94
+ }
95
+
96
+ Suuqencode *encoder = (__bridge Suuqencode *)outputCallbackRefCon;
97
+
98
+ bool isKeyFrame = !CFDictionaryContainsKey(
99
+ (CFDictionaryRef)CFArrayGetValueAtIndex(
100
+ CMSampleBufferGetSampleAttachmentsArray(sampleBuffer, true), 0),
101
+ (const void *)kCMSampleAttachmentKey_NotSync);
102
+
103
+ if (isKeyFrame) {
104
+ CMFormatDescriptionRef format =
105
+ CMSampleBufferGetFormatDescription(sampleBuffer);
106
+ const uint8_t *sparameterSet;
107
+ size_t sparameterSetSize, sparameterSetCount;
108
+ CMVideoFormatDescriptionGetH264ParameterSetAtIndex(
109
+ format, 0, &sparameterSet, &sparameterSetSize, &sparameterSetCount, 0);
110
+
111
+ const uint8_t *pparameterSet;
112
+ size_t pparameterSetSize, pparameterSetCount;
113
+ CMVideoFormatDescriptionGetH264ParameterSetAtIndex(
114
+ format, 1, &pparameterSet, &pparameterSetSize, &pparameterSetCount, 0);
115
+
116
+ NSData *sps = [NSData dataWithBytes:sparameterSet length:sparameterSetSize];
117
+ NSData *pps = [NSData dataWithBytes:pparameterSet length:pparameterSetSize];
118
+
119
+ [encoder sendEncodedData:sps];
120
+ [encoder sendEncodedData:pps];
121
+ }
122
+
123
+ CMBlockBufferRef dataBuffer = CMSampleBufferGetDataBuffer(sampleBuffer);
124
+ size_t length, totalLength;
125
+ char *dataPointer;
126
+ CMBlockBufferGetDataPointer(dataBuffer, 0, &length, &totalLength,
127
+ &dataPointer);
128
+
129
+ NSData *naluData = [NSData dataWithBytes:dataPointer length:length];
130
+ [encoder sendEncodedData:naluData];
107
131
  }
108
132
 
109
133
  - (void)sendEncodedData:(NSData *)data {
110
- NSString *base64Encoded = [data base64EncodedStringWithOptions:0];
111
- [self sendEventWithName:@"onEncodedData" body:base64Encoded];
134
+ NSString *base64Encoded = [data base64EncodedStringWithOptions:0];
135
+ [self sendEventWithName:@"onEncodedData" body:base64Encoded];
112
136
  }
113
137
 
114
- - (NSArray<NSString *> *)supportedEvents
115
- {
116
- return @[@"onEncodedData"];
138
+ - (NSArray<NSString *> *)supportedEvents {
139
+ return @[ @"onEncodedData" ];
117
140
  }
118
141
 
119
- + (BOOL)requiresMainQueueSetup
120
- {
121
- return NO;
142
+ + (BOOL)requiresMainQueueSetup {
143
+ return NO;
122
144
  }
123
145
 
124
146
  @end
@@ -1,6 +1,6 @@
1
1
  import { type TurboModule } from 'react-native';
2
2
  export interface Spec extends TurboModule {
3
- encode(base64Bitmap: string, width: number, height: number): void;
3
+ startEncode(): void;
4
4
  addListener(eventName: string): void;
5
5
  removeListeners(count: number): void;
6
6
  }
@@ -1 +1 @@
1
- {"version":3,"file":"NativeSuuqencode.d.ts","sourceRoot":"","sources":["../../../src/NativeSuuqencode.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,KAAK,WAAW,EAAE,MAAM,cAAc,CAAC;AAErE,MAAM,WAAW,IAAK,SAAQ,WAAW;IACvC,MAAM,CAAC,YAAY,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IAClE,WAAW,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACrC,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CACtC;;AAED,wBAAoE"}
1
+ {"version":3,"file":"NativeSuuqencode.d.ts","sourceRoot":"","sources":["../../../src/NativeSuuqencode.ts"],"names":[],"mappings":"AAAA,OAAO,EAAuB,KAAK,WAAW,EAAE,MAAM,cAAc,CAAC;AAErE,MAAM,WAAW,IAAK,SAAQ,WAAW;IACvC,WAAW,IAAI,IAAI,CAAC;IACpB,WAAW,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAAC;IACrC,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;CACtC;;AAED,wBAAoE"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-suuqencode",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "h264 hardware encode support for iOS, developed by Suuqe Llc.",
5
5
  "main": "./lib/module/index.js",
6
6
  "types": "./lib/typescript/src/index.d.ts",
@@ -1,7 +1,7 @@
1
1
  import { TurboModuleRegistry, type TurboModule } from 'react-native';
2
2
 
3
3
  export interface Spec extends TurboModule {
4
- encode(base64Bitmap: string, width: number, height: number): void;
4
+ startEncode(): void;
5
5
  addListener(eventName: string): void;
6
6
  removeListeners(count: number): void;
7
7
  }