Debian Codegraph @

xbmc/cores/playercorefactory/PlayerSelectionRule.cpp

raw
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9 #include "PlayerSelectionRule.h"
10
11 #include "FileItem.h"
12 #include "ServiceBroker.h"
13 #include "URL.h"
14 #include "settings/Settings.h"
15 #include "settings/SettingsComponent.h"
16 #include "utils/RegExp.h"
17 #include "utils/StreamDetails.h"
18 #include "utils/StringUtils.h"
19 #include "utils/XBMCTinyXML.h"
20 #include "utils/XMLUtils.h"
21 #include "utils/log.h"
22 #include "video/VideoInfoTag.h"
23
24 #include <algorithm>
25
26 CPlayerSelectionRule::CPlayerSelectionRule(TiXmlElement* pRule)
27 {
28 Initialize(pRule);
29 }
30
31 void CPlayerSelectionRule::Initialize(TiXmlElement* pRule)
32 {
33 m_name = XMLUtils::GetAttribute(pRule, "name");
34 if (m_name.empty())
35 m_name = "un-named";
36
37 CLog::Log(LOGDEBUG, "CPlayerSelectionRule::Initialize: creating rule: {}", m_name);
38
39 m_tInternetStream = GetTristate(pRule->Attribute("internetstream"));
40 m_tRemote = GetTristate(pRule->Attribute("remote"));
41 m_tAudio = GetTristate(pRule->Attribute("audio"));
42 m_tVideo = GetTristate(pRule->Attribute("video"));
43 m_tGame = GetTristate(pRule->Attribute("game"));
44
45 m_tBD = GetTristate(pRule->Attribute("bd"));
46 m_tDVD = GetTristate(pRule->Attribute("dvd"));
47 m_tDVDFile = GetTristate(pRule->Attribute("dvdfile"));
48 m_tDiscImage = GetTristate(pRule->Attribute("discimage"));
49 if (m_tDiscImage < 0)
50 {
51 m_tDiscImage = GetTristate(pRule->Attribute("dvdimage"));
52 if (m_tDiscImage >= 0)
53 CLog::Log(LOGWARNING, "\"dvdimage\" tag is deprecated. use \"discimage\"");
54 }
55
56 m_protocols = XMLUtils::GetAttribute(pRule, "protocols");
57 m_fileTypes = XMLUtils::GetAttribute(pRule, "filetypes");
58 m_mimeTypes = XMLUtils::GetAttribute(pRule, "mimetypes");
59 m_fileName = XMLUtils::GetAttribute(pRule, "filename");
60
61 m_audioCodec = XMLUtils::GetAttribute(pRule, "audiocodec");
62 m_audioChannels = XMLUtils::GetAttribute(pRule, "audiochannels");
63 m_videoCodec = XMLUtils::GetAttribute(pRule, "videocodec");
64 m_videoResolution = XMLUtils::GetAttribute(pRule, "videoresolution");
65 m_videoAspect = XMLUtils::GetAttribute(pRule, "videoaspect");
66
67 m_bStreamDetails = m_audioCodec.length() > 0 || m_audioChannels.length() > 0 ||
68 m_videoCodec.length() > 0 || m_videoResolution.length() > 0 || m_videoAspect.length() > 0;
69
70 if (m_bStreamDetails && !CServiceBroker::GetSettingsComponent()->GetSettings()->GetBool(CSettings::SETTING_MYVIDEOS_EXTRACTFLAGS))
71 {
72 CLog::Log(LOGWARNING,
73 "CPlayerSelectionRule::Initialize: rule: {} needs media flagging, which is disabled",
74 m_name);
75 }
76
77 m_playerName = XMLUtils::GetAttribute(pRule, "player");
78
79 TiXmlElement* pSubRule = pRule->FirstChildElement("rule");
80 while (pSubRule)
81 {
82 vecSubRules.emplace_back(std::make_unique<CPlayerSelectionRule>(pSubRule));
83 pSubRule = pSubRule->NextSiblingElement("rule");
84 }
85 }
86
87 int CPlayerSelectionRule::GetTristate(const char* szValue)
88 {
89 if (szValue)
90 {
91 if (StringUtils::CompareNoCase(szValue, "true") == 0)
92 return 1;
93 if (StringUtils::CompareNoCase(szValue, "false") == 0)
94 return 0;
95 }
96 return -1;
97 }
98
99 bool CPlayerSelectionRule::CompileRegExp(const std::string& str, CRegExp& regExp)
100 {
101 return !str.empty() && regExp.RegComp(str.c_str());
102 }
103
104 bool CPlayerSelectionRule::MatchesRegExp(const std::string& str, CRegExp& regExp)
105 {
106 return regExp.RegFind(str, 0) == 0;
107 }
108
109 void CPlayerSelectionRule::GetPlayers(const CFileItem& item, std::vector<std::string>&validPlayers, std::vector<std::string>&players)
110 {
111 CLog::Log(LOGDEBUG, "CPlayerSelectionRule::GetPlayers: considering rule: {}", m_name);
112
113 if (m_bStreamDetails && !item.HasVideoInfoTag())
114 return;
115 if (m_tAudio >= 0 && (m_tAudio > 0) != item.IsAudio())
116 return;
117 if (m_tVideo >= 0 && (m_tVideo > 0) != item.IsVideo())
118 return;
119 if (m_tGame >= 0 && (m_tGame > 0) != item.IsGame())
120 return;
121 if (m_tInternetStream >= 0 && (m_tInternetStream > 0) != item.IsInternetStream())
122 return;
123 if (m_tRemote >= 0 && (m_tRemote > 0) != item.IsRemote())
124 return;
125
126 if (m_tBD >= 0 && (m_tBD > 0) != (item.IsBDFile() && item.IsOnDVD()))
127 return;
128 if (m_tDVD >= 0 && (m_tDVD > 0) != item.IsDVD())
129 return;
130 if (m_tDVDFile >= 0 && (m_tDVDFile > 0) != item.IsDVDFile())
131 return;
132 if (m_tDiscImage >= 0 && (m_tDiscImage > 0) != item.IsDiscImage())
133 return;
134
135 CRegExp regExp(false, CRegExp::autoUtf8);
136
137 if (m_bStreamDetails)
138 {
139 if (!item.GetVideoInfoTag()->HasStreamDetails())
140 {
141 CLog::Log(LOGDEBUG,
142 "CPlayerSelectionRule::GetPlayers: cannot check rule: {}, no StreamDetails",
143 m_name);
144 return;
145 }
146
147 CStreamDetails streamDetails = item.GetVideoInfoTag()->m_streamDetails;
148
149 if (CompileRegExp(m_audioCodec, regExp) && !MatchesRegExp(streamDetails.GetAudioCodec(), regExp))
150 return;
151
152 std::stringstream itoa;
153 itoa << streamDetails.GetAudioChannels();
154 std::string audioChannelsstr = itoa.str();
155
156 if (CompileRegExp(m_audioChannels, regExp) && !MatchesRegExp(audioChannelsstr, regExp))
157 return;
158
159 if (CompileRegExp(m_videoCodec, regExp) && !MatchesRegExp(streamDetails.GetVideoCodec(), regExp))
160 return;
161
162 if (CompileRegExp(m_videoResolution, regExp) &&
163 !MatchesRegExp(CStreamDetails::VideoDimsToResolutionDescription(streamDetails.GetVideoWidth(), streamDetails.GetVideoHeight()), regExp))
164 return;
165
166 if (CompileRegExp(m_videoAspect, regExp) &&
167 !MatchesRegExp(CStreamDetails::VideoAspectToAspectDescription(streamDetails.GetVideoAspect()), regExp))
168 return;
169 }
170
171 CURL url(item.GetDynPath());
172
173 if (CompileRegExp(m_fileTypes, regExp) && !MatchesRegExp(url.GetFileType(), regExp))
174 return;
175
176 if (CompileRegExp(m_protocols, regExp) && !MatchesRegExp(url.GetProtocol(), regExp))
177 return;
178
179 if (CompileRegExp(m_mimeTypes, regExp) && !MatchesRegExp(item.GetMimeType(), regExp))
180 return;
181
182 if (CompileRegExp(m_fileName, regExp) && !MatchesRegExp(item.GetDynPath(), regExp))
183 return;
184
185 CLog::Log(LOGDEBUG, "CPlayerSelectionRule::GetPlayers: matches rule: {}", m_name);
186
187 for (const auto& rule : vecSubRules)
188 rule->GetPlayers(item, validPlayers, players);
189
190 if (std::find(validPlayers.begin(), validPlayers.end(), m_playerName) != validPlayers.end())
191 {
192 CLog::Log(LOGDEBUG, "CPlayerSelectionRule::GetPlayers: adding player: {} for rule: {}",
193 m_playerName, m_name);
194 players.push_back(m_playerName);
195 }
196 }
197
198