MediaManage.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. #include "stdafx.h"
  2. #include "mediaManage.h"
  3. #include <set>
  4. #include <algorithm>
  5. mediaManage::mediaManage()
  6. {
  7. m_curParse.clear();
  8. if (checkDirExist(DEFAULT_RESOURSE_PATH))
  9. {
  10. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Set defaultAdvertPath:%s, defaultDownloadPath:%s", DEFAULT_RESOURSE_PATH, DEFAULT_DOWNLOAD_PATH);
  11. setDefaultAddvertPath(string(DEFAULT_RESOURSE_PATH));
  12. setDefaultDownloadPath(string(DEFAULT_DOWNLOAD_PATH));
  13. }
  14. else
  15. {
  16. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Set defaultAdvertPath:%s, defaultDownloadPath:%s", DEFAULT_RESOURSE_PATH_C, DEFAULT_DOWNLOAD_PATH_C);
  17. setDefaultAddvertPath(string(DEFAULT_RESOURSE_PATH_C));
  18. setDefaultDownloadPath(string(DEFAULT_DOWNLOAD_PATH_C));
  19. }
  20. bloged = false;
  21. }
  22. void mediaManage::setDefaultAddvertPath(string addvertPath){ m_addvertPath = addvertPath; }
  23. void mediaManage::setDefaultDownloadPath(string downloadPath){ m_downloadPath = downloadPath; }
  24. void mediaManage::GetPlayList(vector<ResourceParse> &resourceList)
  25. {
  26. resourceList.clear();
  27. for (vector<ParseInfo>::iterator i = m_curParse.begin(); i != m_curParse.end(); i++)
  28. {
  29. for (vector<ResourceParse>::iterator j = i->m_mediaList.begin(); j != i->m_mediaList.end(); j++)
  30. {
  31. if (checkInVaildTime(j->vaildTime, true) && checkInPlayTime(j->playTime, true))
  32. resourceList.push_back(*j);
  33. }
  34. }
  35. }
  36. size_t mediaManage::GetPlayListByLocal(vector<ResourceParse>& resourceList)
  37. {
  38. size_t uNum = 0;
  39. resourceList.clear();
  40. #ifdef RVC_OS_WIN
  41. for each (auto i in m_localList)
  42. {
  43. if (checkInVaildTime(i.vaildTime, true) && checkInPlayTime(i.playTime, true)) {
  44. resourceList.push_back(i);
  45. uNum++;
  46. }
  47. }
  48. #else
  49. for (vector<ResourceParse>::iterator it = m_localList.begin(); it != m_localList.end(); it++)
  50. {
  51. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("play extend %c:%s, %s, %s, %s", it->type, it->resourcePath.c_str(), it->videoNames.c_str(), it->vaildTime.c_str(), it->playTime.c_str());
  52. if (checkInVaildTime(it->vaildTime, true) && checkInPlayTime(it->playTime, true)) {
  53. resourceList.push_back(*it);
  54. uNum++;
  55. }
  56. }
  57. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("uNum is %d, m_localList size is %d, resourceList size is %d", uNum, m_localList.size(), resourceList.size());
  58. for (vector<ResourceParse>::iterator i = resourceList.begin(); i != resourceList.end(); i++)
  59. {
  60. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("begin play extend %c:%s, %s", i->type, i->resourcePath.c_str(), i->videoNames.c_str());
  61. }
  62. #endif // _WIN32
  63. return uNum;
  64. }
  65. size_t mediaManage::GetPlayListByLocal(CResourceParse** ResourceArr, size_t uSize)
  66. {
  67. size_t uNum = 0;
  68. for (vector<ResourceParse>::iterator it = m_localList.begin(); it != m_localList.end() && uNum < uSize; it++)
  69. {
  70. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("new deep copy play extend %c:%s, %s, %s, %s", it->type, it->resourcePath.c_str(), it->videoNames.c_str(), it->vaildTime.c_str(), it->playTime.c_str());
  71. if (checkInVaildTime(it->vaildTime, true) && checkInPlayTime(it->playTime, true)) {
  72. CResourceParse* pNode = new CResourceParse();
  73. pNode->type = it->type;
  74. pNode->fullScreen = it->fullScreen;
  75. pNode->primMonitor = it->primMonitor;
  76. pNode->simpleMode = it->simpleMode;
  77. pNode->playInterval = it->playInterval;
  78. pNode->priority = it->priority;
  79. if (it->videoNames.length() > 0) {
  80. pNode->pvideoNames = new char[it->videoNames.length() + 1];
  81. memset(pNode->pvideoNames, 0, it->videoNames.length() + 1);
  82. memcpy(pNode->pvideoNames, it->videoNames.c_str(), it->videoNames.length());
  83. }
  84. if (it->vaildTime.length() > 0) {
  85. pNode->pvaildTime = new char[it->vaildTime.length() + 1];
  86. memset(pNode->pvaildTime, 0, it->vaildTime.length() + 1);
  87. memcpy(pNode->pvaildTime, it->vaildTime.c_str(), it->vaildTime.length());
  88. }
  89. if (it->resourcePath.length() > 0) {
  90. pNode->presourcePath = new char[it->resourcePath.length() + 1];
  91. memset(pNode->presourcePath, 0, it->resourcePath.length() + 1);
  92. memcpy(pNode->presourcePath, it->resourcePath.c_str(), it->resourcePath.length());
  93. }
  94. if (it->playTime.length() > 0) {
  95. pNode->playTime = new char[it->playTime.length() + 1];
  96. memset(pNode->playTime, 0, it->playTime.length() + 1);
  97. memcpy(pNode->playTime, it->playTime.c_str(), it->playTime.length());
  98. }
  99. ResourceArr[uNum++] = pNode;
  100. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("aftercopy play extend pNode->type=0x%08x:pNode->presourcePath=0x%08x, pNode->pvideoNames=0x%08x, pNode->pvaildTime=0x%08x, pNode->playTime=0x%08x", pNode->type, pNode->presourcePath, pNode->pvideoNames, pNode->pvaildTime, pNode->playTime);
  101. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("aftercopy play extend %c:%s, %s, %s, %s", pNode->type, pNode->presourcePath, pNode->pvideoNames, pNode->pvaildTime, pNode->playTime);
  102. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("%s:%d ResourceList[%d] address is 0x%08x, pNode address is 0x%08x.", __FUNCTION__, __LINE__, uNum-1, ResourceArr[uNum-1], pNode);
  103. }
  104. }
  105. return uNum;
  106. }
  107. size_t mediaManage::GetPlayListByLocal(rvcResourceParse_t* ResourceArr, size_t uSize)
  108. {
  109. size_t uNum = 0;
  110. for (vector<ResourceParse>::iterator it = m_localList.begin(); it != m_localList.end() && uNum < uSize; it++)
  111. {
  112. if (false == bloged){
  113. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("new deep copy play extend %c:%s, %s, %s, %s", it->type, it->resourcePath.c_str(), it->videoNames.c_str(), it->vaildTime.c_str(), it->playTime.c_str());
  114. }
  115. if (checkInVaildTime(it->vaildTime, true) && checkInPlayTime(it->playTime, true) && IsResourceExist(it->resourcePath, it->videoNames)) {
  116. rvcResourceParse_t* pNode = ResourceArr+uNum;
  117. pNode->type = it->type;
  118. pNode->fullScreen = it->fullScreen;
  119. pNode->primMonitor = it->primMonitor;
  120. pNode->simpleMode = it->simpleMode;
  121. pNode->playInterval = it->playInterval;
  122. pNode->priority = it->priority;
  123. if (it->videoNames.length() > 0) {
  124. memcpy(pNode->strvideoNames, it->videoNames.c_str(), it->videoNames.length());
  125. }
  126. if (it->vaildTime.length() > 0) {
  127. memcpy(pNode->strvaildTime, it->vaildTime.c_str(), it->vaildTime.length());
  128. }
  129. if (it->resourcePath.length() > 0) {
  130. memcpy(pNode->strResourcePath, it->resourcePath.c_str(), it->resourcePath.length());
  131. }
  132. if (it->playTime.length() > 0) {
  133. memcpy(pNode->strplayTime, it->playTime.c_str(), it->playTime.length());
  134. }
  135. uNum++;
  136. }
  137. }
  138. bloged = true;
  139. return uNum;
  140. }
  141. bool mediaManage::InitResourseList(vector<string> resourceList)
  142. {
  143. /*
  144. */
  145. vector<string> needDelete(10), needAdd(10);
  146. set<string> oldResourceList, newResourseList;
  147. //get the old resource List
  148. for (vector<ParseInfo>::iterator j = m_curParse.begin(); j != m_curParse.end(); j++)
  149. oldResourceList.insert(j->m_resourceName);
  150. for (vector<string>::iterator i = resourceList.begin(); i != resourceList.end(); i++)
  151. newResourseList.insert(*i);
  152. auto deletePos = set_difference(oldResourceList.begin(), oldResourceList.end(), newResourseList.begin(), newResourseList.end(), needDelete.begin());//need to delete
  153. needDelete.resize(deletePos - needDelete.begin());
  154. auto addPos = set_difference(newResourseList.begin(), newResourseList.end(), oldResourceList.begin(), oldResourceList.end(), needAdd.begin());//need to add
  155. needAdd.resize(addPos - needAdd.begin());
  156. DeleteResourceList(needDelete);
  157. AddResourceList(needAdd);
  158. return true;
  159. }
  160. bool mediaManage::AddResourceList(vector<string> resourceList)
  161. {
  162. if (0 == resourceList.size())
  163. return true;
  164. for (vector<string>::iterator i = resourceList.begin(); i != resourceList.end(); i++)
  165. {
  166. int pos = i->find(".zip");
  167. if (-1 == pos)
  168. continue;
  169. string dirName = i->substr(0, pos);
  170. string fileName = m_downloadPath + SPLIT_SLASH_STR + *i;
  171. string dirPath = m_addvertPath + SPLIT_SLASH_STR + dirName;
  172. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("dirName:%s, fileName:%s, dirPath:%s", dirName.c_str(), fileName.c_str(), dirPath.c_str());
  173. wstring wfileName, wdirPath;
  174. StringToWstring(wfileName, fileName);
  175. StringToWstring(wdirPath, dirPath);
  176. if (!checkFileExist(dirPath))
  177. {
  178. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("dir %s not exist!", dirPath.c_str());
  179. continue;
  180. }
  181. /*
  182. if (!checkFileExist(dirPath) && createDir(dirPath) && !Unzip2Folder((BSTR)wfileName.c_str(), (BSTR)wdirPath.c_str()))
  183. {
  184. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("unzip %s to %s fail!", fileName.c_str(), dirPath.c_str());
  185. continue;
  186. }
  187. */
  188. string configPath = "";
  189. if (!findVaildConfig(dirPath, configPath))
  190. {
  191. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("can not find config file in %s//%s", dirPath.c_str(), configPath.c_str());
  192. continue;
  193. }
  194. vector<ResourceParse> mediaList;
  195. if (!parseResourceIni(configPath.c_str(), mediaList))
  196. {
  197. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("parse config %s fail!", configPath.c_str());
  198. continue;
  199. }
  200. string resourcePath = dirPath + SPLIT_SLASH_STR;
  201. /* if (!getUniqueDir(dirPath, resourcePath))
  202. {
  203. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("error:find multi resource dir!");
  204. continue;
  205. }
  206. resourcePath = dirPath + "\\" + resourcePath + "\\";
  207. */
  208. for (vector<ResourceParse>::iterator j = mediaList.begin(); j != mediaList.end(); j++)
  209. j->resourcePath = resourcePath;
  210. ParseInfo resourseInfo(*i, mediaList);
  211. m_curParse.push_back(resourseInfo);//modify resourse list
  212. }
  213. return true;
  214. }
  215. void mediaManage::InitResourceListByLocal()
  216. {
  217. string headFile = m_addvertPath + SPLIT_SLASH_STR + HEADINI_NAME;
  218. string branchFile = m_addvertPath + SPLIT_SLASH_STR + BRANCHINI_NAME;
  219. string networkFile = m_addvertPath + SPLIT_SLASH_STR + NETWORKINI_NAME;
  220. vector<ResourceParse> headList, branchList, networkList, allList;
  221. if (!checkFileExist(headFile)) {
  222. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("head config %s not exist!", headFile.c_str());
  223. }
  224. else if (!parseResourceIni(headFile.c_str(), headList)) {
  225. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("parse head config %s fail!", headFile.c_str());
  226. }
  227. if (!checkFileExist(branchFile)) {
  228. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("branch config %s not exist!", branchFile.c_str());
  229. }
  230. else if (!parseResourceIni(branchFile.c_str(), branchList)) {
  231. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("parse branch config %s fail!", branchFile.c_str());
  232. }
  233. if (!checkFileExist(networkFile)) {
  234. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("network config %s not exist!", networkFile.c_str());
  235. }
  236. else if (!parseResourceIni(networkFile.c_str(), networkList)) {
  237. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("parse network config %s fail!", networkFile.c_str());
  238. }
  239. m_localList.clear();
  240. #ifdef RVC_OS_WIN
  241. for each (auto i in headList)
  242. m_localList.push_back(i);
  243. for each (auto i in branchList)
  244. m_localList.push_back(i);
  245. for each (auto i in networkList)
  246. m_localList.push_back(i);
  247. #else
  248. size_t uheadList = 0;
  249. size_t ubranchList = 0;
  250. size_t unetworkList = 0;
  251. for (vector<ResourceParse>::iterator it = headList.begin(); it < headList.end(); ++it){
  252. m_localList.push_back(*it);
  253. uheadList++;
  254. }
  255. for (vector<ResourceParse>::iterator it = branchList.begin(); it < branchList.end(); ++it){
  256. m_localList.push_back(*it);
  257. ubranchList++;
  258. }
  259. for (vector<ResourceParse>::iterator it = networkList.begin(); it < networkList.end(); ++it){
  260. m_localList.push_back(*it);
  261. unetworkList++;
  262. }
  263. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("headList size is %d, branchList size is %d, networkList size is %d, and m_localList size is %u.", uheadList, ubranchList, unetworkList, m_localList.size());
  264. #endif // RVC_OS_WIN
  265. std::stable_sort(m_localList.begin(), m_localList.end(), [](const ResourceParse& a, const ResourceParse& b) {
  266. return a.priority > b.priority;
  267. });
  268. for (auto i = m_localList.begin(); i != m_localList.end(); i++)
  269. {
  270. size_t pos = i->videoNames.find("\\");
  271. if (pos != string::npos){
  272. //Dbg("%s find result is not string::npos", i->videoNames.c_str());
  273. i->resourcePath = m_addvertPath + SPLIT_SLASH_STR + i->videoNames.substr(0, pos) + SPLIT_SLASH_STR;
  274. i->videoNames = i->videoNames.substr(pos + 1, i->videoNames.length() - pos - 1);
  275. }
  276. else {
  277. //Dbg("%s find result is string::npos", i->videoNames.c_str());
  278. i->resourcePath = m_addvertPath + SPLIT_SLASH_STR;
  279. }
  280. }
  281. }
  282. void mediaManage::DeleteResourceList(vector<string> resourceList)
  283. {
  284. if (0 == resourceList.size())
  285. return;
  286. for (vector<string>::iterator i = resourceList.begin(); i != resourceList.end(); i++)
  287. {
  288. for (vector<ParseInfo>::iterator j = m_curParse.begin(); j != m_curParse.end(); j++)
  289. {
  290. if (*i == j->m_resourceName)
  291. {
  292. m_curParse.erase(j);
  293. break;
  294. }
  295. }
  296. }
  297. }
  298. bool mediaManage::findVaildConfig(string dirPath, string &configPath)
  299. {
  300. string headFile = dirPath + SPLIT_SLASH_STR + HEADINI_NAME;
  301. string branchFile = dirPath + SPLIT_SLASH_STR + BRANCHINI_NAME;
  302. string networkFile = dirPath + SPLIT_SLASH_STR + NETWORKINI_NAME;
  303. if (checkFileExist(headFile))
  304. {
  305. configPath = headFile;
  306. return true;
  307. }
  308. else if (checkFileExist(branchFile))
  309. {
  310. configPath = branchFile;
  311. return true;
  312. }
  313. else if (checkFileExist(networkFile))
  314. {
  315. configPath = networkFile;
  316. return true;
  317. }
  318. else
  319. return false;
  320. }
  321. bool mediaManage::IsResourceExist(string strPath, string strName)
  322. {
  323. return checkFileExist(strPath + strName);
  324. }
  325. void mediaManage::clearOutdataResource()
  326. {
  327. vector<string> dirInfo, needDelete(10);
  328. set<string> oldResourceList, newResourseList;
  329. try
  330. {
  331. getDirs(m_addvertPath, dirInfo);
  332. for (vector<ParseInfo>::iterator i = m_curParse.begin(); i != m_curParse.end(); i++)
  333. {
  334. int pos = i->m_resourceName.find(".zip");
  335. if (-1 == pos)
  336. continue;
  337. newResourseList.insert(i->m_resourceName.substr(0, pos));
  338. }
  339. for (vector<string>::iterator i = dirInfo.begin(); i != dirInfo.end(); i++)
  340. {
  341. if(-1 != i->find(DEFAULT_HEAD_CONTAIN) || -1 != i->find(DEFAULT_FEN_CONTAIN) || -1 != i->find(DEFAULT_NETWORK_CONTAIN) || -1 != i->find(DEFAULT_NETWORK2_CONTAIN))
  342. oldResourceList.insert(*i);
  343. }
  344. auto deletePos = set_difference(oldResourceList.begin(), oldResourceList.end(), newResourseList.begin(), newResourseList.end(), needDelete.begin());//need to delete
  345. needDelete.resize(deletePos - needDelete.begin());
  346. for (vector<string>::iterator i = needDelete.begin(); i != needDelete.end(); i++)
  347. {
  348. string path = m_addvertPath + SPLIT_SLASH_STR + *i;
  349. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("remove Dir %s %s!", *i, removeDir(path) ? "success" : "fail");
  350. }
  351. }
  352. catch (exception* e)
  353. {
  354. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("remove Dir exception:%s", e->what());
  355. }
  356. }