MediaManage.cpp 11 KB

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