示例代码
针对开发者的接入反馈的高频问题,腾讯云提供有更加简洁的 API-Example 工程,方便开发者可以快速的了解相关 API 的使用,欢迎使用。
定制推流画面
iOS 平台
如果您有自定义图像处理的需求(例如:堆加字幕),同时又希望复用 LiteAV SDK 的整体流程,您可以按照如下攻略进行定制。
1. 首先需要调用 V2TXLivePusher 的 enableCustomVideoProcess 开启自定义视频处理,才会收到这个回调通知。
2. 处理图像时分为两种情况。
美颜组件会产生新的纹理:
如果您使用的美颜组件会在处理图像的过程中产生一帧全新的纹理(用于承载处理后的图像),那请您在回调函数中将
dstFrame.textureId
设置为新纹理的 ID。- (void) onProcessVideoFrame:(V2TXLiveVideoFrame _Nonnull)srcFrame dstFrame:(V2TXLiveVideoFrame _Nonnull)dstFrame{GLuint dstTextureId = renderItemWithTexture(srcFrame.textureId, srcFrame.width, srcFrame.height);dstFrame.textureId = dstTextureId;}
美颜组件并不自身产生新纹理:
如果您使用的第三方美颜模块并不生成新的纹理,而是需要您设置给该模块一个输入纹理和一个输出纹理,则可以考虑如下方案:
- (void) onProcessVideoFrame:(V2TXLiveVideoFrame _Nonnull)srcFrame dstFrame:(V2TXLiveVideoFrame _Nonnull)dstFrame{thirdparty_process(srcFrame.textureId, srcFrame.width, srcFrame.height, dstFrame.textureId);}
3. 最后,对于 texture 数据的操作,需要一定的 OpenGL 基础知识,另外计算量不宜太大,因为 onProcessVideoFrame 的调用频率跟 FPS 相同,过于繁重的处理很容易造成 GPU 过热。
如果您只希望使用 SDK 来编码和推流(例如已经对接了商汤等产品),视频采集和预处理(即美颜、滤镜这些)全部由自己的代码来控制,可以按如下步骤实现:
1. 调用 V2TXLivePusher 的 enableCustomVideoCapture 接口开启自定义采集。
这样 SDK 本身就不会再采集视频数据,而只是启动编码、流控、发送等跟推流相关的工作。
2. 通过 V2TXLivePusher 的 sendCustomVideoFrame 向 SDK 填充 Video 数据。
/*** @brief 在自定义视频采集模式下,将采集的视频数据发送到SDK。* 在自定义视频采集模式下,SDK不再采集摄像头数据,仅保留编码和发送功能。* 您可以把采集到的 SampleBuffer 打包到 V2TXLiveVideoFrame 中,然后通过该API定期的发送。** @note 需要在 [startPush](@ref V2TXLivePusher#startPush:) 之前调用 [enableCustomVideoCapture](@ref V2TXLivePusher#enableCustomVideoCapture:) 开启自定义采集。** @param videoFrame 向 SDK 发送的 视频帧数据 {@link V2TXLiveVideoFrame}** @return 返回值 {@link V2TXLiveCode}* - V2TXLIVE_OK: 成功* - V2TXLIVE_ERROR_INVALID_PARAMETER: 发送失败,视频帧数据不合法* - V2TXLIVE_ERROR_REFUSED: 您必须先调用 enableCustomVideoCapture 开启自定义视频采集。*/- (V2TXLiveCode)sendCustomVideoFrame:(V2TXLiveVideoFrame *)videoFrame;
Android 平台
如果您有自定义图像处理的需求(例如堆加字幕),同时又希望复用 LiteAV SDK 的整体流程,您可以按照如下攻略进行定制。
1. 首先需要调用 V2TXLivePusher 的 enableCustomVideoProcess 开启自定义视频处理,才会收到这个回调通知。
2. 处理图像时分为两种情况。
美颜组件会产生新的纹理:
如果您使用的美颜组件会在处理图像的过程中产生一帧全新的纹理(用于承载处理后的图像),那请您在回调函数中将
dstFrame.textureId
设置为新纹理的 ID。private class MyPusherObserver extends V2TXLivePusherObserver {@Overridepublic void onGLContextCreated() {mFURenderer.onSurfaceCreated();mFURenderer.setUseTexAsync(true);}@Overridepublic int onProcessVideoFrame(V2TXLiveVideoFrame srcFrame, V2TXLiveVideoFrame dstFrame) {dstFrame.texture.textureId = mFURenderer.onDrawFrameSingleInput(srcFrame.texture.textureId, srcFrame.width, srcFrame.height);return super.onProcessVideoFrame(srcFrame, dstFrame);;}@Overridepublic void onGLContextDestroyed() {mFURenderer.onSurfaceDestroyed();}}
美颜组件并不自身产生新纹理:
如果您使用的第三方美颜模块并不生成新的纹理,而是需要您设置给该模块一个输入纹理和一个输出纹理,则可以考虑如下方案:
@Overridepublic int onProcessVideoFrame(V2TXLiveVideoFrame srcFrame, V2TXLiveVideoFrame dstFrame) {thirdparty_process(srcFrame.texture.textureId, srcFrame.width, srcFrame.height, dstFrame.texture.textureId);return 0;}
3. 最后,对于 texture 数据的操作,需要一定的 OpenGL 基础知识,另外计算量不宜太大,因为 onProcessVideoFrame 的调用频率跟 FPS 相同,过于繁重的处理很容易造成 GPU 过热。
如果您只希望使用 SDK 来编码和推流(例如已经对接了商汤等产品),视频采集预处理(即美颜、滤镜这些)全部由自己的代码来控制,可以按如下步骤实现:
1. 调用 V2TXLivePusher 的 enableCustomVideoCapture 接口开启自定义采集。
这样 SDK 本身就不会再采集视频数据,而只是启动编码、流控、发送等跟推流相关的工作。
2. 通过 V2TXLivePusher 的 sendCustomVideoFrame 向 SDK 填充 Video 数据。
/*** @brief 在自定义视频采集模式下,将采集的视频数据发送到SDK。 <br/>* 在自定义视频采集模式下,SDK不再采集摄像头数据,仅保留编码和发送功能。** @note 需要在 {@link V2TXLivePusher#startPush(String)} 之前调用 {@link V2TXLivePusher#enableCustomVideoCapture(boolean)} 开启自定义采集。** @param videoFrame 向 SDK 发送的 视频帧数据 {@link V2TXLiveVideoFrame}** @return 返回值 {@link V2TXLiveCode}* - V2TXLIVE_OK: 成功* - V2TXLIVE_ERROR_INVALID_PARAMETER: 发送失败,视频帧数据不合法* - V2TXLIVE_ERROR_REFUSED: 发送失败,您必须先调用 enableCustomVideoCapture 开启自定义视频采集*/public abstract int sendCustomVideoFrame(V2TXLiveVideoFrame videoFrame);
定制播放数据
iOS 平台
1. 设置 V2TXLivePlayer 的 V2TXLivePlayerObserver 监听。
@interface V2TXLivePlayer : NSObject/*** @brief 设置播放器回调。<br/>* 通过设置回调,可以监听 V2TXLivePlayer 播放器的一些回调事件,* 包括播放器状态、播放音量回调、音视频首帧回调、统计数据、警告和错误信息等。** @param observer 播放器的回调目标对象,更多信息请查看 {@link V2TXLivePlayerObserver}*/- (void)setObserver:(id<V2TXLivePlayerObserver>)observer;
2. 通过 onRenderVideoFrame 回调捕获 Player 的图像数据。
/*** @brief 视频帧信息。* V2TXLiveVideoFrame 用来描述一帧视频画面的裸数据,它可以是一帧编码前的画面,也可以是一帧解码后的画面。* @note 自定义采集和自定义渲染时使用。自定义采集时,需要使用 V2TXLiveVideoFrame 来包装待发送的视频帧;自定义渲染时,会返回经过 V2TXLiveVideoFrame 包装的视频帧。*/@interface V2TXLiveVideoFrame : NSObject///【字段含义】视频帧像素格式///【推荐取值】V2TXLivePixelFormatNV12@property(nonatomic, assign) V2TXLivePixelFormat pixelFormat;///【字段含义】视频数据包装格式///【推荐取值】V2TXLiveBufferTypePixelBuffer@property(nonatomic, assign) V2TXLiveBufferType bufferType;///【字段含义】bufferType 为 V2TXLiveBufferTypeNSData 时的视频数据@property(nonatomic, strong, nullable) NSData *data;///【字段含义】bufferType 为 V2TXLiveBufferTypePixelBuffer 时的视频数据@property(nonatomic, assign, nullable) CVPixelBufferRef pixelBuffer;///【字段含义】视频宽度@property(nonatomic, assign) NSUInteger width;///【字段含义】视频高度@property(nonatomic, assign) NSUInteger height;///【字段含义】视频帧的顺时针旋转角度@property(nonatomic, assign) V2TXLiveRotation rotation;///【字段含义】视频纹理ID@property (nonatomic, assign) GLuint textureId;@end@protocol V2TXLivePlayerObserver <NSObject>@optional/*** @brief 自定义视频渲染回调** @note 调用 [enableCustomRendering](@ref V2TXLivePlayer#enableCustomRendering:pixelFormat:bufferType:) 开启自定义渲染之后,会收到这个回调通知** @param videoFrame 视频帧数据 {@link V2TXLiveVideoFrame}*/- (void)onRenderVideoFrame:(id<V2TXLivePlayer>)playerframe:(V2TXLiveVideoFrame *)videoFrame;@end
Android 平台
1. 设置 V2TXLivePlayer 的 V2TXLivePlayerObserver 监听。
public abstract void setObserver(V2TXLivePlayerObserver observer)
2. 通过 onRenderVideoFrame 回调捕获 Player 的图像数据。
public final static class V2TXLiveVideoFrame{/// 视频像素格式public V2TXLivePixelFormat pixelFormat = V2TXLivePixelFormat.V2TXLivePixelFormatUnknown;/// 视频数据包装格式public V2TXLiveBufferType bufferType = V2TXLiveBufferType.V2TXLiveBufferTypeUnknown;/// 视频纹理包装类public V2TXLiveTexture texture;/// 视频数据public byte[] data;/// 视频数据public ByteBuffer buffer;/// 视频宽度public int width;/// 视频高度public int height;/// 视频像素的顺时针旋转角度public int rotation;}public abstract class V2TXLivePlayerObserver {/*** 自定义视频渲染回调** @param player 回调该通知的播放器对象* @param videoFrame 视频帧数据 {@link V2TXLiveVideoFrame}*/void onRenderVideoFrame(V2TXLivePlayer player, V2TXLiveVideoFrame videoFrame);}