本文主要是介绍Python 调用腾讯云音视频 TRTC,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Python 调用腾讯云音视频 TRTC
- 前言
- 一、使用Python ctype库调用
- Setp1.封装TRTCC++接口为C库
- Setp2.使用Python调用
前言
在阅读本文之前,您应该具备C++开发和Python开发的基础技能储备
一、使用Python ctype库调用
Setp1.封装TRTCC++接口为C库
在此下载原来项目
链接:https://pan.baidu.com/s/1w0HeGIHbeBu4qwb1tCKGdQ 提取码:tm4r
打开Visual Studio 2019,新建DLL动态库项目,我将其命名为PythonTRTC:
注意:这边的配置一定要选择64位平台,因为Python是64位的
配置TRTCSDK和头文件文件路径:
不熟悉如何配置?参见:https://blog.csdn.net/weixin_45809578/article/details/116004017?spm=1001.2014.3001.5501
现在,我们可以编写代码了。
由于我们需要导出TRTCC++为C接口,我们需要编写C风格的代码来代理调用:
大概如下:
TRTCCTypeInterface.h
:
#pragma once
#include "def.h"
#include <cstdint>
//extern "C" { 是必须要加上的,因为我们期望得到的是C风格的导出,ctype也只能调用C
extern "C" {typedef void* TRTCCLOUDHANDLE;typedef void* TRTCCLOUDCALLBACK;//对应TRTCSDK getTRTCShareInstancePYTHONTRTCAPI TRTCCLOUDHANDLE C_GetTRTCShareInstance();//对应TRTCSDK destroyTRTCShareInstancePYTHONTRTCAPI void C_DestroyTRTCShareInstance();//TODO 在此处封装//获取TRTCSDK的版本号PYTHONTRTCAPI const char* TRTCCloud_getSDKVersion(TRTCCLOUDHANDLE trtchanlde);
}
TRTCCTypeInterface.cpp
#include "pch.h"
#include <TRTC/ITRTCCloud.h>
#include <Live/ITXLivePlayer.h>#include "TRTCCTypeInterface.h"
#include "CTRTCCloudCallback.h"PYTHONTRTCAPI TRTCCLOUDHANDLE C_GetTRTCShareInstance()
{return getTRTCShareInstance();
}PYTHONTRTCAPI void C_DestroyTRTCShareInstance()
{destroyTRTCShareInstance();
}PYTHONTRTCAPI const char* TRTCCloud_getSDKVersion(TRTCCLOUDHANDLE trtchandle)
{ITRTCCloud* pstCloud = (ITRTCCloud*)trtchandle;return pstCloud->getSDKVersion();
}
Setp2.使用Python调用
在此下载示例代码
链接:https://pan.baidu.com/s/1UwIwTnT8zZJl-yvuY0n7zQ 提取码:4806
将编译完成的文件拷贝到Python项目中,如下图:
从ctype加载并导入dll方法
TRTCCImport.py
from ctypes import *class PyTRTCParams(Structure):_fields_ = [('sdkAppId', c_uint32),('userId', c_char_p),('userSig', c_char_p),('roomId', c_uint32),('strRoomId', c_char_p),('role', c_int),('streamId', c_char_p),('userDefineRecordId', c_char_p),('privateMapKey', c_char_p),('businessInfo', c_char_p)]PythonTRTC = windll.LoadLibrary("./PythonTRTC.dll")PythonTRTC.C_GetTRTCShareInstance.restype = c_void_pPythonTRTC.TRTCCloud_getSDKVersion.restype = c_char_p
PythonTRTC.TRTCCloud_getSDKVersion.argtypes = [c_void_p]
# 进入房间
PythonTRTC.TRTCCloud_enterRoom.argtypes = [c_void_p, POINTER(PyTRTCParams), c_int32]
# 退出房间
PythonTRTC.TRTCCloud_exitRoom.argtypes = [c_void_p]
# 开启摄像头
PythonTRTC.TRTCCloud_startLocalPreview.argtypes = [c_void_p, c_void_p]
# 停止摄像头
PythonTRTC.TRTCCloud_stopLocalPreview.argtypes = [c_void_p]
# 更新渲染对象
PythonTRTC.TRTCCloud_updateLocalView.argtypes = [c_void_p, c_void_p]
# 添加回调
PythonTRTC.TRTCCloud_addCallback.argtypes = [c_void_p, c_void_p]
# 移除回调
PythonTRTC.TRTCCloud_removeCallback.argtypes = [c_void_p, c_void_p]
# 创建回调
PythonTRTC.TRTCCloudCallback_Create.restype = c_void_p
# 删除创建的回调
PythonTRTC.TRTCCloudCallback_Delete.argtypes = [c_void_p]
# 设置错误处理函数
PythonTRTC.TRTCCloudCallback_onError.argtypes = [c_void_p, c_void_p]
# 设置警告处理函数
PythonTRTC.TRTCCloudCallback_onWarning.argtypes = [c_void_p, c_void_p]
# 当进入房间处理函数
PythonTRTC.TRTCCloudCallback_onEnterRoom.argtypes = [c_void_p, c_void_p]
# 退出房间回调
PythonTRTC.TRTCCloudCallback_onExitRoom.argtypes = [c_void_p, c_void_p]
main.py
from TRTCCImport import PyTRTCParams, PythonTRTC
from ctypes import *enterRoomParams = PyTRTCParams()
enterRoomParams.sdkAppId = 1
enterRoomParams.userId = b'LiSi'
enterRoomParams.userSig = b'XX'
enterRoomParams.roomId = 0
enterRoomParams.strRoomId = 0
enterRoomParams.role = 20 # 主播
enterRoomParams.streamId = 0
enterRoomParams.userDefineRecordId = 0
enterRoomParams.privateMapKey = 0
enterRoomParams.businessInfo = 0# 获取TRTCCloud 实例
trtccloud = PythonTRTC.C_GetTRTCShareInstance()# 打印版本号
print(PythonTRTC.TRTCCloud_getSDKVersion(trtccloud))trtccloud_callback = PythonTRTC.TRTCCloudCallback_Create()def callback(than, errcode, errmsg, extra_info):print('Code:', errcode, ' what:', errmsg)returnCFUNC_VOID_VP_I_CP_VP = CFUNCTYPE(None, c_void_p, c_int, c_char_p, c_void_p)
trtccloud_callback_onerror = CFUNC_VOID_VP_I_CP_VP(callback)# 指定回调函数
PythonTRTC.TRTCCloudCallback_onError(trtccloud_callback, trtccloud_callback_onerror)PythonTRTC.TRTCCloud_addCallback(trtccloud, trtccloud_callback)print(trtccloud)
print('ptr', byref(enterRoomParams), ' sizeof ', sizeof(enterRoomParams))
# 进入房间
PythonTRTC.TRTCCloud_enterRoom(trtccloud, byref(enterRoomParams), 1)
# TODO MessageLoop
# 退出房间
PythonTRTC.TRTCCloud_exitRoom()# 删除Cloud实例
PythonTRTC.C_DestroyTRTCShareInstance()
这篇关于Python 调用腾讯云音视频 TRTC的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!