iOS Brooadcast Загрузить расширение поделиться CMSampleBufferRef to APP вопрос - PullRequest
0 голосов
/ 16 мая 2019

У меня есть расширение Brooadcast, я надеюсь получить CMSampleBufferRef из расширения Broadadcast, приложение для обработки, как мне поделиться CMSampleBufferRef?

Я пытался преобразовать CMSampleBufferRef в локальное изображение в Brooadcastрасширение и считывать локальное изображение из приложения, но это иногда приводит к сбою расширения Brooadcast.

это мой конвертированный CMSampleBufferRef в код изображения

+ (UIImage*)getImageWithSampleBuffer:(CMSampleBufferRef)sampleBuffer{
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    CVPixelBufferLockBaseAddress(imageBuffer,0);

    size_t width = CVPixelBufferGetWidth(imageBuffer);
    size_t height = CVPixelBufferGetHeight(imageBuffer);
    uint8_t *yBuffer = CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 0);
    size_t yPitch = CVPixelBufferGetBytesPerRowOfPlane(imageBuffer, 0);
    uint8_t *cbCrBuffer = CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 1);
    size_t cbCrPitch = CVPixelBufferGetBytesPerRowOfPlane(imageBuffer, 1);

    int bytesPerPixel = 4;
    uint8_t *rgbBuffer = malloc(width * height * bytesPerPixel);

    for(int y = 0; y < height; y++) {
        uint8_t *rgbBufferLine = &rgbBuffer[y * width * bytesPerPixel];
        uint8_t *yBufferLine = &yBuffer[y * yPitch];
        uint8_t *cbCrBufferLine = &cbCrBuffer[(y >> 1) * cbCrPitch];

        for(int x = 0; x < width; x++) {
            int16_t y = yBufferLine[x];
            int16_t cb = cbCrBufferLine[x & ~1] - 128;
            int16_t cr = cbCrBufferLine[x | 1] - 128;

            uint8_t *rgbOutput = &rgbBufferLine[x*bytesPerPixel];

            int16_t r = (int16_t)roundf( y + cr *  1.4 );
            int16_t g = (int16_t)roundf( y + cb * -0.343 + cr * -0.711 );
            int16_t b = (int16_t)roundf( y + cb *  1.765);

            rgbOutput[0] = 0xff;
            rgbOutput[1] = clamp(b);
            rgbOutput[2] = clamp(g);
            rgbOutput[3] = clamp(r);
        }
    }

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(rgbBuffer, width, height, 8, width * bytesPerPixel, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
    CGImageRef quartzImage = CGBitmapContextCreateImage(context);
    UIImage *image = [UIImage imageWithCGImage:quartzImage];

    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    CGImageRelease(quartzImage);
    free(rgbBuffer);

    CVPixelBufferUnlockBaseAddress(imageBuffer, 0);

    return image;
}

это мой сохраненный UIImage в локальныйКод изображения

- (BOOL)writeMessageObject:(UIImage*)object forIdentifier:(NSString *)identifier {
    if (identifier == nil) {
        return NO;
    }
    if (object) {
        _count++;
        NSData *data = UIImagePNGRepresentation(object);
        NSString *filePath = [self filePathForIdentifier:identifier];
        NSURL *fileURL = [NSURL fileURLWithPath:filePath];

        if (data == nil || filePath == nil || fileURL == nil) {
            return NO;
        }
        NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
        NSError *error = nil;
        __block BOOL success = NO;

        [fileCoordinator
         coordinateWritingItemAtURL:fileURL
         options:0
         error:&error
         byAccessor:^(NSURL *newURL) {
             NSError *writeError = nil;

             success = [data writeToURL:newURL
                                options:NSDataWritingAtomic
                                  error:&writeError];
         }];
        if (!success) {
            return NO;
        }
    }
    return YES;
}

Это то место, где я его использую

- (void)loopWriteImage {
    @autoreleasepool {
        if (self.sampleBuffer) {
            CMSampleBufferRef m_sampleBuffer = nil;
            CMSampleBufferCreateCopy(kCFAllocatorDefault, self.sampleBuffer, &m_sampleBuffer);
            UIImage *newImage = [MXSampleBufferManager getImageWithSampleBuffer:self.sampleBuffer];
            if ([self writeMessageObject:newImage forIdentifier:self.identifier]) {
                NSLog(@"write success ----------");
                CFRelease(m_sampleBuffer);
                [self sendNotificationForMessageWithKey:MOXIE_AI_AUTH_PROCESS];
            }
        }
        [self sleepWithWiriteFile];
    }
}

Я наблюдал за использованием памяти работающей программой, стабильно на 20M, но это иногда приводило к сбою расширения Brooadcast.

Я подозреваю, что это проблема с преобразованием CMSampleBufferRef или проблема с записью локальных изображений.

это сбой Снимок экрана

1557974541700.jpg

это запись о сбое

Incident Identifier: C7FB828A-CC40-444C-B339-1291B38E6C01
CrashReporter Key:   fba2dd9c0edf0441dd7d28a8dce25bf64503db21
Hardware Model:      iPhone7,2
Process:             BroadCast [939]
Path:                /private/var/containers/Bundle/Application/EFDF7F86-2647-4FE8-9C8F-964C3882614D/BroadCastDemo.app/PlugIns/BroadCast.appex/BroadCast
Identifier:          com.sandsyu.BroadCastDemo.BroadCast
Version:             1 (1.0)
Code Type:           ARM-64 (Native)
Role:                Foreground
Parent Process:      launchd [1]
Coalition:           com.sandsyu.BroadCastDemo.BroadCast [562]


Date/Time:           2019-05-13 20:13:25.6584 +0800
Launch Time:         2019-05-13 20:13:00.0817 +0800
OS Version:          iPhone OS 12.0 (16A366)
Baseband Version:    7.10.00
Report Version:      104

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Subtype: KERN_INVALID_ADDRESS at 0x0000000102332840
VM Region Info: 0x102332840 is not in any region.  Bytes after previous region: 1026113  Bytes before following region: 2086848
      REGION TYPE                      START - END             [ VSIZE] PRT/MAX SHRMOD  REGION DETAIL
      mapped file            0000000100808000-0000000102238000 [ 26.2M] r--/r-- SM=ALI  ...t_id=ac9beccf
--->  GAP OF 0x2f8000 BYTES
      MALLOC_LARGE           0000000102530000-0000000102904000 [ 3920K] rw-/rwx SM=PRV  

Termination Signal: Segmentation fault: 11
Termination Reason: Namespace SIGNAL, Code 0xb
Terminating Process: exc handler [939]
Triggered by Thread:  4

Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0:
0   libsystem_kernel.dylib          0x000000022e709ed0 0x22e6f2000 + 98000
1   libsystem_kernel.dylib          0x000000022e7093a8 0x22e6f2000 + 95144
2   CoreFoundation                  0x000000022eb0dfb0 0x22ea62000 + 704432
3   CoreFoundation                  0x000000022eb08e4c 0x22ea62000 + 683596
4   CoreFoundation                  0x000000022eb085b8 0x22ea62000 + 681400
5   GraphicsServices                0x0000000230d7c584 0x230d71000 + 46468
6   UIKitCore                       0x000000025b403558 0x25b09d000 + 3564888
7   libxpc.dylib                    0x000000022e7da098 0x22e7c6000 + 82072
8   libxpc.dylib                    0x000000022e7dca30 0x22e7c6000 + 92720
9   Foundation                      0x000000022f53a050 0x22f4f4000 + 286800
10  PlugInKit                       0x0000000234739498 0x234726000 + 79000
11  PlugInKit                       0x00000002347392c4 0x234726000 + 78532
12  PlugInKit                       0x0000000234739848 0x234726000 + 79944
13  Foundation                      0x000000022f7218bc 0x22f4f4000 + 2283708
14  libdyld.dylib                   0x000000022e5c8b94 0x22e5c8000 + 2964

Thread 1:
0   libsystem_pthread.dylib         0x000000022e79ccfc 0x22e78e000 + 60668

Thread 2:
0   libsystem_pthread.dylib         0x000000022e79ccfc 0x22e78e000 + 60668

Thread 3 name:  com.apple.uikit.eventfetch-thread
Thread 3:
0   libsystem_kernel.dylib          0x000000022e709ed0 0x22e6f2000 + 98000
1   libsystem_kernel.dylib          0x000000022e7093a8 0x22e6f2000 + 95144
2   CoreFoundation                  0x000000022eb0dfb0 0x22ea62000 + 704432
3   CoreFoundation                  0x000000022eb08e4c 0x22ea62000 + 683596
4   CoreFoundation                  0x000000022eb085b8 0x22ea62000 + 681400
5   Foundation                      0x000000022f4fc6a4 0x22f4f4000 + 34468
6   Foundation                      0x000000022f4fc514 0x22f4f4000 + 34068
7   UIKitCore                       0x000000025b360ac0 0x25b09d000 + 2898624
8   Foundation                      0x000000022f62f3b0 0x22f4f4000 + 1291184
9   libsystem_pthread.dylib         0x000000022e7992fc 0x22e78e000 + 45820
10  libsystem_pthread.dylib         0x000000022e79925c 0x22e78e000 + 45660
11  libsystem_pthread.dylib         0x000000022e79cd08 0x22e78e000 + 60680

Thread 4 name:  Dispatch queue: com.apple.root.default-qos
Thread 4 Crashed:
0   BroadCast               0x00000001004d339c 0x1004cc000 + 29596
1   BroadCast               0x00000001004d2ba4 0x1004cc000 + 27556
2   libdispatch.dylib               0x000000022e5b8484 0x22e557000 + 398468
3   libdispatch.dylib               0x000000022e55b874 0x22e557000 + 18548
4   libdispatch.dylib               0x000000022e56bbe4 0x22e557000 + 84964
5   libdispatch.dylib               0x000000022e55b3a8 0x22e557000 + 17320
6   libdispatch.dylib               0x000000022e567a60 0x22e557000 + 68192
7   libdispatch.dylib               0x000000022e568308 0x22e557000 + 70408
8   libsystem_pthread.dylib         0x000000022e79a190 0x22e78e000 + 49552
9   libsystem_pthread.dylib         0x000000022e79cd00 0x22e78e000 + 60672
...