C#调用libEasyPlayer动态库,实现RTSP流播放

jopen 8年前

一、项目背景:

        由于一个项目的附加部分,客户要求实现一个关于视频流媒体的方面内容,而且没有经费,且做为项目的附加条款,采购现成的系统,由于经费的问题,不太现实,所以上到开源社区寻找视频流媒体方面的开源项目,经过一番比较,选中了EasyDarwin。

二、布署服务器端

       下载了服务器端的编译版本直截在本地的Linux服务器上进行了布署。为了方便启动服务器,写了一个脚本。

#!/bin/bash  #file name start_easydarwin_server.sh  sudo ~/EasyDarwin-CentOS-x64-7.0.3-Build15.1130/easydarwin -c ~/EasyDarwin-CentOS-x64-7.0.3-Build15.1130/easydarwin.xml -d

       运行后,结果如下:

[janl@svr1 sh]$ ./start_easydarwin_server.sh  [sudo] password for janl:  WARNING: No module folder exists.  INFO: Module Loaded...QTSSFileModule [static]  INFO: Module Loaded...QTSSReflectorModule [static]  INFO: Module Loaded...EasyRelayModule [static]  INFO: Module Loaded...EasyHLSModule [static]  INFO: Module Loaded...QTSSAccessLogModule [static]  INFO: Module Loaded...QTSSFlowControlModule [static]  INFO: Module Loaded...QTSSPosixFileSysModule [static]  INFO: Module Loaded...QTSSAdminModule [static]  INFO: Module Loaded...QTSSMP3StreamingModule [static]  INFO: Module Loaded...QTSSAccessModule [static]  WARNING: No users file found at ./qtusers.  WARNING: No groups file found at ./qtgroups.  Streaming Server done starting up

    到这服务器端就能运行了。

三、实现我需要的客户端:

    先看一下最后的运行结果:

     从官方下载了EasyClient,这个没有编译好的版本,直截下载了源码,在Win7 x64下用VS2013编译。

     值得注意的一点是,源生下截的代码EasyClient在编译时会报错,提示找不libmp4creator,经过群主帮忙指点,其实

libmp4creator在下边的EasyPlayer项目中就有,直截Copy到EasyClient项目中就可以了。

    后面一切顺利,编译成功,后运行EasyClient可以用了。

四、实现自已想要的客户端:

      EasyClient客户端虽然不错,但是并不是我可以交付给客户的东西,所以实现自已的客户端就成为主要难点。

      EasyClient是用C++编写的,而我对C++并不熟,凭着多年工作经验,只能看懂个大概,最优的方案就是调用EasyClient类库,前端界面用C#重新实现。

     废话不多说了,本着从最容易的着手,就从EasyPlayer着手,从EasyClient目录,Copy了libEasyPlayer.dll及其它动态库,新建了c#项目,解决方案。

     第一个要解决的是,导出函数的定义,在c#中导入libEasyPlayer.dll的API导出函数,这里不得不说,EasyPlayer的作者把EasyPlayer的导出函,封装的很清晰:

    

typedef int (CALLBACK *MediaSourceCallBack)( int _channelId, int *_channelPtr, int _frameType, char *pBuf, RTSP_FRAME_INFO* _frameInfo);  LIB_EASYPLAYER_API int EasyPlayer_Init();  LIB_EASYPLAYER_API void EasyPlayer_Release();  LIB_EASYPLAYER_API int EasyPlayer_OpenStream(const char *url, HWND hWnd, RENDER_FORMAT renderFormat, int rtpovertcp, const char *username, const char *password, MediaSourceCallBack callback=NULL, void *userPtr=NULL);  LIB_EASYPLAYER_API void EasyPlayer_CloseStream(int channelId);  LIB_EASYPLAYER_API int EasyPlayer_SetFrameCache(int channelId, int cache);  LIB_EASYPLAYER_API int EasyPlayer_SetShownToScale(int channelId, int shownToScale);  LIB_EASYPLAYER_API int EasyPlayer_SetDecodeType(int channelId, int decodeKeyframeOnly);  LIB_EASYPLAYER_API int EasyPlayer_SetRenderRect(int channelId, LPRECT lpSrcRect);  LIB_EASYPLAYER_API int EasyPlayer_ShowStatisticalInfo(int channelId, int show);  LIB_EASYPLAYER_API int EasyPlayer_SetDragStartPoint(int channelId, POINT pt);  LIB_EASYPLAYER_API int EasyPlayer_SetDragEndPoint(int channelId, POINT pt);  LIB_EASYPLAYER_API int EasyPlayer_ResetDragPoint(int channelId);  LIB_EASYPLAYER_API int EasyPlayer_StartManuRecording(int channelId);  LIB_EASYPLAYER_API int EasyPlayer_StopManuRecording(int channelId);  LIB_EASYPLAYER_API int EasyPlayer_PlaySound(int channelId);  LIB_EASYPLAYER_API int EasyPlayer_StopSound();

   导入到c#的代码中,具体就是如这个样子。 

/// Return Type: int          [System.Runtime.InteropServices.DllImportAttribute("libEasyPlayer.dll", EntryPoint = "?EasyPlayer_Init@@YAHXZ")]          //[DllImport("libEasyPlayer.dll", EntryPoint = "EasyPlayer_Init", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]          public static extern int EasyPlayer_Init();          /// Return Type: void          [System.Runtime.InteropServices.DllImportAttribute("libEasyPlayer.dll", EntryPoint = "?EasyPlayer_Release@@YAXXZ")]          public static extern void EasyPlayer_Release();

   这里我要提到一个工具,叫Invoke Interop Assistant 这个工具可以帮我将C++的头件,转换为c#的导入代码就是上边的代码,这个工具我是在CSDN上下载的,不过有点遗憾的是,最复杂并且带回调函数的EasyPlayer_OpenStream,无法自动生成,工具报了一个错误,具体错误信息是:

Error: Error processing procedure EasyPlayer_OpenStream: Expected token of type ParenClose but found ParenOpen instead.

我在Baidu上查了一下,没有什么有效的解决办法,而且我也没有弄清 ParenClose 与ParenOpen是否c++的专用术语或是什么,导出函出的定义方式,总之问题没有解决。EasyPlayer_OpenStream 和 回调函数 只好手动来码。

如果谁知道是怎么回事,一定要告诉我一下。

完成的导入函数如下,超长了,贴不上来,我一会传到群空吧。

程序的调试过程遇到两个问题要提醒注意一下:

 1. 找不到动态库,明明libEasyPlayer.dll就在可执行目录,系统提示找不到。经过尝试后我把项目编译配置由AnyCPU改为X86后终于不再报找不到动态库了。

 2. 提示找不动态库的入口点,这个把我郁闷坏了,明明比较和定义一样,就是提示找不到入口点,后来实在没办法了,只好上看雪下了一个Dll导出表查看器,具体的名字叫ExportList,有图有真相啊!

 看到第一行那个函数符号了吗? 我想可能是因为编译字符集或者是什么原因引起的吧,照这个定义就能找到入口点了。

如果你有更好的办法,请不要忘记告诉我一声。

来自: http://my.oschina.net/janl/blog/599852