diff --git a/README.md b/README.md index 53999cf8..82b8c4d6 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ - 提供完整的[MediaServer](https://github.com/xiongziliang/ZLMediaKit/tree/master/server)服务器,可以免开发直接部署为商用服务器。 - 提供完善的[restful api](https://github.com/xiongziliang/ZLMediaKit/wiki/MediaServer%E6%94%AF%E6%8C%81%E7%9A%84HTTP-API)以及[web hook](https://github.com/xiongziliang/ZLMediaKit/wiki/MediaServer%E6%94%AF%E6%8C%81%E7%9A%84HTTP-HOOK-API),支持丰富的业务逻辑。 - 打通了视频监控协议栈与直播协议栈,对RTSP/RTMP支持都很完善。 -- 全面支持H265。 +- 全面支持H265/H264/AAC/G711。 ## 项目定位 diff --git a/api/include/mk_player.h b/api/include/mk_player.h index bd980891..88750e15 100755 --- a/api/include/mk_player.h +++ b/api/include/mk_player.h @@ -90,7 +90,7 @@ API_EXPORT void API_CALL mk_player_seektoByPos(mk_player ctx, int seekPos); /** * 设置播放器开启播放结果回调函数 * @param ctx 播放器指针 - * @param cb 回调函数指针,不得为null + * @param cb 回调函数指针,设置null立即取消回调 * @param user_data 用户数据指针 */ API_EXPORT void API_CALL mk_player_set_on_result(mk_player ctx, on_mk_play_event cb, void *user_data); @@ -98,16 +98,15 @@ API_EXPORT void API_CALL mk_player_set_on_result(mk_player ctx, on_mk_play_event /** * 设置播放被异常中断的回调 * @param ctx 播放器指针 - * @param cb 回调函数指针,不得为null + * @param cb 回调函数指针,设置null立即取消回调 * @param user_data 用户数据指针 */ API_EXPORT void API_CALL mk_player_set_on_shutdown(mk_player ctx, on_mk_play_event cb, void *user_data); /** * 设置音视频数据回调函数 - * 该接口在播放成功事件触发后才有效 * @param ctx 播放器指针 - * @param cb 回调函数指针,不得为null + * @param cb 回调函数指针,设置null立即取消回调 * @param user_data 用户数据指针 */ API_EXPORT void API_CALL mk_player_set_on_data(mk_player ctx, on_mk_play_data cb, void *user_data); diff --git a/api/source/mk_player.cpp b/api/source/mk_player.cpp index 7db8bc69..9fc0abc1 100755 --- a/api/source/mk_player.cpp +++ b/api/source/mk_player.cpp @@ -15,19 +15,131 @@ using namespace std; using namespace toolkit; using namespace mediakit; +class MediaPlayerForC : public std::enable_shared_from_this{ +public: + typedef std::shared_ptr Ptr; + + MediaPlayerForC(){ + _player = std::make_shared(); + } + ~MediaPlayerForC(){} + + MediaPlayer *operator->(){ + return _player.get(); + } + + void setup(){ + weak_ptr weak_self = shared_from_this(); + _player->setOnPlayResult([weak_self](const SockException &ex){ + auto strong_self = weak_self.lock(); + if(strong_self){ + strong_self->onEvent(false,ex); + } + }); + + _player->setOnShutdown([weak_self](const SockException &ex){ + auto strong_self = weak_self.lock(); + if(strong_self){ + strong_self->onEvent(true,ex); + } + }); + } + + void unset(){ + lock_guard lck(_mtx); + _on_play = nullptr; + _on_shutdown = nullptr; + _on_data = nullptr; + } + + void onEvent(bool is_shutdown, const SockException &ex){ + lock_guard lck(_mtx); + if(is_shutdown){ + //播放中断 + if(_on_shutdown){ + _on_shutdown(_on_shutdown_data,ex.getErrCode(),ex.what()); + } + return; + } + + //播放结果 + if(_on_play){ + _on_play(_on_play_data,ex.getErrCode(),ex.what()); + } + + if(ex){ + //播放失败 + return; + } + + //播放成功,添加事件回调 + weak_ptr weak_self = shared_from_this(); + auto delegate = std::make_shared([weak_self](const Frame::Ptr &frame) { + auto strong_self = weak_self.lock(); + if (strong_self) { + strong_self->onData(frame); + } + }); + for (auto &track : _player->getTracks()) { + track->addDelegate(delegate); + } + } + + void onData(const Frame::Ptr &frame){ + lock_guard lck(_mtx); + if(_on_data){ + _on_data(_on_data_data,frame->getTrackType(),frame->getCodecId(),frame->data(),frame->size(),frame->dts(),frame->pts()); + } + } + + void setOnEvent(on_mk_play_event cb, void *user_data, int type) { + lock_guard lck(_mtx); + if(type == 0){ + _on_play_data = user_data; + _on_play = cb; + }else{ + _on_shutdown_data = user_data; + _on_shutdown = cb; + } + } + + void setOnData(on_mk_play_data cb, void *user_data) { + lock_guard lck(_mtx); + _on_data_data = user_data; + _on_data = cb; + } + + MediaPlayer::Ptr& getPlayer(){ + return _player; + } +private: + MediaPlayer::Ptr _player; + recursive_mutex _mtx; + on_mk_play_event _on_play = nullptr; + on_mk_play_data _on_data = nullptr; + on_mk_play_event _on_shutdown = nullptr; + + void *_on_play_data = nullptr; + void *_on_shutdown_data = nullptr; + void *_on_data_data = nullptr; +}; + API_EXPORT mk_player API_CALL mk_player_create() { - MediaPlayer::Ptr *obj = new MediaPlayer::Ptr(new MediaPlayer()); + MediaPlayerForC::Ptr *obj = new MediaPlayerForC::Ptr(new MediaPlayerForC()); + (*obj)->setup(); return obj; } API_EXPORT void API_CALL mk_player_release(mk_player ctx) { assert(ctx); - MediaPlayer::Ptr *obj = (MediaPlayer::Ptr *)ctx; + MediaPlayerForC::Ptr *obj = (MediaPlayerForC::Ptr *)ctx; + (*obj)->unset(); delete obj; } API_EXPORT void API_CALL mk_player_set_option(mk_player ctx,const char* key,const char *val){ assert(ctx && key && val); - MediaPlayer::Ptr &player = *((MediaPlayer::Ptr *)ctx); + MediaPlayerForC &obj = **((MediaPlayerForC::Ptr *)ctx); + auto player = obj.getPlayer(); string key_str(key), val_str(val); player->getPoller()->async([key_str,val_str,player](){ //切换线程后再操作 @@ -36,7 +148,8 @@ API_EXPORT void API_CALL mk_player_set_option(mk_player ctx,const char* key,cons } API_EXPORT void API_CALL mk_player_play(mk_player ctx, const char *url) { assert(ctx && url); - MediaPlayer::Ptr &player = *((MediaPlayer::Ptr *)ctx); + MediaPlayerForC &obj = **((MediaPlayerForC::Ptr *)ctx); + auto player = obj.getPlayer(); string url_str(url); player->getPoller()->async([url_str,player](){ //切换线程后再操作 @@ -46,7 +159,8 @@ API_EXPORT void API_CALL mk_player_play(mk_player ctx, const char *url) { API_EXPORT void API_CALL mk_player_pause(mk_player ctx, int pause) { assert(ctx); - MediaPlayer::Ptr &player = *((MediaPlayer::Ptr *)ctx); + MediaPlayerForC &obj = **((MediaPlayerForC::Ptr *)ctx); + auto player = obj.getPlayer(); player->getPoller()->async([pause,player](){ //切换线程后再操作 player->pause(pause); @@ -55,7 +169,8 @@ API_EXPORT void API_CALL mk_player_pause(mk_player ctx, int pause) { API_EXPORT void API_CALL mk_player_seekto(mk_player ctx, float progress) { assert(ctx); - MediaPlayer::Ptr &player = *((MediaPlayer::Ptr *)ctx); + MediaPlayerForC &obj = **((MediaPlayerForC::Ptr *)ctx); + auto player = obj.getPlayer(); player->getPoller()->async([progress,player](){ //切换线程后再操作 player->seekTo(progress); @@ -74,19 +189,8 @@ API_EXPORT void API_CALL mk_player_seektoByPos(mk_player ctx, int seekPos) static void mk_player_set_on_event(mk_player ctx, on_mk_play_event cb, void *user_data, int type) { 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()); - }); - } - }); + MediaPlayerForC &obj = **((MediaPlayerForC::Ptr *)ctx); + obj.setOnEvent(cb,user_data, type); } API_EXPORT void API_CALL mk_player_set_on_result(mk_player ctx, on_mk_play_event cb, void *user_data) { @@ -99,84 +203,76 @@ API_EXPORT void API_CALL mk_player_set_on_shutdown(mk_player ctx, on_mk_play_eve API_EXPORT void API_CALL mk_player_set_on_data(mk_player ctx, on_mk_play_data cb, void *user_data) { assert(ctx && cb); - MediaPlayer::Ptr &player = *((MediaPlayer::Ptr *)ctx); - player->getPoller()->async([player,cb,user_data](){ - //切换线程后再操作 - auto delegate = std::make_shared([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); - } - }); + MediaPlayerForC &obj = **((MediaPlayerForC::Ptr *)ctx); + obj.setOnData(cb,user_data); } API_EXPORT int API_CALL mk_player_video_codecId(mk_player ctx){ assert(ctx); - MediaPlayer::Ptr& player = *((MediaPlayer::Ptr*)ctx); - auto track = dynamic_pointer_cast(player->getTrack(TrackVideo)); + MediaPlayerForC &obj = **((MediaPlayerForC::Ptr *)ctx); + auto track = dynamic_pointer_cast(obj->getTrack(TrackVideo)); return track ? track->getCodecId() : CodecInvalid; } API_EXPORT int API_CALL mk_player_video_width(mk_player ctx) { assert(ctx); - MediaPlayer::Ptr &player = *((MediaPlayer::Ptr *)ctx); - auto track = dynamic_pointer_cast(player->getTrack(TrackVideo)); + MediaPlayerForC &obj = **((MediaPlayerForC::Ptr *)ctx); + auto track = dynamic_pointer_cast(obj->getTrack(TrackVideo)); return track ? track->getVideoWidth() : 0; } API_EXPORT int API_CALL mk_player_video_height(mk_player ctx) { assert(ctx); - MediaPlayer::Ptr &player = *((MediaPlayer::Ptr *)ctx); - auto track = dynamic_pointer_cast(player->getTrack(TrackVideo)); + MediaPlayerForC &obj = **((MediaPlayerForC::Ptr *)ctx); + auto track = dynamic_pointer_cast(obj->getTrack(TrackVideo)); return track ? track->getVideoHeight() : 0; } API_EXPORT int API_CALL mk_player_video_fps(mk_player ctx) { assert(ctx); - MediaPlayer::Ptr &player = *((MediaPlayer::Ptr *)ctx); - auto track = dynamic_pointer_cast(player->getTrack(TrackVideo)); + MediaPlayerForC &obj = **((MediaPlayerForC::Ptr *)ctx); + auto track = dynamic_pointer_cast(obj->getTrack(TrackVideo)); return track ? track->getVideoFps() : 0; } API_EXPORT int API_CALL mk_player_audio_codecId(mk_player ctx){ assert(ctx); - MediaPlayer::Ptr& player = *((MediaPlayer::Ptr*)ctx); - auto track = dynamic_pointer_cast(player->getTrack(TrackAudio)); + MediaPlayerForC &obj = **((MediaPlayerForC::Ptr *)ctx); + auto track = dynamic_pointer_cast(obj->getTrack(TrackAudio)); return track ? track->getCodecId() : CodecInvalid; } API_EXPORT int API_CALL mk_player_audio_samplerate(mk_player ctx) { assert(ctx); - MediaPlayer::Ptr &player = *((MediaPlayer::Ptr *)ctx); - auto track = dynamic_pointer_cast(player->getTrack(TrackAudio)); + MediaPlayerForC &obj = **((MediaPlayerForC::Ptr *)ctx); + auto track = dynamic_pointer_cast(obj->getTrack(TrackAudio)); return track ? track->getAudioSampleRate() : 0; } API_EXPORT int API_CALL mk_player_audio_bit(mk_player ctx) { assert(ctx); - MediaPlayer::Ptr &player = *((MediaPlayer::Ptr *)ctx); - auto track = dynamic_pointer_cast(player->getTrack(TrackAudio)); + MediaPlayerForC &obj = **((MediaPlayerForC::Ptr *)ctx); + auto track = dynamic_pointer_cast(obj->getTrack(TrackAudio)); return track ? track->getAudioSampleBit() : 0; } API_EXPORT int API_CALL mk_player_audio_channel(mk_player ctx) { assert(ctx); - MediaPlayer::Ptr &player = *((MediaPlayer::Ptr *)ctx); - auto track = dynamic_pointer_cast(player->getTrack(TrackAudio)); + MediaPlayerForC &obj = **((MediaPlayerForC::Ptr *)ctx); + auto track = dynamic_pointer_cast(obj->getTrack(TrackAudio)); return track ? track->getAudioChannel() : 0; } API_EXPORT float API_CALL mk_player_duration(mk_player ctx) { assert(ctx); - MediaPlayer::Ptr &player = *((MediaPlayer::Ptr *)ctx); - return player->getDuration(); + MediaPlayerForC &obj = **((MediaPlayerForC::Ptr *)ctx); + return obj->getDuration(); } API_EXPORT float API_CALL mk_player_progress(mk_player ctx) { assert(ctx); - MediaPlayer::Ptr &player = *((MediaPlayer::Ptr *)ctx); - return player->getProgress(); + MediaPlayerForC &obj = **((MediaPlayerForC::Ptr *)ctx); + return obj->getProgress(); } @@ -189,6 +285,6 @@ API_EXPORT int API_CALL mk_player_progress_pos(mk_player ctx) API_EXPORT float API_CALL mk_player_loss_rate(mk_player ctx, int track_type) { assert(ctx); - MediaPlayer::Ptr &player = *((MediaPlayer::Ptr *)ctx); - return player->getPacketLossRate((TrackType)track_type); + MediaPlayerForC &obj = **((MediaPlayerForC::Ptr *)ctx); + return obj->getPacketLossRate((TrackType)track_type); } diff --git a/api/source/mk_thread.cpp b/api/source/mk_thread.cpp index 950f7be7..b73594e1 100644 --- a/api/source/mk_thread.cpp +++ b/api/source/mk_thread.cpp @@ -17,13 +17,13 @@ using namespace toolkit; API_EXPORT mk_thread API_CALL mk_thread_from_tcp_session(mk_tcp_session ctx){ assert(ctx); - TcpSession *obj = (TcpSession *)ctx; + TcpSessionForC *obj = (TcpSessionForC *)ctx; return obj->getPoller().get(); } API_EXPORT mk_thread API_CALL mk_thread_from_tcp_client(mk_tcp_client ctx){ assert(ctx); - TcpClient::Ptr *client = (TcpClient::Ptr *)ctx; + TcpClientForC::Ptr *client = (TcpClientForC::Ptr *)ctx; return (*client)->getPoller().get(); } @@ -43,18 +43,59 @@ API_EXPORT void API_CALL mk_sync_do(mk_thread ctx,on_mk_async cb, void *user_dat }); } +class TimerForC : public std::enable_shared_from_this{ +public: + typedef std::shared_ptr Ptr; + + TimerForC(on_mk_timer cb, void *user_data){ + _cb = cb; + _user_data = user_data; + } + + ~TimerForC(){} + + uint64_t operator()(){ + lock_guard lck(_mxt); + if(!_cb){ + return 0; + } + return _cb(_user_data); + } + + void cancel(){ + lock_guard lck(_mxt); + _cb = nullptr; + _task->cancel(); + } + + void start(int ms ,EventPoller &poller){ + weak_ptr weak_self = shared_from_this(); + _task = poller.doDelayTask(ms, [weak_self]() { + auto strong_self = weak_self.lock(); + if (!strong_self) { + return (uint64_t) 0; + } + return (*strong_self)(); + }); + } +private: + on_mk_timer _cb = nullptr; + void *_user_data = nullptr; + recursive_mutex _mxt; + DelayTask::Ptr _task; +}; + API_EXPORT mk_timer API_CALL mk_timer_create(mk_thread ctx,uint64_t delay_ms,on_mk_timer cb, void *user_data){ assert(ctx && cb); EventPoller *poller = (EventPoller *)ctx; - auto ret = poller->doDelayTask(delay_ms,[cb,user_data](){ - return cb(user_data); - }); - return new DelayTask::Ptr(ret); + TimerForC::Ptr *ret = new TimerForC::Ptr(new TimerForC(cb, user_data)); + (*ret)->start(delay_ms,*poller); + return ret; } API_EXPORT void API_CALL mk_timer_release(mk_timer ctx){ assert(ctx); - DelayTask::Ptr *obj = (DelayTask::Ptr *)ctx; + TimerForC::Ptr *obj = (TimerForC::Ptr *)ctx; (*obj)->cancel(); delete obj; } \ No newline at end of file