您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

多媒體編程:iOS 影片級的畫面渲染工具類

編輯:iOS 開發綜合

要在 iOS 上把每秒二三十張的影像畫到螢幕上,直覺會想用 UIView 的 drawRect:,但這個方法是被動的:它由主執行緒在系統認為合適的時機呼叫,你無法指定「現在立刻畫這一帧」,也拿不到「畫完了」的時點。

硬把 drawRect 改成主動的土法子

  1. 把影像 pointer 存進 UIView 的屬性裡暫存。
  2. 呼叫 setNeedsDisplay 把重繪旗標打開。
  3. 在播放執行緒呼叫 performSelectorOnMainThread:...waitUntilDone:YES,執行一個自訂方法(例如 mydrawImage)。
  4. 在該方法內呼叫 [[NSRunLoop mainRunLoop] run],手動抽一次訊息泵。

這樣呼叫端就會被阻住,直到主執行緒真的把畫面畫完。問題是:這只是「等主執行緒有空」,並非真正的同步渲染;一旦要做多帧緩存與預播放,狀態會變得很難維護,而且必須繼承 UIView 覆寫方法。

下面這個工具類改用 CALayer 的 drawLayer:inContext: 完成渲染,並用信號量管理帧緩存。同步與非同步只要改 waitUntilDone 的參數即可切換。實測在 iPad mini 1 上大約 25 fps,機器好一點會更快。

頭檔案

#import <Foundation/Foundation.h>
#import <GLKit/GLKit.h>
#import <stdint.h>

/* 渲染影片,只支援 RGB 32bit 格式 */
@interface TKVideoPlayer : NSObject
- (bool)create:(UIView *)target width:(uint16_t)width height:(uint16_t)height frate:(float)frate;
- (bool)destory;
- (bool)update:(uint8_t *)buf len:(uint32_t)len;
- (bool)start;
- (bool)stop;
@end

實作檔案

#define TKVIDEO_FRAME_CACHE_COUNT 8

@interface TKVideoPlayer ()
{
    UIView *_view;
    float _frate;
    uint16_t _width;
    uint16_t _height;
    uint8_t *_buffer;
    uint32_t _length;
    TKTimer2 *_timer;
    bool _state;

    TKLock *_lockEmptyQueue;
    TKLock *_lockFilledQueue;
    std::queue<uint8_t *> _fmEmptyQueue;
    std::queue<uint8_t *> _fmFiledQueue;
    uint8_t *_fmbuffr[TKVIDEO_FRAME_CACHE_COUNT];

    dispatch_semaphore_t _sgEmpty;
    dispatch_semaphore_t _sgfilled;
}
@end

@implementation TKVideoPlayer

- (bool)create:(UIView *)target width:(uint16_t)width height:(uint16_t)height frate:(float)frate {
    _view = target;
    _width = width;
    _height = height;
    _frate = frate;
    _length = width * height * 4;

    // 把 layer 的 delegate 指向自己,才能攔下 drawLayer:inContext:
    _view.layer.delegate = self;

    _sgfilled = dispatch_semaphore_create(TKVIDEO_FRAME_CACHE_COUNT);
    _sgEmpty  = dispatch_semaphore_create(TKVIDEO_FRAME_CACHE_COUNT);

    // 預先配置好所有緩存格,全部先丟進空佇列
    for (int idx = 0; idx < TKVIDEO_FRAME_CACHE_COUNT; idx++) {
        _fmbuffr[idx] = (uint8_t *)malloc(_length);
        _fmEmptyQueue.push(_fmbuffr[idx]);
        dispatch_semaphore_wait(_sgfilled, DISPATCH_TIME_FOREVER);
    }

    _lockFilledQueue = [[TKLock alloc] init];
    [_lockFilledQueue open];
    _lockEmptyQueue = [[TKLock alloc] init];
    [_lockEmptyQueue open];
    return true;
}

- (bool)destory {
    _view.layer.delegate = nil;
    _view = nil;
    _buffer = NULL;

    for (int idx = 0; idx < TKVIDEO_FRAME_CACHE_COUNT; idx++) {
        free(_fmbuffr[idx]);
        _fmbuffr[idx] = NULL;
    }

    [_lockFilledQueue close];
    [_lockFilledQueue release];
    _lockFilledQueue = nil;
    [_lockEmptyQueue close];
    [_lockEmptyQueue release];
    _lockEmptyQueue = nil;

    // 把剩下的信號補回去,避免其他執行緒永遠卡住
    int lastCount = TKVIDEO_FRAME_CACHE_COUNT - _fmEmptyQueue.size() - _fmFiledQueue.size();
    for (int idx = 0; idx < _fmEmptyQueue.size() + lastCount; idx++)
        dispatch_semaphore_signal(_sgfilled);
    for (int idx = 0; idx < _fmFiledQueue.size() + lastCount; idx++)
        dispatch_semaphore_signal(_sgEmpty);

    dispatch_release(_sgfilled); _sgfilled = nil;
    dispatch_release(_sgEmpty);  _sgEmpty = nil;
    return true;
}

#pragma mark 實際繪製

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)context {
    CGRect frame = [layer bounds];

    // 等比縮放並置中
    float scalew = frame.size.width / _width;
    float scaleh = frame.size.height / _height;
    float scale = scalew < scaleh ? scalew : scaleh;
    float image_width  = _width * scale;
    float image_height = _height * scale;
    CGRect rect = CGRectMake((frame.size.width - image_width) / 2,
                             (frame.size.height - image_height) / 2,
                             image_width, image_height);

    if (_state && _buffer) {
        CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, _buffer, _length, NULL);
        CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
        CGImageRef imageRef = CGImageCreate(_width, _height, 8, 32, 4 * _width, colorSpaceRef,
                                            kCGBitmapByteOrder32Little | kCGImageAlphaFirst,
                                            provider, NULL, NO, kCGRenderingIntentDefault);
        // CGContext 的原點在左下,要翻轉一次
        CGContextTranslateCTM(context, 0.0, frame.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);
        CGContextDrawImage(context, rect, imageRef);

        CGImageRelease(imageRef);
        CGColorSpaceRelease(colorSpaceRef);
        CGDataProviderRelease(provider);
    } else {
        CGContextSetRGBFillColor(context, 0, 0, 0, 1);
        CGContextFillRect(context, frame);
    }
}

#pragma mark 寫入一帧

- (bool)update:(uint8_t *)buf len:(uint32_t)len {
    if (_state) {
        // 空佇列沒貨就先等最多 100ms
        dispatch_semaphore_wait(_sgEmpty, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_MSEC * 100));
        [_lockEmptyQueue lock];
        if (_fmEmptyQueue.size() == 0) {
            [_lockEmptyQueue unlock];
            return true;
        }
        uint8_t *cachebuf = _fmEmptyQueue.front();
        _fmEmptyQueue.pop();
        [_lockEmptyQueue unlock];

        memcpy(cachebuf, buf, len);

        [_lockFilledQueue lock];
        _fmFiledQueue.push(cachebuf);
        [_lockFilledQueue unlock];
        dispatch_semaphore_signal(_sgfilled);
    }
    return true;
}

#pragma mark 定時取帧並請主執行緒繪製

- (void)timer_call {
    if (_state) {
        dispatch_semaphore_wait(_sgfilled, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_MSEC * 100));
        [_lockFilledQueue lock];
        if (_fmFiledQueue.size() == 0) {
            [_lockFilledQueue unlock];
            return;
        }
        uint8_t *cachebuf = _fmFiledQueue.front();
        _fmFiledQueue.pop();
        [_lockFilledQueue unlock];

        [self performSelectorOnMainThread:@selector(timerDrawFrame:)
                              withObject:[NSNumber numberWithUnsignedLongLong:(uint64_t)cachebuf]
                           waitUntilDone:false];
    }
}

- (void)timerDrawFrame:(NSNumber *)bufNumber {
    _buffer = (uint8_t *)bufNumber.unsignedLongLongValue;
    if (_state && _buffer) {
        [_view.layer setNeedsDisplay];
        [_view.layer display];       // 直接觸發一次繪製
        // 畫完就把這一格還回空佇列
        [_lockEmptyQueue lock];
        _fmEmptyQueue.push(_buffer);
        [_lockEmptyQueue unlock];
        dispatch_semaphore_signal(_sgEmpty);
    } else {
        [_view.layer setNeedsDisplay];
        [_view.layer display];
    }
}

- (bool)clear {
    [self performSelectorOnMainThread:@selector(timerDrawFrame:)
                          withObject:[NSNumber numberWithUnsignedLongLong:NULL]
                       waitUntilDone:true];
    return true;
}

- (bool)start {
    if (_timer == nil) {
        _timer = [[TKTimer2 alloc] init];
        _timer.delay = 1000 / _frate;
        _timer.objcall = self;
        _timer.selcall = @selector(timer_call);
        [_timer start];
        _state = true;
    }
    return true;
}

- (bool)stop {
    if (_timer) {
        _state = false;
        [_timer stop];
        [self clear];
    }
    return true;
}
@end

相依的小工具

TKTimer2:設定回呼的 target 與 SEL,再給一個毫秒級延遲,start 之後該方法會被週期性呼叫。

@interface TKTimer2 : NSObject
@property (assign, nonatomic) id objcall;
@property (assign, nonatomic) SEL selcall;
@property (assign, nonatomic) uint32_t delay;      // 毫秒
- (void)start;
- (void)stop;
@end

TKLock:以 dispatch semaphore 實作的互斥鎖,多了 trylock: 可以帶逾時。

@interface TKLock : NSObject
- (void)open;
- (void)close;
- (void)lock;
- (void)unlock;
- (bool)trylock:(uint32_t)tick;
@end

@implementation TKLock
{
    dispatch_semaphore_t _sglock;
}

- (void)open  { _sglock = dispatch_semaphore_create(1); }

- (void)close {
    [self trylock:1];
    dispatch_semaphore_signal(_sglock);
    dispatch_release(_sglock);
    _sglock = nil;
}

- (void)lock   { dispatch_semaphore_wait(_sglock, DISPATCH_TIME_FOREVER); }
- (void)unlock { dispatch_semaphore_signal(_sglock); }

- (bool)trylock:(uint32_t)tick {
    long retcode = dispatch_semaphore_wait(_sglock,
        dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_MSEC * tick));
    return (retcode == 0);
}
@end

值得注意的地方

這個設計的重點是「空佇列/滿佇列」兩組緩存搭配兩顆信號量:解碼端拿空的就寫、寫完丟進滿的;渲染端拿滿的就畫、畫完丟回空的。生產者與消費者速度不對等時,最多隻會掉帧,不會卡死或讀到寫到一半的資料。

但靜態地說,這條路徑在現代 iOS 上並不是好選擇。把整帧 RGBA 交給 CoreGraphics 重繪,資料量與 CPU 都吃不消;真實的播放器應該把解碼後的 CVPixelBufferRef 直接貼上 AVSampleBufferDisplayLayer,或以 Metal/OpenGL ES 上傳纹理後渲染,還能順便拿到硬體解碼與 YUV 直接輸出的好處。上面這份程式碼的價值在於把「主動渲染的時序控制」與「多帧緩存的同步機制」講清楚,這兩個觀念換到任何渲染 API 都通用。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved