完善getOriginUrl接口

This commit is contained in:
ziyue 2021-08-16 17:31:13 +08:00
parent 232263669c
commit 10522e4ea5
5 changed files with 41 additions and 9 deletions

View File

@ -40,6 +40,17 @@ string getOriginTypeString(MediaOriginType type){
} }
} }
static string getOriginUrl_l(const MediaSource *thiz) {
if (thiz == MediaSource::NullMediaSource) {
return "";
}
return thiz->getSchema() + "://" + thiz->getVhost() + "/" + thiz->getApp() + "/" + thiz->getId();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
MediaSource * const MediaSource::NullMediaSource = nullptr;
MediaSource::MediaSource(const string &schema, const string &vhost, const string &app, const string &stream_id){ MediaSource::MediaSource(const string &schema, const string &vhost, const string &app, const string &stream_id){
GET_CONFIG(bool, enableVhost, General::kEnableVhost); GET_CONFIG(bool, enableVhost, General::kEnableVhost);
if (!enableVhost) { if (!enableVhost) {
@ -136,9 +147,13 @@ MediaOriginType MediaSource::getOriginType() const {
string MediaSource::getOriginUrl() const { string MediaSource::getOriginUrl() const {
auto listener = _listener.lock(); auto listener = _listener.lock();
if (!listener) { if (!listener) {
return ""; return getOriginUrl_l(this);
} }
return listener->getOriginUrl(const_cast<MediaSource &>(*this)); auto ret = listener->getOriginUrl(const_cast<MediaSource &>(*this));
if (!ret.empty()) {
return ret;
}
return getOriginUrl_l(this);
} }
std::shared_ptr<SockInfo> MediaSource::getOriginSock() const { std::shared_ptr<SockInfo> MediaSource::getOriginSock() const {
@ -567,6 +582,10 @@ void MediaSourceEvent::onReaderChanged(MediaSource &sender, int size){
}, nullptr); }, nullptr);
} }
string MediaSourceEvent::getOriginUrl(MediaSource &sender) const {
return getOriginUrl_l(&sender);
}
MediaOriginType MediaSourceEventInterceptor::getOriginType(MediaSource &sender) const { MediaOriginType MediaSourceEventInterceptor::getOriginType(MediaSource &sender) const {
auto listener = _listener.lock(); auto listener = _listener.lock();
if (!listener) { if (!listener) {
@ -578,9 +597,13 @@ MediaOriginType MediaSourceEventInterceptor::getOriginType(MediaSource &sender)
string MediaSourceEventInterceptor::getOriginUrl(MediaSource &sender) const { string MediaSourceEventInterceptor::getOriginUrl(MediaSource &sender) const {
auto listener = _listener.lock(); auto listener = _listener.lock();
if (!listener) { if (!listener) {
return ""; return MediaSourceEvent::getOriginUrl(sender);
} }
return listener->getOriginUrl(sender); auto ret = listener->getOriginUrl(sender);
if (!ret.empty()) {
return ret;
}
return MediaSourceEvent::getOriginUrl(sender);
} }
std::shared_ptr<SockInfo> MediaSourceEventInterceptor::getOriginSock(MediaSource &sender) const { std::shared_ptr<SockInfo> MediaSourceEventInterceptor::getOriginSock(MediaSource &sender) const {

View File

@ -61,7 +61,7 @@ public:
// 获取媒体源类型 // 获取媒体源类型
virtual MediaOriginType getOriginType(MediaSource &sender) const { return MediaOriginType::unknown; } virtual MediaOriginType getOriginType(MediaSource &sender) const { return MediaOriginType::unknown; }
// 获取媒体源url或者文件路径 // 获取媒体源url或者文件路径
virtual string getOriginUrl(MediaSource &sender) const { return ""; } virtual string getOriginUrl(MediaSource &sender) const;
// 获取媒体源客户端相关信息 // 获取媒体源客户端相关信息
virtual std::shared_ptr<SockInfo> getOriginSock(MediaSource &sender) const { return nullptr; } virtual std::shared_ptr<SockInfo> getOriginSock(MediaSource &sender) const { return nullptr; }
@ -198,7 +198,7 @@ private:
*/ */
class MediaSource: public TrackSource, public enable_shared_from_this<MediaSource> { class MediaSource: public TrackSource, public enable_shared_from_this<MediaSource> {
public: public:
static constexpr MediaSource *NullMediaSource = nullptr; static MediaSource * const NullMediaSource;
using Ptr = std::shared_ptr<MediaSource>; using Ptr = std::shared_ptr<MediaSource>;
using StreamMap = unordered_map<string, weak_ptr<MediaSource> >; using StreamMap = unordered_map<string, weak_ptr<MediaSource> >;
using AppStreamMap = unordered_map<string, StreamMap>; using AppStreamMap = unordered_map<string, StreamMap>;

View File

@ -58,6 +58,14 @@ static string getTrackInfoStr(const TrackSource *track_src){
MultiMediaSourceMuxer::MultiMediaSourceMuxer(const string &vhost, const string &app, const string &stream, float dur_sec, MultiMediaSourceMuxer::MultiMediaSourceMuxer(const string &vhost, const string &app, const string &stream, float dur_sec,
bool enable_rtsp, bool enable_rtmp, bool enable_hls, bool enable_mp4) { bool enable_rtsp, bool enable_rtmp, bool enable_hls, bool enable_mp4) {
_get_origin_url = [this, vhost, app, stream]() {
auto ret = getOriginUrl(*MediaSource::NullMediaSource);
if (!ret.empty()) {
return ret;
}
return vhost + "/" + app + "/" + stream;
};
if (enable_rtmp) { if (enable_rtmp) {
_rtmp = std::make_shared<RtmpMediaSourceMuxer>(vhost, app, stream, std::make_shared<TitleMeta>(dur_sec)); _rtmp = std::make_shared<RtmpMediaSourceMuxer>(vhost, app, stream, std::make_shared<TitleMeta>(dur_sec));
} }
@ -286,7 +294,7 @@ void MultiMediaSourceMuxer::onAllTrackReady() {
if (listener) { if (listener) {
listener->onAllTrackReady(); listener->onAllTrackReady();
} }
InfoL << "stream: " << getOriginUrl(*MediaSource::NullMediaSource) << " , codec info: " << getTrackInfoStr(this); InfoL << "stream: " << _get_origin_url() << " , codec info: " << getTrackInfoStr(this);
} }
void MultiMediaSourceMuxer::resetTracks() { void MultiMediaSourceMuxer::resetTracks() {

View File

@ -145,6 +145,7 @@ private:
Ticker _last_check; Ticker _last_check;
Stamp _stamp[2]; Stamp _stamp[2];
std::weak_ptr<Listener> _track_listener; std::weak_ptr<Listener> _track_listener;
function<string()> _get_origin_url;
#if defined(ENABLE_RTPPROXY) #if defined(ENABLE_RTPPROXY)
mutex _rtp_sender_mtx; mutex _rtp_sender_mtx;
unordered_map<string, RtpSender::Ptr> _rtp_sender; unordered_map<string, RtpSender::Ptr> _rtp_sender;

View File

@ -262,7 +262,7 @@ MediaOriginType RtpProcess::getOriginType(MediaSource &sender) const{
} }
string RtpProcess::getOriginUrl(MediaSource &sender) const { string RtpProcess::getOriginUrl(MediaSource &sender) const {
return _media_info._full_url; return _media_info._schema + "://" + _media_info._vhost + "/" + _media_info._app + "/" + _media_info._streamid;
} }
std::shared_ptr<SockInfo> RtpProcess::getOriginSock(MediaSource &sender) const { std::shared_ptr<SockInfo> RtpProcess::getOriginSock(MediaSource &sender) const {