python可以调用c/c++的动态库,前提是c库的函数必须用extern "C" 声明。
首先python 须引入 ctypes 库 以下示例,展示如何加载C库函数,并注册C库中的回调函数:
import ctypes
from time import sleep
libmp4svr = ctypes.cdll.LoadLibrary('./libmp4RtspServer.so')
#void(*funProgressCallbck)(const char* url,float progress);
func_type = ctypes.CFUNCTYPE(None,ctypes.c_char_p,ctypes.c_float)
def ProgressCallbck(url,fprogress):
print("url:%s,%.4f"%(url,fprogress))
libmp4svr.createMp4RtspServer()
path="./stair-3-2.mp4"
libmp4svr.startStreaming(path.encode(),func_type(ProgressCallbck))
#sleep(10)
while True:
sleep(1)
libmp4svr.stopStreaming()
libmp4svr.destroyMp4RtspServer()
注意: func_type = ctypes.CFUNCTYPE(None,ctypes.c_char_p,ctypes.c_float)中的None表示回调函数的返回值