Changeset 5406


Ignore:
Timestamp:
Aug 3, 2016 10:22:29 AM (8 years ago)
Author:
ming
Message:

Fixed #1948: Modify async dispatch to synchronous on Mac and iOS video device implementation

Thanks to Marc Etcheverry for the patch.

Location:
pjproject/trunk/pjmedia/src/pjmedia-videodev
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjmedia/src/pjmedia-videodev/darwin_dev.m

    r5372 r5406  
    233233    darwin_sizes[idx++].preset_str = AVCaptureSessionPreset1920x1080; 
    234234#endif 
     235} 
     236 
     237static void dispatch_sync_on_main_queue(void (^block)(void)) 
     238{ 
     239    if ([NSThread isMainThread]) { 
     240        block(); 
     241    } else { 
     242        dispatch_sync(dispatch_get_main_queue(), block); 
     243    } 
    235244} 
    236245 
     
    944953                if (strm->prev_layer) { 
    945954                    CALayer *prev_layer = strm->prev_layer; 
    946                     dispatch_async(dispatch_get_main_queue(), ^{ 
     955                    dispatch_sync_on_main_queue(^{ 
    947956                        [prev_layer removeFromSuperlayer]; 
    948957                        [prev_layer release]; 
     
    970979             
    971980            /* Preview layer instantiation should be in main thread! */ 
    972             dispatch_async(dispatch_get_main_queue(), ^{ 
     981            dispatch_sync_on_main_queue(^{ 
    973982                /* Create preview layer */ 
    974983                AVCaptureVideoPreviewLayer *prev_layer = 
     
    10751084            UIView *view = (UIView *)pval; 
    10761085            strm->param.window.info.ios.window = (void *)pval; 
    1077             dispatch_async(dispatch_get_main_queue(), ^{ 
     1086            dispatch_sync_on_main_queue(^{ 
    10781087                [view addSubview:strm->render_view]; 
    10791088            }); 
     
    10881097            r.size = CGSizeMake(strm->param.disp_size.w, 
    10891098                                strm->param.disp_size.h); 
    1090             dispatch_async(dispatch_get_main_queue(), ^{ 
     1099            dispatch_sync_on_main_queue(^{ 
    10911100                strm->render_view.bounds = r; 
    10921101                if (strm->prev_layer) 
     
    11001109            pj_memcpy(&strm->param.window_pos, pval, 
    11011110                      sizeof(strm->param.window_pos)); 
    1102             dispatch_async(dispatch_get_main_queue(), ^{ 
     1111            dispatch_sync_on_main_queue(^{ 
    11031112                strm->render_view.center = 
    11041113                            CGPointMake(strm->param.window_pos.x + 
     
    11121121        case PJMEDIA_VID_DEV_CAP_OUTPUT_HIDE: 
    11131122        { 
    1114             dispatch_async(dispatch_get_main_queue(), ^{ 
     1123            dispatch_sync_on_main_queue(^{ 
    11151124                strm->render_view.hidden = (BOOL)(*((pj_bool_t *)pval)); 
    11161125            }); 
     
    11341143            if (strm->param.dir == PJMEDIA_DIR_RENDER) { 
    11351144#if TARGET_OS_IPHONE 
    1136                 dispatch_async(dispatch_get_main_queue(), ^{ 
     1145                dispatch_sync_on_main_queue(^{ 
    11371146                    strm->render_view.transform = 
    11381147                        CGAffineTransformMakeRotation( 
     
    12191228 
    12201229    if (stream->cap_session) { 
    1221         if ([NSThread isMainThread]) { 
     1230        dispatch_sync_on_main_queue(^{ 
    12221231            [stream->cap_session startRunning]; 
    1223         } else { 
    1224             dispatch_sync(dispatch_get_main_queue(), ^{ 
    1225                 [stream->cap_session startRunning]; 
    1226             }); 
    1227         } 
     1232        }); 
    12281233     
    12291234        if (![stream->cap_session isRunning]) { 
     
    12511256     
    12521257    /* Perform video display in a background thread */ 
    1253     dispatch_async(dispatch_get_main_queue(), ^{ 
     1258    dispatch_sync_on_main_queue(^{ 
    12541259        [stream->vout_delegate update_image]; 
    12551260    }); 
     
    12691274    PJ_LOG(4, (THIS_FILE, "Stopping Darwin video stream")); 
    12701275 
    1271     if ([NSThread isMainThread]) { 
    1272         [stream->cap_session stopRunning]; 
    1273     } else { 
    1274         dispatch_sync(dispatch_get_main_queue(), ^{ 
    1275             [stream->cap_session stopRunning]; 
    1276         }); 
    1277     } 
     1276    dispatch_sync_on_main_queue(^{ 
     1277        [stream->cap_session stopRunning]; 
     1278    }); 
    12781279     
    12791280    return PJ_SUCCESS; 
     
    13131314    if (stream->prev_layer) { 
    13141315        CALayer *prev_layer = stream->prev_layer; 
    1315         dispatch_async(dispatch_get_main_queue(), ^{ 
     1316        dispatch_sync_on_main_queue(^{ 
    13161317            [prev_layer removeFromSuperlayer]; 
    13171318            [prev_layer release]; 
     
    13221323    if (stream->render_view) { 
    13231324        UIView *view = stream->render_view; 
    1324         dispatch_async(dispatch_get_main_queue(), ^{ 
     1325        dispatch_sync_on_main_queue(^{ 
    13251326            [view removeFromSuperview]; 
    13261327            [view release]; 
  • pjproject/trunk/pjmedia/src/pjmedia-videodev/ios_opengl_dev.m

    r5052 r5406  
    2020#include <pj/assert.h> 
    2121#include <pj/log.h> 
     22#include <pj/os.h> 
    2223 
    2324#if defined(PJMEDIA_HAS_VIDEO) && PJMEDIA_HAS_VIDEO != 0 && \ 
     
    129130} 
    130131 
     132static void dispatch_sync_on_main_queue(void (^block)(void)) 
     133{ 
     134    if ([NSThread isMainThread]) { 
     135        block(); 
     136    } else { 
     137        dispatch_sync(dispatch_get_main_queue(), block); 
     138    } 
     139} 
     140 
    131141@implementation GLView 
    132142 
     
    267277     
    268278    rect = CGRectMake(0, 0, strm->param.disp_size.w, strm->param.disp_size.h); 
    269     strm->gl_view = [[GLView alloc] initWithFrame:rect]; 
     279    dispatch_sync_on_main_queue(^{ 
     280        strm->gl_view = [[GLView alloc] initWithFrame:rect]; 
     281    }); 
    270282    if (!strm->gl_view) 
    271283        return PJ_ENOMEM; 
     
    405417        UIView *view = (UIView *)pval; 
    406418        strm->param.window.info.ios.window = (void *)pval; 
    407         dispatch_async(dispatch_get_main_queue(), 
    408                        ^{[view addSubview:strm->gl_view];}); 
     419        dispatch_sync_on_main_queue(^{[view addSubview:strm->gl_view];}); 
    409420        return PJ_SUCCESS; 
    410421    } else if (cap == PJMEDIA_VID_DEV_CAP_OUTPUT_RESIZE) { 
    411422        pj_memcpy(&strm->param.disp_size, pval, sizeof(strm->param.disp_size)); 
    412         dispatch_async(dispatch_get_main_queue(), ^{ 
     423        dispatch_sync_on_main_queue(^{ 
    413424            strm->gl_view.bounds = CGRectMake(0, 0, strm->param.disp_size.w, 
    414425                                              strm->param.disp_size.h); 
     
    417428    } else if (cap == PJMEDIA_VID_DEV_CAP_OUTPUT_POSITION) { 
    418429        pj_memcpy(&strm->param.window_pos, pval, sizeof(strm->param.window_pos)); 
    419         dispatch_async(dispatch_get_main_queue(), ^{ 
     430        dispatch_sync_on_main_queue(^{ 
    420431            strm->gl_view.center = CGPointMake(strm->param.window_pos.x + 
    421432                                               strm->param.disp_size.w/2.0, 
     
    425436        return PJ_SUCCESS; 
    426437    } else if (cap == PJMEDIA_VID_DEV_CAP_OUTPUT_HIDE) { 
    427         dispatch_async(dispatch_get_main_queue(), ^{ 
     438        dispatch_sync_on_main_queue(^{ 
    428439            strm->gl_view.hidden = (BOOL)(*((pj_bool_t *)pval)); 
    429440        }); 
     
    433444        if (strm->param.orient == PJMEDIA_ORIENT_UNKNOWN) 
    434445            return PJ_SUCCESS; 
    435         dispatch_async(dispatch_get_main_queue(), ^{ 
     446        dispatch_sync_on_main_queue(^{ 
    436447            strm->gl_view.transform = 
    437448                CGAffineTransformMakeRotation(((int)strm->param.orient-1) * 
Note: See TracChangeset for help on using the changeset viewer.