谷歌浏览器关闭阅读清单

ECMAScript-6.0

  返回  

Python 调用腾讯云音视频 TRTC

2021/8/20 11:02:11 浏览:

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 getTRTCShareInstance
    PYTHONTRTCAPI TRTCCLOUDHANDLE C_GetTRTCShareInstance();
    //对应TRTCSDK destroyTRTCShareInstance
    PYTHONTRTCAPI 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_p

PythonTRTC.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)
    return


CFUNC_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()

联系我们

如果您对我们的服务有兴趣,请及时和我们联系!

服务热线:18288888888
座机:18288888888
传真:
邮箱:888888@qq.com
地址:郑州市文化路红专路93号