Changeset 4837


Ignore:
Timestamp:
May 7, 2014 5:30:29 AM (11 years ago)
Author:
nanang
Message:

Re #1762: Fix native preview not shown issue by starting capture session from main thread.

File:
1 edited

Legend:

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

    r4836 r4837  
    9797    VOutDelegate                *vout_delegate; 
    9898    void                        *capture_buf; 
     99    AVCaptureVideoPreviewLayer  *prev_layer; 
    99100     
    100101    void                *render_buf; 
     
    715716            ios_init_view(strm); 
    716717             
    717             CALayer *view_layer = strm->render_view.layer; 
    718             CGRect r = strm->render_view.bounds; 
    719              
    720718            /* Preview layer instantiation should be in main thread! */ 
    721719            dispatch_async(dispatch_get_main_queue(), ^{ 
     
    726724                 
    727725                /* Attach preview layer to a UIView */ 
    728                 prev_layer.videoGravity = AVLayerVideoGravityResizeAspectFill; 
    729                 prev_layer.frame = r; 
    730                 [view_layer addSublayer:prev_layer]; 
     726                prev_layer.videoGravity = AVLayerVideoGravityResize; 
     727                prev_layer.frame = strm->render_view.bounds; 
     728                [strm->render_view.layer addSublayer:prev_layer]; 
     729                strm->prev_layer = prev_layer; 
    731730                PJ_LOG(4, (THIS_FILE, "Native preview initialized")); 
    732731            }); 
     
    832831            dispatch_async(dispatch_get_main_queue(), ^{ 
    833832                strm->render_view.bounds = r; 
     833                if (strm->prev_layer) 
     834                    strm->prev_layer.frame = r; 
    834835            }); 
    835836            return PJ_SUCCESS; 
     
    891892 
    892893    if (stream->cap_session) { 
    893         [stream->cap_session startRunning]; 
     894        if ([NSThread isMainThread]) { 
     895            [stream->cap_session startRunning]; 
     896        } else { 
     897            dispatch_sync(dispatch_get_main_queue(), ^{ 
     898                [stream->cap_session startRunning]; 
     899            }); 
     900        } 
    894901     
    895902        if (![stream->cap_session isRunning]) 
     
    906913{ 
    907914    struct ios_stream *stream = (struct ios_stream*)strm; 
    908     //NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
    909915     
    910916    if (stream->frame_size >= frame->size) 
     
    914920     
    915921    /* Perform video display in a background thread */ 
    916     dispatch_async(dispatch_get_main_queue(), 
    917                    ^{[stream->vout_delegate update_image];}); 
    918  
    919     //[pool release]; 
     922    dispatch_async(dispatch_get_main_queue(), ^{ 
     923        [stream->vout_delegate update_image]; 
     924    }); 
    920925     
    921926    return PJ_SUCCESS; 
     
    931936    PJ_LOG(4, (THIS_FILE, "Stopping iOS video stream")); 
    932937 
    933     if (stream->cap_session && [stream->cap_session isRunning]) 
    934         [stream->cap_session stopRunning]; 
     938    if (stream->cap_session && [stream->cap_session isRunning]) { 
     939        if ([NSThread isMainThread]) { 
     940            [stream->cap_session stopRunning]; 
     941        } else { 
     942            dispatch_sync(dispatch_get_main_queue(), ^{ 
     943                [stream->cap_session stopRunning]; 
     944            }); 
     945        } 
     946    } 
    935947     
    936948    return PJ_SUCCESS; 
     
    965977    } 
    966978 
     979    if (stream->prev_layer) { 
     980        CALayer *prev_layer = stream->prev_layer; 
     981        dispatch_async(dispatch_get_main_queue(), ^{ 
     982            [prev_layer removeFromSuperlayer]; 
     983            [prev_layer release]; 
     984        }); 
     985        stream->prev_layer = nil; 
     986    } 
     987     
    967988    if (stream->render_view) { 
    968989        UIView *view = stream->render_view; 
    969         dispatch_async(dispatch_get_main_queue(), 
    970           ^{ 
    971               [view removeFromSuperview]; 
    972               [view release]; 
    973            }); 
    974         stream->render_view = NULL; 
     990        dispatch_async(dispatch_get_main_queue(), ^{ 
     991            [view removeFromSuperview]; 
     992            [view release]; 
     993        }); 
     994        stream->render_view = nil; 
    975995    } 
    976996     
    977997    if (stream->render_data_provider) { 
    978998        CGDataProviderRelease(stream->render_data_provider); 
    979         stream->render_data_provider = NULL; 
     999        stream->render_data_provider = nil; 
    9801000    } 
    9811001 
Note: See TracChangeset for help on using the changeset viewer.