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

11
#include <math.h>
12 13 14
#include "MultiMediaSourceMuxer.h"
namespace mediakit {

xiongziliang committed
15 16
///////////////////////////////MultiMuxerPrivate//////////////////////////////////

17
MultiMuxerPrivate::~MultiMuxerPrivate() {}
xiongziliang committed
18 19
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) {
20
    _stream_url = vhost + " " + app + " " + stream;
21 22 23 24 25 26 27 28
    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) {
29
        _hls = dynamic_pointer_cast<HlsRecorder>(Recorder::createRecorder(Recorder::type_hls, vhost, app, stream));
30 31 32 33 34
    }

    if (enable_mp4) {
        _mp4 = Recorder::createRecorder(Recorder::type_mp4, vhost, app, stream);
    }
35 36

    _ts = std::make_shared<TSMediaSourceMuxer>(vhost, app, stream);
37
    _fmp4 = std::make_shared<FMP4MediaSourceMuxer>(vhost, app, stream);
38 39 40 41 42 43 44 45 46
}

void MultiMuxerPrivate::resetTracks() {
    if (_rtmp) {
        _rtmp->resetTracks();
    }
    if (_rtsp) {
        _rtsp->resetTracks();
    }
47 48 49
    if (_ts) {
        _ts->resetTracks();
    }
50 51 52
    if (_fmp4) {
        _fmp4->resetTracks();
    }
53 54 55 56 57 58 59 60 61 62 63 64 65 66

    //拷贝智能指针,目的是为了防止跨线程调用设置录像相关api导致的线程竞争问题
    auto hls = _hls;
    if (hls) {
        hls->resetTracks();
    }

    auto mp4 = _mp4;
    if (mp4) {
        mp4->resetTracks();
    }
}

void MultiMuxerPrivate::setMediaListener(const std::weak_ptr<MediaSourceEvent> &listener) {
67
    _listener = listener;
68 69 70 71 72 73
    if (_rtmp) {
        _rtmp->setListener(listener);
    }
    if (_rtsp) {
        _rtsp->setListener(listener);
    }
74 75 76
    if (_ts) {
        _ts->setListener(listener);
    }
77 78 79
    if (_fmp4) {
        _fmp4->setListener(listener);
    }
80 81 82
    auto hls = _hls;
    if (hls) {
        hls->setListener(listener);
83 84 85 86
    }
}

int MultiMuxerPrivate::totalReaderCount() const {
87
    auto hls = _hls;
88 89 90
    return (_rtsp ? _rtsp->readerCount() : 0) +
           (_rtmp ? _rtmp->readerCount() : 0) +
           (_ts ? _ts->readerCount() : 0) +
91 92
           (_fmp4 ? _fmp4->readerCount() : 0) +
           (hls ? hls->readerCount() : 0);
93 94
}

95 96
static std::shared_ptr<MediaSinkInterface> makeRecorder(const vector<Track::Ptr> &tracks, Recorder::type type, const string &custom_path, MediaSource &sender){
    auto recorder = Recorder::createRecorder(type, sender.getVhost(), sender.getApp(), sender.getId(), custom_path);
97 98 99 100 101 102 103 104 105 106 107 108
    for (auto &track : tracks) {
        recorder->addTrack(track);
    }
    return recorder;
}

//此函数可能跨线程调用
bool MultiMuxerPrivate::setupRecord(MediaSource &sender, Recorder::type type, bool start, const string &custom_path){
    switch (type) {
        case Recorder::type_hls : {
            if (start && !_hls) {
                //开始录制
109 110
                auto hls = dynamic_pointer_cast<HlsRecorder>(makeRecorder(getTracks(true), type, custom_path, sender));
                if (hls) {
111
                    //设置HlsMediaSource的事件监听器
112
                    hls->setListener(_listener);
113
                }
114
                _hls = hls;
115 116 117 118 119 120 121 122 123
            } else if (!start && _hls) {
                //停止录制
                _hls = nullptr;
            }
            return true;
        }
        case Recorder::type_mp4 : {
            if (start && !_mp4) {
                //开始录制
124
                _mp4 = makeRecorder(getTracks(true), type, custom_path, sender);
125 126 127 128 129 130
            } else if (!start && _mp4) {
                //停止录制
                _mp4 = nullptr;
            }
            return true;
        }
131
        default : return false;
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    }
}

//此函数可能跨线程调用
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) {
xiongziliang committed
157
    _track_listener = listener;
158 159 160 161 162 163 164 165 166
}

void MultiMuxerPrivate::onTrackReady(const Track::Ptr &track) {
    if (_rtmp) {
        _rtmp->addTrack(track);
    }
    if (_rtsp) {
        _rtsp->addTrack(track);
    }
167 168 169
    if (_ts) {
        _ts->addTrack(track);
    }
170 171 172
    if (_fmp4) {
        _fmp4->addTrack(track);
    }
173 174 175 176 177 178 179 180 181 182 183 184

    //拷贝智能指针,目的是为了防止跨线程调用设置录像相关api导致的线程竞争问题
    auto hls = _hls;
    if (hls) {
        hls->addTrack(track);
    }
    auto mp4 = _mp4;
    if (mp4) {
        mp4->addTrack(track);
    }
}

185
bool MultiMuxerPrivate::isEnabled(){
186 187 188
    auto hls = _hls;
    return (_rtmp ? _rtmp->isEnabled() : false) ||
           (_rtsp ? _rtsp->isEnabled() : false) ||
189
           (_ts ? _ts->isEnabled() : false) ||
190
           (_fmp4 ? _fmp4->isEnabled() : false) ||
191
           (hls ? hls->isEnabled() : false) || _mp4;
192 193
}

194 195 196 197 198 199 200
void MultiMuxerPrivate::onTrackFrame(const Frame::Ptr &frame) {
    if (_rtmp) {
        _rtmp->inputFrame(frame);
    }
    if (_rtsp) {
        _rtsp->inputFrame(frame);
    }
201 202 203
    if (_ts) {
        _ts->inputFrame(frame);
    }
204 205 206
    if (_fmp4) {
        _fmp4->inputFrame(frame);
    }
207

208 209 210 211 212 213 214 215 216 217 218 219
    //拷贝智能指针,目的是为了防止跨线程调用设置录像相关api导致的线程竞争问题
    //此处使用智能指针拷贝来确保线程安全,比互斥锁性能更优
    auto hls = _hls;
    if (hls) {
        hls->inputFrame(frame);
    }
    auto mp4 = _mp4;
    if (mp4) {
        mp4->inputFrame(frame);
    }
}

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
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;
        }
    }
    return codec_info;
}

250 251 252 253 254 255 256
void MultiMuxerPrivate::onAllTrackReady() {
    if (_rtmp) {
        _rtmp->onAllTrackReady();
    }
    if (_rtsp) {
        _rtsp->onAllTrackReady();
    }
257 258 259
    if (_fmp4) {
        _fmp4->onAllTrackReady();
    }
xiongziliang committed
260 261
    if (_track_listener) {
        _track_listener->onAllTrackReady();
262
    }
263
    InfoL << "stream: " << _stream_url << " , codec info: " << getTrackInfoStr(this);
264 265
}

xiongziliang committed
266
///////////////////////////////MultiMediaSourceMuxer//////////////////////////////////
267 268

MultiMediaSourceMuxer::~MultiMediaSourceMuxer() {}
xiongziliang committed
269 270 271

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) {
272
    _muxer.reset(new MultiMuxerPrivate(vhost, app, stream, dur_sec, enable_rtsp, enable_rtmp, enable_hls, enable_mp4));
273
    _muxer->setTrackListener(this);
274 275 276
}

void MultiMediaSourceMuxer::setMediaListener(const std::weak_ptr<MediaSourceEvent> &listener) {
277
    _listener = listener;
xiongziliang committed
278
    //拦截事件
279 280 281
    _muxer->setMediaListener(shared_from_this());
}

282 283 284 285
void MultiMediaSourceMuxer::setTrackListener(const std::weak_ptr<MultiMuxerPrivate::Listener> &listener) {
    _track_listener = listener;
}

286 287 288 289 290 291 292 293
int MultiMediaSourceMuxer::totalReaderCount() const {
    return _muxer->totalReaderCount();
}

void MultiMediaSourceMuxer::setTimeStamp(uint32_t stamp) {
    _muxer->setTimeStamp(stamp);
}

294
vector<Track::Ptr> MultiMediaSourceMuxer::getTracks(MediaSource &sender, bool trackReady) const {
295 296 297 298 299 300
    return _muxer->getTracks(trackReady);
}

int MultiMediaSourceMuxer::totalReaderCount(MediaSource &sender) {
    auto listener = _listener.lock();
    if (!listener) {
xiongziliang committed
301
        return totalReaderCount();
302 303 304 305 306
    }
    return listener->totalReaderCount(sender);
}

bool MultiMediaSourceMuxer::setupRecord(MediaSource &sender, Recorder::type type, bool start, const string &custom_path) {
307
    return _muxer->setupRecord(sender, type, start, custom_path);
308 309 310 311 312 313
}

bool MultiMediaSourceMuxer::isRecording(MediaSource &sender, Recorder::type type) {
    return _muxer->isRecording(sender,type);
}

314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
void MultiMediaSourceMuxer::startSendRtp(MediaSource &sender, const string &dst_url, uint16_t dst_port, uint32_t ssrc, bool is_udp, const function<void(const SockException &ex)> &cb){
#if defined(ENABLE_RTPPROXY)
    auto ps_rtp_sender = std::make_shared<PSRtpSender>(ssrc);
    weak_ptr<MultiMediaSourceMuxer> weak_self = shared_from_this();
    ps_rtp_sender->startSend(dst_url, dst_port, is_udp, [weak_self, ps_rtp_sender, cb](const SockException &ex) {
        cb(ex);
        auto strong_self = weak_self.lock();
        if (!strong_self || ex) {
            return;
        }
        for (auto &track : strong_self->_muxer->getTracks(false)) {
            ps_rtp_sender->addTrack(track);
        }
        ps_rtp_sender->addTrackCompleted();
        strong_self->_ps_rtp_sender = ps_rtp_sender;
    });
#else
    cb(SockException(Err_other, "该功能未启用,编译时请打开ENABLE_RTPPROXY宏"));
#endif//ENABLE_RTPPROXY
}

bool MultiMediaSourceMuxer::stopSendRtp(MediaSource &sender){
#if defined(ENABLE_RTPPROXY)
    if (_ps_rtp_sender) {
        _ps_rtp_sender = nullptr;
        return true;
    }
#endif//ENABLE_RTPPROXY
    return false;
}

345 346 347 348 349 350 351 352
void MultiMediaSourceMuxer::addTrack(const Track::Ptr &track) {
    _muxer->addTrack(track);
}

void MultiMediaSourceMuxer::addTrackCompleted() {
    _muxer->addTrackCompleted();
}

353 354
void MultiMediaSourceMuxer::onAllTrackReady(){
    _muxer->setMediaListener(shared_from_this());
xiongziliang committed
355 356 357
    auto listener = _track_listener.lock();
    if(listener){
        listener->onAllTrackReady();
358 359 360
    }
}

361 362 363 364
void MultiMediaSourceMuxer::resetTracks() {
    _muxer->resetTracks();
}

365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
//该类实现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{
        return _dts;
    }

    uint32_t pts() const override{
        return _pts;
    }

    uint32_t prefixSize() const override {
        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();
    }

    uint32_t size() const override {
        return _frame->size();
    }

    CodecId getCodecId() const override {
        return _frame->getCodecId();
    }
private:
    int64_t _dts;
    int64_t _pts;
xiongziliang committed
414
    Frame::Ptr _frame;
415 416
};

417
void MultiMediaSourceMuxer::inputFrame(const Frame::Ptr &frame_in) {
xiongziliang committed
418
    GET_CONFIG(bool, modify_stamp, General::kModifyStamp);
419 420
    auto frame = frame_in;
    if (modify_stamp) {
421
        //开启了时间戳覆盖
422
        frame = std::make_shared<FrameModifyStamp>(frame, _stamp[frame->getTrackType()]);
423
    }
424 425 426 427 428 429 430 431 432
    _muxer->inputFrame(frame);

#if defined(ENABLE_RTPPROXY)
    auto ps_rtp_sender = _ps_rtp_sender;
    if (ps_rtp_sender) {
        ps_rtp_sender->inputFrame(frame);
    }
#endif //ENABLE_RTPPROXY

433 434
}

435
bool MultiMediaSourceMuxer::isEnabled(){
436
#if defined(ENABLE_RTPPROXY)
Zhou Weimin committed
437
    return (_muxer->isEnabled() || _ps_rtp_sender);
438
#else
439
    return _muxer->isEnabled();
440
#endif //ENABLE_RTPPROXY
441 442 443
}


444
}//namespace mediakit