ZLMediaKit/api/source/mk_media.cpp

165 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-17 18:45:31 +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-17 18:45:31 +08:00
*/
#include "mk_media.h"
2019-12-17 18:45:31 +08:00
#include "Util/logger.h"
#include "Common/Device.h"
using namespace std;
using namespace toolkit;
using namespace mediakit;
2020-02-28 17:53:21 +08:00
class MediaHelper : public MediaSourceEvent , public std::enable_shared_from_this<MediaHelper> {
public:
typedef std::shared_ptr<MediaHelper> Ptr;
template<typename ...ArgsType>
MediaHelper(ArgsType &&...args){
_channel = std::make_shared<DevChannel>(std::forward<ArgsType>(args)...);
}
~MediaHelper(){}
void attachEvent(){
2020-04-05 09:26:29 +08:00
_channel->setMediaListener(shared_from_this());
2020-02-28 17:53:21 +08:00
}
DevChannel::Ptr &getChannel(){
return _channel;
}
2020-04-02 10:32:16 +08:00
void setOnClose(on_mk_media_close cb, void *user_data){
_on_close = cb;
_on_close_data = user_data;
}
void setOnSeek(on_mk_media_seek cb, void *user_data){
_on_seek = cb;
_on_seek_data = user_data;
2020-02-28 17:53:21 +08:00
}
protected:
// 通知其停止推流
bool close(MediaSource &sender,bool force) override{
if(!force && _channel->totalReaderCount()){
//非强制关闭且正有人在观看该视频
return false;
}
2020-04-02 10:32:16 +08:00
if(!_on_close){
2020-02-28 17:53:21 +08:00
//未设置回调,没法关闭
2020-03-10 22:55:19 +08:00
WarnL << "请使用mk_media_set_on_close函数设置回调函数!";
2020-02-28 17:53:21 +08:00
return false;
}
2020-03-10 22:55:19 +08:00
//请在回调中调用mk_media_release函数释放资源,否则MediaSource::close()操作不会生效
2020-04-02 10:32:16 +08:00
_on_close(_on_close_data);
2020-03-05 18:04:34 +08:00
WarnL << "close media:" << sender.getSchema() << "/" << sender.getVhost() << "/" << sender.getApp() << "/" << sender.getId() << " " << force;
2020-02-28 17:53:21 +08:00
return true;
}
2020-04-03 22:19:04 +08:00
bool seekTo(MediaSource &sender,uint32_t ui32Stamp) override{
2020-04-02 10:32:16 +08:00
if(!_on_seek){
return false;
}
2020-04-02 10:35:10 +08:00
return _on_seek(_on_seek_data,ui32Stamp);
2020-04-02 10:32:16 +08:00
}
2020-02-28 17:53:21 +08:00
// 观看总人数
int totalReaderCount(MediaSource &sender) override{
return _channel->totalReaderCount();
}
private:
DevChannel::Ptr _channel;
2020-04-02 10:34:25 +08:00
on_mk_media_close _on_close = nullptr;
on_mk_media_seek _on_seek = nullptr;
2020-04-02 10:32:16 +08:00
void *_on_seek_data;
void *_on_close_data;
2020-02-28 17:53:21 +08:00
};
API_EXPORT void API_CALL mk_media_set_on_close(mk_media ctx, on_mk_media_close cb, void *user_data){
assert(ctx);
MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
2020-04-02 10:32:16 +08:00
(*obj)->setOnClose(cb, user_data);
}
API_EXPORT void API_CALL mk_media_set_on_seek(mk_media ctx, on_mk_media_seek cb, void *user_data){
assert(ctx);
MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
(*obj)->setOnSeek(cb, user_data);
2020-02-28 17:53:21 +08:00
}
API_EXPORT int API_CALL mk_media_total_reader_count(mk_media ctx){
assert(ctx);
MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
return (*obj)->getChannel()->totalReaderCount();
}
2020-04-19 00:05:02 +08:00
API_EXPORT mk_media API_CALL mk_media_create(const char *vhost, const char *app, const char *stream, float duration,
int rtsp_enabled, int rtmp_enabled, int hls_enabled, int mp4_enabled) {
2019-12-18 14:43:32 +08:00
assert(vhost && app && stream);
2020-04-19 00:05:02 +08:00
MediaHelper::Ptr *obj(new MediaHelper::Ptr(new MediaHelper(vhost, app, stream, duration,
rtsp_enabled, rtmp_enabled, hls_enabled, mp4_enabled)));
2020-02-28 17:53:21 +08:00
(*obj)->attachEvent();
2019-12-17 18:45:31 +08:00
return (mk_media) obj;
}
API_EXPORT void API_CALL mk_media_release(mk_media ctx) {
2019-12-18 14:43:32 +08:00
assert(ctx);
2020-02-28 17:53:21 +08:00
MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
2019-12-17 18:45:31 +08:00
delete obj;
}
2020-04-18 23:56:27 +08:00
API_EXPORT void API_CALL mk_media_init_video(mk_media ctx, int track_id, int width, int height, int fps){
2019-12-18 14:43:32 +08:00
assert(ctx);
2020-02-28 17:53:21 +08:00
MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
2019-12-17 18:45:31 +08:00
VideoInfo info;
2020-04-18 23:56:27 +08:00
info.codecId = (CodecId)track_id;
info.iFrameRate = fps;
2019-12-17 18:45:31 +08:00
info.iWidth = width;
info.iHeight = height;
2020-02-28 17:53:21 +08:00
(*obj)->getChannel()->initVideo(info);
2019-12-17 18:45:31 +08:00
}
2020-04-18 23:56:27 +08:00
API_EXPORT void API_CALL mk_media_init_audio(mk_media ctx, int track_id, int sample_rate, int channels, int sample_bit){
2019-12-18 14:43:32 +08:00
assert(ctx);
2020-02-28 17:53:21 +08:00
MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
AudioInfo info;
2020-04-18 23:56:27 +08:00
info.codecId = (CodecId)track_id;
info.iSampleRate = sample_rate;
2020-04-18 23:56:27 +08:00
info.iChannel = channels;
info.iSampleBit = sample_bit;
(*obj)->getChannel()->initAudio(info);
}
2020-02-27 11:40:38 +08:00
API_EXPORT void API_CALL mk_media_init_complete(mk_media ctx){
assert(ctx);
2020-02-28 17:53:21 +08:00
MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
(*obj)->getChannel()->addTrackCompleted();
2020-02-27 11:40:38 +08:00
}
2019-12-17 18:45:31 +08:00
API_EXPORT void API_CALL mk_media_input_h264(mk_media ctx, void *data, int len, uint32_t dts, uint32_t pts) {
2019-12-18 14:43:32 +08:00
assert(ctx && data && len > 0);
2020-02-28 17:53:21 +08:00
MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
(*obj)->getChannel()->inputH264((char *) data, len, dts, pts);
2019-12-17 18:45:31 +08:00
}
API_EXPORT void API_CALL mk_media_input_h265(mk_media ctx, void *data, int len, uint32_t dts, uint32_t pts) {
2019-12-18 14:43:32 +08:00
assert(ctx && data && len > 0);
2020-02-28 17:53:21 +08:00
MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
(*obj)->getChannel()->inputH265((char *) data, len, dts, pts);
2019-12-17 18:45:31 +08:00
}
2020-04-18 23:56:27 +08:00
API_EXPORT void API_CALL mk_media_input_aac(mk_media ctx, void *data, int len, uint32_t dts, void *adts) {
2019-12-18 14:43:32 +08:00
assert(ctx && data && len > 0 && adts);
2020-02-28 17:53:21 +08:00
MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
(*obj)->getChannel()->inputAAC((char *) data, len, dts, (char *) adts);
2019-12-17 18:45:31 +08:00
}
2020-04-18 18:46:20 +08:00
API_EXPORT void API_CALL mk_media_input_g711(mk_media ctx, void* data, int len, uint32_t dts){
assert(ctx && data && len > 0);
MediaHelper::Ptr* obj = (MediaHelper::Ptr*) ctx;
(*obj)->getChannel()->inputG711((char*)data, len, dts);
}