ZLMediaKit/api/source/mk_player.cpp

163 lines
5.7 KiB
C++
Raw Normal View History

2019-12-18 11:47:49 +08:00
/*
2020-04-04 20:30:09 +08:00
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
2019-12-18 11:45:33 +08:00
*
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
*
2020-04-04 20:30:09 +08:00
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
2019-12-18 11:45:33 +08:00
*/
#include "mk_player.h"
2019-12-18 11:45:33 +08:00
#include "Util/logger.h"
#include "Player/MediaPlayer.h"
using namespace std;
using namespace toolkit;
using namespace mediakit;
API_EXPORT mk_player API_CALL mk_player_create() {
2020-03-20 11:51:24 +08:00
MediaPlayer::Ptr *obj = new MediaPlayer::Ptr(new MediaPlayer());
return obj;
2019-12-18 11:45:33 +08:00
}
API_EXPORT void API_CALL mk_player_release(mk_player ctx) {
2020-03-20 11:51:24 +08:00
assert(ctx);
MediaPlayer::Ptr *obj = (MediaPlayer::Ptr *)ctx;
delete obj;
2019-12-18 11:45:33 +08:00
}
API_EXPORT void API_CALL mk_player_set_option(mk_player ctx,const char* key,const char *val){
2020-03-20 11:51:24 +08:00
assert(ctx && key && val);
MediaPlayer::Ptr &player = *((MediaPlayer::Ptr *)ctx);
string key_str(key), val_str(val);
player->getPoller()->async([key_str,val_str,player](){
//切换线程后再操作
(*player)[key_str] = val_str;
});
2019-12-18 11:45:33 +08:00
}
API_EXPORT void API_CALL mk_player_play(mk_player ctx, const char *url) {
2020-03-20 11:51:24 +08:00
assert(ctx && url);
MediaPlayer::Ptr &player = *((MediaPlayer::Ptr *)ctx);
string url_str(url);
player->getPoller()->async([url_str,player](){
//切换线程后再操作
player->play(url_str);
});
2019-12-18 11:45:33 +08:00
}
API_EXPORT void API_CALL mk_player_pause(mk_player ctx, int pause) {
2020-03-20 11:51:24 +08:00
assert(ctx);
MediaPlayer::Ptr &player = *((MediaPlayer::Ptr *)ctx);
player->getPoller()->async([pause,player](){
//切换线程后再操作
player->pause(pause);
});
2019-12-18 11:45:33 +08:00
}
API_EXPORT void API_CALL mk_player_seekto(mk_player ctx, float progress) {
assert(ctx);
MediaPlayer::Ptr &player = *((MediaPlayer::Ptr *)ctx);
player->getPoller()->async([progress,player](){
//切换线程后再操作
player->seekTo(progress);
});
}
static void mk_player_set_on_event(mk_player ctx, on_mk_play_event cb, void *user_data, int type) {
2020-03-20 11:51:24 +08:00
assert(ctx && cb);
MediaPlayer::Ptr &player = *((MediaPlayer::Ptr *)ctx);
player->getPoller()->async([cb,user_data,type,player](){
//切换线程后再操作
if(type == 0){
player->setOnPlayResult([cb,user_data](const SockException &ex){
cb(user_data,ex.getErrCode(),ex.what());
});
}else{
player->setOnShutdown([cb,user_data](const SockException &ex){
cb(user_data,ex.getErrCode(),ex.what());
});
}
});
2019-12-18 11:45:33 +08:00
}
API_EXPORT void API_CALL mk_player_set_on_result(mk_player ctx, on_mk_play_event cb, void *user_data) {
2020-03-20 11:51:24 +08:00
mk_player_set_on_event(ctx,cb,user_data,0);
2019-12-18 11:45:33 +08:00
}
API_EXPORT void API_CALL mk_player_set_on_shutdown(mk_player ctx, on_mk_play_event cb, void *user_data) {
2020-03-20 11:51:24 +08:00
mk_player_set_on_event(ctx,cb,user_data,1);
2019-12-18 11:45:33 +08:00
}
API_EXPORT void API_CALL mk_player_set_on_data(mk_player ctx, on_mk_play_data cb, void *user_data) {
2020-03-20 11:51:24 +08:00
assert(ctx && cb);
MediaPlayer::Ptr &player = *((MediaPlayer::Ptr *)ctx);
player->getPoller()->async([player,cb,user_data](){
//切换线程后再操作
auto delegate = std::make_shared<FrameWriterInterfaceHelper>([cb,user_data](const Frame::Ptr &frame){
cb(user_data,frame->getTrackType(),frame->getCodecId(),frame->data(),frame->size(),frame->dts(),frame->pts());
});
for(auto &track : player->getTracks()){
track->addDelegate(delegate);
}
});
2019-12-18 11:45:33 +08:00
}
API_EXPORT int API_CALL mk_player_video_width(mk_player ctx) {
2020-03-20 11:51:24 +08:00
assert(ctx);
MediaPlayer::Ptr &player = *((MediaPlayer::Ptr *)ctx);
auto track = dynamic_pointer_cast<VideoTrack>(player->getTrack(TrackVideo));
return track ? track->getVideoWidth() : 0;
2019-12-18 11:45:33 +08:00
}
API_EXPORT int API_CALL mk_player_video_height(mk_player ctx) {
2020-03-20 11:51:24 +08:00
assert(ctx);
MediaPlayer::Ptr &player = *((MediaPlayer::Ptr *)ctx);
auto track = dynamic_pointer_cast<VideoTrack>(player->getTrack(TrackVideo));
return track ? track->getVideoHeight() : 0;
2019-12-18 11:45:33 +08:00
}
API_EXPORT int API_CALL mk_player_video_fps(mk_player ctx) {
2020-03-20 11:51:24 +08:00
assert(ctx);
MediaPlayer::Ptr &player = *((MediaPlayer::Ptr *)ctx);
auto track = dynamic_pointer_cast<VideoTrack>(player->getTrack(TrackVideo));
return track ? track->getVideoFps() : 0;
2019-12-18 11:45:33 +08:00
}
API_EXPORT int API_CALL mk_player_audio_samplerate(mk_player ctx) {
2020-03-20 11:51:24 +08:00
assert(ctx);
MediaPlayer::Ptr &player = *((MediaPlayer::Ptr *)ctx);
auto track = dynamic_pointer_cast<AudioTrack>(player->getTrack(TrackAudio));
return track ? track->getAudioSampleRate() : 0;
2019-12-18 11:45:33 +08:00
}
API_EXPORT int API_CALL mk_player_audio_bit(mk_player ctx) {
2020-03-20 11:51:24 +08:00
assert(ctx);
MediaPlayer::Ptr &player = *((MediaPlayer::Ptr *)ctx);
auto track = dynamic_pointer_cast<AudioTrack>(player->getTrack(TrackAudio));
return track ? track->getAudioSampleBit() : 0;
2019-12-18 11:45:33 +08:00
}
API_EXPORT int API_CALL mk_player_audio_channel(mk_player ctx) {
2020-03-20 11:51:24 +08:00
assert(ctx);
MediaPlayer::Ptr &player = *((MediaPlayer::Ptr *)ctx);
auto track = dynamic_pointer_cast<AudioTrack>(player->getTrack(TrackAudio));
return track ? track->getAudioChannel() : 0;
2019-12-18 11:45:33 +08:00
}
API_EXPORT float API_CALL mk_player_duration(mk_player ctx) {
2020-03-20 11:51:24 +08:00
assert(ctx);
MediaPlayer::Ptr &player = *((MediaPlayer::Ptr *)ctx);
return player->getDuration();
2019-12-18 11:45:33 +08:00
}
API_EXPORT float API_CALL mk_player_progress(mk_player ctx) {
2020-03-20 11:51:24 +08:00
assert(ctx);
MediaPlayer::Ptr &player = *((MediaPlayer::Ptr *)ctx);
return player->getProgress();
2019-12-18 11:45:33 +08:00
}
API_EXPORT float API_CALL mk_player_loss_rate(mk_player ctx, int track_type) {
2020-03-20 11:51:24 +08:00
assert(ctx);
MediaPlayer::Ptr &player = *((MediaPlayer::Ptr *)ctx);
return player->getPacketLossRate((TrackType)track_type);
2019-12-18 11:45:33 +08:00
}