[英]Texture rendering on iOS using OpenGL ES in Unity project
我正在做一个项目,其中一部分是将视频流式传输到我的iPhone。 我使用笔记本电脑通过ffmpeg创建到iPhone的视频流。
Shell中的流代码如下:
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();
更新功能:
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
留下其他细节,我的库在Unity模拟器上运行良好,但是当我为arm64编译所有库并使用Unity创建的xcode项目来构建我的应用并运行它时,我无法在iPhone中渲染任何纹理,我检查了网络,确定已将数据发送到我的iPhone,并且调试日志显示了该帧已成功解码,并且调用了OnRenderEvent
函数。
供参考:
Unity 5.3.2f1个人
Xcode 7.2.1
iOS 9.2.1
ffmpeg 3.0
好吧,我自己弄清楚了。
事实证明,Unity 5.3.2f1中适用于iOS 9的Auto Graphics API
在默认情况下是Auto Graphics API
,它将是Metal
。因此,在我的应用中,我无法使用OpenGL ES
API来操纵框架。
在将Graphics API
设置为OpenGL ES 3
,一切顺利。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.