RtmpMuxer.cpp 1.73 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2 3 4 5 6 7 8 9
 * 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.
 */
xiongziliang committed
10 11

#include "RtmpMuxer.h"
xiongziliang committed
12
#include "Extension/Factory.h"
xiongziliang committed
13 14 15

namespace mediakit {

16
RtmpMuxer::RtmpMuxer(const TitleMeta::Ptr &title) {
17
    if(!title){
18
        _metadata = std::make_shared<TitleMeta>()->getMetadata();
19
    }else{
20
        _metadata = title->getMetadata();
21
    }
xiongziliang committed
22
    _rtmp_ring = std::make_shared<RtmpRing::RingType>();
23
}
xiongziliang committed
24

xiongziliang committed
25 26 27
void RtmpMuxer::addTrack(const Track::Ptr &track) {
    auto &encoder = _encoder[track->getTrackType()];
    //生成rtmp编码器,克隆该Track,防止循环引用
28
    encoder = Factory::getRtmpCodecByTrack(track->clone(), true);
xiongziliang committed
29 30 31 32 33
    if (!encoder) {
        return;
    }

    //设置rtmp输出环形缓存
xiongziliang committed
34
    encoder->setRtmpRing(_rtmp_ring);
xiongziliang committed
35

36 37
    //添加metadata
    Metadata::addTrack(_metadata,track);
xiongziliang committed
38 39
}

xiongziliang committed
40 41 42 43 44 45
void RtmpMuxer::inputFrame(const Frame::Ptr &frame) {
    auto &encoder = _encoder[frame->getTrackType()];
    if(encoder){
        encoder->inputFrame(frame);
    }
}
xiongziliang committed
46

47 48 49 50 51 52 53 54
void RtmpMuxer::makeConfigPacket(){
    for(auto &encoder : _encoder){
        if(encoder){
            encoder->makeConfigPacket();
        }
    }
}

55 56
const AMFValue &RtmpMuxer::getMetadata() const {
    return _metadata;
xiongziliang committed
57 58
}

xiongziliang committed
59
RtmpRing::RingType::Ptr RtmpMuxer::getRtmpRing() const {
xiongziliang committed
60
    return _rtmp_ring;
xiongziliang committed
61 62
}

xiongziliang committed
63 64 65 66 67 68 69 70
void RtmpMuxer::resetTracks() {
    _metadata.clear();
    for(auto &encoder : _encoder){
        encoder = nullptr;
    }
}


71
}/* namespace mediakit */