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.

File:
1 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]; 
Note: See TracChangeset for help on using the changeset viewer.