MediaSink.cpp 3.99 KB
Newer Older
1 2 3
/*
 * MIT License
 *
xiongziliang committed
4
 * Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#include "MediaSink.h"

28 29
//最多等待未初始化的Track 10秒,超时之后会忽略未初始化的Track
#define MAX_WAIT_MS 10000
30 31 32 33

namespace mediakit{

void MediaSink::addTrack(const Track::Ptr &track_in) {
34
    lock_guard<recursive_mutex> lck(_mtx);
35
    //克隆Track,只拷贝其数据,不拷贝其数据转发关系
36 37 38 39 40 41 42 43 44 45
    auto track = track_in->clone();

    auto codec_id = track->getCodecId();
    _track_map[codec_id] = track;
    auto lam = [this,track](){
        onTrackReady(track);
    };
    if(track->ready()){
        lam();
    }else{
46
        _anyTrackUnReady = true;
47 48
        _allTrackReady = false;
        _trackReadyCallback[codec_id] = lam;
49
        _ticker.resetTime();
50
    }
xiongziliang committed
51

xiongziliang committed
52 53 54
    track->addDelegate(std::make_shared<FrameWriterInterfaceHelper>([this](const Frame::Ptr &frame){
        if(!_anyTrackUnReady){
            onTrackFrame(frame);
xiongziliang committed
55 56
        }
    }));
57 58
}

59
void MediaSink::resetTracks() {
xiongziliang committed
60 61
    lock_guard<recursive_mutex> lck(_mtx);
    _anyTrackUnReady = false;
62 63 64 65 66 67
    _allTrackReady = false;
    _track_map.clear();
    _trackReadyCallback.clear();
    _ticker.resetTime();
}

68
void MediaSink::inputFrame(const Frame::Ptr &frame) {
69
    lock_guard<recursive_mutex> lck(_mtx);
70 71 72 73 74 75
    auto codec_id = frame->getCodecId();
    auto it = _track_map.find(codec_id);
    if (it == _track_map.end()) {
        return;
    }
    it->second->inputFrame(frame);
76

77
    if(!_allTrackReady && !_trackReadyCallback.empty() && it->second->ready()){
3503207480@qq.com committed
78
        //Track由未就绪状态转换成就绪状态,我们就触发onTrackReady回调
79 80 81 82 83 84 85
        auto it_callback = _trackReadyCallback.find(codec_id);
        if(it_callback != _trackReadyCallback.end()){
            it_callback->second();
            _trackReadyCallback.erase(it_callback);
        }
    }

86
    if(!_allTrackReady && (_trackReadyCallback.empty() || _ticker.elapsedTime() > MAX_WAIT_MS)){
87
        _allTrackReady = true;
88
        _anyTrackUnReady = false;
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
        if(!_trackReadyCallback.empty()){
            //这是超时强制忽略未准备好的Track
            _trackReadyCallback.clear();
            //移除未准备好的Track
            for(auto it = _track_map.begin() ; it != _track_map.end() ; ){
                if(!it->second->ready()){
                    it = _track_map.erase(it);
                    continue;
                }
                ++it;
            }
        }

        if(!_track_map.empty()){
            //最少有一个有效的Track
            onAllTrackReady();
        }
106 107 108
    }
}

xiongziliang committed
109 110
vector<Track::Ptr> MediaSink::getTracks(bool trackReady) const{
    vector<Track::Ptr> ret;
111
    lock_guard<recursive_mutex> lck(_mtx);
112
    for (auto &pr : _track_map){
xiongziliang committed
113 114
        if(trackReady && !pr.second->ready()){
            continue;
115
        }
xiongziliang committed
116
        ret.emplace_back(pr.second);
117
    }
xiongziliang committed
118
    return std::move(ret);
119 120
}

121 122

}//namespace mediakit