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
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2019-12-17 18:45:31 +08:00
|
|
|
|
*
|
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
|
|
|
|
*/
|
|
|
|
|
|
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;
|
|
|
|
|
|
2023-04-18 10:38:12 +08:00
|
|
|
|
class MediaHelper: public MediaSourceEvent, public std::enable_shared_from_this<MediaHelper> {
|
2020-02-28 17:53:21 +08:00
|
|
|
|
public:
|
2022-12-02 14:43:06 +08:00
|
|
|
|
using Ptr = std::shared_ptr<MediaHelper>;
|
2023-03-12 09:39:49 +08:00
|
|
|
|
MediaHelper(const char *vhost, const char *app, const char *stream, float duration, const ProtocolOption &option) {
|
2023-03-11 11:02:20 +08:00
|
|
|
|
_poller = EventPollerPool::Instance().getPoller();
|
|
|
|
|
// 在poller线程中创建DevChannel(MultiMediaSourceMuxer)对象,确保严格的线程安全限制
|
2023-03-12 09:39:49 +08:00
|
|
|
|
_poller->sync([&]() { _channel = std::make_shared<DevChannel>(vhost, app, stream, duration, option); });
|
2020-02-28 17:53:21 +08:00
|
|
|
|
}
|
2023-02-11 15:14:18 +08:00
|
|
|
|
|
|
|
|
|
~MediaHelper() = default;
|
2020-02-28 17:53:21 +08:00
|
|
|
|
|
2023-04-18 10:38:12 +08:00
|
|
|
|
void attachEvent() { _channel->setMediaListener(shared_from_this()); }
|
2020-02-28 17:53:21 +08:00
|
|
|
|
|
2023-04-18 10:38:12 +08:00
|
|
|
|
DevChannel::Ptr &getChannel() { return _channel; }
|
2020-02-28 17:53:21 +08:00
|
|
|
|
|
2023-04-18 10:38:12 +08:00
|
|
|
|
void setOnClose(on_mk_media_close cb, std::shared_ptr<void> user_data) {
|
2020-04-02 10:32:16 +08:00
|
|
|
|
_on_close = cb;
|
2023-02-11 15:14:18 +08:00
|
|
|
|
_on_close_data = std::move(user_data);
|
2020-04-02 10:32:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 10:38:12 +08:00
|
|
|
|
void setOnSeek(on_mk_media_seek cb, std::shared_ptr<void> user_data) {
|
2020-04-02 10:32:16 +08:00
|
|
|
|
_on_seek = cb;
|
2023-02-11 15:14:18 +08:00
|
|
|
|
_on_seek_data = std::move(user_data);
|
2020-02-28 17:53:21 +08:00
|
|
|
|
}
|
2020-07-02 18:14:39 +08:00
|
|
|
|
|
2023-02-11 15:14:18 +08:00
|
|
|
|
void setOnPause(on_mk_media_pause cb, std::shared_ptr<void> user_data) {
|
2021-08-09 18:28:43 +08:00
|
|
|
|
_on_pause = cb;
|
2023-02-11 15:14:18 +08:00
|
|
|
|
_on_pause_data = std::move(user_data);
|
2021-08-09 18:28:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-02-11 15:14:18 +08:00
|
|
|
|
void setOnSpeed(on_mk_media_speed cb, std::shared_ptr<void> user_data) {
|
2021-08-09 18:28:43 +08:00
|
|
|
|
_on_speed = cb;
|
2023-02-11 15:14:18 +08:00
|
|
|
|
_on_speed_data = std::move(user_data);
|
2021-08-09 18:28:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 10:38:12 +08:00
|
|
|
|
void setOnRegist(on_mk_media_source_regist cb, std::shared_ptr<void> user_data) {
|
2020-07-02 18:14:39 +08:00
|
|
|
|
_on_regist = cb;
|
2023-02-11 15:14:18 +08:00
|
|
|
|
_on_regist_data = std::move(user_data);
|
2020-07-02 18:14:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-28 17:53:21 +08:00
|
|
|
|
protected:
|
|
|
|
|
// 通知其停止推流
|
2022-09-18 20:36:47 +08:00
|
|
|
|
bool close(MediaSource &sender) override {
|
|
|
|
|
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()操作不会生效
|
2023-02-11 15:14:18 +08:00
|
|
|
|
_on_close(_on_close_data.get());
|
2022-09-18 20:36:47 +08:00
|
|
|
|
WarnL << "close media: " << sender.getUrl();
|
2020-02-28 17:53:21 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-12 16:07:31 +08:00
|
|
|
|
bool seekTo(MediaSource &sender, uint32_t stamp) override {
|
|
|
|
|
if (!_on_seek) {
|
2020-04-02 10:32:16 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-02-11 15:14:18 +08:00
|
|
|
|
return _on_seek(_on_seek_data.get(), stamp);
|
2020-04-02 10:32:16 +08:00
|
|
|
|
}
|
2021-08-12 16:07:31 +08:00
|
|
|
|
|
|
|
|
|
// 通知暂停或恢复
|
|
|
|
|
bool pause(MediaSource &sender, bool pause) override {
|
|
|
|
|
if (!_on_pause) {
|
2021-08-09 18:28:43 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-02-11 15:14:18 +08:00
|
|
|
|
return _on_pause(_on_pause_data.get(), pause);
|
2021-08-09 18:28:43 +08:00
|
|
|
|
}
|
2021-08-12 16:07:31 +08:00
|
|
|
|
|
2021-08-09 18:28:43 +08:00
|
|
|
|
//通知倍数播放
|
2021-08-12 16:07:31 +08:00
|
|
|
|
bool speed(MediaSource &sender, float speed) override {
|
|
|
|
|
if (!_on_speed) {
|
2021-08-09 18:28:43 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-02-11 15:14:18 +08:00
|
|
|
|
return _on_speed(_on_speed_data.get(), speed);
|
2021-08-09 18:28:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 10:38:12 +08:00
|
|
|
|
void onRegist(MediaSource &sender, bool regist) override {
|
2020-07-02 18:14:39 +08:00
|
|
|
|
if (_on_regist) {
|
2023-02-26 21:45:14 +08:00
|
|
|
|
_on_regist(_on_regist_data.get(), (mk_media_source)&sender, regist);
|
2020-07-02 18:14:39 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 10:38:12 +08:00
|
|
|
|
toolkit::EventPoller::Ptr getOwnerPoller(MediaSource &sender) override { return _poller; }
|
2023-03-11 11:02:20 +08:00
|
|
|
|
|
2020-02-28 17:53:21 +08:00
|
|
|
|
private:
|
2023-03-11 11:02:20 +08:00
|
|
|
|
EventPoller::Ptr _poller;
|
2020-02-28 17:53:21 +08:00
|
|
|
|
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;
|
2021-08-09 18:28:43 +08:00
|
|
|
|
on_mk_media_pause _on_pause = nullptr;
|
|
|
|
|
on_mk_media_speed _on_speed = nullptr;
|
2020-07-02 18:14:39 +08:00
|
|
|
|
on_mk_media_source_regist _on_regist = nullptr;
|
2023-02-11 15:14:18 +08:00
|
|
|
|
std::shared_ptr<void> _on_seek_data;
|
|
|
|
|
std::shared_ptr<void> _on_pause_data;
|
|
|
|
|
std::shared_ptr<void> _on_speed_data;
|
|
|
|
|
std::shared_ptr<void> _on_close_data;
|
|
|
|
|
std::shared_ptr<void> _on_regist_data;
|
2020-02-28 17:53:21 +08:00
|
|
|
|
};
|
|
|
|
|
|
2023-04-18 10:38:12 +08:00
|
|
|
|
API_EXPORT void API_CALL mk_media_set_on_close(mk_media ctx, on_mk_media_close cb, void *user_data) {
|
2023-02-11 15:14:18 +08:00
|
|
|
|
mk_media_set_on_close2(ctx, cb, user_data, nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
API_EXPORT void API_CALL mk_media_set_on_close2(mk_media ctx, on_mk_media_close cb, void *user_data, on_user_data_free user_data_free) {
|
2020-02-28 17:53:21 +08:00
|
|
|
|
assert(ctx);
|
|
|
|
|
MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
|
2023-02-11 15:14:18 +08:00
|
|
|
|
std::shared_ptr<void> ptr(user_data, user_data_free ? user_data_free : [](void *) {});
|
|
|
|
|
(*obj)->setOnClose(cb, std::move(ptr));
|
2020-04-02 10:32:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-12 16:07:31 +08:00
|
|
|
|
API_EXPORT void API_CALL mk_media_set_on_seek(mk_media ctx, on_mk_media_seek cb, void *user_data) {
|
2023-02-11 15:14:18 +08:00
|
|
|
|
mk_media_set_on_seek2(ctx, cb, user_data, nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
API_EXPORT void API_CALL mk_media_set_on_seek2(mk_media ctx, on_mk_media_seek cb, void *user_data, on_user_data_free user_data_free) {
|
2020-04-02 10:32:16 +08:00
|
|
|
|
assert(ctx);
|
|
|
|
|
MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
|
2023-02-11 15:14:18 +08:00
|
|
|
|
std::shared_ptr<void> ptr(user_data, user_data_free ? user_data_free : [](void *) {});
|
|
|
|
|
(*obj)->setOnSeek(cb, std::move(ptr));
|
2020-02-28 17:53:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-12 16:07:31 +08:00
|
|
|
|
API_EXPORT void API_CALL mk_media_set_on_pause(mk_media ctx, on_mk_media_pause cb, void *user_data) {
|
2023-02-11 15:14:18 +08:00
|
|
|
|
mk_media_set_on_pause2(ctx, cb, user_data, nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
API_EXPORT void API_CALL mk_media_set_on_pause2(mk_media ctx, on_mk_media_pause cb, void *user_data, on_user_data_free user_data_free) {
|
2021-08-09 18:28:43 +08:00
|
|
|
|
assert(ctx);
|
2021-08-12 16:07:31 +08:00
|
|
|
|
MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
|
2023-02-11 15:14:18 +08:00
|
|
|
|
std::shared_ptr<void> ptr(user_data, user_data_free ? user_data_free : [](void *) {});
|
|
|
|
|
(*obj)->setOnPause(cb, std::move(ptr));
|
2021-08-09 18:28:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-12 16:07:31 +08:00
|
|
|
|
API_EXPORT void API_CALL mk_media_set_on_speed(mk_media ctx, on_mk_media_speed cb, void *user_data) {
|
2023-02-11 15:14:18 +08:00
|
|
|
|
mk_media_set_on_speed2(ctx, cb, user_data, nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 10:38:12 +08:00
|
|
|
|
API_EXPORT void API_CALL mk_media_set_on_speed2(mk_media ctx, on_mk_media_speed cb, void *user_data, on_user_data_free user_data_free) {
|
2021-08-09 18:28:43 +08:00
|
|
|
|
assert(ctx);
|
2021-08-12 16:07:31 +08:00
|
|
|
|
MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
|
2023-02-11 15:14:18 +08:00
|
|
|
|
std::shared_ptr<void> ptr(user_data, user_data_free ? user_data_free : [](void *) {});
|
|
|
|
|
(*obj)->setOnSpeed(cb, std::move(ptr));
|
2021-08-09 18:28:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 10:38:12 +08:00
|
|
|
|
API_EXPORT void API_CALL mk_media_set_on_regist(mk_media ctx, on_mk_media_source_regist cb, void *user_data) {
|
2023-02-11 15:14:18 +08:00
|
|
|
|
mk_media_set_on_regist2(ctx, cb, user_data, nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
API_EXPORT void API_CALL mk_media_set_on_regist2(mk_media ctx, on_mk_media_source_regist cb, void *user_data, on_user_data_free user_data_free) {
|
2020-07-02 18:14:39 +08:00
|
|
|
|
assert(ctx);
|
|
|
|
|
MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
|
2023-02-11 15:14:18 +08:00
|
|
|
|
std::shared_ptr<void> ptr(user_data, user_data_free ? user_data_free : [](void *) {});
|
|
|
|
|
(*obj)->setOnRegist(cb, std::move(ptr));
|
2020-07-02 18:14:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 10:38:12 +08:00
|
|
|
|
API_EXPORT int API_CALL mk_media_total_reader_count(mk_media ctx) {
|
2020-02-28 17:53:21 +08:00
|
|
|
|
assert(ctx);
|
|
|
|
|
MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
|
|
|
|
|
return (*obj)->getChannel()->totalReaderCount();
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-20 09:26:00 +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) {
|
2019-12-18 14:43:32 +08:00
|
|
|
|
assert(vhost && app && stream);
|
2022-03-12 13:19:21 +08:00
|
|
|
|
ProtocolOption option;
|
|
|
|
|
option.enable_hls = hls_enabled;
|
|
|
|
|
option.enable_mp4 = mp4_enabled;
|
|
|
|
|
|
|
|
|
|
MediaHelper::Ptr *obj(new MediaHelper::Ptr(new MediaHelper(vhost, app, stream, duration, option)));
|
2020-02-28 17:53:21 +08:00
|
|
|
|
(*obj)->attachEvent();
|
2019-12-17 18:45:31 +08:00
|
|
|
|
return (mk_media) obj;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-11 11:40:32 +08:00
|
|
|
|
API_EXPORT mk_media API_CALL mk_media_create2(const char *vhost, const char *app, const char *stream, float duration, mk_ini ini) {
|
|
|
|
|
assert(vhost && app && stream && ini);
|
|
|
|
|
ProtocolOption option(*((mINI *)ini));
|
|
|
|
|
MediaHelper::Ptr *obj(new MediaHelper::Ptr(new MediaHelper(vhost, app, stream, duration, option)));
|
|
|
|
|
(*obj)->attachEvent();
|
|
|
|
|
return (mk_media) obj;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-17 18:45:31 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 10:38:12 +08:00
|
|
|
|
API_EXPORT int API_CALL mk_media_init_video(mk_media ctx, int codec_id, int width, int height, float fps, int bit_rate) {
|
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;
|
2021-02-02 15:11:16 +08:00
|
|
|
|
info.codecId = (CodecId)codec_id;
|
2020-04-18 23:56:27 +08:00
|
|
|
|
info.iFrameRate = fps;
|
2019-12-17 18:45:31 +08:00
|
|
|
|
info.iWidth = width;
|
|
|
|
|
info.iHeight = height;
|
2022-05-25 15:38:32 +08:00
|
|
|
|
info.iBitRate = bit_rate;
|
2021-09-27 14:34:26 +08:00
|
|
|
|
return (*obj)->getChannel()->initVideo(info);
|
2019-12-17 18:45:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 10:38:12 +08:00
|
|
|
|
API_EXPORT int API_CALL mk_media_init_audio(mk_media ctx, int codec_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;
|
2020-04-08 15:42:52 +08:00
|
|
|
|
AudioInfo info;
|
2021-02-02 15:11:16 +08:00
|
|
|
|
info.codecId = (CodecId)codec_id;
|
2020-04-08 15:42:52 +08:00
|
|
|
|
info.iSampleRate = sample_rate;
|
2020-04-18 23:56:27 +08:00
|
|
|
|
info.iChannel = channels;
|
2020-04-08 15:42:52 +08:00
|
|
|
|
info.iSampleBit = sample_bit;
|
2021-09-27 14:34:26 +08:00
|
|
|
|
return (*obj)->getChannel()->initAudio(info);
|
2020-04-08 15:42:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 10:38:12 +08:00
|
|
|
|
API_EXPORT void API_CALL mk_media_init_track(mk_media ctx, mk_track track) {
|
2022-05-25 15:38:32 +08:00
|
|
|
|
assert(ctx && track);
|
|
|
|
|
MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
|
|
|
|
|
(*obj)->getChannel()->addTrack(*((Track::Ptr *) track));
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 10:38:12 +08:00
|
|
|
|
API_EXPORT void API_CALL mk_media_init_complete(mk_media ctx) {
|
2020-02-27 11:40:38 +08:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 10:38:12 +08:00
|
|
|
|
API_EXPORT int API_CALL mk_media_input_frame(mk_media ctx, mk_frame frame) {
|
2022-05-25 15:38:32 +08:00
|
|
|
|
assert(ctx && frame);
|
|
|
|
|
MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
|
|
|
|
|
return (*obj)->getChannel()->inputFrame(*((Frame::Ptr *) frame));
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-08 17:13:39 +08:00
|
|
|
|
API_EXPORT int API_CALL mk_media_input_h264(mk_media ctx, const void *data, int len, uint64_t dts, uint64_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;
|
2021-09-27 14:34:26 +08:00
|
|
|
|
return (*obj)->getChannel()->inputH264((const char *) data, len, dts, pts);
|
2019-12-17 18:45:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-08 17:13:39 +08:00
|
|
|
|
API_EXPORT int API_CALL mk_media_input_h265(mk_media ctx, const void *data, int len, uint64_t dts, uint64_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;
|
2021-09-27 14:34:26 +08:00
|
|
|
|
return (*obj)->getChannel()->inputH265((const char *) data, len, dts, pts);
|
2019-12-17 18:45:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-08 17:13:39 +08:00
|
|
|
|
API_EXPORT void API_CALL mk_media_input_yuv(mk_media ctx, const char *yuv[3], int linesize[3], uint64_t cts) {
|
2022-05-25 15:38:32 +08:00
|
|
|
|
assert(ctx && yuv && linesize);
|
|
|
|
|
MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
|
|
|
|
|
(*obj)->getChannel()->inputYUV((char **) yuv, linesize, cts);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-08 17:13:39 +08:00
|
|
|
|
API_EXPORT int API_CALL mk_media_input_aac(mk_media ctx, const void *data, int len, uint64_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;
|
2021-09-27 14:34:26 +08:00
|
|
|
|
return (*obj)->getChannel()->inputAAC((const char *) data, len, dts, (char *) adts);
|
2019-12-17 18:45:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 10:38:12 +08:00
|
|
|
|
API_EXPORT int API_CALL mk_media_input_pcm(mk_media ctx, void *data, int len, uint64_t pts) {
|
2023-03-03 15:24:12 +08:00
|
|
|
|
assert(ctx && data && len > 0);
|
|
|
|
|
MediaHelper::Ptr* obj = (MediaHelper::Ptr*) ctx;
|
|
|
|
|
return (*obj)->getChannel()->inputPCM((char*)data, len, pts);
|
2020-06-24 11:18:04 +08:00
|
|
|
|
}
|
2020-06-24 08:54:15 +08:00
|
|
|
|
|
2023-04-18 10:38:12 +08:00
|
|
|
|
API_EXPORT int API_CALL mk_media_input_audio(mk_media ctx, const void *data, int len, uint64_t dts) {
|
2020-04-08 15:42:52 +08:00
|
|
|
|
assert(ctx && data && len > 0);
|
|
|
|
|
MediaHelper::Ptr* obj = (MediaHelper::Ptr*) ctx;
|
2021-09-27 14:34:26 +08:00
|
|
|
|
return (*obj)->getChannel()->inputAudio((const char*)data, len, dts);
|
2020-04-08 15:42:52 +08:00
|
|
|
|
}
|
2020-11-21 22:13:08 +08:00
|
|
|
|
|
2023-02-11 15:14:18 +08:00
|
|
|
|
API_EXPORT void API_CALL mk_media_start_send_rtp(mk_media ctx, const char *dst_url, uint16_t dst_port, const char *ssrc, int is_udp, on_mk_media_send_rtp_result cb, void *user_data) {
|
|
|
|
|
mk_media_start_send_rtp2(ctx, dst_url, dst_port, ssrc, is_udp, cb, user_data, nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 10:38:12 +08:00
|
|
|
|
API_EXPORT void API_CALL mk_media_start_send_rtp2(mk_media ctx, const char *dst_url, uint16_t dst_port, const char *ssrc, int is_udp, on_mk_media_send_rtp_result cb, void *user_data,
|
|
|
|
|
on_user_data_free user_data_free) {
|
2020-11-21 22:13:08 +08:00
|
|
|
|
assert(ctx && dst_url && ssrc);
|
|
|
|
|
MediaHelper::Ptr* obj = (MediaHelper::Ptr*) ctx;
|
2022-04-03 18:25:36 +08:00
|
|
|
|
|
|
|
|
|
MediaSourceEvent::SendRtpArgs args;
|
|
|
|
|
args.dst_url = dst_url;
|
|
|
|
|
args.dst_port = dst_port;
|
|
|
|
|
args.ssrc = ssrc;
|
|
|
|
|
args.is_udp = is_udp;
|
|
|
|
|
|
2022-08-27 10:17:06 +08:00
|
|
|
|
// sender参数无用
|
|
|
|
|
auto ref = *obj;
|
2023-02-11 15:14:18 +08:00
|
|
|
|
std::shared_ptr<void> ptr(user_data, user_data_free ? user_data_free : [](void *) {});
|
|
|
|
|
(*obj)->getChannel()->getOwnerPoller(MediaSource::NullMediaSource())->async([args, ref, cb, ptr]() {
|
|
|
|
|
ref->getChannel()->startSendRtp(MediaSource::NullMediaSource(), args, [cb, ptr](uint16_t local_port, const SockException &ex) {
|
2022-08-27 10:17:06 +08:00
|
|
|
|
if (cb) {
|
2023-02-11 15:14:18 +08:00
|
|
|
|
cb(ptr.get(), local_port, ex.getErrCode(), ex.what());
|
2022-08-27 10:17:06 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
2020-11-21 22:13:08 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-18 10:38:12 +08:00
|
|
|
|
API_EXPORT void API_CALL mk_media_stop_send_rtp(mk_media ctx, const char *ssrc) {
|
2020-11-21 22:13:08 +08:00
|
|
|
|
assert(ctx);
|
2022-08-27 10:17:06 +08:00
|
|
|
|
MediaHelper::Ptr *obj = (MediaHelper::Ptr *)ctx;
|
|
|
|
|
// sender参数无用
|
|
|
|
|
auto ref = *obj;
|
|
|
|
|
string ssrc_str = ssrc ? ssrc : "";
|
2022-10-29 16:48:37 +08:00
|
|
|
|
(*obj)->getChannel()->getOwnerPoller(MediaSource::NullMediaSource())->async([ref, ssrc_str]() {
|
2022-08-27 10:17:06 +08:00
|
|
|
|
ref->getChannel()->stopSendRtp(MediaSource::NullMediaSource(), ssrc_str);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
API_EXPORT mk_thread API_CALL mk_media_get_owner_thread(mk_media ctx) {
|
|
|
|
|
MediaHelper::Ptr *obj = (MediaHelper::Ptr *)ctx;
|
2023-02-26 21:45:14 +08:00
|
|
|
|
return (mk_thread)(*obj)->getChannel()->getOwnerPoller(MediaSource::NullMediaSource()).get();
|
2020-11-21 22:13:08 +08:00
|
|
|
|
}
|