C#封装libEasyRTMP过程介绍
在编程语言中真正的霸主多年来一直是C++,所有的操作系统和绝大多数的商品软件都是用C++作为主要开发语言的。但是C#几乎集中了所有关于软件开发和软件工程研究的最新成果:面向对象、类型安全、组件技术、自动内存管理、跨平台异常处理、版本控制、代码安全管理……。
本文我主要写一下C#封装libEasyRTMP的过程给大家参考一下,具体过程如下:
1、新建一个C#类库项目,如下图
2、查看接口函数
3、新建一个类EasyRTMPNetSDK,封装如下:
public class EasyRTMPNetSDK
{
[StructLayoutAttribute(LayoutKind.Sequential)]
public struct EASY_AV_Frame
{
public uint u32AVFrameFlag; /* 帧标志 视频 or 音频 */
public uint u32AVFrameLen; /* 帧的长度 */
public uint u32VFrameType; /* 视频的类型,I帧或P帧 */
public IntPtr pBuffer; /* 数据 */
public uint u32TimestampSec; /* 时间戳(秒)*/
public uint u32TimestampUsec; /* 时间戳(微秒) */
}
public enum EASY_RTMP_STATE_T
{
EASY_RTMP_STATE_CONNECTING = 1, /* 连接中 */
EASY_RTMP_STATE_CONNECTED, /* 连接成功 */
EASY_RTMP_STATE_CONNECT_FAILED, /* 连接失败 */
EASY_RTMP_STATE_CONNECT_ABORT, /* 连接异常中断 */
EASY_RTMP_STATE_PUSHING, /* 推流中 */
EASY_RTMP_STATE_DISCONNECTED, /* 断开连接 */
EASY_RTMP_STATE_ERROR
}
[StructLayoutAttribute(LayoutKind.Sequential)]
public struct EASY_MEDIA_INFO_T
{
/// <summary>
/// 视频编码类型
/// </summary>
public uint u32VideoCodec;
/// <summary>
/// 视频帧率
/// </summary>
public uint u32VideoFps;
/// <summary>
/// 音频编码类型
/// </summary>
public uint u32AudioCodec;
/// <summary>
/// 音频采样率
/// </summary>
public uint u32AudioSamplerate;
/// <summary>
/// 音频通道数
/// </summary>
public uint u32AudioChannel;
/// <summary>
/// 音频采样精度
/// </summary>
public uint u32AudioBitsPerSample;
/// <summary>
/// 视频sps帧长度
/// </summary>
public uint u32VpsLength;
/// <summary>
/// 视频sps帧长度
/// </summary>
public uint u32SpsLength;
/// <summary>
/// 视频pps帧长度
/// </summary>
public uint u32PpsLength;
/// <summary>
/// 视频pps帧长度
/// </summary>
public uint u32SeiLength;
/// <summary>
/// 视频u8Vps帧内容
/// </summary>
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 256)]
public byte[] u8Vps;
/// <summary>
/// 视频sps帧内容
/// </summary>
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 256)]
public byte[] u8Sps;
/// <summary>
/// 视频sps帧内容
/// </summary>
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 128)]
public byte[] u8Pps;
/// <summary>
/// 视频u8Sei帧内容
/// </summary>
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 128)]
public byte[] u8Sei;
}
public delegate int EasyRTMPCallBack(int _id, EASY_RTMP_STATE_T _state, ref EASY_AV_Frame _frame, IntPtr _userptr);
[DllImport(@"Lib\libeasyrtmp.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "_EasyRTMP_Activate@4")]
public static extern int EasyRTMP_Activate(string license = "79736C36655969576B5A7541767A52656F6374414A65394659584E35556C524E55463949535573755A58686C4B56634D5671442F706634675A57467A65513D3D");
[DllImport(@"Lib\libeasyrtmp.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "_EasyRTMP_Create@0")]
/* 创建推送句柄 返回为句柄值 */
public static extern IntPtr EasyRTMP_Create();
[DllImport(@"Lib\libeasyrtmp.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "_EasyRTMP_SetCallback@12")]
/* 设置流传输事件回调 userptr传输自定义对象指针*/
public static extern uint EasyRTMP_SetCallback(IntPtr handle, EasyRTMPCallBack callback, IntPtr userptr);
/* 开始流传输 serverAddr:流媒体服务器地址、port:流媒体端口、streamName:流名称<xxx.sdp>、username/password:推送携带的用户名密码、pstruStreamInfo:推送的媒体定义、bufferKSize:以k为单位的缓冲区大小<512~2048之间,默认512> bool createlogfile:创建日志文件*/
[DllImport(@"Lib\libeasyrtmp.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "_EasyRTMP_Connect@8")]
public static extern uint EasyRTMP_Connect(IntPtr handle, string url);
[DllImport(@"Lib\libeasyrtmp.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "_EasyRTMP_InitMetadata@12")]
/* 停止流传输 */
public static extern uint EasyRTMP_InitMetadata(IntPtr handle, ref EASY_MEDIA_INFO_T pstruStreamInfo, uint bufferKSize);
[DllImport(@"Lib\libeasyrtmp.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "_EasyRTMP_SendPacket@8")]
/* 推流 frame:具体推送的流媒体帧 */
public static extern uint EasyRTMP_SendPacket(IntPtr pushPtr, ref EASY_AV_Frame frame);
[DllImport(@"Lib\libeasyrtmp.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "_EasyRTMP_Release@4")]
/* 释放推送句柄 */
public static extern uint EasyRTMP_Release(IntPtr pushPtr);
}