You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

604 lines
23 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
12 months ago
​
12 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Dr.Frucht Systems Ltd.
  3. // Copyright � 2000-2022. All Rights Reserved.
  4. // Version 1.0 11/19/2022
  5. // Last modification: 11/19/2022
  6. // PROJECT:
  7. // SUBSYSTEM:
  8. // FILE: mojoKernel.cpp
  9. // AUTHOR: gagua
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // LRF_Kernel.cpp
  13. //----------------------------------------------------------------------------
  14. //
  15. #include "servaKernel.h"
  16. #include "../data/json/json_tool.h"
  17. #include <strings.h>
  18. #include <sys/time.h>
  19. #include <time.h>
  20. #include <json/value.h>
  21. #include <json/writer.h>
  22. #include <json/reader.h>
  23. //#include "../common/kernelEventFilterAPF.h"
  24. //#include "../common/kernelEventFilterTrain.h"
  25. //#include "../common/kernelModuleEmbedVideo.h"
  26. #include "../common/serverMySQL_dataBinder.h"
  27. #include "servaModBusManager.h"
  28. #define LOG( X ) kernelPrototype::m_pLog->Log( "KRNL", this, X )
  29. #ifdef DEBUG
  30. #define LOG_ERROR( X ) {\
  31. char _tBfrErrMSG234[256];\
  32. sprintf( _tBfrErrMSG234, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  33. kernelPrototype::m_pLog->Log( "KRNL", this, _LOG_ERROR, _tBfrErrMSG234 );\
  34. }
  35. #else
  36. #define LOG_ERROR( X ) kernelPrototype::m_pLog->Log( "KRNL", this, _LOG_ERROR, X );
  37. #endif
  38. servaKernel *servaKernel::_instance = NULL;
  39. servaKernel *servaKernel::getInstance( config_setting_t *ptr ){
  40. if ( _instance == NULL ) {
  41. _instance = new servaKernel( ptr );
  42. };
  43. return _instance;
  44. };
  45. servaKernel::servaKernel( config_setting_t *ptr ):kernelPrototype( ptr ){
  46. //m_pEventHeap = basicEventHeap::getInstance();
  47. // memset( &m_sSysInfo, 0, sizeof( DBC_SYS_LOAD_INFO ));
  48. //m_pShadow = NULL;
  49. //m_pSystemAnchor = NULL;
  50. // m_bSessionStarted = false;
  51. // m_bWorkWithRemoteServer = false;
  52. // m_bKeepLocalReport = true;
  53. // m_bMediaMode = 0;
  54. m_pLifeMonitor = servaLifeMonitoring::getInstance();
  55. m_pSM = NULL;
  56. m_pRPC = NULL;//??
  57. m_pModBus = NULL;
  58. memset( &m_sServaConfig, 0, sizeof( DBC_SERVA_CONFIG ));
  59. setWM( WM_NONE );
  60. };
  61. servaKernel::~servaKernel(){
  62. WaitFinish( true );
  63. //stopSession( false );
  64. //while(1){
  65. // char tB[200];
  66. // sprintf( tB, "[%d] Seconds Till System Restart", timeout );
  67. // LOG( "BopBop!" );
  68. // SLEEP(1000);
  69. //};
  70. if ( m_pModBus ) {
  71. delete( (servaModBusManager *) m_pModBus );
  72. };
  73. #ifdef LINUX_KERNEL_WATCHDOG
  74. LOG("Disable WD!");
  75. if( m_iWatchdogFd > -1 ) {
  76. char tB[64];
  77. sprintf( tB, "-1\n" );
  78. //ToDo: Make Error Handling here!
  79. write( m_iWatchdogFd, &tB, strlen( tB ) );
  80. };
  81. LOG("WatchDog turned Off!");
  82. #endif
  83. _instance = NULL;
  84. };
  85. bool servaKernel::create(){
  86. if( m_pSettings != NULL ) {
  87. config_setting_t * pSetting;
  88. int tVal;
  89. const char * tBuf;
  90. char tBuffer[128];
  91. LOG("Getting Settings ...");
  92. /*if( !config_setting_lookup_string( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, &tBuf ) ){
  93. char * tVar;
  94. char * tTS;
  95. tTS = getTStr(NULL);
  96. tVar = MD5String(tTS);
  97. free(tTS);
  98. sprintf( m_sIdentity, tVar );
  99. pSetting = config_setting_add( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, CONFIG_TYPE_STRING );
  100. config_setting_set_string( pSetting, tVar );
  101. free( tVar );
  102. basicSettingsProvider::getInstance()->compromise();
  103. LOG("Server Identity not defined - generating new!");
  104. } else {
  105. sprintf( m_sIdentity, tBuf );
  106. sprintf( tBuffer, "Server Identity Read From Settings: %s", tBuf );
  107. LOG( tBuffer );
  108. };
  109. if( !config_setting_lookup_int( m_pSettings, MOJO_KERNEL_TAG_SITE_ID, &tVal ) ){
  110. tVal = m_nInstanceID = 1;
  111. pSetting = config_setting_add( m_pSettings, MOJO_KERNEL_TAG_SITE_ID, CONFIG_TYPE_INT );
  112. config_setting_set_int( pSetting, tVal );
  113. basicSettingsProvider::getInstance()->compromise();
  114. LOG("Site ID not defined - using [1] as number!");
  115. } else {
  116. m_nInstanceID = tVal;
  117. sprintf( tBuffer, "App Instance ID Read From Settings: %d", m_nInstanceID );
  118. LOG( tBuffer );
  119. };*/
  120. basicSettingsProvider::getInstance()->validate();
  121. } else {
  122. LOG_ERROR("Failed Read Config Settings!");
  123. return false;
  124. };
  125. LOG("Register Server ... ");
  126. registerServA();
  127. m_pSM = servaSessionManager::getInstance( m_pSettings );
  128. if ( m_pSM ){
  129. cleanUpMess();
  130. if ( m_pSM->create() ) {
  131. LOG("Session Manager Created");
  132. servaModBusManager * tModBus = servaModBusManager::getInstance();
  133. if( tModBus ){
  134. LOG("Have ModBus Instance");
  135. if( tModBus->create() ){
  136. LOG("Modbus Created");
  137. m_pModBus = (void *) tModBus;
  138. } else delete( tModBus );
  139. };
  140. /* Test Composer */
  141. //servaVideoComposer *tVC = new servaVideoComposer( 49 );
  142. //if ( tVC ) {
  143. // tVC->compose();
  144. //};
  145. m_pRPC = servaRemoteClient::getInstance(this);
  146. if ( m_pRPC ) {
  147. if( m_pRPC->start() ) return init();
  148. };
  149. };
  150. };
  151. return false;
  152. };
  153. void servaKernel::m_fThreadProc( void ){
  154. time_t tTimeTSeconds = 0;
  155. time_t cTimeTSeconds = 0;
  156. m_bThreadStatus = TH_WORKING;
  157. LOG("ServA Started!");
  158. while ( m_bThreadStatus < TH_ENDED ) {
  159. if( DBC_RPC_WEB_CMD * tCMD = m_pDBC->fetchWebCmd( 0 ) ) {
  160. m_pDBC->updateWebCmdStatus( tCMD->id, DBC_CMD_STARTED );
  161. switch( tCMD->cmd ){
  162. case CMD_KRNL_RELOAD:{
  163. LOG("Reload ServA Node!");
  164. m_bThreadStatus = TH_ENDED;
  165. setExitCause( EXIT_CODE_RELOAD_SERVICES );
  166. break;
  167. };
  168. case CMD_KRNL_RESTART:{
  169. LOG("Restart ServA Node!");
  170. m_bThreadStatus = TH_ENDED;
  171. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  172. break;
  173. };
  174. case CMD_KRNL_FACTORY_RESET:{
  175. LOG("Restart ServA -> Factory Reset!");
  176. m_bThreadStatus = TH_ENDED;
  177. setExitCause( EXIT_CODE_FACTORY_RESET );
  178. break;
  179. };
  180. case CMD_NODES_RELOAD:{
  181. //if( m_pNodeFactory != NULL ) {
  182. // m_pNodeFactory->reloadNodes();
  183. //};
  184. break;
  185. };
  186. case CMD_NODE_RELOAD:{
  187. uint32_t tNodeID = atoi( tCMD->param );
  188. //if( m_pNodeFactory != NULL ) {
  189. // m_pNodeFactory->reloadNode( tNodeID );
  190. //};
  191. break;
  192. };
  193. case CMD_KRNL_PNP:{
  194. //if( m_pHardwareManager!= NULL ) {
  195. // LOG_ERROR( "Restart and Detect HardWare!" );
  196. // // Must be shutdowned all service that uses hardware prior to detection!
  197. // m_bThreadStatus = TH_ENDED;
  198. // setExitCause( EXIT_CODE_DETECT_HARDWARE ); // Go -> Detect Hardware in BACKEND
  199. //};
  200. break;
  201. };
  202. case CMD_KRNL_UPDATE_IDENT:{
  203. //LOG("Restart Mojo -> Update Ident!");
  204. //sprintf( m_sIdentity, tCMD->PARAM );
  205. //config_setting_remove ( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY );
  206. //config_setting_t *pSetting = config_setting_add( m_pSettings, MOJO_KERNEL_TAG_SERVER_IDENTITY, CONFIG_TYPE_STRING );
  207. //config_setting_set_string( pSetting, tCMD->PARAM );
  208. //basicSettingsProvider::getInstance()->compromise();
  209. //basicSettingsProvider::getInstance()->validate();
  210. //m_pDBC->updateServerIdentity( m_nServerID, m_sIdentity );
  211. m_bThreadStatus = TH_ENDED;
  212. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  213. break;
  214. };
  215. case CMD_KRNL_REPLACE_DATABASE:{
  216. LOG("Restart Mojo -> Replace Database!");
  217. //CSTR tNAME = m_pDBC->getDataParam( 0 );
  218. //CSTR tUSER = m_pDBC->getDataParam( 1 );
  219. //CSTR tGROUP = m_pDBC->getDataParam( 2 );
  220. //if ( tNAME ) {
  221. // char tB[256];
  222. // sprintf( tB,"./data/%s.db", tCMD->PARAM );
  223. // if ( fileExist( tB )) {
  224. // LOG("Restart Mojo -> Close Database!");
  225. //Closing Database
  226. // delete( m_pDBC );
  227. // m_pDBC = NULL;
  228. // LOG("Restart Mojo -> Delete Database!");
  229. // //unlink( tNAME );
  230. // LOG("Restart Mojo -> Copy Database!");
  231. // fileCopy( tB, tNAME );
  232. // LOG("Restart Mojo -> Chown Database!");
  233. // ch_chown( tNAME, tUSER, tGROUP );
  234. // LOG("Restart Mojo -> Delete Old Database!");
  235. // unlink( tB );
  236. // m_bThreadStatus = TH_ENDED;
  237. // LOG("Restart Mojo -> Exit!");
  238. // setExitCause( EXIT_CODE_NO_DATABASE );//EXIT_CODE_RELOAD_SERVICES );
  239. // m_bThreadStatus = TH_ENDED;
  240. // } else {
  241. // LOG_ERROR("New Database Does not Exists!");
  242. // };
  243. //free( tNAME );
  244. //free( tUSER );
  245. //free( tGROUP );
  246. //};
  247. break;
  248. };
  249. case CMD_KRNL_SYNC_TIME:{
  250. uint32_t uts = atoi( tCMD->param );
  251. if( uts ){
  252. #if ( __GLIBC__ < 3 ) && ( __GLIBC_MINOR__ < 31 )
  253. time_t ts = (time_t)uts;
  254. if( stime( &ts ) == 0 ){
  255. // Restert Server;
  256. m_bThreadStatus = TH_ENDED;
  257. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  258. };
  259. #else
  260. timespec ts;
  261. ts.tv_sec = (time_t)uts;
  262. ts.tv_nsec = 0;
  263. if( clock_settime( CLOCK_REALTIME, &ts ) == 0 ){
  264. // Restert Server;
  265. m_bThreadStatus = TH_ENDED;
  266. setExitCause( EXIT_CODE_KERNEL_CLEAR );
  267. };
  268. #endif
  269. };
  270. break;
  271. };
  272. case CMD_KRNL_SYSTEM_TASK:{
  273. break;
  274. };
  275. case CMD_KRNL_PURGE_SESSION:{
  276. if ( tCMD->aux_id ) {
  277. LOG("Purge Session");
  278. if (m_pSM) m_pSM->eraseSession( tCMD->aux_id );
  279. };
  280. break;
  281. };
  282. case CMD_KRNL_SET_SESSION_HZL:{
  283. uint8_t tLVL = atoi( tCMD->param );
  284. if ( tCMD->aux_id ) {
  285. LOG("Life Session Hazard Level Updated by Operator");
  286. /* Actual DataBase change done in WebApp Here we just making internal Event */
  287. SESSION_RATE_DATA *tData = (SESSION_RATE_DATA *) malloc( sizeof( SESSION_RATE_DATA ) );
  288. if ( tData ) {
  289. tData->level = tLVL;
  290. tData->track_id = tCMD->aux_id;
  291. EVENT_CONTAINER * tEvt = createBasicEvent( 0, EVT_CLASS_SYSTEM, EVT_SRV_SESSION_RATED, NULL, NULL, tData );
  292. if ( tEvt ) {
  293. basicEventHeap::getInstance()->broadcast( tEvt );
  294. } else {
  295. free( tData );
  296. };
  297. };
  298. };
  299. break;
  300. };
  301. case CMD_KRNL_TEST_MODBUS:{
  302. MODBUS_TEST_DATA *tData = (MODBUS_TEST_DATA *) malloc( sizeof( MODBUS_TEST_DATA ) );
  303. if ( tData ) {
  304. tData->id = tCMD->aux_id;
  305. tData->c_OutBits = atoi( tCMD->param );
  306. EVENT_CONTAINER * tEvt = createBasicEvent( 0, EVT_CLASS_SYSTEM, EVT_SRV_MODBUS_TEST_M, NULL, NULL, tData );
  307. if ( tEvt ) {
  308. basicEventHeap::getInstance()->broadcast( tEvt );
  309. } else {
  310. free( tData );
  311. };
  312. };
  313. break;
  314. };
  315. case CMD_KRN_COMPOSE_SESSION_VIDEOS:{
  316. uint64_t session_id = tCMD->aux_id;
  317. if ( session_id ) {
  318. char tB[256];
  319. sprintf( tB, "Ordered Compose of session %lld", session_id );
  320. LOG( tB );
  321. servaVideoComposer *tCMP = new servaVideoComposer( session_id );
  322. if ( tCMP ) {
  323. if ( tCMP->compose() ) {
  324. m_vComposersPool.push_back(tCMP);
  325. } else {
  326. LOG("Disengage Composer!");
  327. };
  328. };
  329. };
  330. break;
  331. };
  332. case CMD_KRN_SCADA_SNOOZE_POST_MESSAGE:{
  333. uint64_t message_id = tCMD->aux_id;
  334. if ( message_id ) {
  335. #ifdef DEBUG
  336. char tB[256];
  337. sprintf( tB, "Ordered Snooze %lld SCADA Message", message_id );
  338. LOG( tB );
  339. #endif
  340. MODBUS_POST_DATA *tData = m_pDBC->getScadaPostData( message_id );
  341. if ( tData ) {
  342. m_pDBC->updateScadaPostStatus( message_id, 1 );
  343. EVENT_CONTAINER * tEvt = createBasicEvent( 0, EVT_CLASS_SYSTEM, EVT_SRV_MODBUS_S_POSTBOX, NULL, NULL, tData );
  344. if ( tEvt ) {
  345. basicEventHeap::getInstance()->broadcast( tEvt );
  346. } else {
  347. free( tData );
  348. };
  349. };
  350. };
  351. break;
  352. };
  353. case CMD_KRN_SCADA_DISMISS_POST_MESSAGE:{
  354. uint64_t message_id = tCMD->aux_id;
  355. if ( message_id ) {
  356. #ifdef DEBUG
  357. char tB[256];
  358. sprintf( tB, "Ordered Dismiss %lld SCADA Message", message_id );
  359. LOG( tB );
  360. #endif
  361. MODBUS_POST_DATA *tData = m_pDBC->getScadaPostData( message_id );
  362. if ( tData ) {
  363. m_pDBC->deleteScadaPost( message_id );
  364. EVENT_CONTAINER * tEvt = createBasicEvent( 0, EVT_CLASS_SYSTEM, EVT_SRV_MODBUS_D_POSTBOX, NULL, NULL, tData );
  365. if ( tEvt ) {
  366. basicEventHeap::getInstance()->broadcast( tEvt );
  367. } else {
  368. free( tData );
  369. };
  370. };
  371. };
  372. break;
  373. };
  374. //Debug Functionality from Platform View
  375. case CMD_KRN_SCADA_DISMISS_ALRM_PLATFORM:{
  376. uint64_t platform_id = tCMD->aux_id;
  377. if( platform_id ) {
  378. #ifdef DEBUG
  379. char tB[256];
  380. sprintf( tB, "Ordered Dismiss %lld SCADA Platform ALARMS", platform_id );
  381. LOG( tB );
  382. #endif
  383. EVENT_CONTAINER * tEvt = createBasicEvent( platform_id, EVT_CLASS_SYSTEM, EVT_SRV_MODBUS_CLEAR_S );
  384. if ( tEvt ) {
  385. basicEventHeap::getInstance()->broadcast( tEvt );
  386. } else {
  387. // free( tData );
  388. };
  389. };
  390. break;
  391. };
  392. case CMD_KRN_SCADA_DISMISS_TOTT_PLATFORM:{
  393. uint64_t platform_id = tCMD->aux_id;
  394. if( platform_id ) {
  395. #ifdef DEBUG
  396. char tB[256];
  397. sprintf( tB, "Ordered Dismiss %lld SCADA Platform ALL", platform_id );
  398. LOG( tB );
  399. #endif
  400. EVENT_CONTAINER * tEvt = createBasicEvent( platform_id, EVT_CLASS_SYSTEM, EVT_SRV_MODBUS_CLEAR_P );
  401. if ( tEvt ) {
  402. basicEventHeap::getInstance()->broadcast( tEvt );
  403. } else {
  404. //free( tData );
  405. };
  406. };
  407. break;
  408. };
  409. };
  410. m_pDBC->updateWebCmdStatus( tCMD->id, DBC_CMD_READY_TO_CLEAN );
  411. free( tCMD );
  412. };
  413. cTimeTSeconds = time( NULL );
  414. if( cTimeTSeconds != tTimeTSeconds ){ //Each Second
  415. tTimeTSeconds = cTimeTSeconds;
  416. #ifdef LINUX_KERNEL_WATCHDOG
  417. if( m_iWatchdogFd > -1 ) {
  418. char tB[64];
  419. sprintf( tB, "2000\n" );
  420. //ToDo: Make Error Handling here!
  421. write( m_iWatchdogFd, &tB, strlen( tB ) );
  422. };
  423. #endif
  424. m_pDBC->cleanWebCmd();
  425. updateStatus();
  426. if ( m_vEncodersPool.size() ) {
  427. for ( V_MEDIA_ENCODERS::iterator idx = m_vEncodersPool.begin(); idx < m_vEncodersPool.end(); ) {
  428. if ( (*idx)->getStatus() == ENCODER_FINISHED ) {
  429. handleEncodedVideo( *idx );
  430. m_vEncodersPool.erase( idx );
  431. } else idx++;
  432. };
  433. };
  434. if ( m_vComposersPool.size() ) {
  435. for ( V_VIDEO_COMPOSERS::iterator idx = m_vComposersPool.begin(); idx< m_vComposersPool.end(); ) {
  436. if ( (*idx)->isFinished() ) {
  437. delete( *idx );
  438. m_vComposersPool.erase( idx );
  439. } else idx++;
  440. };
  441. };
  442. if ( m_sServaConfig.encoderLimit ) {
  443. if (m_vEncodersPool.size() < m_sServaConfig.encoderLimit) {
  444. if (uint64_t new_id = m_pDBC->getRecentMediaId()) {
  445. LOG("New Encoder!");
  446. if ( processVideoFile( new_id ) ) {
  447. updateEncodeStatus( new_id, MEDIA_ENCODE_PROGRESS, 0 );
  448. } else {
  449. LOG_ERROR("Cannot Encode Queued Video -> Removing Task!");
  450. m_pDBC->removeRemoteMediaInfo( new_id );
  451. };
  452. };
  453. };
  454. } else LOG_ERROR("Video Encoders Disabled??");
  455. if ( m_pSM ) m_pSM->validate();
  456. if ( m_pLifeMonitor ) m_pLifeMonitor->validate();
  457. if ( m_pModBus ) ((servaModBusManager *)m_pModBus)->validate();
  458. //m_pHardwareManager->updateSysInfo( &m_sSysInfo );
  459. if ( m_pVideoComposer == NULL ) {
  460. uint64_t tSID = m_pDBC->getSessionWithUnmergedVideo( m_sServaConfig.composerProceedDelay );
  461. if ( tSID ) {
  462. m_pVideoComposer = new servaVideoComposer( tSID );
  463. if ( m_pVideoComposer ) {
  464. if( !m_pVideoComposer->compose() ){
  465. delete( m_pVideoComposer );
  466. m_pVideoComposer = NULL;
  467. };
  468. };
  469. };
  470. };
  471. };
  472. SLEEP(SLEEP_KERNEL_INTERVAL);
  473. };
  474. };
  475. void servaKernel::updateEncodeStatus( uint64_t dbid, uint8_t status, uint8_t perc ){
  476. m_pDBC->updateEncodeStatus( dbid, status, perc );
  477. };
  478. bool servaKernel::processVideoFile( uint64_t dbid ){
  479. if ( dbid == 0 ) return false;
  480. DBC_RPC_MEDIA_INFO *NFO = m_pDBC->getRemoteMediaInfo( dbid );
  481. if ( NFO ) {
  482. if ( strcmp( NFO->fmt, "mp4" ) == 0 ) {
  483. #ifdef DEBUG
  484. LOG("Skip Convertion of MP4");
  485. #endif
  486. DBC_RPC_MEDIA_INFO_EX *tP = m_pDBC->getRemoteMediaInfoEx( dbid );
  487. if ( tP ) {
  488. if( !m_pSM->addVideo( tP )){
  489. LOG_ERROR("Cannot Add to session Media Info!");
  490. };
  491. #ifdef DEBUG
  492. LOG("Delete Extended Info");
  493. #endif
  494. free( tP );
  495. } else {
  496. LOG_ERROR("Cannot get Extended Encoder Queue Media Info!");
  497. };
  498. free( NFO );
  499. return false;
  500. };
  501. servaMediaEncoder *ptr = new servaMediaEncoder(dbid);
  502. if ( ptr ) {
  503. #ifdef DEBUG
  504. LOG("Encoder Constructed!");
  505. #endif
  506. char tA[MAX_PATH];
  507. char tB[MAX_PATH];
  508. //Todo -> Instead GetSessionBase -> calc Base based on TS of video
  509. CSTR BASE = m_pSM->getSessionBase( NFO->session_id );
  510. if( BASE ){
  511. sprintf( tA,"%s/%s.%s", BASE, NFO->uuid, NFO->fmt );
  512. LOG(tA);
  513. sprintf( tB,"%s/%s.mp4", BASE, NFO->uuid );
  514. LOG(tB);
  515. free(BASE);
  516. if ( ptr->create( tA, tB, NFO->fps ) ) {
  517. #ifdef DEBUG
  518. LOG("Encoder Created!");
  519. #endif
  520. m_vEncodersPool.push_back( ptr );
  521. return true;
  522. };
  523. };
  524. delete( ptr );
  525. };
  526. free( NFO );
  527. } else LOG("No Video Info!!");
  528. return false;
  529. };
  530. void servaKernel::handleEncodedVideo( servaMediaEncoder *ptr ){
  531. if ( ptr ) {
  532. m_pDBC->updateEncodeFormat( ptr->getDBID(), ptr->getDstFormat() );
  533. DBC_RPC_MEDIA_INFO_EX *tP = m_pDBC->getRemoteMediaInfoEx( ptr->getDBID() );
  534. if ( tP ) {
  535. if( m_pSM->addVideo( tP )){
  536. m_pDBC->removeRemoteMediaInfo( ptr->getDBID() );
  537. } else {
  538. LOG_ERROR("Cannot Add to session Media Info!");
  539. };
  540. LOG("Delete Extended Info");
  541. free( tP );
  542. } else {
  543. LOG_ERROR("Cannot get Extended Encoder Queue Media Info!");
  544. };
  545. LOG("Delete Encoder");
  546. delete( ptr );
  547. LOG("Done");
  548. };
  549. };
  550. bool servaKernel::dispatchEvent( EVENT_CONTAINER *Evt ){
  551. if ( Evt == NULL ) return false;
  552. /*
  553. switch ( Evt->m_eClass ) {
  554. case EVT_CLASS_VIDEO:{
  555. switch ( Evt->m_eType ) {
  556. case EVT_SRV_VIDEO_CHUNK:{
  557. SERVA_VIDEO_TRANSFER_INFO *tData = ( SERVA_VIDEO_TRANSFER_INFO * ) Evt->m_pEvtData;
  558. if ( tData ){
  559. if ( tData->status == MEDIA_STORED ) {
  560. updateEncodeStatus(tData->db_id, tData->status, tData->perc);
  561. };
  562. };
  563. return false; // Kernel is Final Destination for that Event
  564. };
  565. };
  566. break;
  567. };
  568. };*/
  569. return true;
  570. };
  571. //Todo -> Startup Cleanup Media Encoder Queue (with ability to finish unfinishing jobs -> Drop just unsucessful transfers
  572. // Make Treshold of Running Encoders to a server
  573. #undef LOG
  574. #undef LOG_ERROR