2020-04-05 09:26:29 +08:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2020-04-05 09:26:29 +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.
|
|
|
|
|
*/
|
|
|
|
|
|
2020-09-12 19:20:18 +08:00
|
|
|
|
#include <math.h>
|
2020-11-15 00:59:31 +08:00
|
|
|
|
#include "Common/config.h"
|
2020-04-05 09:26:29 +08:00
|
|
|
|
#include "MultiMediaSourceMuxer.h"
|
2021-01-23 09:44:37 +08:00
|
|
|
|
|
|
|
|
|
namespace toolkit {
|
|
|
|
|
StatisticImp(mediakit::MultiMediaSourceMuxer);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-05 09:26:29 +08:00
|
|
|
|
namespace mediakit {
|
|
|
|
|
|
2020-09-06 17:52:07 +08:00
|
|
|
|
///////////////////////////////MultiMuxerPrivate//////////////////////////////////
|
|
|
|
|
|
2020-04-05 09:26:29 +08:00
|
|
|
|
MultiMuxerPrivate::~MultiMuxerPrivate() {}
|
2020-09-06 17:52:07 +08:00
|
|
|
|
MultiMuxerPrivate::MultiMuxerPrivate(const string &vhost, const string &app, const string &stream, float dur_sec,
|
|
|
|
|
bool enable_rtsp, bool enable_rtmp, bool enable_hls, bool enable_mp4) {
|
2020-09-12 19:20:18 +08:00
|
|
|
|
_stream_url = vhost + " " + app + " " + stream;
|
2020-04-05 09:26:29 +08:00
|
|
|
|
if (enable_rtmp) {
|
|
|
|
|
_rtmp = std::make_shared<RtmpMediaSourceMuxer>(vhost, app, stream, std::make_shared<TitleMeta>(dur_sec));
|
|
|
|
|
}
|
|
|
|
|
if (enable_rtsp) {
|
|
|
|
|
_rtsp = std::make_shared<RtspMediaSourceMuxer>(vhost, app, stream, std::make_shared<TitleSdp>(dur_sec));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (enable_hls) {
|
2020-09-12 19:20:18 +08:00
|
|
|
|
_hls = dynamic_pointer_cast<HlsRecorder>(Recorder::createRecorder(Recorder::type_hls, vhost, app, stream));
|
2020-04-05 09:26:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (enable_mp4) {
|
|
|
|
|
_mp4 = Recorder::createRecorder(Recorder::type_mp4, vhost, app, stream);
|
|
|
|
|
}
|
2020-09-20 00:21:46 +08:00
|
|
|
|
|
|
|
|
|
_ts = std::make_shared<TSMediaSourceMuxer>(vhost, app, stream);
|
2020-09-27 16:54:55 +08:00
|
|
|
|
|
|
|
|
|
#if defined(ENABLE_MP4)
|
2020-09-20 19:45:37 +08:00
|
|
|
|
_fmp4 = std::make_shared<FMP4MediaSourceMuxer>(vhost, app, stream);
|
2020-09-27 16:54:55 +08:00
|
|
|
|
#endif
|
2020-04-05 09:26:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MultiMuxerPrivate::resetTracks() {
|
|
|
|
|
if (_rtmp) {
|
|
|
|
|
_rtmp->resetTracks();
|
|
|
|
|
}
|
|
|
|
|
if (_rtsp) {
|
|
|
|
|
_rtsp->resetTracks();
|
|
|
|
|
}
|
2020-09-20 00:21:46 +08:00
|
|
|
|
if (_ts) {
|
|
|
|
|
_ts->resetTracks();
|
|
|
|
|
}
|
2020-09-27 16:54:55 +08:00
|
|
|
|
#if defined(ENABLE_MP4)
|
2020-09-20 19:45:37 +08:00
|
|
|
|
if (_fmp4) {
|
|
|
|
|
_fmp4->resetTracks();
|
|
|
|
|
}
|
2020-09-27 16:54:55 +08:00
|
|
|
|
#endif
|
2020-04-05 09:26:29 +08:00
|
|
|
|
|
|
|
|
|
//拷贝智能指针,目的是为了防止跨线程调用设置录像相关api导致的线程竞争问题
|
|
|
|
|
auto hls = _hls;
|
|
|
|
|
if (hls) {
|
|
|
|
|
hls->resetTracks();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto mp4 = _mp4;
|
|
|
|
|
if (mp4) {
|
|
|
|
|
mp4->resetTracks();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MultiMuxerPrivate::setMediaListener(const std::weak_ptr<MediaSourceEvent> &listener) {
|
2020-09-12 19:20:18 +08:00
|
|
|
|
_listener = listener;
|
2020-04-05 09:26:29 +08:00
|
|
|
|
if (_rtmp) {
|
|
|
|
|
_rtmp->setListener(listener);
|
|
|
|
|
}
|
|
|
|
|
if (_rtsp) {
|
|
|
|
|
_rtsp->setListener(listener);
|
|
|
|
|
}
|
2020-09-20 00:21:46 +08:00
|
|
|
|
if (_ts) {
|
|
|
|
|
_ts->setListener(listener);
|
|
|
|
|
}
|
2020-09-27 16:54:55 +08:00
|
|
|
|
#if defined(ENABLE_MP4)
|
2020-09-20 19:45:37 +08:00
|
|
|
|
if (_fmp4) {
|
|
|
|
|
_fmp4->setListener(listener);
|
|
|
|
|
}
|
2020-09-27 16:54:55 +08:00
|
|
|
|
#endif
|
2020-09-12 19:20:18 +08:00
|
|
|
|
auto hls = _hls;
|
|
|
|
|
if (hls) {
|
|
|
|
|
hls->setListener(listener);
|
2020-04-05 09:26:29 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int MultiMuxerPrivate::totalReaderCount() const {
|
2020-09-12 19:20:18 +08:00
|
|
|
|
auto hls = _hls;
|
2020-09-20 00:21:46 +08:00
|
|
|
|
return (_rtsp ? _rtsp->readerCount() : 0) +
|
|
|
|
|
(_rtmp ? _rtmp->readerCount() : 0) +
|
|
|
|
|
(_ts ? _ts->readerCount() : 0) +
|
2020-09-27 16:54:55 +08:00
|
|
|
|
#if defined(ENABLE_MP4)
|
2020-09-20 19:45:37 +08:00
|
|
|
|
(_fmp4 ? _fmp4->readerCount() : 0) +
|
2020-09-27 16:54:55 +08:00
|
|
|
|
#endif
|
2020-09-20 19:45:37 +08:00
|
|
|
|
(hls ? hls->readerCount() : 0);
|
2020-04-05 09:26:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-07 10:41:57 +08:00
|
|
|
|
static std::shared_ptr<MediaSinkInterface> makeRecorder(MediaSource &sender, const vector<Track::Ptr> &tracks, Recorder::type type, const string &custom_path, size_t max_second){
|
|
|
|
|
auto recorder = Recorder::createRecorder(type, sender.getVhost(), sender.getApp(), sender.getId(), custom_path, max_second);
|
2020-04-05 09:26:29 +08:00
|
|
|
|
for (auto &track : tracks) {
|
|
|
|
|
recorder->addTrack(track);
|
|
|
|
|
}
|
|
|
|
|
return recorder;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//此函数可能跨线程调用
|
2021-03-07 10:41:57 +08:00
|
|
|
|
bool MultiMuxerPrivate::setupRecord(MediaSource &sender, Recorder::type type, bool start, const string &custom_path, size_t max_second){
|
2020-04-05 09:26:29 +08:00
|
|
|
|
switch (type) {
|
|
|
|
|
case Recorder::type_hls : {
|
|
|
|
|
if (start && !_hls) {
|
|
|
|
|
//开始录制
|
2021-03-07 10:41:57 +08:00
|
|
|
|
auto hls = dynamic_pointer_cast<HlsRecorder>(makeRecorder(sender, getTracks(true), type, custom_path, max_second));
|
2020-09-12 19:20:18 +08:00
|
|
|
|
if (hls) {
|
2020-04-05 09:26:29 +08:00
|
|
|
|
//设置HlsMediaSource的事件监听器
|
2020-09-12 19:20:18 +08:00
|
|
|
|
hls->setListener(_listener);
|
2020-04-05 09:26:29 +08:00
|
|
|
|
}
|
2020-09-12 19:20:18 +08:00
|
|
|
|
_hls = hls;
|
2020-04-05 09:26:29 +08:00
|
|
|
|
} else if (!start && _hls) {
|
|
|
|
|
//停止录制
|
|
|
|
|
_hls = nullptr;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
case Recorder::type_mp4 : {
|
|
|
|
|
if (start && !_mp4) {
|
|
|
|
|
//开始录制
|
2021-03-07 10:41:57 +08:00
|
|
|
|
_mp4 = makeRecorder(sender, getTracks(true), type, custom_path, max_second);
|
2020-04-05 09:26:29 +08:00
|
|
|
|
} else if (!start && _mp4) {
|
|
|
|
|
//停止录制
|
|
|
|
|
_mp4 = nullptr;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-04-29 11:59:45 +08:00
|
|
|
|
default : return false;
|
2020-04-05 09:26:29 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//此函数可能跨线程调用
|
|
|
|
|
bool MultiMuxerPrivate::isRecording(MediaSource &sender, Recorder::type type){
|
|
|
|
|
switch (type){
|
|
|
|
|
case Recorder::type_hls :
|
|
|
|
|
return _hls ? true : false;
|
|
|
|
|
case Recorder::type_mp4 :
|
|
|
|
|
return _mp4 ? true : false;
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MultiMuxerPrivate::setTimeStamp(uint32_t stamp) {
|
|
|
|
|
if (_rtmp) {
|
|
|
|
|
_rtmp->setTimeStamp(stamp);
|
|
|
|
|
}
|
|
|
|
|
if (_rtsp) {
|
|
|
|
|
_rtsp->setTimeStamp(stamp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MultiMuxerPrivate::setTrackListener(Listener *listener) {
|
2020-09-06 17:52:07 +08:00
|
|
|
|
_track_listener = listener;
|
2020-04-05 09:26:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MultiMuxerPrivate::onTrackReady(const Track::Ptr &track) {
|
|
|
|
|
if (_rtmp) {
|
|
|
|
|
_rtmp->addTrack(track);
|
|
|
|
|
}
|
|
|
|
|
if (_rtsp) {
|
|
|
|
|
_rtsp->addTrack(track);
|
|
|
|
|
}
|
2020-09-20 00:21:46 +08:00
|
|
|
|
if (_ts) {
|
|
|
|
|
_ts->addTrack(track);
|
|
|
|
|
}
|
2020-09-27 16:54:55 +08:00
|
|
|
|
#if defined(ENABLE_MP4)
|
2020-09-20 19:45:37 +08:00
|
|
|
|
if (_fmp4) {
|
|
|
|
|
_fmp4->addTrack(track);
|
|
|
|
|
}
|
2020-09-27 16:54:55 +08:00
|
|
|
|
#endif
|
2020-04-05 09:26:29 +08:00
|
|
|
|
|
|
|
|
|
//拷贝智能指针,目的是为了防止跨线程调用设置录像相关api导致的线程竞争问题
|
|
|
|
|
auto hls = _hls;
|
|
|
|
|
if (hls) {
|
|
|
|
|
hls->addTrack(track);
|
|
|
|
|
}
|
|
|
|
|
auto mp4 = _mp4;
|
|
|
|
|
if (mp4) {
|
|
|
|
|
mp4->addTrack(track);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-29 11:59:45 +08:00
|
|
|
|
bool MultiMuxerPrivate::isEnabled(){
|
2020-09-12 19:20:18 +08:00
|
|
|
|
auto hls = _hls;
|
|
|
|
|
return (_rtmp ? _rtmp->isEnabled() : false) ||
|
|
|
|
|
(_rtsp ? _rtsp->isEnabled() : false) ||
|
2020-09-20 00:21:46 +08:00
|
|
|
|
(_ts ? _ts->isEnabled() : false) ||
|
2020-09-27 16:54:55 +08:00
|
|
|
|
#if defined(ENABLE_MP4)
|
2020-09-20 19:45:37 +08:00
|
|
|
|
(_fmp4 ? _fmp4->isEnabled() : false) ||
|
2020-09-27 16:54:55 +08:00
|
|
|
|
#endif
|
2020-09-12 19:20:18 +08:00
|
|
|
|
(hls ? hls->isEnabled() : false) || _mp4;
|
2020-04-29 11:59:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-05 09:26:29 +08:00
|
|
|
|
void MultiMuxerPrivate::onTrackFrame(const Frame::Ptr &frame) {
|
|
|
|
|
if (_rtmp) {
|
|
|
|
|
_rtmp->inputFrame(frame);
|
|
|
|
|
}
|
|
|
|
|
if (_rtsp) {
|
|
|
|
|
_rtsp->inputFrame(frame);
|
|
|
|
|
}
|
2020-09-20 00:21:46 +08:00
|
|
|
|
if (_ts) {
|
|
|
|
|
_ts->inputFrame(frame);
|
|
|
|
|
}
|
2020-09-27 16:54:55 +08:00
|
|
|
|
#if defined(ENABLE_MP4)
|
2020-09-20 19:45:37 +08:00
|
|
|
|
if (_fmp4) {
|
|
|
|
|
_fmp4->inputFrame(frame);
|
|
|
|
|
}
|
2020-09-27 16:54:55 +08:00
|
|
|
|
#endif
|
2020-09-20 00:21:46 +08:00
|
|
|
|
|
2020-04-05 09:26:29 +08:00
|
|
|
|
//拷贝智能指针,目的是为了防止跨线程调用设置录像相关api导致的线程竞争问题
|
|
|
|
|
//此处使用智能指针拷贝来确保线程安全,比互斥锁性能更优
|
|
|
|
|
auto hls = _hls;
|
|
|
|
|
if (hls) {
|
|
|
|
|
hls->inputFrame(frame);
|
|
|
|
|
}
|
|
|
|
|
auto mp4 = _mp4;
|
|
|
|
|
if (mp4) {
|
|
|
|
|
mp4->inputFrame(frame);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-12 19:20:18 +08:00
|
|
|
|
static string getTrackInfoStr(const TrackSource *track_src){
|
|
|
|
|
_StrPrinter codec_info;
|
|
|
|
|
auto tracks = track_src->getTracks(true);
|
|
|
|
|
for (auto &track : tracks) {
|
|
|
|
|
auto codec_type = track->getTrackType();
|
|
|
|
|
codec_info << track->getCodecName();
|
|
|
|
|
switch (codec_type) {
|
|
|
|
|
case TrackAudio : {
|
|
|
|
|
auto audio_track = dynamic_pointer_cast<AudioTrack>(track);
|
|
|
|
|
codec_info << "["
|
|
|
|
|
<< audio_track->getAudioSampleRate() << "/"
|
|
|
|
|
<< audio_track->getAudioChannel() << "/"
|
|
|
|
|
<< audio_track->getAudioSampleBit() << "] ";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case TrackVideo : {
|
|
|
|
|
auto video_track = dynamic_pointer_cast<VideoTrack>(track);
|
|
|
|
|
codec_info << "["
|
|
|
|
|
<< video_track->getVideoWidth() << "/"
|
|
|
|
|
<< video_track->getVideoHeight() << "/"
|
|
|
|
|
<< round(video_track->getVideoFps()) << "] ";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-17 20:15:08 +08:00
|
|
|
|
return std::move(codec_info);
|
2020-09-12 19:20:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-05 09:26:29 +08:00
|
|
|
|
void MultiMuxerPrivate::onAllTrackReady() {
|
|
|
|
|
if (_rtmp) {
|
|
|
|
|
_rtmp->onAllTrackReady();
|
|
|
|
|
}
|
|
|
|
|
if (_rtsp) {
|
|
|
|
|
_rtsp->onAllTrackReady();
|
|
|
|
|
}
|
2020-09-27 16:54:55 +08:00
|
|
|
|
#if defined(ENABLE_MP4)
|
2020-09-20 19:45:37 +08:00
|
|
|
|
if (_fmp4) {
|
|
|
|
|
_fmp4->onAllTrackReady();
|
|
|
|
|
}
|
2020-09-27 16:54:55 +08:00
|
|
|
|
#endif
|
2020-09-06 17:52:07 +08:00
|
|
|
|
if (_track_listener) {
|
|
|
|
|
_track_listener->onAllTrackReady();
|
2020-04-05 09:26:29 +08:00
|
|
|
|
}
|
2020-09-12 19:20:18 +08:00
|
|
|
|
InfoL << "stream: " << _stream_url << " , codec info: " << getTrackInfoStr(this);
|
2020-04-05 09:26:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-06 17:52:07 +08:00
|
|
|
|
///////////////////////////////MultiMediaSourceMuxer//////////////////////////////////
|
2020-04-05 09:26:29 +08:00
|
|
|
|
|
|
|
|
|
MultiMediaSourceMuxer::~MultiMediaSourceMuxer() {}
|
2020-09-06 17:52:07 +08:00
|
|
|
|
|
|
|
|
|
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) {
|
2020-04-05 09:26:29 +08:00
|
|
|
|
_muxer.reset(new MultiMuxerPrivate(vhost, app, stream, dur_sec, enable_rtsp, enable_rtmp, enable_hls, enable_mp4));
|
2020-08-08 12:20:13 +08:00
|
|
|
|
_muxer->setTrackListener(this);
|
2020-04-05 09:26:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MultiMediaSourceMuxer::setMediaListener(const std::weak_ptr<MediaSourceEvent> &listener) {
|
2020-10-24 23:31:58 +08:00
|
|
|
|
setDelegate(listener);
|
2020-09-06 17:52:07 +08:00
|
|
|
|
//拦截事件
|
2020-04-05 09:26:29 +08:00
|
|
|
|
_muxer->setMediaListener(shared_from_this());
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-08 12:20:13 +08:00
|
|
|
|
void MultiMediaSourceMuxer::setTrackListener(const std::weak_ptr<MultiMuxerPrivate::Listener> &listener) {
|
|
|
|
|
_track_listener = listener;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-05 09:26:29 +08:00
|
|
|
|
int MultiMediaSourceMuxer::totalReaderCount() const {
|
|
|
|
|
return _muxer->totalReaderCount();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MultiMediaSourceMuxer::setTimeStamp(uint32_t stamp) {
|
|
|
|
|
_muxer->setTimeStamp(stamp);
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-06 17:54:52 +08:00
|
|
|
|
vector<Track::Ptr> MultiMediaSourceMuxer::getTracks(MediaSource &sender, bool trackReady) const {
|
2020-04-05 09:26:29 +08:00
|
|
|
|
return _muxer->getTracks(trackReady);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int MultiMediaSourceMuxer::totalReaderCount(MediaSource &sender) {
|
2020-10-24 23:31:58 +08:00
|
|
|
|
auto listener = getDelegate();
|
2020-04-05 09:26:29 +08:00
|
|
|
|
if (!listener) {
|
2020-09-06 17:52:07 +08:00
|
|
|
|
return totalReaderCount();
|
2020-04-05 09:26:29 +08:00
|
|
|
|
}
|
|
|
|
|
return listener->totalReaderCount(sender);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-07 10:41:57 +08:00
|
|
|
|
bool MultiMediaSourceMuxer::setupRecord(MediaSource &sender, Recorder::type type, bool start, const string &custom_path, size_t max_second) {
|
|
|
|
|
return _muxer->setupRecord(sender, type, start, custom_path, max_second);
|
2020-04-05 09:26:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool MultiMediaSourceMuxer::isRecording(MediaSource &sender, Recorder::type type) {
|
|
|
|
|
return _muxer->isRecording(sender,type);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-02 20:43:02 +08:00
|
|
|
|
void MultiMediaSourceMuxer::startSendRtp(MediaSource &sender, const string &dst_url, uint16_t dst_port, const string &ssrc, bool is_udp, uint16_t src_port, const function<void(uint16_t local_port, const SockException &ex)> &cb){
|
2020-09-06 17:56:05 +08:00
|
|
|
|
#if defined(ENABLE_RTPPROXY)
|
2020-10-24 23:33:13 +08:00
|
|
|
|
RtpSender::Ptr rtp_sender = std::make_shared<RtpSender>(atoi(ssrc.data()));
|
2020-09-06 17:56:05 +08:00
|
|
|
|
weak_ptr<MultiMediaSourceMuxer> weak_self = shared_from_this();
|
2021-01-02 20:43:02 +08:00
|
|
|
|
rtp_sender->startSend(dst_url, dst_port, is_udp, src_port, [weak_self, rtp_sender, cb, ssrc](uint16_t local_port, const SockException &ex) {
|
|
|
|
|
cb(local_port, ex);
|
2020-09-06 17:56:05 +08:00
|
|
|
|
auto strong_self = weak_self.lock();
|
|
|
|
|
if (!strong_self || ex) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
for (auto &track : strong_self->_muxer->getTracks(false)) {
|
2020-10-24 23:33:13 +08:00
|
|
|
|
rtp_sender->addTrack(track);
|
2020-09-06 17:56:05 +08:00
|
|
|
|
}
|
2020-10-24 23:33:13 +08:00
|
|
|
|
rtp_sender->addTrackCompleted();
|
2020-12-27 18:41:53 +08:00
|
|
|
|
lock_guard<mutex> lck(strong_self->_rtp_sender_mtx);
|
|
|
|
|
strong_self->_rtp_sender[ssrc] = rtp_sender;
|
2020-09-06 17:56:05 +08:00
|
|
|
|
});
|
|
|
|
|
#else
|
2021-01-03 15:55:11 +08:00
|
|
|
|
cb(0, SockException(Err_other, "该功能未启用,编译时请打开ENABLE_RTPPROXY宏"));
|
2020-09-06 17:56:05 +08:00
|
|
|
|
#endif//ENABLE_RTPPROXY
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-27 18:41:53 +08:00
|
|
|
|
bool MultiMediaSourceMuxer::stopSendRtp(MediaSource &sender, const string& ssrc){
|
2020-09-06 17:56:05 +08:00
|
|
|
|
#if defined(ENABLE_RTPPROXY)
|
2020-12-27 18:41:53 +08:00
|
|
|
|
if (ssrc.empty()) {
|
|
|
|
|
//关闭全部
|
|
|
|
|
lock_guard<mutex> lck(_rtp_sender_mtx);
|
|
|
|
|
auto size = _rtp_sender.size();
|
|
|
|
|
_rtp_sender.clear();
|
|
|
|
|
return size;
|
|
|
|
|
}
|
|
|
|
|
//关闭特定的
|
|
|
|
|
lock_guard<mutex> lck(_rtp_sender_mtx);
|
|
|
|
|
return _rtp_sender.erase(ssrc);
|
|
|
|
|
#else
|
2020-09-06 17:56:05 +08:00
|
|
|
|
return false;
|
2020-12-27 18:41:53 +08:00
|
|
|
|
#endif//ENABLE_RTPPROXY
|
2020-09-06 17:56:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-05 09:26:29 +08:00
|
|
|
|
void MultiMediaSourceMuxer::addTrack(const Track::Ptr &track) {
|
2020-12-26 16:52:36 +08:00
|
|
|
|
if (CodecL16 == track->getCodecId()) {
|
|
|
|
|
WarnL << "L16音频格式目前只支持RTSP协议推流拉流!!!";
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-04-05 09:26:29 +08:00
|
|
|
|
_muxer->addTrack(track);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MultiMediaSourceMuxer::addTrackCompleted() {
|
|
|
|
|
_muxer->addTrackCompleted();
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-08 12:20:13 +08:00
|
|
|
|
void MultiMediaSourceMuxer::onAllTrackReady(){
|
|
|
|
|
_muxer->setMediaListener(shared_from_this());
|
2020-09-06 17:52:07 +08:00
|
|
|
|
auto listener = _track_listener.lock();
|
|
|
|
|
if(listener){
|
|
|
|
|
listener->onAllTrackReady();
|
2020-08-08 12:20:13 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-05 09:26:29 +08:00
|
|
|
|
void MultiMediaSourceMuxer::resetTracks() {
|
|
|
|
|
_muxer->resetTracks();
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-25 12:07:38 +08:00
|
|
|
|
//该类实现frame级别的时间戳覆盖
|
|
|
|
|
class FrameModifyStamp : public Frame{
|
|
|
|
|
public:
|
|
|
|
|
typedef std::shared_ptr<FrameModifyStamp> Ptr;
|
|
|
|
|
FrameModifyStamp(const Frame::Ptr &frame, Stamp &stamp){
|
|
|
|
|
_frame = frame;
|
|
|
|
|
//覆盖时间戳
|
|
|
|
|
stamp.revise(frame->dts(), frame->pts(), _dts, _pts, true);
|
|
|
|
|
}
|
|
|
|
|
~FrameModifyStamp() override {}
|
|
|
|
|
|
|
|
|
|
uint32_t dts() const override{
|
2021-01-17 18:31:50 +08:00
|
|
|
|
return (uint32_t)_dts;
|
2020-05-25 12:07:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t pts() const override{
|
2021-01-17 18:31:50 +08:00
|
|
|
|
return (uint32_t)_pts;
|
2020-05-25 12:07:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-17 18:31:50 +08:00
|
|
|
|
size_t prefixSize() const override {
|
2020-05-25 12:07:38 +08:00
|
|
|
|
return _frame->prefixSize();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool keyFrame() const override {
|
|
|
|
|
return _frame->keyFrame();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool configFrame() const override {
|
|
|
|
|
return _frame->configFrame();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool cacheAble() const override {
|
|
|
|
|
return _frame->cacheAble();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *data() const override {
|
|
|
|
|
return _frame->data();
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-17 18:31:50 +08:00
|
|
|
|
size_t size() const override {
|
2020-05-25 12:07:38 +08:00
|
|
|
|
return _frame->size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CodecId getCodecId() const override {
|
|
|
|
|
return _frame->getCodecId();
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
int64_t _dts;
|
|
|
|
|
int64_t _pts;
|
2020-09-06 17:52:07 +08:00
|
|
|
|
Frame::Ptr _frame;
|
2020-05-25 12:07:38 +08:00
|
|
|
|
};
|
|
|
|
|
|
2020-09-06 17:56:05 +08:00
|
|
|
|
void MultiMediaSourceMuxer::inputFrame(const Frame::Ptr &frame_in) {
|
2020-09-06 17:52:07 +08:00
|
|
|
|
GET_CONFIG(bool, modify_stamp, General::kModifyStamp);
|
2020-09-06 17:56:05 +08:00
|
|
|
|
auto frame = frame_in;
|
|
|
|
|
if (modify_stamp) {
|
2020-05-25 12:07:38 +08:00
|
|
|
|
//开启了时间戳覆盖
|
2020-09-06 17:56:05 +08:00
|
|
|
|
frame = std::make_shared<FrameModifyStamp>(frame, _stamp[frame->getTrackType()]);
|
2020-05-25 12:07:38 +08:00
|
|
|
|
}
|
2020-09-06 17:56:05 +08:00
|
|
|
|
_muxer->inputFrame(frame);
|
|
|
|
|
|
|
|
|
|
#if defined(ENABLE_RTPPROXY)
|
2020-12-27 18:41:53 +08:00
|
|
|
|
lock_guard<mutex> lck(_rtp_sender_mtx);
|
|
|
|
|
for (auto &pr : _rtp_sender) {
|
|
|
|
|
pr.second->inputFrame(frame);
|
2020-09-06 17:56:05 +08:00
|
|
|
|
}
|
|
|
|
|
#endif //ENABLE_RTPPROXY
|
|
|
|
|
|
2020-04-05 09:26:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-29 11:59:45 +08:00
|
|
|
|
bool MultiMediaSourceMuxer::isEnabled(){
|
2020-11-15 00:59:31 +08:00
|
|
|
|
GET_CONFIG(uint32_t, stream_none_reader_delay_ms, General::kStreamNoneReaderDelayMS);
|
|
|
|
|
if (!_is_enable || _last_check.elapsedTime() > stream_none_reader_delay_ms) {
|
|
|
|
|
//无人观看时,每次检查是否真的无人观看
|
|
|
|
|
//有人观看时,则延迟一定时间检查一遍是否无人观看了(节省性能)
|
2020-09-10 19:28:30 +08:00
|
|
|
|
#if defined(ENABLE_RTPPROXY)
|
2020-12-27 18:41:53 +08:00
|
|
|
|
_is_enable = (_muxer->isEnabled() || _rtp_sender.size());
|
2020-09-12 19:20:18 +08:00
|
|
|
|
#else
|
2020-11-15 00:59:31 +08:00
|
|
|
|
_is_enable = _muxer->isEnabled();
|
2020-09-12 19:20:18 +08:00
|
|
|
|
#endif //ENABLE_RTPPROXY
|
2020-11-15 00:59:31 +08:00
|
|
|
|
if (_is_enable) {
|
|
|
|
|
//无人观看时,不刷新计时器,因为无人观看时每次都会检查一遍,所以刷新计数器无意义且浪费cpu
|
|
|
|
|
_last_check.resetTime();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return _is_enable;
|
2020-04-29 11:59:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-09-10 19:28:30 +08:00
|
|
|
|
}//namespace mediakit
|