2018-10-27 22:40:44 +08:00
|
|
|
|
/*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
2018-10-27 22:40:44 +08:00
|
|
|
|
*
|
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
|
|
|
|
*
|
2020-04-04 20:30:09 +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.
|
2018-10-27 22:40:44 +08:00
|
|
|
|
*/
|
2020-04-04 20:30:09 +08:00
|
|
|
|
|
2018-10-27 22:40:44 +08:00
|
|
|
|
#include "MediaSink.h"
|
|
|
|
|
|
2018-11-17 15:25:33 +08:00
|
|
|
|
//最多等待未初始化的Track 10秒,超时之后会忽略未初始化的Track
|
2020-01-13 15:48:55 +08:00
|
|
|
|
#define MAX_WAIT_MS_READY 10000
|
|
|
|
|
|
|
|
|
|
//如果添加Track,最多等待3秒
|
|
|
|
|
#define MAX_WAIT_MS_ADD_TRACK 3000
|
|
|
|
|
|
2018-10-27 22:40:44 +08:00
|
|
|
|
|
|
|
|
|
namespace mediakit{
|
|
|
|
|
|
|
|
|
|
void MediaSink::addTrack(const Track::Ptr &track_in) {
|
2018-10-28 01:04:18 +08:00
|
|
|
|
lock_guard<recursive_mutex> lck(_mtx);
|
2019-10-11 16:51:10 +08:00
|
|
|
|
//克隆Track,只拷贝其数据,不拷贝其数据转发关系
|
2018-10-27 22:40:44 +08:00
|
|
|
|
auto track = track_in->clone();
|
|
|
|
|
auto codec_id = track->getCodecId();
|
|
|
|
|
_track_map[codec_id] = track;
|
2020-04-26 15:19:32 +08:00
|
|
|
|
_all_track_ready = false;
|
|
|
|
|
_track_ready_callback[codec_id] = [this, track]() {
|
2018-10-27 22:40:44 +08:00
|
|
|
|
onTrackReady(track);
|
|
|
|
|
};
|
2020-01-13 15:48:55 +08:00
|
|
|
|
_ticker.resetTime();
|
2019-10-12 10:29:40 +08:00
|
|
|
|
|
2020-01-13 15:48:55 +08:00
|
|
|
|
track->addDelegate(std::make_shared<FrameWriterInterfaceHelper>([this](const Frame::Ptr &frame) {
|
2020-04-26 15:19:32 +08:00
|
|
|
|
if (_all_track_ready) {
|
2019-12-03 13:05:23 +08:00
|
|
|
|
onTrackFrame(frame);
|
2020-04-26 15:19:32 +08:00
|
|
|
|
} else {
|
|
|
|
|
//还有Track未就绪,先缓存之
|
|
|
|
|
_frame_unread[frame->getCodecId()].emplace_back(Frame::getCacheAbleFrame(frame));
|
2020-04-17 17:47:10 +08:00
|
|
|
|
}
|
2019-10-12 10:29:40 +08:00
|
|
|
|
}));
|
2018-10-27 22:40:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-11 16:51:10 +08:00
|
|
|
|
void MediaSink::resetTracks() {
|
2019-10-11 17:51:46 +08:00
|
|
|
|
lock_guard<recursive_mutex> lck(_mtx);
|
2020-04-26 15:19:32 +08:00
|
|
|
|
_all_track_ready = false;
|
2019-10-11 16:51:10 +08:00
|
|
|
|
_track_map.clear();
|
2020-04-26 15:19:32 +08:00
|
|
|
|
_track_ready_callback.clear();
|
2019-10-11 16:51:10 +08:00
|
|
|
|
_ticker.resetTime();
|
2020-02-25 14:59:40 +08:00
|
|
|
|
_max_track_size = 2;
|
2020-04-26 15:19:32 +08:00
|
|
|
|
_frame_unread.clear();
|
2019-10-11 16:51:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-27 22:40:44 +08:00
|
|
|
|
void MediaSink::inputFrame(const Frame::Ptr &frame) {
|
2018-10-28 01:04:18 +08:00
|
|
|
|
lock_guard<recursive_mutex> lck(_mtx);
|
2020-02-27 12:31:57 +08:00
|
|
|
|
auto it = _track_map.find(frame->getCodecId());
|
2018-10-27 22:40:44 +08:00
|
|
|
|
if (it == _track_map.end()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-04-17 15:19:42 +08:00
|
|
|
|
it->second->inputFrame(frame);
|
2020-04-26 15:19:32 +08:00
|
|
|
|
checkTrackIfReady(nullptr);
|
2020-02-27 12:31:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MediaSink::checkTrackIfReady_l(const Track::Ptr &track){
|
|
|
|
|
//Track由未就绪状态转换成就绪状态,我们就触发onTrackReady回调
|
2020-04-26 15:19:32 +08:00
|
|
|
|
auto it_callback = _track_ready_callback.find(track->getCodecId());
|
|
|
|
|
if (it_callback != _track_ready_callback.end() && track->ready()) {
|
2020-02-27 12:31:57 +08:00
|
|
|
|
it_callback->second();
|
2020-04-26 15:19:32 +08:00
|
|
|
|
_track_ready_callback.erase(it_callback);
|
2020-02-27 12:31:57 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-17 15:25:33 +08:00
|
|
|
|
|
2020-02-27 12:31:57 +08:00
|
|
|
|
void MediaSink::checkTrackIfReady(const Track::Ptr &track){
|
2020-04-26 15:19:32 +08:00
|
|
|
|
if (!_all_track_ready && !_track_ready_callback.empty()) {
|
2020-02-27 12:31:57 +08:00
|
|
|
|
if (track) {
|
|
|
|
|
checkTrackIfReady_l(track);
|
|
|
|
|
} else {
|
|
|
|
|
for (auto &pr : _track_map) {
|
|
|
|
|
checkTrackIfReady_l(pr.second);
|
|
|
|
|
}
|
2018-10-27 22:40:44 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-26 15:19:32 +08:00
|
|
|
|
if(!_all_track_ready){
|
2020-01-13 15:48:55 +08:00
|
|
|
|
if(_ticker.elapsedTime() > MAX_WAIT_MS_READY){
|
|
|
|
|
//如果超过规定时间,那么不再等待并忽略未准备好的Track
|
2020-02-25 14:59:40 +08:00
|
|
|
|
emitAllTrackReady();
|
2020-01-13 15:48:55 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-26 15:19:32 +08:00
|
|
|
|
if(!_track_ready_callback.empty()){
|
2020-01-13 15:48:55 +08:00
|
|
|
|
//在超时时间内,如果存在未准备好的Track,那么继续等待
|
|
|
|
|
return;
|
2018-11-17 15:25:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-02-25 14:59:40 +08:00
|
|
|
|
if(_track_map.size() == _max_track_size){
|
2020-01-13 15:48:55 +08:00
|
|
|
|
//如果已经添加了音视频Track,并且不存在未准备好的Track,那么说明所有Track都准备好了
|
2020-02-25 14:59:40 +08:00
|
|
|
|
emitAllTrackReady();
|
2020-01-13 15:48:55 +08:00
|
|
|
|
return;
|
2018-11-17 15:25:33 +08:00
|
|
|
|
}
|
2020-01-13 15:48:55 +08:00
|
|
|
|
|
|
|
|
|
if(_track_map.size() == 1 && _ticker.elapsedTime() > MAX_WAIT_MS_ADD_TRACK){
|
|
|
|
|
//如果只有一个Track,那么在该Track添加后,我们最多还等待若干时间(可能后面还会添加Track)
|
2020-02-25 14:59:40 +08:00
|
|
|
|
emitAllTrackReady();
|
2020-01-13 15:48:55 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-25 14:59:40 +08:00
|
|
|
|
void MediaSink::addTrackCompleted(){
|
2020-04-26 15:19:32 +08:00
|
|
|
|
lock_guard<recursive_mutex> lck(_mtx);
|
|
|
|
|
_max_track_size = _track_map.size();
|
2020-02-27 12:31:57 +08:00
|
|
|
|
checkTrackIfReady(nullptr);
|
2020-02-25 14:59:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MediaSink::emitAllTrackReady() {
|
2020-04-26 15:19:32 +08:00
|
|
|
|
if (_all_track_ready) {
|
2020-01-22 19:09:53 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-17 17:47:10 +08:00
|
|
|
|
DebugL << "all track ready use " << _ticker.elapsedTime() << "ms";
|
2020-04-26 15:19:32 +08:00
|
|
|
|
if (!_track_ready_callback.empty()) {
|
2020-01-13 15:48:55 +08:00
|
|
|
|
//这是超时强制忽略未准备好的Track
|
2020-04-26 15:19:32 +08:00
|
|
|
|
_track_ready_callback.clear();
|
2020-01-13 15:48:55 +08:00
|
|
|
|
//移除未准备好的Track
|
2020-01-22 19:09:53 +08:00
|
|
|
|
for (auto it = _track_map.begin(); it != _track_map.end();) {
|
|
|
|
|
if (!it->second->ready()) {
|
2020-04-18 18:46:20 +08:00
|
|
|
|
WarnL << "track not ready for a long time, ignored: " << it->second->getCodecName();
|
2020-01-13 15:48:55 +08:00
|
|
|
|
it = _track_map.erase(it);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
++it;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-22 19:09:53 +08:00
|
|
|
|
if (!_track_map.empty()) {
|
2020-01-13 15:48:55 +08:00
|
|
|
|
//最少有一个有效的Track
|
2020-04-26 15:19:32 +08:00
|
|
|
|
_all_track_ready = true;
|
2020-01-13 15:48:55 +08:00
|
|
|
|
onAllTrackReady();
|
2020-04-26 15:19:32 +08:00
|
|
|
|
|
|
|
|
|
//全部Track就绪,我们一次性把之前的帧输出
|
|
|
|
|
for(auto &pr : _frame_unread){
|
|
|
|
|
if (_track_map.find(pr.first) == _track_map.end()) {
|
|
|
|
|
//该Track已经被移除
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
pr.second.for_each([&](const Frame::Ptr &frame) {
|
|
|
|
|
onTrackFrame(frame);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
_frame_unread.clear();
|
2018-10-27 22:40:44 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-03 12:32:57 +08:00
|
|
|
|
vector<Track::Ptr> MediaSink::getTracks(bool trackReady) const{
|
|
|
|
|
vector<Track::Ptr> ret;
|
2018-10-28 01:04:18 +08:00
|
|
|
|
lock_guard<recursive_mutex> lck(_mtx);
|
2018-10-28 00:15:27 +08:00
|
|
|
|
for (auto &pr : _track_map){
|
2019-12-03 12:32:57 +08:00
|
|
|
|
if(trackReady && !pr.second->ready()){
|
|
|
|
|
continue;
|
2018-10-28 00:15:27 +08:00
|
|
|
|
}
|
2019-12-03 12:32:57 +08:00
|
|
|
|
ret.emplace_back(pr.second);
|
2018-10-28 00:15:27 +08:00
|
|
|
|
}
|
2019-12-03 12:32:57 +08:00
|
|
|
|
return std::move(ret);
|
2018-10-28 00:15:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-27 22:40:44 +08:00
|
|
|
|
|
|
|
|
|
}//namespace mediakit
|