#include "Carousel.h"#include "Json.h"#include "Config.h"#include "ChannelFile.h"Carousel::Carousel(QObject *parent) : QObject(parent){elapsedCarousel = new QElapsedTimer;for(int i=0;i<Config::chns.count();i++){Channel *chn = Config::chns[i];if(chn->type == "mix")mix = chn;}}void Carousel::init(){if(mix == nullptr)return;config = Json::loadFile(CAROUSEL_CONF).toMap();bool autorun = config["autorun"].toBool();if(autorun && mix->enable)start();}bool Carousel::start(){if(hadCarousel)return false;defLays = Json::loadFile(DEFLAYS_CONF).toList();active = config["active"].toString();curMode = config["modes"].toMap()[active].toMap();QStringList sourceKeys;if(active == "step" || active == "multi"){QString layId = curMode["layId"].toString();QVariantMap layMap;for(int i=0;i<defLays.count();i++){if(layId == defLays[i].toMap()["layId"].toString())layMap = defLays[i].toMap();}if(layMap.isEmpty())return false;QVariantList layouts = layMap["layouts"].toList();foreach (QVariant layout, layouts){QVariantMap posMap = layout.toMap()["pos"].toMap();layoutList << posMap;curLayoutChnList << -1;}}for (int i = 0; i <((active=="step"||active=="layout")?1:layoutList.count()); i++)sourceKeys << QString("source%1").arg(i + 1);for(int i=0;i<sourceKeys.count();i++){QString key = sourceKeys[i];sourceMap[key] = addUid(curMode.contains(key) ? curMode[key].toList() : QVariantList());if(!carouselTimerMap.contains(key)){carouselTimerMap[key] = new QTimer(this);connect(carouselTimerMap[key], &QTimer::timeout, this, [this,key]() {carouselTimeout(key);});}if(!elapsedTimerMap.contains(key))elapsedTimerMap[key] = new QElapsedTimer;}QVariantMap audioMap = curMode["audio"].toMap();QString audioType = audioMap["type"].toString();if(audioType == "channel"){bool hadLine = audioMap["line"].toBool();QVariantList mixA = audioMap["mixA"].toList();if(hadLine)mixA.insert(0,"line");this->updateMixA(mixA);}for(int i=0;i<sourceKeys.count();i++){QString key = sourceKeys[i];carouselTimerMap[key]->start(0);}hadCarousel = true;elapsedCarousel->restart();return true;}bool Carousel::stop(){hadCarousel = false;elapsedCarousel->invalidate();sourceMap.clear();curSrcMap.clear();curLayoutChnList.clear();layoutList.clear();for (auto it = carouselTimerMap.constBegin(); it != carouselTimerMap.constEnd(); ++it) {QTimer* timer = it.value();timer->stop();}for (auto it = elapsedTimerMap.constBegin(); it != elapsedTimerMap.constEnd(); ++it) {QElapsedTimer* elapsed = it.value();elapsed->invalidate();}QVariantList source = mix->data["srcV"].toList();QVariantList srcV;for(int i=0;i<source.count();i++){int chnId = source[i].toInt();Channel *chn = Config::findChannelById(chnId);if(chn != NULL)srcV << chn->overlay->name();elsesrcV << "unknow";}mix->video->setData(QVariantMap{{"src",srcV},{"layout",mix->data["layout"]}});this->updateMixA(mix->data["srcA"].toList());return true;}void Carousel::carouselTimeout(const QString &sourceKey){QVariantList source = sourceMap[sourceKey];int step = (active=="multi"||active=="layout")?1:curMode["step"].toInt();int duration = 0;QVariantList srclist = source.mid(0,qMin(active=="step"?layoutList.count():1, source.count()));if(srclist.isEmpty())return;if(active == "step"){QString direct = curMode["direct"].toString();if(layoutList.count() > 1 && step == 1 && direct == "forward")std::reverse(srclist.begin(), srclist.end());}curSrcMap[sourceKey] = srclist;duration = srclist[0].toMap()["duration"].toInt();if(active == "step"){curLayoutChnList.clear();for(int i=0;i<srclist.count();i++)curLayoutChnList << srclist[i].toMap()["chnId"];}if(active == "multi"){int index=-1;for(int i=0;i<layoutList.count();i++){QString curSourceKey = QString("source%1").arg(i + 1);if(curSourceKey == sourceKey){index = i;break;}}if(index == -1)return;curLayoutChnList[index] = srclist[0].toMap()["chnId"];}QVariantList layoutAudioList;if(active == "layout"){layoutList.clear();QString layId = srclist[0].toMap()["layId"].toString();QVariantMap layMap;for(int i=0;i<defLays.count();i++){if(layId == defLays[i].toMap()["layId"].toString())layMap = defLays[i].toMap();}if(layMap.isEmpty())return;QVariantList layouts = layMap["layouts"].toList();QVariantList srcV;foreach (QVariant layout, layouts){QVariantMap posMap = layout.toMap()["pos"].toMap();layoutList << posMap;srcV << layout.toMap()["id"].toInt();if(layout.toMap()["ado"].toBool())layoutAudioList << layout.toMap()["id"].toInt();}bool hadChnId = false;for (const QVariant &chnId : srcV){if (chnId.toInt() >= 0){hadChnId = true;break;}}if(!hadChnId){srcV.clear();for(int i=0;i<Config::chns.count();i++){Channel *chn = Config::chns[i];if(chn->type == "mix")continue;if(chn->enable){if(chn->type == "net" || chn->type == "file"){bool hadDecodeV = false;if(chn->type == "net")hadDecodeV = chn->data["net"].toMap()["decodeV"].toBool();if(chn->type == "file")hadDecodeV = chn->data["decodeV"].toBool();if(hadDecodeV)srcV << chn->id;}elsesrcV << chn->id;}}}curLayoutChnList.clear();for (const QVariant &chnId : srcV)curLayoutChnList << chnId.toInt();if (curLayoutChnList.count() > layoutList.count())curLayoutChnList = curLayoutChnList.mid(0, layoutList.count());while (curLayoutChnList.count() < layoutList.count())curLayoutChnList.append(-1);}QVariantList srcVList;for(int i=0;i<curLayoutChnList.count();i++){bool isInt = false;int chnId = curLayoutChnList[i].toInt(&isInt);if(!isInt){foreach (Channel *chn, Config::chns){if(chn->type != "file")continue;chnId = chn->id;}}Channel *chn = Config::findChannelById(chnId);if(chn != NULL){if(chn->type == "file"){if(active == "step" || (active == "multi" && sourceKey == QString("source%1").arg(i+1))){ChannelFile *chnFile = (ChannelFile*)chn;chnFile->carouselPlay(curLayoutChnList[i].toString());}if(active == "layout"){ChannelFile *chnFile = (ChannelFile*)chn;chnFile->inputFile->stop(true);chnFile->play(0,0);}}srcVList << chn->overlay->name();chn->overlay->linkV(mix->video);}elsesrcVList << "unknow";}mix->video->setData(QVariantMap{{"src",srcVList},{"layout",layoutList}});QVariantList srcAList;QVariantMap audioMap = curMode["audio"].toMap();QString audioType = audioMap["type"].toString();bool hadLine = audioMap["line"].toBool();QVariantList mixA = audioMap["mixA"].toList();if(audioType == "position" || audioType == "layout"){if(audioType == "position"){for(int j=0;j<mixA.count();j++){int pos = mixA[j].toInt();if(curLayoutChnList.count() > pos)srcAList << curLayoutChnList[pos].toInt();}}elsesrcAList = layoutAudioList;if(hadLine)srcAList.insert(0,"line");this->updateMixA(srcAList);}sourceMap[sourceKey] = rotateArray(source,step);carouselTimerMap[sourceKey]->start(duration * 1000);elapsedTimerMap[sourceKey]->restart();}void Carousel::updateMixA(const QVariantList &list){if(Channel::lineGain)Channel::lineGain->unLinkA(mix->audio);for(int i=0;i<Config::chns.count();i++){Channel *chn = Config::chns[i];if(chn->gain)chn->gain->unLinkA(mix->audio);}QVariantMap dataMixA;dataMixA["bufLen"]=20;for(int i=0;i<list.count();i++){if(list[i].toString() == "-1")continue;if(list[i] != "line"){Channel *chn=Config::findChannelById(list[i].toInt());if(chn->audio==NULL )continue;LinkObject *a=chn->gain;if(!dataMixA.contains("main"))dataMixA["main"]=a->name();a->linkA(mix->audio);}else{if(Channel::lineGain!=NULL){Channel::lineGain->linkA(mix->audio);dataMixA["main"]=Channel::lineGain->name();}}}mix->audio->setData(dataMixA);}QVariantMap Carousel::getState(){QVariantMap stateMap,allSrcMap;stateMap["activeMode"] = active;stateMap["hadCarousel"] = hadCarousel;stateMap["duration"] = hadCarousel ? (elapsedCarousel->elapsed()/1000)+1 : 0;for (auto it = curSrcMap.constBegin(); it != curSrcMap.constEnd(); ++it) {QString sourceKey = it.key();QVariantList sourcelist = sourceMap[sourceKey];QVariantList curlist = curSrcMap[sourceKey];for(int j=0;j<sourcelist.count();j++){QVariantMap srcMap = sourcelist[j].toMap();QString srcUid = srcMap["uid"].toString();srcMap["playTime"] = 0;if(curlist.count() > 0){if(srcUid == curlist[0].toMap()["uid"].toString())srcMap["playTime"] = hadCarousel ? (elapsedTimerMap[sourceKey]->elapsed()/1000 + 1) : 0;}sourcelist[j] = srcMap;}allSrcMap[sourceKey] = sourcelist;}stateMap["source"] = allSrcMap;return stateMap;}bool Carousel::update(const QVariantMap &cfg){if(hadCarousel)return false;config = cfg;Json::saveFile(cfg,CAROUSEL_CONF);return true;}QVariantList Carousel::rotateArray(const QVariantList &arr, int step){if (arr.isEmpty() || step <= 0 || step > arr.count())return arr;QVariantList result = arr;int count = result.count();QVariantList rotated;rotated.reserve(count);rotated << result.mid(step) << result.mid(0, step);return rotated;}QVariantList Carousel::addUid(const QVariantList &list){QVariantList out;out.reserve(list.size());for (int i = 0; i < list.size(); ++i) {QVariantMap src = list[i].toMap();src["uid"] = QString("uid_%1").arg(i);out << src;}return out;}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。