MediaManage.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. #include "stdafx.h"
  2. #include "mediaManage.h"
  3. #include <set>
  4. #include <algorithm>
  5. #include "../Event.h"
  6. mediaManage::mediaManage()
  7. {
  8. m_curParse.clear();
  9. if (ExistsDirA(DEFAULT_RESOURSE_PATH))
  10. {
  11. setDefaultAddvertPath(string(DEFAULT_RESOURSE_PATH));
  12. setDefaultDownloadPath(string(DEFAULT_DOWNLOAD_PATH));
  13. }
  14. else
  15. {
  16. setDefaultAddvertPath(string(DEFAULT_RESOURSE_PATH_C));
  17. setDefaultDownloadPath(string(DEFAULT_DOWNLOAD_PATH_C));
  18. }
  19. m_ilist_size = 0;
  20. }
  21. void mediaManage::setDefaultAddvertPath(string addvertPath){ m_addvertPath = addvertPath; }
  22. void mediaManage::setDefaultDownloadPath(string downloadPath){ m_downloadPath = downloadPath; }
  23. void mediaManage::GetPlayList(vector<ResourceParse> &resourceList)
  24. {
  25. resourceList.clear();
  26. for (vector<ParseInfo>::iterator i = m_curParse.begin(); i != m_curParse.end(); i++)
  27. {
  28. for (vector<ResourceParse>::iterator j = i->m_mediaList.begin(); j != i->m_mediaList.end(); j++)
  29. {
  30. if (checkInVaildTime(j->vaildTime, true) && checkInPlayTime(j->playTime, true))
  31. resourceList.push_back(*j);
  32. }
  33. }
  34. }
  35. int mediaManage::GetPlayListByLocal(vector<ResourceParse>& resourceList)
  36. {
  37. int iNum = 0;
  38. resourceList.clear();
  39. for (vector<ResourceParse>::iterator it = m_localList.begin(); it != m_localList.end(); it++)
  40. {
  41. if (checkInVaildTime(it->vaildTime, true) && checkInPlayTime(it->playTime, true)) {
  42. resourceList.push_back(*it);
  43. iNum++;
  44. }
  45. }
  46. return iNum;
  47. }
  48. int mediaManage::GetPlayListByLocal(CResourceParse** ResourceArr, int iSize)
  49. {
  50. int iNum = 0;
  51. for (vector<ResourceParse>::iterator it = m_localList.begin(); it != m_localList.end() && iNum < iSize; it++)
  52. {
  53. if (checkInVaildTime(it->vaildTime, true) && checkInPlayTime(it->playTime, true)) {
  54. CResourceParse* pNode = new CResourceParse();
  55. pNode->type = it->type;
  56. pNode->fullScreen = it->fullScreen;
  57. pNode->primMonitor = it->primMonitor;
  58. pNode->simpleMode = it->simpleMode;
  59. pNode->playInterval = it->playInterval;
  60. pNode->priority = it->priority;
  61. if (it->videoNames.length() > 0) {
  62. pNode->pvideoNames = new char[it->videoNames.length() + 1];
  63. memset(pNode->pvideoNames, 0, it->videoNames.length() + 1);
  64. memcpy(pNode->pvideoNames, it->videoNames.c_str(), it->videoNames.length());
  65. }
  66. if (it->vaildTime.length() > 0) {
  67. pNode->pvaildTime = new char[it->vaildTime.length() + 1];
  68. memset(pNode->pvaildTime, 0, it->vaildTime.length() + 1);
  69. memcpy(pNode->pvaildTime, it->vaildTime.c_str(), it->vaildTime.length());
  70. }
  71. if (it->resourcePath.length() > 0) {
  72. pNode->presourcePath = new char[it->resourcePath.length() + 1];
  73. memset(pNode->presourcePath, 0, it->resourcePath.length() + 1);
  74. memcpy(pNode->presourcePath, it->resourcePath.c_str(), it->resourcePath.length());
  75. }
  76. if (it->playTime.length() > 0) {
  77. pNode->playTime = new char[it->playTime.length() + 1];
  78. memset(pNode->playTime, 0, it->playTime.length() + 1);
  79. memcpy(pNode->playTime, it->playTime.c_str(), it->playTime.length());
  80. }
  81. ResourceArr[iNum++] = pNode;
  82. }
  83. }
  84. return iNum;
  85. }
  86. int mediaManage::GetPlayListByLocal(rvcResourceParse_t* ResourceArr, int iSize)
  87. {
  88. int iNum = 0;
  89. for (vector<ResourceParse>::iterator it = m_localList.begin(); it != m_localList.end() && iNum < iSize; it++)
  90. {
  91. if (checkInVaildTime(it->vaildTime, true) && checkInPlayTime(it->playTime, true) && IsResourceExist(it->resourcePath, it->videoNames)) {
  92. rvcResourceParse_t* pNode = ResourceArr+iNum;
  93. pNode->type = it->type;
  94. pNode->fullScreen = it->fullScreen;
  95. pNode->primMonitor = it->primMonitor;
  96. pNode->simpleMode = it->simpleMode;
  97. pNode->playInterval = it->playInterval;
  98. pNode->priority = it->priority;
  99. if (it->videoNames.length() > 0) {
  100. memcpy(pNode->strvideoNames, it->videoNames.c_str(), it->videoNames.length());
  101. }
  102. if (it->vaildTime.length() > 0) {
  103. memcpy(pNode->strvaildTime, it->vaildTime.c_str(), it->vaildTime.length());
  104. }
  105. if (it->resourcePath.length() > 0) {
  106. memcpy(pNode->strResourcePath, it->resourcePath.c_str(), it->resourcePath.length());
  107. }
  108. if (it->playTime.length() > 0) {
  109. memcpy(pNode->strplayTime, it->playTime.c_str(), it->playTime.length());
  110. }
  111. iNum++;
  112. }
  113. }
  114. return iNum;
  115. }
  116. bool mediaManage::AddResourceList(vector<string> resourceList)
  117. {
  118. if (0 == resourceList.size())
  119. return true;
  120. for (vector<string>::iterator i = resourceList.begin(); i != resourceList.end(); i++)
  121. {
  122. int pos = i->find(".zip");
  123. if (-1 == pos)
  124. continue;
  125. string dirName = i->substr(0, pos);
  126. string fileName = m_downloadPath + SPLIT_SLASH_STR + *i;
  127. string dirPath = m_addvertPath + SPLIT_SLASH_STR + dirName;
  128. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("dirName:%s, fileName:%s, dirPath:%s", dirName.c_str(), fileName.c_str(), dirPath.c_str());
  129. wstring wfileName, wdirPath;
  130. StringToWstring(wfileName, fileName);
  131. StringToWstring(wdirPath, dirPath);
  132. if (!ExistsFileA(dirPath.c_str()))
  133. {
  134. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("dir %s not exist!", dirPath.c_str());
  135. continue;
  136. }
  137. string configPath = "";
  138. if (!findVaildConfig(dirPath, configPath))
  139. {
  140. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("can not find config file in %s//%s", dirPath.c_str(), configPath.c_str());
  141. continue;
  142. }
  143. vector<ResourceParse> mediaList;
  144. if (!parseResourceIni(configPath.c_str(), mediaList))
  145. {
  146. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("parse config %s fail!", configPath.c_str());
  147. continue;
  148. }
  149. string resourcePath = dirPath + SPLIT_SLASH_STR;
  150. for (vector<ResourceParse>::iterator j = mediaList.begin(); j != mediaList.end(); j++)
  151. j->resourcePath = resourcePath;
  152. ParseInfo resourseInfo(*i, mediaList);
  153. m_curParse.push_back(resourseInfo);//modify resourse list
  154. }
  155. return true;
  156. }
  157. void mediaManage::InitResourceListByLocal()
  158. {
  159. string headFile = m_addvertPath + SPLIT_SLASH_STR + HEADINI_NAME;
  160. string branchFile = m_addvertPath + SPLIT_SLASH_STR + BRANCHINI_NAME;
  161. string networkFile = m_addvertPath + SPLIT_SLASH_STR + NETWORKINI_NAME;
  162. vector<ResourceParse> headList, branchList, networkList, allList;
  163. if (!ExistsFileA(headFile.c_str())) {
  164. //DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("head config %s not exist!", headFile.c_str());
  165. }
  166. else if (!parseResourceIni(headFile.c_str(), headList)) {
  167. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("parse head config %s fail!", headFile.c_str());
  168. }
  169. if (!ExistsFileA(branchFile.c_str())) {
  170. //DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("branch config %s not exist!", branchFile.c_str());
  171. }
  172. else if (!parseResourceIni(branchFile.c_str(), branchList)) {
  173. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("parse branch config %s fail!", branchFile.c_str());
  174. }
  175. if (!ExistsFileA(networkFile.c_str())) {
  176. //DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("network config %s not exist!", networkFile.c_str());
  177. }
  178. else if (!parseResourceIni(networkFile.c_str(), networkList)) {
  179. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("parse network config %s fail!", networkFile.c_str());
  180. }
  181. m_localList.clear();
  182. uint32_t uheadList = 0;
  183. uint32_t ubranchList = 0;
  184. uint32_t unetworkList = 0;
  185. bool blog = false;
  186. for (vector<ResourceParse>::iterator it = headList.begin(); it < headList.end(); ++it) {
  187. m_localList.push_back(*it);
  188. uheadList++;
  189. }
  190. for (vector<ResourceParse>::iterator it = branchList.begin(); it < branchList.end(); ++it) {
  191. m_localList.push_back(*it);
  192. ubranchList++;
  193. }
  194. for (vector<ResourceParse>::iterator it = networkList.begin(); it < networkList.end(); ++it) {
  195. m_localList.push_back(*it);
  196. unetworkList++;
  197. }
  198. if (m_ilist_size != m_localList.size()) {
  199. m_ilist_size = (int)m_localList.size();
  200. blog = true;
  201. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("adv play list size is %d, head(%d), branch(%d), network(%d).", m_localList.size(), uheadList, ubranchList, unetworkList);
  202. }
  203. std::stable_sort(m_localList.begin(), m_localList.end(), [](const ResourceParse& a, const ResourceParse& b) {
  204. return a.priority > b.priority;
  205. });
  206. CSimpleStringA strJson("");
  207. CSimpleStringA strValidTimeJson("");
  208. int index = 0;
  209. for (auto i = m_localList.begin(); i != m_localList.end(); i++)
  210. {
  211. size_t pos = i->videoNames.find("\\");
  212. if (pos != string::npos){
  213. i->resourcePath = m_addvertPath + SPLIT_SLASH_STR + i->videoNames.substr(0, pos) + SPLIT_SLASH_STR;
  214. i->videoNames = i->videoNames.substr(pos + 1, i->videoNames.length() - pos - 1);
  215. }
  216. else {
  217. i->resourcePath = m_addvertPath + SPLIT_SLASH_STR;
  218. }
  219. if (blog) {
  220. strJson += CSimpleStringA::Format("\"%d\":\"%s\",", index, i->videoNames.c_str());
  221. strValidTimeJson += CSimpleStringA::Format("\"%d\":\"%s\",", index, i->vaildTime.c_str());
  222. index++;
  223. }
  224. }
  225. if (blog) {
  226. if (strJson.GetLength() > 0) {
  227. strJson[strJson.GetLength() - 1] = '\0';
  228. }
  229. LogWarn(Severity_Low, Error_Debug, LOG_EVT_ADV_PLAYLIST_INFO, CSimpleStringA::Format("[{%s}]", strJson.GetData()).GetData());
  230. if (strValidTimeJson.GetLength() > 0) {
  231. strValidTimeJson[strValidTimeJson.GetLength() - 1] = '\0';
  232. }
  233. LogWarn(Severity_Low, Error_Debug, LOG_EVT_ADV_PLAYLIST_VALIDTIME, CSimpleStringA::Format("[{%s}]", strValidTimeJson.GetData()).GetData());
  234. }
  235. }
  236. bool mediaManage::findVaildConfig(string dirPath, string &configPath)
  237. {
  238. string headFile = dirPath + SPLIT_SLASH_STR + HEADINI_NAME;
  239. string branchFile = dirPath + SPLIT_SLASH_STR + BRANCHINI_NAME;
  240. string networkFile = dirPath + SPLIT_SLASH_STR + NETWORKINI_NAME;
  241. if (ExistsFileA(headFile.c_str()))
  242. {
  243. configPath = headFile;
  244. return true;
  245. }
  246. else if (ExistsFileA(branchFile.c_str()))
  247. {
  248. configPath = branchFile;
  249. return true;
  250. }
  251. else if (ExistsFileA(networkFile.c_str()))
  252. {
  253. configPath = networkFile;
  254. return true;
  255. }
  256. else
  257. return false;
  258. }
  259. bool mediaManage::IsResourceExist(string strPath, string strName)
  260. {
  261. return ExistsFileA((strPath + strName).c_str());
  262. }