mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-06-20 07:12:21 +08:00
添加c接口及测试程序
This commit is contained in:
70
c_wrapper/src/cleaner.h
Executable file
70
c_wrapper/src/cleaner.h
Executable file
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
|
||||
*
|
||||
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef SRC_CLEANER_H_
|
||||
#define SRC_CLEANER_H_
|
||||
|
||||
#include <list>
|
||||
#include <mutex>
|
||||
#include <functional>
|
||||
using namespace std;
|
||||
|
||||
|
||||
class cleaner {
|
||||
public:
|
||||
cleaner(){}
|
||||
virtual ~cleaner(){
|
||||
lock_guard<recursive_mutex> lck(_mtx);
|
||||
for(auto &fun : _cleanInvokerList){
|
||||
fun();
|
||||
}
|
||||
_cleanInvokerList.clear();
|
||||
}
|
||||
static cleaner &Instance(){
|
||||
static cleaner *instance(new cleaner);
|
||||
return *instance;
|
||||
}
|
||||
static void Destory(){
|
||||
delete &Instance();
|
||||
}
|
||||
template<typename FUN>
|
||||
void push_front(FUN &&fun){
|
||||
lock_guard<recursive_mutex> lck(_mtx);
|
||||
_cleanInvokerList.push_front(fun);
|
||||
}
|
||||
|
||||
template<typename FUN>
|
||||
void push_back(FUN &&fun){
|
||||
lock_guard<recursive_mutex> lck(_mtx);
|
||||
_cleanInvokerList.push_back(fun);
|
||||
}
|
||||
|
||||
private:
|
||||
recursive_mutex _mtx;
|
||||
list<function<void()> > _cleanInvokerList;
|
||||
};
|
||||
|
||||
#endif /* SRC_CLEANER_H_ */
|
||||
191
c_wrapper/src/common.cpp
Executable file
191
c_wrapper/src/common.cpp
Executable file
@@ -0,0 +1,191 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
|
||||
*
|
||||
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "common.h"
|
||||
#include <stdarg.h>
|
||||
#include <unordered_map>
|
||||
#include "Util/logger.h"
|
||||
#include "Util/onceToken.h"
|
||||
#include "Util/TimeTicker.h"
|
||||
#include "Network/TcpServer.h"
|
||||
#include "Poller/EventPoller.h"
|
||||
#include "Rtsp/UDPServer.h"
|
||||
#include "Rtsp/RtspSession.h"
|
||||
#include "Rtmp/RtmpSession.h"
|
||||
#include "Http/HttpSession.h"
|
||||
#include "cleaner.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace ZL::Util;
|
||||
using namespace ZL::Rtmp;
|
||||
using namespace ZL::Rtsp;
|
||||
using namespace ZL::Http;
|
||||
|
||||
static TcpServer<RtspSession>::Ptr s_pRtspSrv;
|
||||
static TcpServer<RtmpSession>::Ptr s_pRtmpSrv;
|
||||
static TcpServer<HttpSession>::Ptr s_pHttpSrv;
|
||||
|
||||
//////////////////////////environment init///////////////////////////
|
||||
|
||||
API_EXPORT void CALLTYPE onAppStart(){
|
||||
static onceToken s_token([](){
|
||||
Logger::Instance().add(std::make_shared<ConsoleChannel>("stdout", LTrace));
|
||||
EventPoller::Instance(true);
|
||||
|
||||
cleaner::Instance().push_back([](){
|
||||
s_pRtspSrv.reset();
|
||||
s_pRtmpSrv.reset();
|
||||
s_pHttpSrv.reset();
|
||||
WorkThreadPool::Destory();
|
||||
UDPServer::Destory();
|
||||
AsyncTaskThread::Destory();
|
||||
EventPoller::Destory();
|
||||
DebugL << "clear common" << endl;
|
||||
Logger::Destory();
|
||||
});
|
||||
},nullptr);
|
||||
}
|
||||
|
||||
|
||||
API_EXPORT void CALLTYPE onAppExit(){
|
||||
cleaner::Destory();
|
||||
}
|
||||
|
||||
API_EXPORT int CALLTYPE initHttpServer(unsigned short port){
|
||||
s_pHttpSrv.reset(new TcpServer<HttpSession>());
|
||||
try {
|
||||
s_pHttpSrv->start(port);
|
||||
return 0;
|
||||
} catch (std::exception &ex) {
|
||||
s_pHttpSrv.reset();
|
||||
WarnL << ex.what();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
API_EXPORT int CALLTYPE initRtspServer(unsigned short port) {
|
||||
s_pRtspSrv.reset(new TcpServer<RtspSession>());
|
||||
try {
|
||||
s_pRtspSrv->start(port);
|
||||
return 0;
|
||||
} catch (std::exception &ex) {
|
||||
s_pRtspSrv.reset();
|
||||
WarnL << ex.what();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
API_EXPORT int CALLTYPE initRtmpServer(unsigned short port) {
|
||||
s_pRtmpSrv.reset(new TcpServer<RtmpSession>());
|
||||
try {
|
||||
s_pRtmpSrv->start(port);
|
||||
return 0;
|
||||
} catch (std::exception &ex) {
|
||||
s_pRtmpSrv.reset();
|
||||
WarnL << ex.what();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
API_EXPORT void CALLTYPE listenEvent_onPlay(onEventPlay cb,void *userData){
|
||||
NoticeCenter::Instance().addListener((void *)(cb),Config::Broadcast::kBroadcastRtspSessionPlay,
|
||||
[cb,userData](BroadcastRtspSessionPlayArgs){
|
||||
static unordered_map<string, void *> s_timerKeyMap;
|
||||
static mutex s_mtx;
|
||||
uint64_t tag;
|
||||
{
|
||||
lock_guard<mutex> lck(s_mtx);
|
||||
//每个stream随机分配一个内存地址并且不重复
|
||||
tag = (uint64_t)&s_timerKeyMap[stream];
|
||||
}
|
||||
string appTmp(app);
|
||||
string streamTmp(stream);
|
||||
AsyncTaskThread::Instance().CancelTask(tag);
|
||||
int i = 2;
|
||||
AsyncTaskThread::Instance().DoTaskDelay(tag,50,[cb,userData,appTmp,streamTmp,i](){
|
||||
InfoL << "listenEvent_onPlay:" << appTmp << " " << streamTmp << " " << i;
|
||||
cb(userData,appTmp.data(),streamTmp.data());
|
||||
return (--const_cast<int &>(i)) > 0;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
API_EXPORT void CALLTYPE listenEvent_onRegistRtsp(onEventRegistMediaSrc cb,void *userData){
|
||||
NoticeCenter::Instance().addListener((void *)(cb),Config::Broadcast::kBroadcastRtspSrcRegisted,
|
||||
[cb,userData](BroadcastRtspSrcRegistedArgs){
|
||||
cb(userData,app,stream);
|
||||
});
|
||||
}
|
||||
|
||||
API_EXPORT void CALLTYPE listenEvent_onRegistRtmp(onEventRegistMediaSrc cb,void *userData){
|
||||
NoticeCenter::Instance().addListener((void *)(cb),Config::Broadcast::kBroadcastRtmpSrcRegisted,
|
||||
[cb,userData](BroadcastRtmpSrcRegistedArgs){
|
||||
cb(userData,app,stream);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
API_EXPORT void CALLTYPE log_printf(LogType level,const char* file, const char* function, int line,const char *fmt,...){
|
||||
LogInfoMaker info((LogLevel)level,file,function,line);
|
||||
va_list pArg;
|
||||
va_start(pArg, fmt);
|
||||
char buf[4096];
|
||||
int n = vsprintf(buf, fmt, pArg);
|
||||
buf[n] = '\0';
|
||||
va_end(pArg);
|
||||
info << buf;
|
||||
}
|
||||
API_EXPORT void CALLTYPE log_setLevel(LogType level){
|
||||
Logger::Instance().setLevel((LogLevel)level);
|
||||
}
|
||||
|
||||
class LogoutChannel: public LogChannel {
|
||||
public:
|
||||
LogoutChannel(const string &name, onLogOut cb, LogLevel level = LDebug)
|
||||
:LogChannel(name, level, "%Y-%m-%d %H:%M:%S"){
|
||||
_cb = cb;
|
||||
}
|
||||
virtual ~LogoutChannel(){}
|
||||
void write(const LogInfo_ptr &logInfo){
|
||||
if (level() > logInfo->getLevel()) {
|
||||
return;
|
||||
}
|
||||
stringstream strStream;
|
||||
logInfo->format(strStream, timeFormat().data(), false);
|
||||
auto strTmp = strStream.str();
|
||||
if (_cb) {
|
||||
_cb(strTmp.data(), strTmp.size());
|
||||
}
|
||||
}
|
||||
private:
|
||||
onLogOut _cb = nullptr;
|
||||
};
|
||||
|
||||
API_EXPORT void CALLTYPE log_setOnLogOut(onLogOut cb){
|
||||
std::shared_ptr<LogoutChannel> chn(new LogoutChannel("LogoutChannel",cb,LTrace));
|
||||
Logger::Instance().add(chn);
|
||||
}
|
||||
|
||||
|
||||
155
c_wrapper/src/common.h
Executable file
155
c_wrapper/src/common.h
Executable file
@@ -0,0 +1,155 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
|
||||
*
|
||||
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef SRC_COMMON_H_
|
||||
#define SRC_COMMON_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#if defined(_WIN32)
|
||||
#if defined(MediaKitWrapper_EXPORTS)
|
||||
#define API_EXPORT __declspec(dllexport)
|
||||
#else
|
||||
#define API_EXPORT __declspec(dllimport)
|
||||
#endif
|
||||
|
||||
#define CALLTYPE __cdecl
|
||||
#else
|
||||
#define API_EXPORT
|
||||
#define CALLTYPE
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/////////////////////////environment init////////////////////////////////
|
||||
API_EXPORT void CALLTYPE onAppStart();
|
||||
API_EXPORT void CALLTYPE onAppExit();
|
||||
|
||||
/*
|
||||
* 描述:创建Http服务器
|
||||
* 参数:port:htt监听端口,推荐80
|
||||
* 返回值:0:成功;-1:失败
|
||||
*/
|
||||
API_EXPORT int CALLTYPE initHttpServer(unsigned short port);
|
||||
|
||||
/*
|
||||
* 描述:创建RTSP服务器
|
||||
* 参数:port:rtsp监听端口,推荐554
|
||||
* 返回值:0:成功;-1:失败
|
||||
*/
|
||||
API_EXPORT int CALLTYPE initRtspServer(unsigned short port);
|
||||
|
||||
/*
|
||||
* 描述:创建RTMP服务器
|
||||
* 参数:port:rtmp监听端口,推荐1935
|
||||
* 返回值:0:成功;-1:失败
|
||||
*/
|
||||
API_EXPORT int CALLTYPE initRtmpServer(unsigned short port);
|
||||
|
||||
/*
|
||||
* 描述:播放事件回调函数定义
|
||||
* 参数:userData:用户数据指针strApp:应用名,strStream:流名称
|
||||
*/
|
||||
typedef void (CALLTYPE *onEventPlay)(void *userData,const char *strApp,const char *strStream);
|
||||
|
||||
/*
|
||||
* 描述:监听事件
|
||||
* 参数:cb:回调函数指针,userData:用户数据指针
|
||||
* 返回值:无
|
||||
*/
|
||||
API_EXPORT void CALLTYPE listenEvent_onPlay(onEventPlay cb,void *userData);
|
||||
|
||||
/*
|
||||
* 描述:注册RTSP事件
|
||||
* 参数:userData:用户数据指针strApp:应用名,strStream:流名称
|
||||
*/
|
||||
typedef void (CALLTYPE *onEventRegistMediaSrc)(void *userData,const char *strApp,const char *strStream);
|
||||
|
||||
/*
|
||||
* 描述:监听事件
|
||||
* 参数:cb:回调函数指针,userData:用户数据指针
|
||||
* 返回值:无
|
||||
*/
|
||||
API_EXPORT void CALLTYPE listenEvent_onRegistRtsp(onEventRegistMediaSrc cb,void *userData);
|
||||
|
||||
/*
|
||||
* 描述:监听事件
|
||||
* 参数:cb:回调函数指针,userData:用户数据指针
|
||||
* 返回值:无
|
||||
*/
|
||||
API_EXPORT void CALLTYPE listenEvent_onRegistRtmp(onEventRegistMediaSrc cb,void *userData);
|
||||
|
||||
|
||||
/////////////////////////日志////////////////////////////////
|
||||
|
||||
typedef enum {
|
||||
//日志级别
|
||||
LogTrace = 0, LogDebug, LogInfo, LogWarn, LogError, LogFatal,
|
||||
} LogType;
|
||||
|
||||
typedef void(CALLTYPE *onLogOut)(const char *strLog, int iLogLen);
|
||||
|
||||
/*
|
||||
* 描述:设置Log输出回调
|
||||
* 参数:onLogOut:回调函数
|
||||
* 返回值:无
|
||||
*/
|
||||
API_EXPORT void CALLTYPE log_setOnLogOut(onLogOut);
|
||||
|
||||
/*
|
||||
* 描述:设在日志显示级别
|
||||
* 参数:level:日志级别
|
||||
* 返回值:无
|
||||
*/
|
||||
API_EXPORT void CALLTYPE log_setLevel(LogType level);
|
||||
|
||||
/*
|
||||
* 描述:打印日志
|
||||
* 参数:level:日志级别;file:文件名称,function:函数名称,line:所在行数,fmt:格式化字符串
|
||||
* 返回值:无
|
||||
*/
|
||||
API_EXPORT void CALLTYPE log_printf(LogType level, const char* file, const char* function, int line, const char *fmt, ...);
|
||||
|
||||
|
||||
#define log_trace(fmt,...) log_printf(LogTrace,__FILE__,__FUNCTION__,__LINE__,fmt,__VA_ARGS__)
|
||||
#define log_debug(fmt,...) log_printf(LogDebug,__FILE__,__FUNCTION__,__LINE__,fmt,__VA_ARGS__)
|
||||
#define log_info(fmt,...) log_printf(LogInfo,__FILE__,__FUNCTION__,__LINE__,fmt,__VA_ARGS__)
|
||||
#define log_warn(fmt,...) log_printf(LogWarn,__FILE__,__FUNCTION__,__LINE__,fmt,__VA_ARGS__)
|
||||
#define log_error(fmt,...) log_printf(LogError,__FILE__,__FUNCTION__,__LINE__,fmt,__VA_ARGS__)
|
||||
#define log_fatal(fmt,...) log_printf(LogFatal,__FILE__,__FUNCTION__,__LINE__,fmt,__VA_ARGS__)
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif /* SRC_COMMON_H_ */
|
||||
73
c_wrapper/src/httpdownloader.cpp
Executable file
73
c_wrapper/src/httpdownloader.cpp
Executable file
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
|
||||
*
|
||||
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "httpdownloader.h"
|
||||
|
||||
#include "Util/logger.h"
|
||||
#include "Util/TimeTicker.h"
|
||||
#include "Util/onceToken.h"
|
||||
#include "Http/HttpDownloader.h"
|
||||
#include "cleaner.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace ZL::Util;
|
||||
using namespace ZL::Http;
|
||||
|
||||
|
||||
static recursive_mutex s_mtxMapDownloader;
|
||||
static unordered_map<void *, HttpDownloader::Ptr> s_mapDownloader;
|
||||
|
||||
static onceToken s_token([](){
|
||||
cleaner::Instance().push_front([](){
|
||||
lock_guard<recursive_mutex> lck(s_mtxMapDownloader);
|
||||
s_mapDownloader.clear();
|
||||
DebugL << "clear httpdownloader" << endl;
|
||||
});
|
||||
},nullptr);
|
||||
|
||||
|
||||
|
||||
API_EXPORT HttpDownloaderContex CALLTYPE createDownloader(){
|
||||
HttpDownloader::Ptr ret(new HttpDownloader());
|
||||
lock_guard<recursive_mutex> lck(s_mtxMapDownloader);
|
||||
s_mapDownloader.emplace(ret.get(),ret);
|
||||
return ret.get();
|
||||
}
|
||||
API_EXPORT void CALLTYPE downloader_startDownload(HttpDownloaderContex ctx,const char *url,downloader_onResult cb,void *userData){
|
||||
HttpDownloader *ptr = (HttpDownloader *)ctx;
|
||||
string urlTmp(url);
|
||||
ptr->startDownload(url, [cb,userData,urlTmp](int code,const char *errMsg,const char *filePath){
|
||||
if(cb){
|
||||
InfoL << code << " " << errMsg << " " << filePath << " " << urlTmp;
|
||||
cb(userData,code,errMsg,filePath);
|
||||
}
|
||||
});
|
||||
}
|
||||
API_EXPORT void CALLTYPE releaseDownloader(HttpDownloaderContex ctx){
|
||||
lock_guard<recursive_mutex> lck(s_mtxMapDownloader);
|
||||
s_mapDownloader.erase(ctx);
|
||||
}
|
||||
|
||||
49
c_wrapper/src/httpdownloader.h
Executable file
49
c_wrapper/src/httpdownloader.h
Executable file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
|
||||
*
|
||||
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef SRC_HTTPDOWNLOADER_H_
|
||||
#define SRC_HTTPDOWNLOADER_H_
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
////////////////////////////////////////Httpdownloader/////////////////////////////////////////////////
|
||||
typedef void * HttpDownloaderContex;
|
||||
typedef void(CALLTYPE *downloader_onResult)(void *userData,int code,const char *errMsg,const char *filePath);
|
||||
|
||||
API_EXPORT HttpDownloaderContex CALLTYPE createDownloader();
|
||||
API_EXPORT void CALLTYPE downloader_startDownload(HttpDownloaderContex ctx,const char *url,downloader_onResult cb,void *userData);
|
||||
API_EXPORT void CALLTYPE releaseDownloader(HttpDownloaderContex ctx);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* SRC_HTTPDOWNLOADER_H_ */
|
||||
90
c_wrapper/src/media.cpp
Executable file
90
c_wrapper/src/media.cpp
Executable file
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
|
||||
*
|
||||
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "media.h"
|
||||
#include "Util/logger.h"
|
||||
#include "Util/TimeTicker.h"
|
||||
#include "Util/onceToken.h"
|
||||
#include "Device/Device.h"
|
||||
#include "cleaner.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace ZL::DEV;
|
||||
using namespace ZL::Util;
|
||||
|
||||
static recursive_mutex s_mtxMapMedia;
|
||||
static unordered_map<void *, DevChannel::Ptr> s_mapMedia;
|
||||
|
||||
static onceToken s_token([](){
|
||||
cleaner::Instance().push_front([](){
|
||||
lock_guard<recursive_mutex> lck(s_mtxMapMedia);
|
||||
s_mapMedia.clear();
|
||||
DebugL << "clear media" << endl;
|
||||
});
|
||||
},nullptr);
|
||||
|
||||
|
||||
|
||||
//////////////////////////Rtsp media///////////////////////////
|
||||
API_EXPORT MediaContext CALLTYPE createMedia(const char *appName,const char *mediaName) {
|
||||
DevChannel::Ptr ret(new DevChannel(appName,mediaName));
|
||||
lock_guard<recursive_mutex> lck(s_mtxMapMedia);
|
||||
s_mapMedia.emplace((void *) (ret.get()), ret);
|
||||
return ret.get();
|
||||
}
|
||||
API_EXPORT void CALLTYPE releaseMedia(MediaContext ctx) {
|
||||
lock_guard<recursive_mutex> lck(s_mtxMapMedia);
|
||||
s_mapMedia.erase(ctx);
|
||||
}
|
||||
|
||||
API_EXPORT void CALLTYPE media_initVideo(MediaContext ctx, int width, int height, int frameRate) {
|
||||
DevChannel *ptr = (DevChannel *) ctx;
|
||||
VideoInfo info;
|
||||
info.iFrameRate = frameRate;
|
||||
info.iWidth = width;
|
||||
info.iHeight = height;
|
||||
ptr->initVideo(info);
|
||||
}
|
||||
API_EXPORT void CALLTYPE media_initAudio(MediaContext ctx, int channel, int sampleBit, int sampleRate) {
|
||||
DevChannel *ptr = (DevChannel *) ctx;
|
||||
AudioInfo info;
|
||||
info.iSampleRate = sampleRate;
|
||||
info.iChannel = channel;
|
||||
info.iSampleBit = sampleBit;
|
||||
ptr->initAudio(info);
|
||||
}
|
||||
API_EXPORT void CALLTYPE media_inputH264(MediaContext ctx, void *data, int len, unsigned long stamp) {
|
||||
//TimeTicker();
|
||||
DevChannel *ptr = (DevChannel *) ctx;
|
||||
ptr->inputH264((char *) data, len, stamp);
|
||||
}
|
||||
API_EXPORT void CALLTYPE media_inputAAC(MediaContext ctx, void *data, int len,unsigned long stamp) {
|
||||
//TimeTicker();
|
||||
DevChannel *ptr = (DevChannel *) ctx;
|
||||
ptr->inputAAC((char *) data, len, stamp);
|
||||
}
|
||||
|
||||
|
||||
87
c_wrapper/src/media.h
Executable file
87
c_wrapper/src/media.h
Executable file
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
|
||||
*
|
||||
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef SRC_MEDIA_H_
|
||||
#define SRC_MEDIA_H_
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
////////////////////////////////Rtsp media////////////////////////////////////////////
|
||||
|
||||
typedef void* MediaContext;
|
||||
/*
|
||||
* 描述:创建一个媒体源
|
||||
* 参数:mediaName:媒体名称,url地址的一部分
|
||||
* 返回值:媒体源句柄
|
||||
*/
|
||||
API_EXPORT MediaContext CALLTYPE createMedia(const char *appName,const char *mediaName);
|
||||
|
||||
/*
|
||||
* 描述:销毁媒体源
|
||||
* 参数:ctx:媒体源句柄
|
||||
* 返回值:无
|
||||
*/
|
||||
API_EXPORT void CALLTYPE releaseMedia(MediaContext ctx);
|
||||
|
||||
/*
|
||||
* 描述:初始化媒体源的视频信息
|
||||
* 参数:ctx:媒体源句柄;width:视频宽度;height:视频高度;frameRate:视频fps
|
||||
* 返回值:无
|
||||
*/
|
||||
API_EXPORT void CALLTYPE media_initVideo(MediaContext ctx, int width, int height, int frameRate);
|
||||
|
||||
/*
|
||||
* 描述:初始化媒体源的音频信息
|
||||
* 参数:ctx:媒体源句柄;channel:声道数;sampleBit:音频采样位数,支持16bit;sampleRate:音频采样率
|
||||
* 返回值:无
|
||||
*/
|
||||
API_EXPORT void CALLTYPE media_initAudio(MediaContext ctx, int channel, int sampleBit, int sampleRate);
|
||||
|
||||
/*
|
||||
* 描述:输入单帧H264视频,需要输入SPS和PPS帧,帧起始字节00 00 01,00 00 00 01均可
|
||||
* 参数:ctx:媒体源句柄;data:单帧H264数据;len:单帧H264数据字节数;stamp:时间戳,毫秒
|
||||
* 返回值:无
|
||||
*/
|
||||
API_EXPORT void CALLTYPE media_inputH264(MediaContext ctx, void *data, int len, unsigned long stamp);
|
||||
|
||||
/*
|
||||
* 描述:输入单帧AAC音频(有adts头)
|
||||
* 参数:ctx:媒体源句柄;data:单帧AAC数据;len:单帧AAC数据字节数;stamp:时间戳,毫秒
|
||||
* 返回值:无
|
||||
*/
|
||||
API_EXPORT void CALLTYPE media_inputAAC(MediaContext ctx, void *data, int len, unsigned long stamp);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* SRC_MEDIA_H_ */
|
||||
37
c_wrapper/src/mediakit.h
Executable file
37
c_wrapper/src/mediakit.h
Executable file
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
|
||||
*
|
||||
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef SRC_RTSPAPI_H_
|
||||
#define SRC_RTSPAPI_H_
|
||||
|
||||
#include "common.h"
|
||||
#include "httpdownloader.h"
|
||||
#include "media.h"
|
||||
#include "player.h"
|
||||
#include "proxyplayer.h"
|
||||
|
||||
|
||||
#endif /* SRC_RTSPAPI_H_ */
|
||||
349
c_wrapper/src/player.cpp
Executable file
349
c_wrapper/src/player.cpp
Executable file
@@ -0,0 +1,349 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
|
||||
*
|
||||
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "player.h"
|
||||
#include "Util/logger.h"
|
||||
#include "Util/TimeTicker.h"
|
||||
#include "Util/onceToken.h"
|
||||
#include "Thread/ThreadPool.h"
|
||||
#include "Poller/EventPoller.h"
|
||||
#include "Player/MediaPlayer.h"
|
||||
#include "H264/H264Parser.h"
|
||||
#include "cleaner.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace ZL::Util;
|
||||
using namespace ZL::Thread;
|
||||
using namespace ZL::Player;
|
||||
using namespace ZL::Rtmp;
|
||||
using namespace ZL::Rtsp;
|
||||
|
||||
static recursive_mutex s_mtxMapPlayer;
|
||||
static unordered_map<void *, MediaPlayer::Ptr> s_mapPlayer;
|
||||
|
||||
static onceToken s_token([](){
|
||||
cleaner::Instance().push_front([](){
|
||||
lock_guard<recursive_mutex> lck(s_mtxMapPlayer);
|
||||
s_mapPlayer.clear();
|
||||
DebugL << "clear player" << endl;
|
||||
});
|
||||
},nullptr);
|
||||
|
||||
|
||||
////////////////////////rtsp player/////////////////////////////////////////
|
||||
#define getPlayer(ctx) \
|
||||
MediaPlayer::Ptr player;\
|
||||
{\
|
||||
lock_guard<recursive_mutex> lck(s_mtxMapPlayer);\
|
||||
auto it = s_mapPlayer.find(ctx);\
|
||||
if(it != s_mapPlayer.end()){\
|
||||
player = it->second;\
|
||||
}\
|
||||
}
|
||||
API_EXPORT PlayerContext CALLTYPE createPlayer() {
|
||||
lock_guard<recursive_mutex> lck(s_mtxMapPlayer);
|
||||
MediaPlayer::Ptr ret(new MediaPlayer());
|
||||
s_mapPlayer.emplace(ret.get(), ret);
|
||||
if(s_mapPlayer.size() > 16){
|
||||
FatalL << s_mapPlayer.size();
|
||||
}
|
||||
return ret.get();
|
||||
}
|
||||
API_EXPORT void CALLTYPE releasePlayer(PlayerContext ctx) {
|
||||
getPlayer(ctx);
|
||||
if (!player) {
|
||||
return;
|
||||
}
|
||||
player_setOnGetAudio(ctx, nullptr, nullptr);
|
||||
player_setOnGetVideo(ctx, nullptr, nullptr);
|
||||
player_setOnPlayResult(ctx, nullptr, nullptr);
|
||||
player_setOnShutdown(ctx, nullptr, nullptr);
|
||||
lock_guard<recursive_mutex> lck(s_mtxMapPlayer);
|
||||
s_mapPlayer.erase(ctx);
|
||||
|
||||
ASYNC_TRACE([player]() {
|
||||
lock_guard<recursive_mutex> lck(s_mtxMapPlayer);
|
||||
player->teardown();
|
||||
});
|
||||
}
|
||||
|
||||
API_EXPORT void CALLTYPE player_setOptionInt(PlayerContext ctx,const char* key,int val){
|
||||
string keyTmp(key);
|
||||
ASYNC_TRACE([ctx,keyTmp,val](){
|
||||
getPlayer(ctx);
|
||||
if (!player) {
|
||||
return;
|
||||
}
|
||||
(*player)[keyTmp] = val;
|
||||
});
|
||||
}
|
||||
API_EXPORT void CALLTYPE player_setOptionString(PlayerContext ctx,const char* key,const char *val){
|
||||
string keyTmp(key);
|
||||
string valTmp(val);
|
||||
ASYNC_TRACE([ctx,keyTmp,valTmp](){
|
||||
getPlayer(ctx);
|
||||
if (!player) {
|
||||
return;
|
||||
}
|
||||
(*player)[keyTmp] = valTmp;
|
||||
});
|
||||
}
|
||||
API_EXPORT void CALLTYPE player_play(PlayerContext ctx, const char* url) {
|
||||
string urlTmp(url);
|
||||
ASYNC_TRACE([ctx,urlTmp](){
|
||||
getPlayer(ctx);
|
||||
if (!player) {
|
||||
return;
|
||||
}
|
||||
player->play(urlTmp.data());
|
||||
});
|
||||
}
|
||||
|
||||
API_EXPORT void CALLTYPE player_pause(PlayerContext ctx, int pause) {
|
||||
ASYNC_TRACE([ctx,pause](){
|
||||
getPlayer(ctx);
|
||||
if (!player) {
|
||||
return;
|
||||
}
|
||||
player->pause(pause);
|
||||
});
|
||||
}
|
||||
|
||||
API_EXPORT void CALLTYPE player_seekTo(PlayerContext ctx, float fProgress) {
|
||||
ASYNC_TRACE([ctx,fProgress]() {
|
||||
getPlayer(ctx);
|
||||
if (!player) {
|
||||
return;
|
||||
}
|
||||
return player->seekTo(fProgress);
|
||||
});
|
||||
}
|
||||
API_EXPORT void CALLTYPE player_setOnShutdown(PlayerContext ctx, player_onResult cb, void *userData) {
|
||||
getPlayer(ctx);
|
||||
if (!player) {
|
||||
return;
|
||||
}
|
||||
if(cb){
|
||||
SYNC_TRACE([&](){
|
||||
player->setOnShutdown([cb,userData](const SockException &ex) {
|
||||
cb(userData,ex.getErrCode(),ex.what());
|
||||
});
|
||||
});
|
||||
}else{
|
||||
SYNC_TRACE([&](){
|
||||
player->setOnShutdown(nullptr);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
API_EXPORT void CALLTYPE player_setOnPlayResult(PlayerContext ctx, player_onResult cb, void *userData) {
|
||||
getPlayer(ctx);
|
||||
if (!player) {
|
||||
return;
|
||||
}
|
||||
if (cb) {
|
||||
SYNC_TRACE([&](){
|
||||
player->setOnPlayResult([cb,userData](const SockException &ex) {
|
||||
cb(userData,ex.getErrCode(),ex.what());
|
||||
});
|
||||
});
|
||||
} else {
|
||||
SYNC_TRACE([&](){
|
||||
player->setOnPlayResult(nullptr);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
API_EXPORT void CALLTYPE player_setOnGetVideo(PlayerContext ctx, player_onGetH264 cb,
|
||||
void *userData) {
|
||||
getPlayer(ctx);
|
||||
if (!player) {
|
||||
return;
|
||||
}
|
||||
if (cb) {
|
||||
std::shared_ptr<H264Parser> pParser(new H264Parser());
|
||||
SYNC_TRACE([&](){
|
||||
player->setOnVideoCB([cb,userData,pParser](const H264Frame &frame) {
|
||||
pParser->inputH264(frame.data, frame.timeStamp);
|
||||
cb(userData, (void *)frame.data.data(), frame.data.size(), frame.timeStamp,pParser->getPts());
|
||||
});
|
||||
});
|
||||
} else {
|
||||
SYNC_TRACE([&](){
|
||||
player->setOnVideoCB(nullptr);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
API_EXPORT void CALLTYPE player_setOnGetAudio(PlayerContext ctx, player_onGetAAC cb,
|
||||
void *userData) {
|
||||
getPlayer(ctx);
|
||||
if (!player) {
|
||||
return;
|
||||
}
|
||||
if (cb) {
|
||||
SYNC_TRACE([&](){
|
||||
player->setOnAudioCB([cb,userData](const AdtsFrame &frame) {
|
||||
cb(userData, (void *)frame.data, frame.aac_frame_length, frame.timeStamp);
|
||||
});
|
||||
});
|
||||
} else {
|
||||
SYNC_TRACE([&](){
|
||||
player->setOnAudioCB(nullptr);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
API_EXPORT int CALLTYPE player_getVideoWidth(PlayerContext ctx) {
|
||||
getPlayer(ctx);
|
||||
if (!player) {
|
||||
return -1;
|
||||
}
|
||||
return player->getVideoWidth();
|
||||
}
|
||||
|
||||
API_EXPORT int CALLTYPE player_getVideoHeight(PlayerContext ctx) {
|
||||
getPlayer(ctx);
|
||||
if (!player) {
|
||||
return -1;
|
||||
}
|
||||
return player->getVideoHeight();
|
||||
}
|
||||
|
||||
API_EXPORT int CALLTYPE player_getVideoFps(PlayerContext ctx) {
|
||||
getPlayer(ctx);
|
||||
if (!player) {
|
||||
return -1;
|
||||
}
|
||||
return player->getVideoFps();
|
||||
}
|
||||
|
||||
API_EXPORT int CALLTYPE player_getAudioSampleRate(PlayerContext ctx) {
|
||||
getPlayer(ctx);
|
||||
if (!player) {
|
||||
return -1;
|
||||
}
|
||||
return player->getAudioSampleRate();
|
||||
}
|
||||
|
||||
API_EXPORT int CALLTYPE player_getAudioSampleBit(PlayerContext ctx) {
|
||||
getPlayer(ctx);
|
||||
if (!player) {
|
||||
return -1;
|
||||
}
|
||||
return player->getAudioSampleBit();
|
||||
}
|
||||
|
||||
API_EXPORT int CALLTYPE player_getAudioChannel(PlayerContext ctx) {
|
||||
getPlayer(ctx);
|
||||
if (!player) {
|
||||
return -1;
|
||||
}
|
||||
return player->getAudioChannel();
|
||||
}
|
||||
|
||||
API_EXPORT int CALLTYPE player_getH264PPS(PlayerContext ctx, char *buf, int bufsize) {
|
||||
getPlayer(ctx);
|
||||
if (!player) {
|
||||
return -1;
|
||||
}
|
||||
if (bufsize < (int) player->getPps().size() || player->getPps().empty()) {
|
||||
return -1;
|
||||
}
|
||||
memcpy(buf, player->getPps().data(), player->getPps().size());
|
||||
return player->getPps().size();
|
||||
}
|
||||
API_EXPORT int CALLTYPE player_getH264SPS(PlayerContext ctx, char *buf, int bufsize) {
|
||||
getPlayer(ctx);
|
||||
if (!player) {
|
||||
return -1;
|
||||
}
|
||||
if (bufsize < (int) player->getSps().size() || player->getSps().empty()) {
|
||||
return -1;
|
||||
}
|
||||
memcpy(buf, player->getSps().data(), player->getSps().size());
|
||||
return player->getSps().size();
|
||||
}
|
||||
|
||||
API_EXPORT int CALLTYPE player_getAacCfg(PlayerContext ctx, char *buf, int bufsize) {
|
||||
getPlayer(ctx);
|
||||
if (!player) {
|
||||
return -1;
|
||||
}
|
||||
if (bufsize < (int) player->getAudioCfg().size()) {
|
||||
return -1;
|
||||
}
|
||||
memcpy(buf, player->getAudioCfg().data(), player->getAudioCfg().size());
|
||||
return player->getAudioCfg().size();
|
||||
}
|
||||
|
||||
API_EXPORT int CALLTYPE player_containAudio(PlayerContext ctx) {
|
||||
getPlayer(ctx);
|
||||
if (!player) {
|
||||
return -1;
|
||||
}
|
||||
return player->containAudio();
|
||||
|
||||
}
|
||||
API_EXPORT int CALLTYPE player_containVideo(PlayerContext ctx) {
|
||||
getPlayer(ctx);
|
||||
if (!player) {
|
||||
return -1;
|
||||
}
|
||||
return player->containVideo();
|
||||
}
|
||||
|
||||
API_EXPORT int CALLTYPE player_isInited(PlayerContext ctx) {
|
||||
getPlayer(ctx);
|
||||
if (!player) {
|
||||
return -1;
|
||||
}
|
||||
return player->isInited();
|
||||
}
|
||||
API_EXPORT float CALLTYPE player_getDuration(PlayerContext ctx) {
|
||||
getPlayer(ctx);
|
||||
if (!player) {
|
||||
return -1;
|
||||
}
|
||||
return player->getDuration();
|
||||
}
|
||||
|
||||
API_EXPORT float CALLTYPE player_getProgress(PlayerContext ctx) {
|
||||
getPlayer(ctx);
|
||||
if (!player) {
|
||||
return -1;
|
||||
}
|
||||
return player->getProgress();
|
||||
}
|
||||
|
||||
API_EXPORT float CALLTYPE player_getLossRate(PlayerContext ctx, int trackId) {
|
||||
getPlayer(ctx);
|
||||
if (!player) {
|
||||
return -1;
|
||||
}
|
||||
return player->getRtpLossRate(trackId);
|
||||
}
|
||||
|
||||
235
c_wrapper/src/player.h
Executable file
235
c_wrapper/src/player.h
Executable file
@@ -0,0 +1,235 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
|
||||
*
|
||||
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef SRC_PLAYER_H_
|
||||
#define SRC_PLAYER_H_
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
////////////////////////////////////////RTSP Player/////////////////////////////////////////////////
|
||||
typedef void* PlayerContext;
|
||||
typedef void(CALLTYPE *player_onResult)(void *userData,int errCode,const char *errMsg);
|
||||
typedef void(CALLTYPE *player_onGetAAC)(void *userData,void *data,int len,unsigned long timeStamp);
|
||||
typedef void(CALLTYPE *player_onGetH264)(void *userData,void *data,int len,unsigned long dts,unsigned long pts);
|
||||
/*
|
||||
* 描述:创建一个Rtsp播放器
|
||||
* 参数:无
|
||||
* 返回值:Rtsp播放器句柄
|
||||
*/
|
||||
API_EXPORT PlayerContext CALLTYPE createPlayer();
|
||||
|
||||
/*
|
||||
* 描述:销毁一个播放器
|
||||
* 参数:ctx:播放器句柄
|
||||
* 返回值:无
|
||||
*/
|
||||
API_EXPORT void CALLTYPE releasePlayer(PlayerContext ctx);
|
||||
|
||||
|
||||
/*
|
||||
* 描述:设置播放器配置选项
|
||||
* 参数: ctx:播放器句柄
|
||||
* key:配置项键,例如 rtp_type
|
||||
* val:值,例如 //设置rtp传输类型,可选项有0(tcp,默认)、1(udp)、2(组播)
|
||||
* 返回值:无
|
||||
*/
|
||||
API_EXPORT void CALLTYPE player_setOptionInt(PlayerContext ctx,const char* key,int val);
|
||||
API_EXPORT void CALLTYPE player_setOptionString(PlayerContext ctx,const char* key,const char *val);
|
||||
|
||||
|
||||
/*
|
||||
* 描述:播放rtsp链接(仅支持H264与AAC负载)
|
||||
* 参数: ctx:播放器句柄
|
||||
* url:rtsp链接
|
||||
* 返回值:无
|
||||
*/
|
||||
API_EXPORT void CALLTYPE player_play(PlayerContext ctx,const char* url);
|
||||
|
||||
/*
|
||||
* 描述:暂停播放RTSP
|
||||
* 参数:ctx:播放器句柄;pause:1:暂停播放,0:恢复播放
|
||||
* 返回值:无
|
||||
*/
|
||||
API_EXPORT void CALLTYPE player_pause(PlayerContext ctx,int pause);
|
||||
|
||||
/*
|
||||
* 描述:设置播放器异常停止回调函数
|
||||
* 参数:ctx:播放器句柄;cb:回调函数指针;userData:用户数据指针
|
||||
* 返回值:无
|
||||
*/
|
||||
API_EXPORT void CALLTYPE player_setOnShutdown(PlayerContext ctx,player_onResult cb,void *userData);
|
||||
|
||||
/*
|
||||
* 描述:设置播放器播放结果回调函数
|
||||
* 参数:ctx:播放器句柄,cb:回调函数指针;userData:用户数据指针
|
||||
* 返回值:无
|
||||
*/
|
||||
API_EXPORT void CALLTYPE player_setOnPlayResult(PlayerContext ctx,player_onResult cb,void *userData);
|
||||
|
||||
/*
|
||||
* 描述:设置播放器收到视频帧回调,I帧前为SPS,PPS帧;每帧包含00 00 00 01的帧头
|
||||
* 参数:ctx:播放器句柄,cb:回调函数指针;userData:用户数据指针
|
||||
* 返回值:无
|
||||
*/
|
||||
API_EXPORT void CALLTYPE player_setOnGetVideo(PlayerContext ctx,player_onGetH264 cb,void *userData);
|
||||
|
||||
|
||||
/*
|
||||
* 描述:设置播放器收到音频帧回调,每帧数据包含ADTS头
|
||||
* 参数:ctx:播放器句柄,cb:回调函数指针;userData:用户数据指针
|
||||
* 返回值:无
|
||||
*/
|
||||
API_EXPORT void CALLTYPE player_setOnGetAudio(PlayerContext ctx,player_onGetAAC cb,void *userData);
|
||||
|
||||
/*
|
||||
* 描述:获取视频宽度
|
||||
* 参数:ctx:播放器句柄
|
||||
* 返回值:视频宽度
|
||||
*/
|
||||
API_EXPORT int CALLTYPE player_getVideoWidth(PlayerContext ctx);
|
||||
|
||||
/*
|
||||
* 描述:获取视频高度
|
||||
* 参数:ctx:播放器句柄
|
||||
* 返回值:视频高度
|
||||
*/
|
||||
API_EXPORT int CALLTYPE CALLTYPE player_getVideoHeight(PlayerContext ctx);
|
||||
|
||||
/*
|
||||
* 描述:获取视频帧率
|
||||
* 参数:ctx:播放器句柄
|
||||
* 返回值:视频帧率
|
||||
*/
|
||||
API_EXPORT int CALLTYPE player_getVideoFps(PlayerContext ctx);
|
||||
|
||||
/*
|
||||
* 描述:获取音频采样率
|
||||
* 参数:ctx:播放器句柄
|
||||
* 返回值:音频采样率
|
||||
*/
|
||||
API_EXPORT int CALLTYPE player_getAudioSampleRate(PlayerContext ctx);
|
||||
|
||||
/*
|
||||
* 描述:获取音频采样位数(8bit或16bit)
|
||||
* 参数:ctx:播放器句柄
|
||||
* 返回值:音频采样位数
|
||||
*/
|
||||
API_EXPORT int CALLTYPE player_getAudioSampleBit(PlayerContext ctx);
|
||||
|
||||
/*
|
||||
* 描述:获取音频通道数(单声道1,双声道2)
|
||||
* 参数:ctx:播放器句柄
|
||||
* 返回值:音频通道数
|
||||
*/
|
||||
API_EXPORT int CALLTYPE player_getAudioChannel(PlayerContext ctx);
|
||||
|
||||
|
||||
/*
|
||||
* 描述:获取H264的PPS帧
|
||||
* 参数:ctx:播放器句柄,buf:存放PPS数据的缓存;bufsize:缓存大小
|
||||
* 返回值:帧数据长度
|
||||
*/
|
||||
API_EXPORT int CALLTYPE player_getH264PPS(PlayerContext ctx,char *buf,int bufsize);
|
||||
|
||||
|
||||
/*
|
||||
* 描述:获取H264的SPS帧
|
||||
* 参数:ctx:播放器句柄,buf:存放SPS数据的缓存;bufsize:缓存大小
|
||||
* 返回值:帧数据长度
|
||||
*/
|
||||
API_EXPORT int CALLTYPE player_getH264SPS(PlayerContext ctx,char *buf,int bufsize);
|
||||
|
||||
|
||||
/*
|
||||
* 描述:获取AAC编码配置信息
|
||||
* 参数:ctx:播放器句柄;buf:存放CFG数据的缓存;bufsize:缓存大小
|
||||
* 返回值:CFG数据长度
|
||||
*/
|
||||
API_EXPORT int CALLTYPE player_getAacCfg(PlayerContext ctx,char *buf,int bufsize);
|
||||
|
||||
|
||||
/*
|
||||
* 描述:是否包含音频数据
|
||||
* 参数:ctx:播放器句柄
|
||||
* 返回值:1:包含,0:不包含
|
||||
*/
|
||||
API_EXPORT int CALLTYPE player_containAudio(PlayerContext ctx);
|
||||
|
||||
|
||||
/*
|
||||
* 描述:是否包含视频数据
|
||||
* 参数:ctx:播放器句柄
|
||||
* 返回值:1:包含,0:不包含
|
||||
*/
|
||||
API_EXPORT int CALLTYPE player_containVideo(PlayerContext ctx);
|
||||
|
||||
|
||||
/*
|
||||
* 描述:是否已经初始化完成(获取完整的播放信息)
|
||||
* 参数:ctx:播放器句柄
|
||||
* 返回值:1:初始化完成,0:未完成
|
||||
*/
|
||||
API_EXPORT int CALLTYPE player_isInited(PlayerContext ctx);
|
||||
|
||||
/*
|
||||
* 描述:获取点播的时间长度,单位为秒(小于等于0,说明是直播,否则为点播)
|
||||
* 参数:ctx:播放器句柄
|
||||
* 返回值:点播的时间长度,单位秒
|
||||
*/
|
||||
API_EXPORT float CALLTYPE player_getDuration(PlayerContext ctx);
|
||||
|
||||
/*
|
||||
* 描述:获取点播播放进度
|
||||
* 参数:ctx:播放器句柄
|
||||
* 返回值:点播播放进度,取值范围未 0.0~1.0
|
||||
*/
|
||||
API_EXPORT float CALLTYPE player_getProgress(PlayerContext ctx);
|
||||
|
||||
/*
|
||||
* 描述:设置点播播放进度
|
||||
* 参数:ctx:播放器句柄;fProgress:播放进度,取值范围未 0.0~1.0
|
||||
* 返回值:无
|
||||
*/
|
||||
API_EXPORT void CALLTYPE player_seekTo(PlayerContext ctx, float fProgress);
|
||||
|
||||
/*
|
||||
* 描述:获取丢包率
|
||||
* 参数:ctx:播放器句柄;trackId:如果是-1,则返回总丢包率,否则返回视频或者音频的丢包率
|
||||
* 返回值:丢包率
|
||||
*/
|
||||
API_EXPORT float CALLTYPE player_getLossRate(PlayerContext ctx,int trackId);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* SRC_PLAYER_H_ */
|
||||
60
c_wrapper/src/proxyplayer.cpp
Normal file
60
c_wrapper/src/proxyplayer.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
|
||||
*
|
||||
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
#include "cleaner.h"
|
||||
#include "proxyplayer.h"
|
||||
#include "Device/PlayerProxy.h"
|
||||
#include "Util/onceToken.h"
|
||||
|
||||
using namespace ZL::DEV;
|
||||
using namespace ZL::Util;
|
||||
|
||||
static recursive_mutex s_mtxMapProxyPlayer;
|
||||
static unordered_map<void *, PlayerProxy::Ptr> s_mapProxyPlayer;
|
||||
|
||||
static onceToken s_token([](){
|
||||
cleaner::Instance().push_front([](){
|
||||
lock_guard<recursive_mutex> lck(s_mtxMapProxyPlayer);
|
||||
s_mapProxyPlayer.clear();
|
||||
DebugL << "clear proxyplayer" << endl;
|
||||
});
|
||||
},nullptr);
|
||||
|
||||
API_EXPORT ProxyPlayerContext CALLTYPE createProxyPlayer(const char *app,const char *stream,int rtp_type){
|
||||
PlayerProxy::Ptr ret(new PlayerProxy(app,stream));
|
||||
(*ret)[RtspPlayer::kRtpType] = rtp_type;
|
||||
|
||||
lock_guard<recursive_mutex> lck(s_mtxMapProxyPlayer);
|
||||
s_mapProxyPlayer.emplace(ret.get(),ret);
|
||||
return ret.get();
|
||||
}
|
||||
API_EXPORT void CALLTYPE releaseProxyPlayer(ProxyPlayerContext ctx){
|
||||
lock_guard<recursive_mutex> lck(s_mtxMapProxyPlayer);
|
||||
s_mapProxyPlayer.erase(ctx);
|
||||
}
|
||||
API_EXPORT void CALLTYPE proxyPlayer_play(ProxyPlayerContext ctx,const char *url){
|
||||
PlayerProxy *ptr = (PlayerProxy *)ctx;
|
||||
ptr->play(url);
|
||||
}
|
||||
67
c_wrapper/src/proxyplayer.h
Normal file
67
c_wrapper/src/proxyplayer.h
Normal file
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
|
||||
*
|
||||
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef SRC_PROXYPLAYER_H_
|
||||
#define SRC_PROXYPLAYER_H_
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef void* ProxyPlayerContext;
|
||||
|
||||
/*
|
||||
* 描述:创建一个代理播放器
|
||||
* 参数:app:应用名,生成url的一部分,rtsp://127.0.0.1/app/xxxx
|
||||
* stream:媒体流名,生成url的一部分,rtsp://127.0.0.1/xxxx/stream
|
||||
* rtp_type:如果播放的是rtsp连接则通过该参数配置设置rtp传输方式:RTP_TCP = 0, RTP_UDP = 1, RTP_MULTICAST = 2
|
||||
* 返回值:代理播放器句柄
|
||||
*/
|
||||
API_EXPORT ProxyPlayerContext CALLTYPE createProxyPlayer(const char *app,const char *stream,int rtp_type);
|
||||
|
||||
/*
|
||||
* 描述:销毁代理播放器
|
||||
* 参数:ctx:代理播放器句柄
|
||||
* 返回值:无
|
||||
*/
|
||||
API_EXPORT void CALLTYPE releaseProxyPlayer(ProxyPlayerContext ctx);
|
||||
|
||||
|
||||
/*
|
||||
* 描述:开始播放
|
||||
* 参数:url:rtsp/rtmp连接
|
||||
* 返回值:无
|
||||
*/
|
||||
API_EXPORT void CALLTYPE proxyPlayer_play(ProxyPlayerContext ctx,const char *url);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* SRC_PROXYPLAYER_H_ */
|
||||
Reference in New Issue
Block a user