2016-03-03 335 views
1

我正在開發一個項目,其中一部分是將視頻流式傳輸到我的iPhone。我用我的筆記本電腦用ffmpeg創建視頻流到我的iPhone。在Unity項目中使用OpenGL ES進行紋理渲染

殼流代碼如下:

ffmpeg \ 
    -f avfoundation -i "1" -s 1280*720 -r 29.97 \ 
    -c:v mpeg2video -q:v 20 -pix_fmt yuv420p -g 1 -threads 4\ 
    -f mpegts udp://192.168.1.102:6666 

這一點,我成功地創建我的視頻流。

在Unity中,我想解碼視頻流以創建紋理。在我經歷了一些ffmpeg教程和Unity教程之後,我創建了我的鏈接庫。一些代碼低於(問我是否需要更多):

在我的圖書館:

緩衝頁頭:

uint8_t *buffer; 
int buffer_size; 
buffer_size = avpicture_get_size(AV_PIX_FMT_RGBA, VIEW_WIDTH, VIEW_HEIGHT); 

buffer = (uint8_t *) av_malloc(buffer_size*sizeof(uint8_t)); 

avpicture_fill((AVPicture *) pFrameRGB, buffer, AV_PIX_FMT_RGBA, 
       VIEW_WIDTH, VIEW_HEIGHT); 

的getContext:

is->sws_ctx = sws_getContext 
    (
    is->video_st->codec->width, 
    is->video_st->codec->height, 
    is->video_st->codec->pix_fmt, 
    VIEW_WIDTH, 
    VIEW_HEIGHT, 
    AV_PIX_FMT_RGBA, 
    SWS_BILINEAR, 
    NULL, 
    NULL, 
    NULL 
    ); 

sws_scale:

sws_scale(
      is->sws_ctx, 
      (uint8_t const * const *)pFrame->data, 
      pFrame->linesize, 
      0, 
      is->video_st->codec->height, 
      pFrameRGB->data, 
      pFrameRGB->linesize 
     ); 

紋理渲染:

static void UNITY_INTERFACE_API OnRenderEvent(int texID) 
{ 
    GLuint gltex = (GLuint)(size_t)(texID); 

    glBindTexture(GL_TEXTURE_2D, gltex); 

    glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, VIEW_WIDTH, VIEW_HEIGHT, 
        GL_RGBA, GL_UNSIGNED_BYTE, pFrameRGB->data[0]); 

    glGetError(); 
    return; 
} 

extern "C" UnityRenderingEvent UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API GetRenderEventFunc() 
{ 
    return OnRenderEvent; 
} 

在Unity:

質地創建:

private Texture2D texture; 
    private int texID; 
    texture = new Texture2D (width, height, TextureFormat.RGBA32, false); 
    texture.filterMode = FilterMode.Point; 
    texture.Apply(); 
    GetComponent<Renderer>().material.mainTexture = texture; 
    texID = texture.GetNativeTexturePtr().ToInt32(); 

更新FUNC:

void Update() 
    { 
     GL.IssuePluginEvent(GetRenderEventFunc(), texID); 
    } 

視頻流信息:

Input #0, mpegts, from 'udp://0.0.0.0:6666': 
    Duration: N/A, start: 2.534467, bitrate: N/A 
    Program 1 
    Metadata: 
     service_name : Service01 
     service_provider: FFmpeg 
    Stream #0:0[0x100]: Video: mpeg2video (Main) ([2][0][0][0]/0x0002), yuv420p(tv), 1280x720 [SAR 1:1 DAR 16:9], max. 104857 kb/s, 29.97 fps, 29.97 tbr, 90k tbn, 59.94 tbc 

保留其他的細節,我的圖書館正常工作的統一模擬器,但是當我編譯我的arm64所有圖書館和使用的統一創建打造我的應用程序的Xcode項目並運行它,我不能得到任何紋理呈現在我的iPhone,我檢查了我的網絡,我敢肯定,數據已發送到我的iPhone和調試日誌告訴我,該幀已被成功解碼也調用OnRenderEvent功能。

FYI:

統一5.3.2f1個人

的Xcode 7.2.1

的iOS 9.2.1

的ffmpeg 3.0

回答

0

好吧,我想通了自己。

事實證明,Unity 5.3.2f1中iOS 9的圖形API默認爲Auto Graphics API,因此在我的應用程序中,我不能使用OpenGL ES API來操作我的框架。

我設定Graphics APIOpenGL ES 3,一切順利。