|
3 | 3 | #import "DFPrivateSimulatorDisplayBridge.h" |
4 | 4 | #import "XCWAccessibilityBridge.h" |
5 | 5 | #import "XCWChromeRenderer.h" |
| 6 | +#import "XCWH264Encoder.h" |
6 | 7 | #import "XCWNativeSession.h" |
7 | 8 | #import "XCWSimctl.h" |
8 | 9 |
|
9 | 10 | #import <AppKit/AppKit.h> |
10 | 11 | #import <CoreFoundation/CoreFoundation.h> |
| 12 | +#import <CoreVideo/CoreVideo.h> |
11 | 13 | #include <stdlib.h> |
12 | 14 | #include <string.h> |
13 | 15 |
|
@@ -63,10 +65,190 @@ static xcw_native_owned_bytes XCWOwnedBytesFromData(NSData *data) { |
63 | 65 | return bytes; |
64 | 66 | } |
65 | 67 |
|
| 68 | +static xcw_native_shared_bytes XCWSharedBytesFromData(NSData *data) { |
| 69 | + if (data.length == 0) { |
| 70 | + return (xcw_native_shared_bytes){0}; |
| 71 | + } |
| 72 | + |
| 73 | + CFTypeRef owner = CFRetain((__bridge CFTypeRef)data); |
| 74 | + return (xcw_native_shared_bytes){ |
| 75 | + .data = data.bytes, |
| 76 | + .length = data.length, |
| 77 | + .owner = (const void *)owner, |
| 78 | + }; |
| 79 | +} |
| 80 | + |
66 | 81 | static XCWNativeSession *XCWNativeSessionFromHandle(void *handle) { |
67 | 82 | return (__bridge XCWNativeSession *)handle; |
68 | 83 | } |
69 | 84 |
|
| 85 | +@interface XCWNativeH264Encoder : NSObject |
| 86 | + |
| 87 | +- (instancetype)initWithFrameCallback:(xcw_native_frame_callback)callback |
| 88 | + userData:(void *)userData; |
| 89 | +- (BOOL)encodeRGBA:(const uint8_t *)rgba |
| 90 | + length:(size_t)length |
| 91 | + width:(uint32_t)width |
| 92 | + height:(uint32_t)height |
| 93 | + error:(NSError * _Nullable __autoreleasing *)error; |
| 94 | +- (void)requestKeyFrame; |
| 95 | +- (void)invalidate; |
| 96 | + |
| 97 | +@end |
| 98 | + |
| 99 | +@implementation XCWNativeH264Encoder { |
| 100 | + XCWH264Encoder *_encoder; |
| 101 | + xcw_native_frame_callback _callback; |
| 102 | + void *_callbackUserData; |
| 103 | + uint64_t _frameSequence; |
| 104 | +} |
| 105 | + |
| 106 | +- (instancetype)initWithFrameCallback:(xcw_native_frame_callback)callback |
| 107 | + userData:(void *)userData { |
| 108 | + self = [super init]; |
| 109 | + if (self == nil) { |
| 110 | + return nil; |
| 111 | + } |
| 112 | + |
| 113 | + _callback = callback; |
| 114 | + _callbackUserData = userData; |
| 115 | + __weak typeof(self) weakSelf = self; |
| 116 | + @synchronized (XCWNativeH264Encoder.class) { |
| 117 | + const char *previousCodec = getenv("SIMDECK_VIDEO_CODEC"); |
| 118 | + char *previousCodecCopy = previousCodec != NULL ? strdup(previousCodec) : NULL; |
| 119 | + const char *androidCodec = getenv("SIMDECK_ANDROID_VIDEO_CODEC"); |
| 120 | + if (androidCodec == NULL || strlen(androidCodec) == 0) { |
| 121 | + androidCodec = "software"; |
| 122 | + } |
| 123 | + setenv("SIMDECK_VIDEO_CODEC", androidCodec, 1); |
| 124 | + _encoder = [[XCWH264Encoder alloc] initWithOutputHandler:^(NSData *sampleData, |
| 125 | + uint64_t timestampUs, |
| 126 | + BOOL isKeyFrame, |
| 127 | + NSString * _Nullable codec, |
| 128 | + NSData * _Nullable decoderConfig, |
| 129 | + CGSize dimensions) { |
| 130 | + __strong typeof(weakSelf) strongSelf = weakSelf; |
| 131 | + if (strongSelf == nil || strongSelf->_callback == NULL || sampleData.length == 0) { |
| 132 | + return; |
| 133 | + } |
| 134 | + strongSelf->_frameSequence += 1; |
| 135 | + xcw_native_frame frame = { |
| 136 | + .frame_sequence = strongSelf->_frameSequence, |
| 137 | + .timestamp_us = timestampUs, |
| 138 | + .is_keyframe = isKeyFrame, |
| 139 | + .width = (uint32_t)llround(dimensions.width), |
| 140 | + .height = (uint32_t)llround(dimensions.height), |
| 141 | + .codec = codec.UTF8String, |
| 142 | + .description = XCWSharedBytesFromData(decoderConfig), |
| 143 | + .data = XCWSharedBytesFromData(sampleData), |
| 144 | + }; |
| 145 | + strongSelf->_callback(&frame, strongSelf->_callbackUserData); |
| 146 | + }]; |
| 147 | + if (previousCodecCopy != NULL) { |
| 148 | + setenv("SIMDECK_VIDEO_CODEC", previousCodecCopy, 1); |
| 149 | + free(previousCodecCopy); |
| 150 | + } else { |
| 151 | + unsetenv("SIMDECK_VIDEO_CODEC"); |
| 152 | + } |
| 153 | + } |
| 154 | + return self; |
| 155 | +} |
| 156 | + |
| 157 | +- (void)dealloc { |
| 158 | + [self invalidate]; |
| 159 | +} |
| 160 | + |
| 161 | +- (BOOL)encodeRGBA:(const uint8_t *)rgba |
| 162 | + length:(size_t)length |
| 163 | + width:(uint32_t)width |
| 164 | + height:(uint32_t)height |
| 165 | + error:(NSError * _Nullable __autoreleasing *)error { |
| 166 | + if (rgba == NULL || width == 0 || height == 0) { |
| 167 | + if (error != NULL) { |
| 168 | + *error = [NSError errorWithDomain:@"SimDeck.NativeH264Encoder" |
| 169 | + code:1 |
| 170 | + userInfo:@{ NSLocalizedDescriptionKey: @"RGBA frame input was empty." }]; |
| 171 | + } |
| 172 | + return NO; |
| 173 | + } |
| 174 | + size_t expectedLength = (size_t)width * (size_t)height * 4; |
| 175 | + if (length < expectedLength) { |
| 176 | + if (error != NULL) { |
| 177 | + *error = [NSError errorWithDomain:@"SimDeck.NativeH264Encoder" |
| 178 | + code:2 |
| 179 | + userInfo:@{ NSLocalizedDescriptionKey: @"RGBA frame input was truncated." }]; |
| 180 | + } |
| 181 | + return NO; |
| 182 | + } |
| 183 | + |
| 184 | + NSDictionary *attributes = @{ |
| 185 | + (__bridge NSString *)kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_32BGRA), |
| 186 | + (__bridge NSString *)kCVPixelBufferWidthKey: @(width), |
| 187 | + (__bridge NSString *)kCVPixelBufferHeightKey: @(height), |
| 188 | + (__bridge NSString *)kCVPixelBufferIOSurfacePropertiesKey: @{}, |
| 189 | + }; |
| 190 | + CVPixelBufferRef pixelBuffer = NULL; |
| 191 | + CVReturn createStatus = CVPixelBufferCreate(kCFAllocatorDefault, |
| 192 | + (size_t)width, |
| 193 | + (size_t)height, |
| 194 | + kCVPixelFormatType_32BGRA, |
| 195 | + (__bridge CFDictionaryRef)attributes, |
| 196 | + &pixelBuffer); |
| 197 | + if (createStatus != kCVReturnSuccess || pixelBuffer == NULL) { |
| 198 | + if (error != NULL) { |
| 199 | + *error = [NSError errorWithDomain:@"SimDeck.NativeH264Encoder" |
| 200 | + code:createStatus |
| 201 | + userInfo:@{ NSLocalizedDescriptionKey: @"Unable to allocate a VideoToolbox pixel buffer." }]; |
| 202 | + } |
| 203 | + return NO; |
| 204 | + } |
| 205 | + |
| 206 | + CVReturn lockStatus = CVPixelBufferLockBaseAddress(pixelBuffer, 0); |
| 207 | + if (lockStatus != kCVReturnSuccess) { |
| 208 | + CVPixelBufferRelease(pixelBuffer); |
| 209 | + if (error != NULL) { |
| 210 | + *error = [NSError errorWithDomain:@"SimDeck.NativeH264Encoder" |
| 211 | + code:lockStatus |
| 212 | + userInfo:@{ NSLocalizedDescriptionKey: @"Unable to lock a VideoToolbox pixel buffer." }]; |
| 213 | + } |
| 214 | + return NO; |
| 215 | + } |
| 216 | + |
| 217 | + uint8_t *dst = CVPixelBufferGetBaseAddress(pixelBuffer); |
| 218 | + size_t dstRowBytes = CVPixelBufferGetBytesPerRow(pixelBuffer); |
| 219 | + size_t srcRowBytes = (size_t)width * 4; |
| 220 | + for (uint32_t y = 0; y < height; y += 1) { |
| 221 | + const uint8_t *srcRow = rgba + ((size_t)y * srcRowBytes); |
| 222 | + uint8_t *dstRow = dst + ((size_t)y * dstRowBytes); |
| 223 | + for (uint32_t x = 0; x < width; x += 1) { |
| 224 | + const uint8_t *src = srcRow + ((size_t)x * 4); |
| 225 | + uint8_t *pixel = dstRow + ((size_t)x * 4); |
| 226 | + pixel[0] = src[2]; |
| 227 | + pixel[1] = src[1]; |
| 228 | + pixel[2] = src[0]; |
| 229 | + pixel[3] = src[3]; |
| 230 | + } |
| 231 | + } |
| 232 | + CVPixelBufferUnlockBaseAddress(pixelBuffer, 0); |
| 233 | + [_encoder encodePixelBuffer:pixelBuffer]; |
| 234 | + CVPixelBufferRelease(pixelBuffer); |
| 235 | + return YES; |
| 236 | +} |
| 237 | + |
| 238 | +- (void)requestKeyFrame { |
| 239 | + [_encoder requestKeyFrame]; |
| 240 | +} |
| 241 | + |
| 242 | +- (void)invalidate { |
| 243 | + [_encoder invalidate]; |
| 244 | +} |
| 245 | + |
| 246 | +@end |
| 247 | + |
| 248 | +static XCWNativeH264Encoder *XCWNativeH264EncoderFromHandle(void *handle) { |
| 249 | + return (__bridge XCWNativeH264Encoder *)handle; |
| 250 | +} |
| 251 | + |
70 | 252 | static BOOL XCWPerformSimctlAction(char **errorMessage, BOOL (^action)(XCWSimctl *simctl, NSError **error)) { |
71 | 253 | XCWSimctl *simctl = [[XCWSimctl alloc] init]; |
72 | 254 | NSError *error = nil; |
@@ -916,6 +1098,58 @@ void xcw_native_session_set_frame_callback(void *handle, xcw_native_frame_callba |
916 | 1098 | } |
917 | 1099 | } |
918 | 1100 |
|
| 1101 | +void *xcw_native_h264_encoder_create(xcw_native_frame_callback callback, void *user_data, char **error_message) { |
| 1102 | + @autoreleasepool { |
| 1103 | + XCWNativeH264Encoder *encoder = [[XCWNativeH264Encoder alloc] initWithFrameCallback:callback |
| 1104 | + userData:user_data]; |
| 1105 | + if (encoder == nil) { |
| 1106 | + if (error_message != NULL) { |
| 1107 | + *error_message = XCWCopyCString(@"Unable to create the native H.264 encoder."); |
| 1108 | + } |
| 1109 | + return NULL; |
| 1110 | + } |
| 1111 | + return (__bridge_retained void *)encoder; |
| 1112 | + } |
| 1113 | +} |
| 1114 | + |
| 1115 | +void xcw_native_h264_encoder_destroy(void *handle) { |
| 1116 | + if (handle == NULL) { |
| 1117 | + return; |
| 1118 | + } |
| 1119 | + @autoreleasepool { |
| 1120 | + XCWNativeH264Encoder *encoder = CFBridgingRelease(handle); |
| 1121 | + [encoder invalidate]; |
| 1122 | + } |
| 1123 | +} |
| 1124 | + |
| 1125 | +bool xcw_native_h264_encoder_encode_rgba(void *handle, |
| 1126 | + const uint8_t *rgba, |
| 1127 | + size_t length, |
| 1128 | + uint32_t width, |
| 1129 | + uint32_t height, |
| 1130 | + uint64_t timestamp_us, |
| 1131 | + char **error_message) { |
| 1132 | + (void)timestamp_us; |
| 1133 | + @autoreleasepool { |
| 1134 | + NSError *error = nil; |
| 1135 | + BOOL ok = [XCWNativeH264EncoderFromHandle(handle) encodeRGBA:rgba |
| 1136 | + length:length |
| 1137 | + width:width |
| 1138 | + height:height |
| 1139 | + error:&error]; |
| 1140 | + if (!ok) { |
| 1141 | + XCWSetErrorMessage(error_message, error); |
| 1142 | + } |
| 1143 | + return ok; |
| 1144 | + } |
| 1145 | +} |
| 1146 | + |
| 1147 | +void xcw_native_h264_encoder_request_keyframe(void *handle) { |
| 1148 | + @autoreleasepool { |
| 1149 | + [XCWNativeH264EncoderFromHandle(handle) requestKeyFrame]; |
| 1150 | + } |
| 1151 | +} |
| 1152 | + |
919 | 1153 | void xcw_native_free_string(char *value) { |
920 | 1154 | if (value != NULL) { |
921 | 1155 | free(value); |
|
0 commit comments