2019-12-18 11:47:49 +08:00
|
|
|
|
/*
|
2019-12-17 18:45:31 +08:00
|
|
|
|
* MIT License
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2019 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.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-12-27 10:10: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(){
|
|
|
|
|
_channel->setListener(shared_from_this());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DevChannel::Ptr &getChannel(){
|
|
|
|
|
return _channel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setCallBack(on_mk_media_close cb, void *user_data){
|
|
|
|
|
_cb = cb;
|
|
|
|
|
_user_data = user_data;
|
|
|
|
|
}
|
|
|
|
|
protected:
|
|
|
|
|
// 通知其停止推流
|
|
|
|
|
bool close(MediaSource &sender,bool force) override{
|
|
|
|
|
if(!force && _channel->totalReaderCount()){
|
|
|
|
|
//非强制关闭且正有人在观看该视频
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if(!_cb){
|
|
|
|
|
//未设置回调,没法关闭
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if(!_cb(_user_data)){
|
|
|
|
|
//回调选择返回不关闭该视频
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//回调中已经关闭该视频
|
|
|
|
|
string err = StrPrinter << "close media:" << sender.getSchema() << "/" << sender.getVhost() << "/" << sender.getApp() << "/" << sender.getId() << " " << force;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 通知无人观看
|
|
|
|
|
void onNoneReader(MediaSource &sender) override{
|
|
|
|
|
if(_channel->totalReaderCount()){
|
|
|
|
|
//统计有误,还有人在看
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
MediaSourceEvent::onNoneReader(sender);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 观看总人数
|
|
|
|
|
int totalReaderCount(MediaSource &sender) override{
|
|
|
|
|
return _channel->totalReaderCount();
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
DevChannel::Ptr _channel;
|
|
|
|
|
on_mk_media_close _cb;
|
|
|
|
|
void *_user_data;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
(*obj)->setCallBack(cb,user_data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-18 14:43:32 +08:00
|
|
|
|
API_EXPORT mk_media API_CALL mk_media_create(const char *vhost, const char *app, const char *stream, float duration, int hls_enabled, int mp4_enabled) {
|
|
|
|
|
assert(vhost && app && stream);
|
2020-02-28 17:53:21 +08:00
|
|
|
|
MediaHelper::Ptr *obj(new MediaHelper::Ptr(new MediaHelper(vhost, app, stream, duration, true, true, hls_enabled, mp4_enabled)));
|
|
|
|
|
(*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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
API_EXPORT void API_CALL mk_media_init_h264(mk_media ctx, int width, int height, int frameRate) {
|
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;
|
|
|
|
|
info.iFrameRate = frameRate;
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
API_EXPORT void API_CALL mk_media_init_h265(mk_media ctx, int width, int height, int frameRate) {
|
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;
|
|
|
|
|
info.iFrameRate = frameRate;
|
|
|
|
|
info.iWidth = width;
|
|
|
|
|
info.iHeight = height;
|
2020-02-28 17:53:21 +08:00
|
|
|
|
(*obj)->getChannel()->initH265Video(info);
|
2019-12-17 18:45:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
API_EXPORT void API_CALL mk_media_init_aac(mk_media ctx, int channel, int sample_bit, int sample_rate, int profile) {
|
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
|
|
|
|
AudioInfo info;
|
|
|
|
|
info.iSampleRate = sample_rate;
|
|
|
|
|
info.iChannel = channel;
|
|
|
|
|
info.iSampleBit = sample_bit;
|
|
|
|
|
info.iProfile = profile;
|
2020-02-28 17:53:21 +08:00
|
|
|
|
(*obj)->getChannel()->initAudio(info);
|
2019-12-17 18:45:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
API_EXPORT void API_CALL mk_media_input_aac(mk_media ctx, void *data, int len, uint32_t dts, int with_adts_header) {
|
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()->inputAAC((char *) data, len, dts, with_adts_header);
|
2019-12-17 18:45:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
API_EXPORT void API_CALL mk_media_input_aac1(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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|