mk_media.cpp 6.54 KB
Newer Older
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
3 4 5
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
xiongziliang committed
6 7 8
 * 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.
9 10
 */

11
#include "mk_media.h"
12 13 14 15 16 17 18
#include "Util/logger.h"
#include "Common/Device.h"

using namespace std;
using namespace toolkit;
using namespace mediakit;

19 20 21 22 23 24 25 26 27 28
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(){
29
        _channel->setMediaListener(shared_from_this());
30 31 32 33 34 35
    }

    DevChannel::Ptr &getChannel(){
        return _channel;
    }

36 37 38 39 40 41 42 43
    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;
44
    }
45 46 47 48 49 50

    void setOnRegist(on_mk_media_source_regist cb, void *user_data){
        _on_regist = cb;
        _on_regist_data = user_data;
    }

51 52 53 54 55 56 57
protected:
    // 通知其停止推流
    bool close(MediaSource &sender,bool force) override{
        if(!force && _channel->totalReaderCount()){
            //非强制关闭且正有人在观看该视频
            return false;
        }
58
        if(!_on_close){
59
            //未设置回调,没法关闭
60
            WarnL << "请使用mk_media_set_on_close函数设置回调函数!";
61 62
            return false;
        }
63
        //请在回调中调用mk_media_release函数释放资源,否则MediaSource::close()操作不会生效
64
        _on_close(_on_close_data);
xiongziliang committed
65
        WarnL << "close media:" << sender.getSchema() << "/" << sender.getVhost() << "/" << sender.getApp() << "/" << sender.getId() << " " << force;
66 67 68
        return true;
    }

xiongziliang committed
69
    bool seekTo(MediaSource &sender,uint32_t ui32Stamp) override{
70 71 72
        if(!_on_seek){
            return false;
        }
73
        return _on_seek(_on_seek_data,ui32Stamp);
74
    }
75 76 77 78
    // 观看总人数
    int totalReaderCount(MediaSource &sender) override{
        return _channel->totalReaderCount();
    }
79 80 81 82 83 84 85

    void onRegist(MediaSource &sender, bool regist) override{
        if (_on_regist) {
            _on_regist(_on_regist_data, &sender, regist);
        }
    }

86 87
private:
    DevChannel::Ptr _channel;
88 89
    on_mk_media_close _on_close = nullptr;
    on_mk_media_seek _on_seek = nullptr;
90
    on_mk_media_source_regist _on_regist = nullptr;
91 92
    void *_on_seek_data;
    void *_on_close_data;
93
    void *_on_regist_data;
94 95 96 97 98
};

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;
99 100 101 102 103 104 105
    (*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);
106 107
}

108 109 110 111 112 113
API_EXPORT void API_CALL mk_media_set_on_regist(mk_media ctx, on_mk_media_source_regist cb, void *user_data){
    assert(ctx);
    MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
    (*obj)->setOnRegist(cb, user_data);
}

114 115 116 117 118 119
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();
}

120 121
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) {
xiongziliang committed
122
    assert(vhost && app && stream);
123
    MediaHelper::Ptr *obj(new MediaHelper::Ptr(new MediaHelper(vhost, app, stream, duration, hls_enabled, mp4_enabled)));
124
    (*obj)->attachEvent();
125 126 127 128
    return (mk_media) obj;
}

API_EXPORT void API_CALL mk_media_release(mk_media ctx) {
xiongziliang committed
129
    assert(ctx);
130
    MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
131 132 133
    delete obj;
}

xiongziliang committed
134
API_EXPORT void API_CALL mk_media_init_video(mk_media ctx, int track_id, int width, int height, int fps){
xiongziliang committed
135
    assert(ctx);
136
    MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
137
    VideoInfo info;
xiongziliang committed
138 139
    info.codecId = (CodecId)track_id;
    info.iFrameRate = fps;
140 141
    info.iWidth = width;
    info.iHeight = height;
142
    (*obj)->getChannel()->initVideo(info);
143 144
}

xiongziliang committed
145
API_EXPORT void API_CALL mk_media_init_audio(mk_media ctx, int track_id, int sample_rate, int channels, int sample_bit){
xiongziliang committed
146
    assert(ctx);
147
    MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
148
    AudioInfo info;
xiongziliang committed
149
    info.codecId = (CodecId)track_id;
150
    info.iSampleRate = sample_rate;
xiongziliang committed
151
    info.iChannel = channels;
152 153 154 155
    info.iSampleBit = sample_bit;
    (*obj)->getChannel()->initAudio(info);
}

156 157
API_EXPORT void API_CALL mk_media_init_complete(mk_media ctx){
    assert(ctx);
158 159
    MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
    (*obj)->getChannel()->addTrackCompleted();
160 161
}

162
API_EXPORT void API_CALL mk_media_input_h264(mk_media ctx, void *data, int len, uint32_t dts, uint32_t pts) {
xiongziliang committed
163
    assert(ctx && data && len > 0);
164 165
    MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
    (*obj)->getChannel()->inputH264((char *) data, len, dts, pts);
166 167 168
}

API_EXPORT void API_CALL mk_media_input_h265(mk_media ctx, void *data, int len, uint32_t dts, uint32_t pts) {
xiongziliang committed
169
    assert(ctx && data && len > 0);
170 171
    MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
    (*obj)->getChannel()->inputH265((char *) data, len, dts, pts);
172 173
}

xiongziliang committed
174
API_EXPORT void API_CALL mk_media_input_aac(mk_media ctx, void *data, int len, uint32_t dts, void *adts) {
xiongziliang committed
175
    assert(ctx && data && len > 0 && adts);
176 177
    MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
    (*obj)->getChannel()->inputAAC((char *) data, len, dts, (char *) adts);
178 179
}

180
API_EXPORT void API_CALL mk_media_input_pcm(mk_media ctx, void *data , int len, uint32_t pts){
lyg1949 committed
181 182 183 184
#ifdef ENABLE_FAAC
	assert(ctx && data && len > 0);
	MediaHelper::Ptr* obj = (MediaHelper::Ptr*) ctx;
	(*obj)->getChannel()->inputPCM((char*)data, len, pts);
185 186
#else
    WarnL << "aac编码未启用,该方法无效,编译时请打开ENABLE_FAAC选项";
lyg1949 committed
187
#endif //ENABLE_FAAC
188
}
lyg1949 committed
189

190
API_EXPORT void API_CALL mk_media_input_audio(mk_media ctx, void* data, int len, uint32_t dts){
191 192
    assert(ctx && data && len > 0);
    MediaHelper::Ptr* obj = (MediaHelper::Ptr*) ctx;
193
    (*obj)->getChannel()->inputAudio((char*)data, len, dts);
194
}