diff --git a/api/include/mk_events_objects.h b/api/include/mk_events_objects.h index 2aba7ada..b4eb0998 100644 --- a/api/include/mk_events_objects.h +++ b/api/include/mk_events_objects.h @@ -115,6 +115,16 @@ API_EXPORT int API_CALL mk_media_source_close(const mk_media_source ctx,int forc //MediaSource::seekTo() API_EXPORT int API_CALL mk_media_source_seek_to(const mk_media_source ctx,uint32_t stamp); +/** + * rtp推流成功与否的回调(第一次成功后,后面将一直重试) + */ +typedef void(API_CALL *on_mk_media_source_send_rtp_result)(void *user_data, int err, const char *msg); + +//MediaSource::startSendRtp,请参考mk_media_start_send_rtp,注意ctx参数类型不一样 +API_EXPORT void API_CALL mk_media_source_start_send_rtp(const mk_media_source ctx, const char *dst_url, uint16_t dst_port, const char *ssrc, int is_udp, on_mk_media_source_send_rtp_result cb, void *user_data); +//MediaSource::stopSendRtp,请参考mk_media_stop_send_rtp,注意ctx参数类型不一样 +API_EXPORT int API_CALL mk_media_source_stop_send_rtp(const mk_media_source ctx); + //MediaSource::find() API_EXPORT void API_CALL mk_media_source_find(const char *schema, const char *vhost, diff --git a/api/include/mk_media.h b/api/include/mk_media.h index d41f3dc7..7fcdf1c9 100755 --- a/api/include/mk_media.h +++ b/api/include/mk_media.h @@ -173,6 +173,30 @@ typedef void(API_CALL *on_mk_media_source_regist)(void *user_data, mk_media_sour */ API_EXPORT void API_CALL mk_media_set_on_regist(mk_media ctx, on_mk_media_source_regist cb, void *user_data); +/** + * rtp推流成功与否的回调(第一次成功后,后面将一直重试) + */ +typedef on_mk_media_source_send_rtp_result on_mk_media_send_rtp_result; + +/** + * 开始发送ps-rtp流 + * @param ctx 对象指针 + * @param dst_url 目标ip或域名 + * @param dst_port 目标端口 + * @param ssrc rtp的ssrc,10进制的字符串打印 + * @param is_udp 是否为udp + * @param cb 启动成功或失败回调 + * @param user_data 回调用户指针 + */ +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); + +/** + * 停止ps-rtp发送 + * @param ctx 对象指针 + * @return 1成功,0失败 + */ +API_EXPORT int API_CALL mk_media_stop_send_rtp(mk_media ctx); + #ifdef __cplusplus } #endif diff --git a/api/source/mk_events_objects.cpp b/api/source/mk_events_objects.cpp index 6ce06679..50bcbff4 100644 --- a/api/source/mk_events_objects.cpp +++ b/api/source/mk_events_objects.cpp @@ -211,6 +211,22 @@ API_EXPORT int API_CALL mk_media_source_seek_to(const mk_media_source ctx,uint32 return src->seekTo(stamp); } +API_EXPORT void API_CALL mk_media_source_start_send_rtp(const mk_media_source ctx, const char *dst_url, uint16_t dst_port, const char *ssrc, int is_udp, on_mk_media_source_send_rtp_result cb, void *user_data){ + assert(ctx && dst_url && ssrc); + MediaSource *src = (MediaSource *)ctx; + src->startSendRtp(dst_url, dst_port, ssrc, is_udp, [cb, user_data](const SockException &ex){ + if (cb) { + cb(user_data, ex.getErrCode(), ex.what()); + } + }); +} + +API_EXPORT int API_CALL mk_media_source_stop_send_rtp(const mk_media_source ctx){ + assert(ctx); + MediaSource *src = (MediaSource *) ctx; + return src->stopSendRtp(); +} + API_EXPORT void API_CALL mk_media_source_find(const char *schema, const char *vhost, const char *app, diff --git a/api/source/mk_media.cpp b/api/source/mk_media.cpp index 1ad6ba64..53fb024e 100755 --- a/api/source/mk_media.cpp +++ b/api/source/mk_media.cpp @@ -178,13 +178,9 @@ API_EXPORT void API_CALL mk_media_input_aac(mk_media ctx, void *data, int len, u } API_EXPORT void API_CALL mk_media_input_pcm(mk_media ctx, void *data , int len, uint32_t pts){ -#ifdef ENABLE_FAAC assert(ctx && data && len > 0); MediaHelper::Ptr* obj = (MediaHelper::Ptr*) ctx; (*obj)->getChannel()->inputPCM((char*)data, len, pts); -#else - WarnL << "aac编码未启用,该方法无效,编译时请打开ENABLE_FAAC选项"; -#endif //ENABLE_FAAC } API_EXPORT void API_CALL mk_media_input_audio(mk_media ctx, void* data, int len, uint32_t dts){ @@ -192,3 +188,21 @@ API_EXPORT void API_CALL mk_media_input_audio(mk_media ctx, void* data, int len, MediaHelper::Ptr* obj = (MediaHelper::Ptr*) ctx; (*obj)->getChannel()->inputAudio((char*)data, len, dts); } + +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){ + assert(ctx && dst_url && ssrc); + MediaHelper::Ptr* obj = (MediaHelper::Ptr*) ctx; + //sender参数无用 + (*obj)->getChannel()->startSendRtp(*(MediaSource *) 1, dst_url, dst_port, ssrc, is_udp, [cb, user_data](const SockException &ex){ + if (cb) { + cb(user_data, ex.getErrCode(), ex.what()); + } + }); +} + +API_EXPORT int API_CALL mk_media_stop_send_rtp(mk_media ctx){ + assert(ctx); + MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx; + //sender参数无用 + return (*obj)->getChannel()->stopSendRtp(*(MediaSource *) 1); +} \ No newline at end of file diff --git a/server/WebApi.cpp b/server/WebApi.cpp index aa67c1ad..008cff6b 100644 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -1309,8 +1309,6 @@ void installWebApi() { } void unInstallWebApi(){ - RtpSelector::Instance().clear(); - { lock_guard lck(s_proxyMapMtx); s_proxyMap.clear(); @@ -1323,6 +1321,7 @@ void unInstallWebApi(){ { #if defined(ENABLE_RTPPROXY) + RtpSelector::Instance().clear(); lock_guard lck(s_rtpServerMapMtx); s_rtpServerMap.clear(); #endif diff --git a/src/Common/Device.cpp b/src/Common/Device.cpp index 50aa4407..ed0b6fe7 100644 --- a/src/Common/Device.cpp +++ b/src/Common/Device.cpp @@ -16,6 +16,13 @@ #include "Extension/G711.h" #include "Extension/H264.h" #include "Extension/H265.h" +#ifdef ENABLE_FAAC +#include "Codec/AACEncoder.h" +#endif //ENABLE_FAAC + +#ifdef ENABLE_X264 +#include "Codec/H264Encoder.h" +#endif //ENABLE_X264 using namespace toolkit; namespace mediakit { @@ -26,8 +33,8 @@ DevChannel::DevChannel(const string &vhost, const string &app, const string &str DevChannel::~DevChannel() {} -#ifdef ENABLE_X264 void DevChannel::inputYUV(char* apcYuv[3], int aiYuvLen[3], uint32_t uiStamp) { +#ifdef ENABLE_X264 //TimeTicker1(50); if (!_pH264Enc) { _pH264Enc.reset(new H264Encoder()); @@ -43,11 +50,13 @@ void DevChannel::inputYUV(char* apcYuv[3], int aiYuvLen[3], uint32_t uiStamp) { inputH264((char *) pOut[i].pucData, pOut[i].iLength, uiStamp); } } -} +#else + WarnL << "h264编码未启用,该方法无效,编译时请打开ENABLE_X264选项"; #endif //ENABLE_X264 +} -#ifdef ENABLE_FAAC void DevChannel::inputPCM(char* pcData, int iDataLen, uint32_t uiStamp) { +#ifdef ENABLE_FAAC if (!_pAacEnc) { _pAacEnc.reset(new AACEncoder()); if (!_pAacEnc->init(_audio->iSampleRate, _audio->iChannel, _audio->iSampleBit)) { @@ -62,8 +71,10 @@ void DevChannel::inputPCM(char* pcData, int iDataLen, uint32_t uiStamp) { inputAAC((char *) pucOut + 7, iRet - 7, uiStamp, (char *)pucOut); } } -} +#else + WarnL << "aac编码未启用,该方法无效,编译时请打开ENABLE_FAAC选项"; #endif //ENABLE_FAAC +} void DevChannel::inputH264(const char *data, int len, uint32_t dts, uint32_t pts) { if(dts == 0){ diff --git a/src/Common/Device.h b/src/Common/Device.h index 694570e7..675f7522 100644 --- a/src/Common/Device.h +++ b/src/Common/Device.h @@ -20,16 +20,11 @@ using namespace std; using namespace toolkit; -#ifdef ENABLE_FAAC -#include "Codec/AACEncoder.h" -#endif //ENABLE_FAAC - -#ifdef ENABLE_X264 -#include "Codec/H264Encoder.h" -#endif //ENABLE_X264 - namespace mediakit { +class H264Encoder; +class AACEncoder; + class VideoInfo { public: CodecId codecId = CodecH264; @@ -107,7 +102,6 @@ public: */ void inputAudio(const char *data, int len, uint32_t dts); -#ifdef ENABLE_X264 /** * 输入yuv420p视频帧,内部会完成编码并调用inputH264方法 * @param apcYuv @@ -115,9 +109,7 @@ public: * @param uiStamp */ void inputYUV(char *apcYuv[3], int aiYuvLen[3], uint32_t uiStamp); -#endif //ENABLE_X264 -#ifdef ENABLE_FAAC /** * 输入pcm数据,内部会完成编码并调用inputAAC方法 @@ -126,20 +118,13 @@ public: * @param uiStamp */ void inputPCM(char *pcData, int iDataLen, uint32_t uiStamp); -#endif //ENABLE_FAAC private: MediaOriginType getOriginType(MediaSource &sender) const override; private: - -#ifdef ENABLE_X264 std::shared_ptr _pH264Enc; -#endif //ENABLE_X264 - -#ifdef ENABLE_FAAC std::shared_ptr _pAacEnc; -#endif //ENABLE_FAAC std::shared_ptr _video; std::shared_ptr _audio; SmoothTicker _aTicker[2]; diff --git a/src/Http/HttpClient.cpp b/src/Http/HttpClient.cpp index 706bd294..05baa5ab 100644 --- a/src/Http/HttpClient.cpp +++ b/src/Http/HttpClient.cpp @@ -46,6 +46,7 @@ void HttpClient::sendRequest(const string &strUrl, float fTimeOutSec) { if (_path.empty()) { _path = "/"; } + auto host_header = host; uint16_t port = atoi(FindField(host.data(), ":", NULL).data()); if (port <= 0) { //默认端口 @@ -54,7 +55,7 @@ void HttpClient::sendRequest(const string &strUrl, float fTimeOutSec) { //服务器域名 host = FindField(host.data(), NULL, ":"); } - _header.emplace("Host", host); + _header.emplace("Host", host_header); _header.emplace("Tools", SERVER_NAME); _header.emplace("Connection", "keep-alive"); _header.emplace("Accept", "*/*"); diff --git a/src/Record/Recorder.cpp b/src/Record/Recorder.cpp index 62f32d76..1dbe4ead 100644 --- a/src/Record/Recorder.cpp +++ b/src/Record/Recorder.cpp @@ -78,7 +78,8 @@ std::shared_ptr Recorder::createRecorder(type type, const st switch (type) { case Recorder::type_hls: { #if defined(ENABLE_HLS) - auto ret = std::make_shared(path, string(VHOST_KEY) + "=" + vhost, 0); + GET_CONFIG(bool, enable_vhost, General::kEnableVhost); + auto ret = std::make_shared(path, enable_vhost ? string(VHOST_KEY) + "=" + vhost : "", 0); InfoL << "create Hls Record ret "<setMediaSource(vhost, app, stream_id); return ret; @@ -95,7 +96,8 @@ std::shared_ptr Recorder::createRecorder(type type, const st case Recorder::type_hls_record: { #if defined(ENABLE_HLS) - auto ret = std::make_shared(path, string(VHOST_KEY) + "=" + vhost, 2); + GET_CONFIG(bool, enable_vhost, General::kEnableVhost); + auto ret = std::make_shared(path, enable_vhost ? string(VHOST_KEY) + "=" + vhost : "", 2); InfoL << "create Hls Record ret "<setMediaSource(vhost, app, stream_id); return ret; diff --git a/src/Rtp/RtpCache.cpp b/src/Rtp/RtpCache.cpp index 59ce4294..3069d306 100644 --- a/src/Rtp/RtpCache.cpp +++ b/src/Rtp/RtpCache.cpp @@ -10,6 +10,8 @@ #include "RtpCache.h" +#if defined(ENABLE_RTPPROXY) + namespace mediakit{ RtpCache::RtpCache(onFlushed cb) { @@ -31,3 +33,5 @@ void RtpCachePS::onRTP(Buffer::Ptr buffer) { } }//namespace mediakit + +#endif//#if defined(ENABLE_RTPPROXY) \ No newline at end of file diff --git a/src/Rtp/RtpCache.h b/src/Rtp/RtpCache.h index b6d00e6a..d0e94517 100644 --- a/src/Rtp/RtpCache.h +++ b/src/Rtp/RtpCache.h @@ -11,6 +11,8 @@ #ifndef ZLMEDIAKIT_RTPCACHE_H #define ZLMEDIAKIT_RTPCACHE_H +#if defined(ENABLE_RTPPROXY) + #include "PSEncoder.h" #include "Extension/CommonRtp.h" @@ -46,4 +48,5 @@ protected: }; }//namespace mediakit +#endif//ENABLE_RTPPROXY #endif //ZLMEDIAKIT_RTPCACHE_H