ZLMediaKit/c_wrapper/src/player.cpp

350 lines
8.5 KiB
C++
Raw Normal View History

2017-12-13 18:15:22 +08:00
/*
* 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;\
}\
}
2017-12-19 10:34:52 +08:00
API_EXPORT PlayerContext API_CALL createPlayer() {
2017-12-13 18:15:22 +08:00
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();
}
2017-12-19 10:34:52 +08:00
API_EXPORT void API_CALL releasePlayer(PlayerContext ctx) {
2017-12-13 18:15:22 +08:00
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();
});
}
2017-12-19 10:34:52 +08:00
API_EXPORT void API_CALL player_setOptionInt(PlayerContext ctx,const char* key,int val){
2017-12-13 18:15:22 +08:00
string keyTmp(key);
ASYNC_TRACE([ctx,keyTmp,val](){
getPlayer(ctx);
if (!player) {
return;
}
(*player)[keyTmp] = val;
});
}
2017-12-19 10:34:52 +08:00
API_EXPORT void API_CALL player_setOptionString(PlayerContext ctx,const char* key,const char *val){
2017-12-13 18:15:22 +08:00
string keyTmp(key);
string valTmp(val);
ASYNC_TRACE([ctx,keyTmp,valTmp](){
getPlayer(ctx);
if (!player) {
return;
}
(*player)[keyTmp] = valTmp;
});
}
2017-12-19 10:34:52 +08:00
API_EXPORT void API_CALL player_play(PlayerContext ctx, const char* url) {
2017-12-13 18:15:22 +08:00
string urlTmp(url);
ASYNC_TRACE([ctx,urlTmp](){
getPlayer(ctx);
if (!player) {
return;
}
player->play(urlTmp.data());
});
}
2017-12-19 10:34:52 +08:00
API_EXPORT void API_CALL player_pause(PlayerContext ctx, int pause) {
2017-12-13 18:15:22 +08:00
ASYNC_TRACE([ctx,pause](){
getPlayer(ctx);
if (!player) {
return;
}
player->pause(pause);
});
}
2017-12-19 10:34:52 +08:00
API_EXPORT void API_CALL player_seekTo(PlayerContext ctx, float fProgress) {
2017-12-13 18:15:22 +08:00
ASYNC_TRACE([ctx,fProgress]() {
getPlayer(ctx);
if (!player) {
return;
}
return player->seekTo(fProgress);
});
}
2017-12-19 10:34:52 +08:00
API_EXPORT void API_CALL player_setOnShutdown(PlayerContext ctx, player_onResult cb, void *userData) {
2017-12-13 18:15:22 +08:00
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);
});
}
}
2017-12-19 10:34:52 +08:00
API_EXPORT void API_CALL player_setOnPlayResult(PlayerContext ctx, player_onResult cb, void *userData) {
2017-12-13 18:15:22 +08:00
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);
});
}
}
2017-12-19 10:34:52 +08:00
API_EXPORT void API_CALL player_setOnGetVideo(PlayerContext ctx, player_onGetH264 cb,
2017-12-13 18:15:22 +08:00
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);
});
}
}
2017-12-19 10:34:52 +08:00
API_EXPORT void API_CALL player_setOnGetAudio(PlayerContext ctx, player_onGetAAC cb,
2017-12-13 18:15:22 +08:00
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);
});
}
}
2017-12-19 10:34:52 +08:00
API_EXPORT int API_CALL player_getVideoWidth(PlayerContext ctx) {
2017-12-13 18:15:22 +08:00
getPlayer(ctx);
if (!player) {
return -1;
}
return player->getVideoWidth();
}
2017-12-19 10:34:52 +08:00
API_EXPORT int API_CALL player_getVideoHeight(PlayerContext ctx) {
2017-12-13 18:15:22 +08:00
getPlayer(ctx);
if (!player) {
return -1;
}
return player->getVideoHeight();
}
2017-12-19 10:34:52 +08:00
API_EXPORT int API_CALL player_getVideoFps(PlayerContext ctx) {
2017-12-13 18:15:22 +08:00
getPlayer(ctx);
if (!player) {
return -1;
}
return player->getVideoFps();
}
2017-12-19 10:34:52 +08:00
API_EXPORT int API_CALL player_getAudioSampleRate(PlayerContext ctx) {
2017-12-13 18:15:22 +08:00
getPlayer(ctx);
if (!player) {
return -1;
}
return player->getAudioSampleRate();
}
2017-12-19 10:34:52 +08:00
API_EXPORT int API_CALL player_getAudioSampleBit(PlayerContext ctx) {
2017-12-13 18:15:22 +08:00
getPlayer(ctx);
if (!player) {
return -1;
}
return player->getAudioSampleBit();
}
2017-12-19 10:34:52 +08:00
API_EXPORT int API_CALL player_getAudioChannel(PlayerContext ctx) {
2017-12-13 18:15:22 +08:00
getPlayer(ctx);
if (!player) {
return -1;
}
return player->getAudioChannel();
}
2017-12-19 10:34:52 +08:00
API_EXPORT int API_CALL player_getH264PPS(PlayerContext ctx, char *buf, int bufsize) {
2017-12-13 18:15:22 +08:00
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();
}
2017-12-19 10:34:52 +08:00
API_EXPORT int API_CALL player_getH264SPS(PlayerContext ctx, char *buf, int bufsize) {
2017-12-13 18:15:22 +08:00
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();
}
2017-12-19 10:34:52 +08:00
API_EXPORT int API_CALL player_getAacCfg(PlayerContext ctx, char *buf, int bufsize) {
2017-12-13 18:15:22 +08:00
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();
}
2017-12-19 10:34:52 +08:00
API_EXPORT int API_CALL player_containAudio(PlayerContext ctx) {
2017-12-13 18:15:22 +08:00
getPlayer(ctx);
if (!player) {
return -1;
}
return player->containAudio();
}
2017-12-19 10:34:52 +08:00
API_EXPORT int API_CALL player_containVideo(PlayerContext ctx) {
2017-12-13 18:15:22 +08:00
getPlayer(ctx);
if (!player) {
return -1;
}
return player->containVideo();
}
2017-12-19 10:34:52 +08:00
API_EXPORT int API_CALL player_isInited(PlayerContext ctx) {
2017-12-13 18:15:22 +08:00
getPlayer(ctx);
if (!player) {
return -1;
}
return player->isInited();
}
2017-12-19 10:34:52 +08:00
API_EXPORT float API_CALL player_getDuration(PlayerContext ctx) {
2017-12-13 18:15:22 +08:00
getPlayer(ctx);
if (!player) {
return -1;
}
return player->getDuration();
}
2017-12-19 10:34:52 +08:00
API_EXPORT float API_CALL player_getProgress(PlayerContext ctx) {
2017-12-13 18:15:22 +08:00
getPlayer(ctx);
if (!player) {
return -1;
}
return player->getProgress();
}
2017-12-19 10:34:52 +08:00
API_EXPORT float API_CALL player_getLossRate(PlayerContext ctx, int trackId) {
2017-12-13 18:15:22 +08:00
getPlayer(ctx);
if (!player) {
return -1;
}
return player->getRtpLossRate(trackId);
}