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.

1205 lines
35 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
la_mojo: fix memory leaks and heap corruption Continuous leaks: - protocolParser: on package-pool overflow the oldest package was removed but never freed while the newest (still pooled) one was freed. Every packet a handler does not consume (e.g. other sensors on a shared bus) leaked once the pool was full. Free the removed package, free pooled packages in the destructor and getPackage(), use memmove for the overlapping buffer shifts. - SQLite_Connector::Query: loop only while sqlite3_step() returns SQLITE_ROW. Looping until SQLITE_DONE spun forever allocating rows on SQLITE_BUSY/ERROR. Set a busy timeout, close handles after a failed open, fix escapeBinary() writing into uninitialised buffers. - basicThread/basicDualThread: threads that ended by themselves were never joined, leaking their stacks on every stream drop/restart. Always join once (detach when called from the thread itself); check pthread_create result correctly. - curlRequest: free the header list; initialise m_pPostData and the content type; curlRequestsManager deletes requests it cannot queue. - kernelModuleModemManager: free the phonebook ID list on every reload. - deviceNodeFactory: do not append the same pending node every cycle. - streamHandler: free frames left in the encoding queue on destruction. Heap corruption: - kernelModuleFactory: module that failed to start was deleted twice. - mojoRemoteClient: basicRing::push() returns an ID (may be 0), not a bool; compare with -1. Fix packetExtractor and destructor leaks. - utilizeBasicEvent: payloads are malloc'ed, release with free(). - videoAVLIBSource: avoid sws context double free, NULL check order. - trim(): memmove for overlapping copy. Smaller leaks: dropSession duplicate query, LRF upload data, report backupper / fly hunter settings, Kafka consumer handle, BVMS uniq ID. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
2 days ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
la_mojo: fix memory leaks and heap corruption Continuous leaks: - protocolParser: on package-pool overflow the oldest package was removed but never freed while the newest (still pooled) one was freed. Every packet a handler does not consume (e.g. other sensors on a shared bus) leaked once the pool was full. Free the removed package, free pooled packages in the destructor and getPackage(), use memmove for the overlapping buffer shifts. - SQLite_Connector::Query: loop only while sqlite3_step() returns SQLITE_ROW. Looping until SQLITE_DONE spun forever allocating rows on SQLITE_BUSY/ERROR. Set a busy timeout, close handles after a failed open, fix escapeBinary() writing into uninitialised buffers. - basicThread/basicDualThread: threads that ended by themselves were never joined, leaking their stacks on every stream drop/restart. Always join once (detach when called from the thread itself); check pthread_create result correctly. - curlRequest: free the header list; initialise m_pPostData and the content type; curlRequestsManager deletes requests it cannot queue. - kernelModuleModemManager: free the phonebook ID list on every reload. - deviceNodeFactory: do not append the same pending node every cycle. - streamHandler: free frames left in the encoding queue on destruction. Heap corruption: - kernelModuleFactory: module that failed to start was deleted twice. - mojoRemoteClient: basicRing::push() returns an ID (may be 0), not a bool; compare with -1. Fix packetExtractor and destructor leaks. - utilizeBasicEvent: payloads are malloc'ed, release with free(). - videoAVLIBSource: avoid sws context double free, NULL check order. - trim(): memmove for overlapping copy. Smaller leaks: dropSession duplicate query, LRF upload data, report backupper / fly hunter settings, Kafka consumer handle, BVMS uniq ID. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0139veDo3XyavMuVhdnHcN16
2 days ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 months ago
​
​
​
​
​
​
​
2 months ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "videoAVLIBSource.h"
  2. #ifndef LRF_PROXY_ONLY
  3. //#define DEBUG_EXTRA
  4. #include "libavutil/error.h"
  5. #define LOG(X) m_pLog->Log("LibAV_SRC", this, X)
  6. #ifdef DEBUG
  7. #define LOG_ERROR(X) \
  8. { \
  9. char _tBfrErrMSG2w34[4096]; \
  10. sprintf(_tBfrErrMSG2w34, "In file %s[%d] in func:%s %s", __FILE__, __LINE__, __func__, X); \
  11. m_pLog->Log("LIBAV_SRC", this, _LOG_ERROR, _tBfrErrMSG2w34); \
  12. }
  13. #else
  14. #define LOG_ERROR(X) m_pLog->Log("LIBAV_SRC", this, _LOG_ERROR, X);
  15. #endif
  16. static int interrupt_cb(void * ctx)
  17. {
  18. if ( ctx != NULL ) {
  19. basicLogCollection::getInstance()->Log("LibAV_CB", NULL, "INTERUPT!");
  20. }
  21. // do something
  22. return 0;
  23. }
  24. static const AVIOInterruptCB int_cb = {interrupt_cb, NULL};
  25. // https://ffmpeg.org/doxygen/3.4/ffplay_8c_source.html#l02732
  26. videoAVLIBSource::videoAVLIBSource(void * opaque) :
  27. basicMediaSource(),
  28. basicThread()
  29. {
  30. m_pLog = basicLogCollection::getInstance();
  31. m_bStreamingSource = true;
  32. m_bSizeIsAutomatic = false;
  33. m_nOutWidth = m_nInWidth = m_nOutHeight = m_nInHeight = -1;
  34. m_bSrcType = AVLIB_SOURCE_NOTDEF;
  35. if (opaque)
  36. {
  37. m_pOpaque = opaque;
  38. }
  39. else
  40. {
  41. m_pOpaque = (void *) this;
  42. };
  43. if (! initLibAV())
  44. {
  45. return;
  46. }
  47. m_pVideoState = (VideoState *) av_mallocz(sizeof(VideoState));
  48. if (m_pVideoState == NULL)
  49. {
  50. LOG_ERROR("VideoState Memory Allocation Error");
  51. return;
  52. };
  53. m_eMediaStatus = m_pVideoState->m_bStatus = MS_UNDEFINED;
  54. m_pVideoState->m_iVideoStreamID = -1;
  55. #ifndef _NOAUDIO_
  56. m_pVideoState->m_iAudioStreamID = -1;
  57. #endif
  58. m_pVideoState->m_bStatus = MS_INIT;
  59. }
  60. videoAVLIBSource::videoAVLIBSource(char * Source, void * opaque) :
  61. basicMediaSource(),
  62. basicThread()
  63. {
  64. m_pLog = basicLogCollection::getInstance();
  65. m_bStreamingSource = true;
  66. m_bSizeIsAutomatic = false;
  67. m_nOutWidth = m_nInWidth = m_nOutHeight = m_nInHeight = -1;
  68. m_bSrcType = AVLIB_SOURCE_NOTDEF;
  69. if (opaque)
  70. {
  71. m_pOpaque = opaque;
  72. }
  73. else
  74. {
  75. m_pOpaque = (void *) this;
  76. };
  77. if (! initLibAV())
  78. {
  79. return;
  80. }
  81. m_pVideoState = (VideoState *) av_mallocz(sizeof(VideoState));
  82. if (m_pVideoState == NULL)
  83. {
  84. LOG_ERROR("VideoState Memory Allocation Error");
  85. return;
  86. };
  87. m_eMediaStatus = m_pVideoState->m_bStatus = MS_UNDEFINED;
  88. m_pVideoState->m_pFormatCtx = avformat_alloc_context();
  89. if (m_pVideoState->m_pFormatCtx == NULL)
  90. {
  91. LOG_ERROR("Video Format Context Memory Allocation Error");
  92. return;
  93. };
  94. m_pVideoState->m_iVideoStreamID = -1;
  95. #ifndef _NOAUDIO_
  96. m_pVideoState->m_iAudioStreamID = -1;
  97. #endif
  98. m_pVideoState->m_bStatus = MS_INIT;
  99. _open(Source);
  100. }
  101. videoAVLIBSource::~videoAVLIBSource()
  102. {
  103. LOG("Destructor");
  104. if (m_pVideoState != NULL)
  105. {
  106. _close();
  107. av_free(m_pVideoState);
  108. m_pVideoState = NULL;
  109. };
  110. }
  111. bool videoAVLIBSource::setOutputSize(int Width, int Height)
  112. {
  113. if (m_pVideoState)
  114. {
  115. if (m_pVideoState->m_bStatus == MS_INIT)
  116. {
  117. if (Width && Height)
  118. {
  119. m_bSizeIsAutomatic = false;
  120. m_nOutWidth = Width;
  121. m_nOutHeight = Height;
  122. return true;
  123. };
  124. };
  125. };
  126. return false;
  127. }
  128. bool videoAVLIBSource::setProportionalByWidth(int Width)
  129. {
  130. if (m_pVideoState)
  131. {
  132. if (m_pVideoState->m_bStatus == MS_INIT)
  133. {
  134. if (Width > 0)
  135. {
  136. m_bSizeIsAutomatic = true;
  137. m_nOutWidth = Width;
  138. return true;
  139. };
  140. };
  141. };
  142. return false;
  143. }
  144. bool videoAVLIBSource::setProportionalByHeight(int Height)
  145. {
  146. if (m_pVideoState)
  147. {
  148. if (m_pVideoState->m_bStatus == MS_INIT)
  149. {
  150. if (Height > 0)
  151. {
  152. m_bSizeIsAutomatic = true;
  153. m_nOutHeight = Height;
  154. return true;
  155. };
  156. };
  157. };
  158. return false;
  159. }
  160. bool videoAVLIBSource::setFrameAspectRatio(FrameAspectRatio AspectRatio)
  161. {
  162. if( AspectRatio ) AspectRatio = AspectRatio;
  163. return true;
  164. }
  165. bool videoAVLIBSource::_close(void)
  166. {
  167. LOG("CLOSE");
  168. if (m_pVideoState == NULL)
  169. {
  170. return true;
  171. }
  172. // set Close Status - to prevent continue
  173. m_eMediaStatus = m_pVideoState->m_bStatus = MS_CLOSED;
  174. m_pMediaDestination = NULL; //??
  175. //_stop(); //stops the Thread
  176. basicThread::waitThreadEnd(true);
  177. if (m_pVideoState->m_pFormatOpts)
  178. {
  179. av_dict_free(&m_pVideoState->m_pFormatOpts);
  180. m_pVideoState->m_pFormatOpts = NULL;
  181. };
  182. if (m_pVideoState->m_pFormatCtx)
  183. {
  184. avformat_close_input(&m_pVideoState->m_pFormatCtx);
  185. m_pVideoState->m_iVideoStreamID = -1;
  186. };
  187. if (m_pVideoState->m_bStatus > MS_INIT)
  188. {
  189. #if (LIBAVCODEC_VERSION_MAJOR < 58) // && ( LIBAVCODEC_VERSION_MINOR < 50 )
  190. #ifndef _NOAUDIO_
  191. if (m_pVideoState->m_pAudioCtx != NULL)
  192. {
  193. avcodec_close(m_pVideoState->m_pAudioCtx);
  194. avcodec_free_context(&m_pVideoState->m_pAudioCtx);
  195. };
  196. #endif
  197. if (m_pVideoState->m_pCodecCtx != NULL)
  198. {
  199. avcodec_close(m_pVideoState->m_pCodecCtx);
  200. m_pVideoState->m_iVideoStreamID = -1;
  201. };
  202. #else
  203. #ifndef _NOAUDIO_
  204. if (m_pVideoState->m_pAudioCtx != NULL)
  205. {
  206. avcodec_close(m_pVideoState->m_pAudioCtx);
  207. avcodec_free_context(&m_pVideoState->m_pAudioCtx);
  208. };
  209. #endif
  210. if (m_pVideoState->m_pCodecCtx != NULL)
  211. {
  212. avcodec_close(m_pVideoState->m_pCodecCtx);
  213. avcodec_free_context(&m_pVideoState->m_pCodecCtx);
  214. };
  215. #endif
  216. m_eMediaStatus = m_pVideoState->m_bStatus = MS_INIT;
  217. };
  218. sws_freeContext(m_pVideoState->m_pSwsCtx);
  219. m_pVideoState->m_pSwsCtx = NULL; // _close() runs again from the destructor - avoid double free
  220. return true;
  221. }
  222. bool videoAVLIBSource::_continue(void)
  223. {
  224. if (m_pVideoState->m_bStatus == MS_STOPED)
  225. {
  226. return basicThread::init();
  227. };
  228. return false;
  229. }
  230. bool videoAVLIBSource::_stop(void)
  231. {
  232. LOG("STOP");
  233. return basicThread::uninit();
  234. }
  235. // https://www.youtube.com/watch?v=Lu3LZmhC_DY
  236. bool videoAVLIBSource::_restart(void)
  237. {
  238. if (m_pVideoState == NULL)
  239. {
  240. return false;
  241. }
  242. if (m_pVideoState->m_bStatus < MS_OPENED)
  243. {
  244. return false;
  245. }
  246. if (m_pVideoState->m_pFormatCtx == NULL)
  247. {
  248. LOG_ERROR("Video Format Context Memory Allocation Error");
  249. return false;
  250. };
  251. // Stop Thread
  252. basicThread::uninit();
  253. int RetVal;
  254. switch (m_bSrcType)
  255. {
  256. case AVLIB_SOURCE_DDSHOW:
  257. {
  258. #if (LIBAVCODEC_VERSION_MAJOR >= 60 )
  259. //const added for ffmpeg 60
  260. const AVInputFormat * iformat = av_find_input_format("dshow");
  261. #else
  262. AVInputFormat * iformat = av_find_input_format("dshow");
  263. #endif
  264. if ((RetVal = avformat_open_input(&m_pVideoState->m_pFormatCtx, m_pVideoState->m_pFileName, iformat, &m_pVideoState->m_pFormatOpts)) != 0)
  265. {
  266. char tB[2048];
  267. // av_free( iformat );
  268. char tBuffer[128];
  269. av_strerror(RetVal, tBuffer, 128);
  270. sprintf(tB, "VideoSource Couldn't open input device[%s](Code:%d): %s", tBuffer, RetVal, m_pVideoState->m_pFileName);
  271. LOG_ERROR(tB);
  272. m_eMediaStatus = m_pVideoState->m_bStatus = MS_INIT;
  273. return false;
  274. };
  275. break;
  276. };
  277. case AVLIB_SOURCE_V4L2:
  278. case AVLIB_SOURCE_IPSTREAM:
  279. {
  280. if ((RetVal = avformat_open_input(&m_pVideoState->m_pFormatCtx, m_pVideoState->m_pFileName, NULL, &m_pVideoState->m_pFormatOpts)) != 0)
  281. {
  282. // av_dict_free(&options);
  283. char tB[2048];
  284. char tBuffer[128];
  285. av_strerror(RetVal, tBuffer, 128);
  286. sprintf(tB, "VideoSource Couldn't open input stream[%s](Code:%d): %s", tBuffer, RetVal, m_pVideoState->m_pFileName);
  287. LOG_ERROR(tB);
  288. m_eMediaStatus = m_pVideoState->m_bStatus = MS_INIT;
  289. return false;
  290. };
  291. break;
  292. };
  293. };
  294. #ifdef DEBUG
  295. LOG("DataStream Opened");
  296. #endif
  297. m_eMediaStatus = m_pVideoState->m_bStatus = MS_OPENED;
  298. if (avformat_find_stream_info(m_pVideoState->m_pFormatCtx, NULL) < 0)
  299. {
  300. char tB[2048];
  301. sprintf(tB, "Couldn't find stream information: %s", m_pVideoState->m_pFileName);
  302. LOG_ERROR(tB);
  303. return false;
  304. };
  305. #if (LIBAVCODEC_VERSION_MAJOR < 58) // && ( LIBAVCODEC_VERSION_MINOR < 50 )
  306. for (unsigned int i = 0; i < m_pVideoState->m_pFormatCtx->nb_streams; i++)
  307. {
  308. // if( m_pVideoState->m_pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ){
  309. if (m_pVideoState->m_pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
  310. {
  311. LOG("VIDEO stream detected.");
  312. if (m_pVideoState->m_iVideoStreamID < 0)
  313. {
  314. LOG("VIDEO stream assigned.");
  315. m_pVideoState->m_iVideoStreamID = i;
  316. };
  317. };
  318. #ifndef _NOAUDIO_
  319. if (m_pVideoState->m_pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
  320. {
  321. LOG("AUDIO stream detected.");
  322. if (m_pVideoState->m_iAudioStreamID < 0)
  323. {
  324. LOG("AUDIO stream assigned.");
  325. m_pVideoState->m_iAudioStreamID = i;
  326. }
  327. };
  328. #endif
  329. };
  330. if (m_pVideoState->m_iVideoStreamID == -1)
  331. {
  332. LOG_ERROR("Didn't find a video stream.");
  333. return false;
  334. };
  335. #ifndef _NOAUDIO_
  336. if (m_pVideoState->m_iAudioStreamID == -1)
  337. {
  338. LOG_ERROR("Didn't find a audio stream.");
  339. return false;
  340. };
  341. #endif
  342. // Stream found
  343. m_pVideoState->m_pCodecCtx = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID]->codec;
  344. // m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID]->codecpar->codec_id
  345. m_pVideoState->m_pVideoCodec = avcodec_find_decoder(m_pVideoState->m_pCodecCtx->codec_id);
  346. #else
  347. #ifdef DEBUG_EXTRA
  348. LOG("YABABA-2!");
  349. #endif
  350. /* find the video stream information */
  351. #if (LIBAVCODEC_VERSION_MAJOR >= 60 )
  352. //cast ffmpeg 60
  353. m_pVideoState->m_iVideoStreamID = av_find_best_stream(m_pVideoState->m_pFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, const_cast<const AVCodec**>(&m_pVideoState->m_pVideoCodec), 0);
  354. #else
  355. m_pVideoState->m_iVideoStreamID = av_find_best_stream(m_pVideoState->m_pFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, &m_pVideoState->m_pVideoCodec, 0);
  356. #endif
  357. if (m_pVideoState->m_iVideoStreamID == -1)
  358. {
  359. LOG_ERROR("Didn't find a video stream.");
  360. return false;
  361. };
  362. #ifndef _NOAUDIO_
  363. if (m_pVideoState->m_iAudioStreamID == -1)
  364. {
  365. LOG_ERROR("Didn't find a audio stream.");
  366. return false;
  367. };
  368. #endif
  369. if (! (m_pVideoState->m_pCodecCtx = avcodec_alloc_context3(m_pVideoState->m_pVideoCodec)))
  370. {
  371. LOG_ERROR("Can not create decoder context!");
  372. return false;
  373. };
  374. m_pVideoState->m_pVideoStr = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID];
  375. if (avcodec_parameters_to_context(m_pVideoState->m_pCodecCtx, m_pVideoState->m_pVideoStr->codecpar) < 0)
  376. {
  377. LOG_ERROR("Can not set Context parameters!");
  378. return false;
  379. };
  380. #endif
  381. if (m_pVideoState->m_pVideoCodec == NULL)
  382. {
  383. char tB[2048];
  384. // sprintf(Buffer, "Codec [%d] not found for %s", m_pVideoState->m_pCodecCtx->codec_id, m_pVideoState->m_pFileName);
  385. sprintf(tB, "Codec [%d] not found for %s", m_pVideoState->m_pCodecCtx->codec_id, m_pVideoState->m_pFileName);
  386. LOG_ERROR(tB);
  387. return false;
  388. };
  389. /*
  390. End-user application may configure the number of threads to utilize (via AVCodecContext::thread_count property set before avcodec_open2())
  391. and threaded mode (AVCodecContext::thread_type set to FF_THREAD_FRAME or FF_THREAD_SLICE).
  392. Thread pool will be managed by FFmpeg itself, although some answers say that it is also possible using application-provided pool.
  393. */
  394. m_pVideoState->m_pCodecCtx->thread_count = 4;
  395. // m_pVideoState->m_pCodecCtx->thread_type = FF_THREAD_FRAME;
  396. // if (is_live)
  397. m_pVideoState->m_pCodecCtx->thread_count = FF_THREAD_SLICE;
  398. // else
  399. // m_pVideoState->m_pCodecCtx->thread_count = FF_THREAD_SLICE | FF_THREAD_FRAME;
  400. #ifdef DEBUG_EXTRA
  401. LOG("Before Open2");
  402. #endif
  403. if (avcodec_open2(m_pVideoState->m_pCodecCtx, m_pVideoState->m_pVideoCodec, NULL) < 0)
  404. {
  405. char tB[2048];
  406. sprintf(tB, "Could not open Video codec[%d] for %s", m_pVideoState->m_pCodecCtx->codec_id, m_pVideoState->m_pFileName);
  407. LOG_ERROR(tB);
  408. return false;
  409. };
  410. /* we do not send complete frames */
  411. // if( m_pVideoState->m_pVideoCodec->capabilities & AV_CODEC_CAP_TRUNCATED ) m_pVideoState->m_pCodecCtx->flags |= AV_CODEC_CAP_TRUNCATED;
  412. #ifndef _NOAUDIO_
  413. m_pVideoState->m_pAudioCtx = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iAudioStreamID]->codec;
  414. m_pVideoState->m_pAudioCodec = avcodec_find_decoder(m_pVideoState->m_pAudioCtx->codec_id);
  415. if (! m_pVideoState->m_pAudioCodec)
  416. {
  417. LOG_ERROR("Unsupported Audio codec!");
  418. return false;
  419. };
  420. if (avcodec_open2(m_pVideoState->m_pAudioCtx, m_pVideoState->m_pAudioCodec, NULL) < 0)
  421. {
  422. // SDL_LogError( SDL_LOG_CATEGORY_AUDIO, "Could not open Audio codec[%d].", m_pVideoState->m_pAudioCtx->codec_id );
  423. LOG_ERROR("Could not open Audio codec.");
  424. return false;
  425. };
  426. #endif
  427. return basicThread::init();
  428. }
  429. bool videoAVLIBSource::_open(char * URI, bool ShowDeviceMenu, float framerate, int inputPin, int width, int height, int s_width, int s_height)
  430. {
  431. if (URI == NULL)
  432. {
  433. return false;
  434. }
  435. if (m_pVideoState == NULL)
  436. {
  437. return false;
  438. }
  439. if (m_bThreadStatus > TH_PRE_INIT)
  440. {
  441. return false;
  442. }
  443. m_pVideoState->m_pFormatCtx = avformat_alloc_context();
  444. if (m_pVideoState->m_pFormatCtx == NULL)
  445. {
  446. LOG_ERROR("Video Format Context Memory Allocation Error");
  447. return false;
  448. };
  449. if (m_pVideoState->m_bStatus == MS_INIT)
  450. {
  451. char Buffer[1248] = {0};
  452. char tChar[128] = {0};
  453. strncpy(m_pVideoState->m_pFileName, URI, MAX_PATH_LEN);
  454. CSTR tMD5 = MD5String(m_pVideoState->m_pFileName);
  455. if (tMD5)
  456. {
  457. sprintf(m_pCookie, "%s", tMD5);
  458. free(tMD5);
  459. }
  460. else
  461. {
  462. sprintf(m_pCookie, "some-random-id");
  463. }
  464. m_eMediaStatus = m_pVideoState->m_bStatus = MS_OPENING;
  465. if (strncmp(m_pVideoState->m_pFileName, "video=", 6) == 0)
  466. {
  467. m_bSrcType = AVLIB_SOURCE_DDSHOW;
  468. #ifdef DEBUG
  469. LOG("MS-Video Device");
  470. #endif
  471. // AVDictionary *options = NULL;
  472. if (width != -1 && height != -1)
  473. {
  474. m_bSizeIsAutomatic = false;
  475. char tChar[10];
  476. sprintf(tChar, "%dx%d", width, height);
  477. av_dict_set(&m_pVideoState->m_pFormatOpts, "video_size", tChar, 0);
  478. }
  479. else
  480. {
  481. // m_bSizeIsAutomatic = true;
  482. };
  483. // av_dict_set(&m_pVideoState->m_pFormatOpts, "pixel_format", "yuyv422", 0 );
  484. av_dict_set(&m_pVideoState->m_pFormatOpts, "video_codec", "mjpeg", 0);
  485. if (inputPin != -1)
  486. {
  487. sprintf(tChar, "%d", inputPin);
  488. av_dict_set(&m_pVideoState->m_pFormatOpts, "crossbar_video_input_pin_number", tChar, 0);
  489. };
  490. if (framerate != -1)
  491. {
  492. sprintf(tChar, "%f", framerate);
  493. av_dict_set(&m_pVideoState->m_pFormatOpts, "framerate", tChar, 0);
  494. };
  495. av_dict_set(&m_pVideoState->m_pFormatOpts, "rtbufsize", "500000k", 0);
  496. if (ShowDeviceMenu)
  497. {
  498. av_dict_set(&m_pVideoState->m_pFormatOpts, "show_video_device_dialog", "true", 0);
  499. av_dict_set(&m_pVideoState->m_pFormatOpts, "show_video_crossbar_connection_dialog", "true", 0);
  500. // sprintf(Buffer, "data/%s.settings", m_pCookie);
  501. // av_dict_set(&options, "video_device_save", Buffer, 0);
  502. #ifndef _NOAUDIO_
  503. av_dict_set(&m_pVideoState->m_pFormatOpts, "show_audio_crossbar_connection_dialog", "true", 0);
  504. sprintf(Buffer, "%s-a.settings", URI);
  505. av_dict_set(&m_pVideoState->m_pFormatOpts, "audio_device_save", Buffer, 0);
  506. #endif
  507. }
  508. else
  509. {
  510. sprintf(Buffer, "data/%s.settings", m_pCookie);
  511. // if (!fileExist(Buffer)) av_dict_set(&m_pVideoState->m_pFormatOpts, "video_device_load", Buffer, 0);
  512. };
  513. // av_dict_set(&m_pVideoState->m_pFormatOpts, "crossbar_video_input_pin_number", "1", 0);
  514. // av_dict_set(&m_pVideoState->m_pFormatOpts, "show_audio_crossbar_connection_dialog", "true", 0);
  515. // av_dict_set(&m_pVideoState->m_pFormatOpts, "show_video_crossbar_connection_dialog", "true", 0);
  516. // AVInputFormat * is Internal Const in CoDec Lib
  517. #if (LIBAVCODEC_VERSION_MAJOR >= 60 )
  518. const AVInputFormat * iformat = av_find_input_format("dshow");
  519. #else
  520. AVInputFormat * iformat = av_find_input_format("dshow");
  521. #endif
  522. // av_dict_set(&options, "pixel_format", "bgr24", 0);
  523. av_dict_set(&m_pVideoState->m_pFormatOpts, "timeout", "5000", 0);
  524. // AVDeviceInfoList *device_list;
  525. // m_pVideoState->m_pFormatCtx->iformat = iformat;
  526. // avdevice_list_devices(m_pVideoState->m_pFormatCtx, &device_list);
  527. // avdevice_free_list_devices(&device_list);
  528. if (avformat_open_input(&m_pVideoState->m_pFormatCtx, m_pVideoState->m_pFileName, iformat, &m_pVideoState->m_pFormatOpts) != 0)
  529. {
  530. av_dict_free(&m_pVideoState->m_pFormatOpts);
  531. m_pVideoState->m_pFormatOpts = NULL;
  532. sprintf(Buffer, "VideoSource Couldn't open input device: %s", URI);
  533. LOG_ERROR(Buffer);
  534. m_eMediaStatus = m_pVideoState->m_bStatus = MS_INIT;
  535. return false;
  536. };
  537. /*AVDictionaryEntry *e;
  538. if (e = av_dict_get(options, "", NULL, AV_DICT_IGNORE_SUFFIX)) {
  539. sprintf(Buffer, "Option %s not recognized by the demuxer", e->key);
  540. LOG_ERROR(Buffer);
  541. };*/
  542. // if (iformat!=NULL)
  543. // if (iformat->priv_class != NULL){
  544. // const AVOption *cOption = NULL;
  545. // while((cOption = av_opt_next(&iformat->priv_class, cOption))!= NULL )
  546. // serializeOption(cOption);
  547. // };
  548. // av_dict_free(&options);
  549. // AVOption *mpOption = NULL;
  550. }
  551. else
  552. {
  553. if (strncmp(URI, "/dev/video", 10) == 0)
  554. {
  555. m_bSrcType = AVLIB_SOURCE_V4L2;
  556. }
  557. else
  558. {
  559. m_bSrcType = AVLIB_SOURCE_IPSTREAM;
  560. };
  561. // AVDictionary *options = NULL;
  562. // av_dict_set(&m_pVideoState->m_pFormatOpts, "movflags", "faststart", 0);
  563. if (m_bSrcType == AVLIB_SOURCE_IPSTREAM)
  564. {
  565. #ifdef DEBUG
  566. LOG("IP Stream Source");
  567. #endif
  568. av_dict_set(&m_pVideoState->m_pFormatOpts, "rtsp_transport", "tcp", 0);
  569. av_dict_set(&m_pVideoState->m_pFormatOpts, "stimeout", "5000000", 0);
  570. av_dict_set(&m_pVideoState->m_pFormatOpts, "buffer_size", "4096000", 0);
  571. /*
  572. av_dict_set_int(&opts, "rw_timeout", 5000000, 0);
  573. av_dict_set_int(&opts, "tcp_nodelay", 1, 0);
  574. av_dict_set_int(&opts, "stimeout", 10000000, 0);
  575. av_dict_set(&opts, "user_agent", "Mozilla/5.0", 0);
  576. av_dict_set(&opts, "rtsp_transport", "tcp", 0);
  577. av_dict_set(&opts, "rtsp_flags", "prefer_tcp", 0);
  578. av_dict_set_int(&opts, "buffer_size", BUFSIZE, 0);
  579. pFormatCtx->flags |= AVFMT_FLAG_NONBLOCK;
  580. pFormatCtx->flags |= AVFMT_FLAG_DISCARD_CORRUPT;
  581. pFormatCtx->flags |= AVFMT_FLAG_NOBUFFER;
  582. */
  583. }
  584. else
  585. {
  586. #ifdef DEBUG
  587. LOG("Linux Video Device");
  588. #endif
  589. if (width != -1 && height != -1)
  590. {
  591. m_bSizeIsAutomatic = false;
  592. sprintf(tChar, "%dx%d", width, height);
  593. av_dict_set(&m_pVideoState->m_pFormatOpts, "video_size", tChar, 0);
  594. switch (inputPin)
  595. {
  596. case 0:
  597. LOG("PIX YUYV");
  598. av_dict_set(&m_pVideoState->m_pFormatOpts, "pixel_format", "yuv420p", 0);
  599. break;
  600. case 1:
  601. LOG("PIX MJPG");
  602. av_dict_set(&m_pVideoState->m_pFormatOpts, "pixel_format", "mjpeg", 0);
  603. break;
  604. default: av_dict_set(&m_pVideoState->m_pFormatOpts, "pixel_format", "rgb24", 0); break;
  605. };
  606. }
  607. else
  608. {
  609. // m_bSizeIsAutomatic = true;
  610. };
  611. av_dict_set(&m_pVideoState->m_pFormatOpts, "fflags", "nobuffer", 0);
  612. };
  613. av_dict_set(&m_pVideoState->m_pFormatOpts, "max_delay", "100", 0);
  614. // av_dict_set(&options, "analyzeduration", "1000000", 0);
  615. if (framerate != -1)
  616. {
  617. sprintf(tChar, "%0.2f", framerate);
  618. // sprintf(tChar, "120" );
  619. av_dict_set(&m_pVideoState->m_pFormatOpts, "framerate", tChar, 0);
  620. // av_dict_set(&options, "frame_rate", tChar, 0);
  621. };
  622. // av_dict_set(&options, "timeout", "8000", 0);
  623. int RetVal = 0;
  624. // LOG("Before Open1");
  625. if ((RetVal = avformat_open_input(&m_pVideoState->m_pFormatCtx, m_pVideoState->m_pFileName, NULL, &m_pVideoState->m_pFormatOpts)) != 0)
  626. {
  627. av_dict_free(&m_pVideoState->m_pFormatOpts);
  628. m_pVideoState->m_pFormatOpts = NULL;
  629. char tBuffer[128];
  630. av_strerror(RetVal, tBuffer, 128);
  631. sprintf(Buffer, "VideoSource Couldn't open input stream[%s](Code:%d): %s", tBuffer, RetVal, m_pVideoState->m_pFileName);
  632. LOG_ERROR(Buffer);
  633. return false;
  634. };
  635. // LOG("After Open1");
  636. AVDictionaryEntry * e;
  637. if ((e = av_dict_get(m_pVideoState->m_pFormatOpts, "", NULL, AV_DICT_IGNORE_SUFFIX)))
  638. {
  639. sprintf(Buffer, "Option %s not recognized by the demuxer", e->key);
  640. LOG_ERROR(Buffer);
  641. };
  642. // e is pointer to entry!
  643. // av_dict_free( &e );
  644. };
  645. #ifdef DEBUG
  646. LOG("DataStream Opened");
  647. #endif
  648. m_eMediaStatus = m_pVideoState->m_bStatus = MS_OPENED;
  649. if (avformat_find_stream_info(m_pVideoState->m_pFormatCtx, NULL) < 0)
  650. {
  651. sprintf(Buffer, "Couldn't find stream information: %s", URI);
  652. LOG_ERROR(Buffer);
  653. return false;
  654. };
  655. #if (LIBAVCODEC_VERSION_MAJOR < 58) // && ( LIBAVCODEC_VERSION_MINOR < 50 )
  656. for (unsigned int i = 0; i < m_pVideoState->m_pFormatCtx->nb_streams; i++)
  657. {
  658. // if( m_pVideoState->m_pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ){
  659. if (m_pVideoState->m_pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
  660. {
  661. LOG("VIDEO stream detected.");
  662. if (m_pVideoState->m_iVideoStreamID < 0)
  663. {
  664. LOG("VIDEO stream assigned.");
  665. m_pVideoState->m_iVideoStreamID = i;
  666. };
  667. };
  668. #ifndef _NOAUDIO_
  669. if (m_pVideoState->m_pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
  670. {
  671. LOG("AUDIO stream detected.");
  672. if (m_pVideoState->m_iAudioStreamID < 0)
  673. {
  674. LOG("AUDIO stream assigned.");
  675. m_pVideoState->m_iAudioStreamID = i;
  676. }
  677. };
  678. #endif
  679. };
  680. if (m_pVideoState->m_iVideoStreamID == -1)
  681. {
  682. LOG_ERROR("Didn't find a video stream.");
  683. return false;
  684. };
  685. // Video Stream found
  686. m_pVideoState->m_pCodecCtx = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID]->codec;
  687. // Static pointer
  688. m_pVideoState->m_pVideoCodec = avcodec_find_decoder(m_pVideoState->m_pCodecCtx->codec_id);
  689. #ifndef _NOAUDIO_
  690. if (m_pVideoState->m_iAudioStreamID == -1)
  691. {
  692. LOG_ERROR("Didn't find a audio stream.");
  693. return false;
  694. };
  695. // ToDo: Bind Audio Codec
  696. #endif
  697. #else
  698. #ifdef DEBUG_EXTRA
  699. LOG("YABABA-2!");
  700. #endif
  701. /* find the video stream information */
  702. #if (LIBAVCODEC_VERSION_MAJOR >= 60 )
  703. m_pVideoState->m_iVideoStreamID = av_find_best_stream(m_pVideoState->m_pFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, const_cast<const AVCodec**>(&m_pVideoState->m_pVideoCodec), 0);
  704. #else
  705. m_pVideoState->m_iVideoStreamID = av_find_best_stream(m_pVideoState->m_pFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, &m_pVideoState->m_pVideoCodec, 0);
  706. #endif
  707. if (m_pVideoState->m_iVideoStreamID == -1)
  708. {
  709. LOG_ERROR("Didn't find a video stream.");
  710. return false;
  711. };
  712. #ifndef _NOAUDIO_
  713. if (m_pVideoState->m_iAudioStreamID == -1)
  714. {
  715. LOG_ERROR("Didn't find a audio stream.");
  716. return false;
  717. };
  718. #endif
  719. if (! (m_pVideoState->m_pCodecCtx = avcodec_alloc_context3(m_pVideoState->m_pVideoCodec)))
  720. {
  721. LOG_ERROR("Can not create decoder context!");
  722. return false;
  723. };
  724. m_pVideoState->m_pVideoStr = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID];
  725. if (avcodec_parameters_to_context(m_pVideoState->m_pCodecCtx, m_pVideoState->m_pVideoStr->codecpar) < 0)
  726. {
  727. LOG_ERROR("Can not set Context parameters!");
  728. return false;
  729. };
  730. #endif
  731. if (m_pVideoState->m_pVideoCodec == NULL)
  732. {
  733. // sprintf(Buffer, "Codec [%d] not found for %s", m_pVideoState->m_pCodecCtx->codec_id, URI);
  734. sprintf(Buffer, "Codec [%d] not found for %s", m_pVideoState->m_pCodecCtx->codec_id, URI);
  735. LOG_ERROR(Buffer);
  736. return false;
  737. };
  738. /*
  739. End-user application may configure the number of threads to utilize (via AVCodecContext::thread_count property set before avcodec_open2())
  740. and threaded mode (AVCodecContext::thread_type set to FF_THREAD_FRAME or FF_THREAD_SLICE).
  741. Thread pool will be managed by FFmpeg itself, although some answers say that it is also possible using application-provided pool.
  742. */
  743. m_pVideoState->m_pCodecCtx->thread_count = 4;
  744. // m_pVideoState->m_pCodecCtx->thread_type = FF_THREAD_FRAME;
  745. // if (is_live)
  746. m_pVideoState->m_pCodecCtx->thread_count = FF_THREAD_SLICE;
  747. // else
  748. // m_pVideoState->m_pCodecCtx->thread_count = FF_THREAD_SLICE | FF_THREAD_FRAME;
  749. #ifdef DEBUG_EXTRA
  750. LOG("Before Open2");
  751. #endif
  752. if (avcodec_open2(m_pVideoState->m_pCodecCtx, m_pVideoState->m_pVideoCodec, NULL) < 0)
  753. {
  754. sprintf(Buffer, "Could not open Video codec[%d] for %s", m_pVideoState->m_pCodecCtx->codec_id, URI);
  755. LOG_ERROR(Buffer);
  756. return false;
  757. };
  758. //ffmpeg 6 removes AV_CODEC_CAP_TRUNCATED and AV_CODEC_FLAG_TRUNCATED
  759. #if (LIBAVCODEC_VERSION_MAJOR >= 60 )
  760. #else
  761. /* we do not send complete frames */
  762. if (m_pVideoState->m_pVideoCodec->capabilities & AV_CODEC_CAP_TRUNCATED)
  763. {
  764. m_pVideoState->m_pCodecCtx->flags |= AV_CODEC_CAP_TRUNCATED;
  765. }
  766. #endif // LIBAVCODEC_VERSION_MAJOR 60
  767. #ifndef _NOAUDIO_
  768. m_pVideoState->m_pAudioCtx = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iAudioStreamID]->codec;
  769. m_pVideoState->m_pAudioCodec = avcodec_find_decoder(m_pVideoState->m_pAudioCtx->codec_id);
  770. if (! m_pVideoState->m_pAudioCodec)
  771. {
  772. LOG_ERROR("Unsupported Audio codec!");
  773. return false;
  774. };
  775. if (avcodec_open2(m_pVideoState->m_pAudioCtx, m_pVideoState->m_pAudioCodec, NULL) < 0)
  776. {
  777. // SDL_LogError( SDL_LOG_CATEGORY_AUDIO, "Could not open Audio codec[%d].", m_pVideoState->m_pAudioCtx->codec_id );
  778. LOG_ERROR("Could not open Audio codec.");
  779. return false;
  780. };
  781. #endif
  782. #ifdef DEBUG_EXTRA
  783. char tB[256];
  784. sprintf(tB, "Setting Frame [%dx%d]", s_width, s_height);
  785. LOG(tB);
  786. sprintf(tB, "Source Frame [%dx%d]", m_pVideoState->m_pCodecCtx->width, m_pVideoState->m_pCodecCtx->height);
  787. LOG(tB);
  788. #endif
  789. if ((s_width > 0) && (s_height > 0))
  790. {
  791. m_nOutWidth = s_width;
  792. m_nOutHeight = s_height;
  793. }
  794. else
  795. {
  796. if (m_bSizeIsAutomatic)
  797. {
  798. #ifdef DEBUG_EXTRA
  799. LOG("Size is Automatic");
  800. #endif
  801. if ((m_nOutWidth == -1) && (m_nOutHeight == -1))
  802. {
  803. m_nOutWidth = m_pVideoState->m_pCodecCtx->width;
  804. m_nOutHeight = m_pVideoState->m_pCodecCtx->height;
  805. }
  806. else
  807. {
  808. // Proportional
  809. if (m_nOutHeight == -1)
  810. {
  811. float k = ((float) m_pVideoState->m_pCodecCtx->height) / ((float) m_pVideoState->m_pCodecCtx->width);
  812. m_nOutHeight = (int) (((float) m_nOutWidth) * k);
  813. }
  814. else
  815. {
  816. float k = ((float) m_pVideoState->m_pCodecCtx->width) / ((float) m_pVideoState->m_pCodecCtx->height);
  817. m_nOutWidth = (int) (((float) m_nOutHeight) * k);
  818. };
  819. };
  820. }
  821. else
  822. {
  823. #ifdef DEBUG_EXTRA
  824. LOG("Size is Manual");
  825. #endif
  826. if ((width > 0) && (height > 0))
  827. {
  828. m_nOutWidth = width;
  829. m_nOutHeight = height;
  830. }
  831. else
  832. {
  833. m_nOutWidth = m_pVideoState->m_pCodecCtx->width;
  834. m_nOutHeight = m_pVideoState->m_pCodecCtx->height;
  835. };
  836. };
  837. };
  838. #ifdef DEBUG_EXTRA
  839. sprintf(tB, "Using Size [%dx%d]", m_nOutWidth, m_nOutHeight);
  840. LOG(tB);
  841. sprintf(tB, "swsPar: IN: %dx%d [%d] OUT: %dx%d [%d] {%d}", m_pVideoState->m_pCodecCtx->width, m_pVideoState->m_pCodecCtx->height, m_pVideoState->m_pCodecCtx->pix_fmt, m_nOutWidth, m_nOutHeight, AV_PIX_FMT_BGR24, SWS_BICUBIC);
  842. LOG(tB);
  843. #endif
  844. if ( m_pVideoState->m_pCodecCtx->pix_fmt < 0 ) {
  845. LOG_ERROR("Error Detectin Pixel Format of the stream!!");
  846. return false;
  847. };
  848. m_pVideoState->m_pSwsCtx = sws_getContext(m_pVideoState->m_pCodecCtx->width, m_pVideoState->m_pCodecCtx->height, m_pVideoState->m_pCodecCtx->pix_fmt, m_nOutWidth, m_nOutHeight, AV_PIX_FMT_BGR24,
  849. SWS_BICUBIC, // SWS_FAST_BILINEAR,// | SWS_ACCURATE_RND,//SWS_BILINEAR,//SWS_BICUBIC,
  850. NULL, NULL, NULL);
  851. if (m_pVideoState->m_pSwsCtx == NULL)
  852. {
  853. LOG_ERROR("Error Allocating SwsCtx!");
  854. return false;
  855. };
  856. #ifdef DEBUG
  857. LOG("Allocation Done->Start Thread!");
  858. #endif
  859. return init();
  860. };
  861. return false;
  862. }
  863. void videoAVLIBSource::m_fThreadProc(void)
  864. {
  865. //AVPacket * pPacket = NULL;
  866. cv::Mat mRGB(m_nOutHeight, m_nOutWidth, CV_8UC3, cv::Scalar(255, 0, 255));
  867. AVFrame * pFrame = NULL;
  868. AVFrame * pFrameBGR = NULL;
  869. uint8_t * pBGRbuffer = NULL;
  870. int dec_ret;
  871. char tB[2048];
  872. /** @todo Valgrind is reporting several important memory leaks in this method.
  873. *
  874. * The first one has to do with @p av_frame_alloc():
  875. * The next one has to do with @p av_image_alloc():
  876. */
  877. m_bThreadStatus = TH_WORKING;
  878. pthread_setname_np(pthread_self(), "AVLib Source");
  879. //AVPacket Packet;
  880. //av_init_packet(&Packet); // Optional in newer API
  881. AVPacket *Packet = av_packet_alloc();
  882. if (!Packet) {
  883. m_bThreadStatus = TH_ENDED;
  884. LOG_ERROR("Error allocating SRC AVFrame!");
  885. } else {
  886. Packet->data = NULL;
  887. Packet->size = 0;
  888. pFrame = av_frame_alloc();
  889. if (pFrame == NULL) {
  890. m_bThreadStatus = TH_ENDED;
  891. LOG_ERROR("Error allocating SRC AVFrame!");
  892. } else {
  893. pFrameBGR = av_frame_alloc();
  894. if (pFrameBGR == NULL) {
  895. m_bThreadStatus = TH_ENDED;
  896. LOG_ERROR("Error allocating BGR AVFrame!");
  897. } else {
  898. dec_ret = av_image_alloc(pFrameBGR->data, pFrameBGR->linesize, m_nOutWidth, m_nOutHeight, AV_PIX_FMT_BGR24, BUFFER_ALIGN);
  899. sprintf(tB, "Allocated: %d bytes to Frame", dec_ret);
  900. LOG(tB);
  901. if (dec_ret < 0) {
  902. m_bThreadStatus = TH_ENDED;
  903. LOG_ERROR("Error allocating BGR AVFrame Buffer!");
  904. } else {
  905. // pBGRbuffer = (uint8_t *) av_malloc( av_image_get_buffer_size( AV_PIX_FMT_BGR24, m_nWidth , m_nHeight , BUFFER_ALIGN ));
  906. // if ( pBGRbuffer == NULL ) {
  907. // m_bThreadStatus = TH_ENDED;
  908. // LOG_ERROR("Error allocating BGR AVFrame Buffer!");
  909. // } else {
  910. // av_image_fill_arrays ( pFrameBGR->data, pFrameBGR->linesize, pBGRbuffer, AV_PIX_FMT_BGR24, m_nWidth, m_nHeight, BUFFER_ALIGN );
  911. //
  912. // pFrameBGR->width = m_nWidth;
  913. // pFrameBGR->height = m_nHeight;
  914. // m_pVideoState->m_bStatus = MS_ALOCATED;
  915. #ifdef DEBUG
  916. LOG("Video Reader Thread Started.");
  917. #endif
  918. };
  919. };
  920. };
  921. };// Packet allocation
  922. // cv::setBreakOnError( true );
  923. m_eMediaStatus = m_pVideoState->m_bStatus = MS_STOPED;
  924. while (m_pMediaDestination == NULL && m_bThreadStatus == TH_WORKING)
  925. {
  926. SLEEP(20);
  927. #ifdef DEBUG_EXTRA
  928. LOG("Waiting Destination");
  929. #endif
  930. };
  931. m_eMediaStatus = m_pVideoState->m_bStatus = MS_PRESENT;
  932. if (m_bSrcType == AVLIB_SOURCE_IPSTREAM)
  933. {
  934. av_read_play(m_pVideoState->m_pFormatCtx);
  935. };
  936. #ifdef DEBUG
  937. LOG("Presentation Started.");
  938. #endif
  939. while (m_pVideoState->m_bStatus < MS_CLOSING && m_bThreadStatus != TH_ENDED)
  940. {
  941. if (m_pMediaDestination != NULL)
  942. {
  943. dec_ret = av_read_frame(m_pVideoState->m_pFormatCtx, Packet);
  944. if (dec_ret >= 0)
  945. {
  946. if (Packet->stream_index == m_pVideoState->m_iVideoStreamID)
  947. {
  948. m_pVideoState->m_bStatus = MS_PRESENT;
  949. dec_ret = avcodec_send_packet(m_pVideoState->m_pCodecCtx, Packet);
  950. if (dec_ret < 0 && dec_ret != AVERROR(EAGAIN) /* && dec_ret != AVERROR_EOF */)
  951. {
  952. LOG_ERROR("Error sending video package to decoder");
  953. m_eMediaStatus = m_pVideoState->m_bStatus = MS_CLOSING;
  954. m_bThreadStatus = TH_ENDED;
  955. }
  956. else
  957. {
  958. while (dec_ret >= 0 && m_bThreadStatus != TH_ENDED)
  959. {
  960. dec_ret = avcodec_receive_frame(m_pVideoState->m_pCodecCtx, pFrame);
  961. if (dec_ret == AVERROR(EAGAIN) || dec_ret == AVERROR_EOF)
  962. {
  963. break; // Skip to next data
  964. }
  965. else if (dec_ret < 0)
  966. {
  967. LOG_ERROR("Error decoding video frame");
  968. m_eMediaStatus = m_pVideoState->m_bStatus = MS_CLOSING;
  969. m_bThreadStatus = TH_ENDED;
  970. break;
  971. };
  972. sws_scale(m_pVideoState->m_pSwsCtx, (const uint8_t * const *) pFrame->data, pFrame->linesize, 0, m_pVideoState->m_pCodecCtx->height, pFrameBGR->data, pFrameBGR->linesize);
  973. try
  974. {
  975. if (m_pMediaDestination)
  976. {
  977. memcpy(mRGB.data, pFrameBGR->data[0], pFrameBGR->linesize[0] * m_nOutHeight);
  978. if (! m_pMediaDestination->pushFrame(mRGB, m_pOpaque))
  979. {
  980. m_pMediaDestination = NULL;
  981. LOG_ERROR("Error Consume Frame by Media Destination!");
  982. m_eMediaStatus = m_pVideoState->m_bStatus = MS_CLOSING;
  983. m_bThreadStatus = TH_ENDED;
  984. };
  985. };
  986. }
  987. catch (cv::Exception & e)
  988. {
  989. LOG_ERROR(e.msg.c_str());
  990. m_eMediaStatus = m_pVideoState->m_bStatus = MS_CLOSING;
  991. m_pMediaDestination = NULL;
  992. m_bThreadStatus = TH_ENDED;
  993. break;
  994. };
  995. };
  996. };
  997. };
  998. av_packet_unref( Packet );
  999. }
  1000. else if (dec_ret != AVERROR(EAGAIN) && dec_ret != AVERROR_EOF)
  1001. {
  1002. LOG_ERROR("Error reading video package");
  1003. char tBuffer[128];
  1004. av_strerror(dec_ret, tBuffer, 128);
  1005. LOG_ERROR(tBuffer);
  1006. m_eMediaStatus = m_pVideoState->m_bStatus = MS_CLOSING;
  1007. m_bThreadStatus = TH_ENDED;
  1008. }
  1009. else
  1010. {
  1011. if (dec_ret == AVERROR_EOF)
  1012. {
  1013. avformat_close_input(&m_pVideoState->m_pFormatCtx);
  1014. if ((dec_ret = avformat_open_input(&m_pVideoState->m_pFormatCtx, m_pVideoState->m_pFileName, NULL, &m_pVideoState->m_pFormatOpts)) != 0)
  1015. {
  1016. char tBuffer[128];
  1017. av_strerror(dec_ret, tBuffer, 128);
  1018. sprintf(tB, "VideoSource Couldn't open input stream[%s](Code:%d): %s", tBuffer, dec_ret, m_pVideoState->m_pFileName);
  1019. LOG_ERROR(tB);
  1020. m_pVideoState->m_bStatus = MS_CLOSING;
  1021. m_bThreadStatus = TH_ENDED;
  1022. };
  1023. };
  1024. };
  1025. SLEEP(5);
  1026. }
  1027. else
  1028. {
  1029. SLEEP(5);
  1030. }
  1031. };
  1032. #ifdef DEBUG
  1033. LOG("Video Source Ended!");
  1034. #endif
  1035. if ( Packet ) {
  1036. av_packet_free( &Packet );
  1037. };
  1038. if (pFrame != NULL)
  1039. {
  1040. av_frame_free(&pFrame);
  1041. };
  1042. if (pFrameBGR)
  1043. {
  1044. if (pFrameBGR->data[0])
  1045. {
  1046. av_freep(&pFrameBGR->data[0]);
  1047. }
  1048. av_frame_free(&pFrameBGR);
  1049. };
  1050. if (pBGRbuffer != NULL)
  1051. {
  1052. av_free(pBGRbuffer);
  1053. };
  1054. m_bThreadStatus = TH_ENDED;
  1055. m_eMediaStatus = m_pVideoState->m_bStatus = MS_STOPED;
  1056. #ifdef DEBUG
  1057. LOG("Video Structures Cleared!");
  1058. #endif
  1059. }
  1060. /*
  1061. JPEG2000 isn't jpeg. To encode JPEG images, use AV_CODEC_ID_MJPEG. MJPEG stands for "motion JPEG", which is how a sequence of JPEG pictures making up a video stream is typically called.
  1062. int save_frame_as_jpeg( AVCodecContext *pCodecCtx, AVFrame *pFrame, int FrameNo) {
  1063. AVCodec *jpegCodec = avcodec_find_encoder(AV_CODEC_ID_JPEG2000);
  1064. if (!jpegCodec) {
  1065. return -1;
  1066. }
  1067. AVCodecContext *jpegContext = avcodec_alloc_context3(jpegCodec);
  1068. if (!jpegContext) {
  1069. return -1;
  1070. }
  1071. jpegContext->pix_fmt = pCodecCtx->pix_fmt;
  1072. jpegContext->height = pFrame->height;
  1073. jpegContext->width = pFrame->width;
  1074. if (avcodec_open2(jpegContext, jpegCodec, NULL) < 0) {
  1075. return -1;
  1076. }
  1077. FILE *JPEGFile;
  1078. char JPEGFName[256];
  1079. AVPacket packet = {.data = NULL, .size = 0};
  1080. av_init_packet(&packet);
  1081. int gotFrame;
  1082. if (avcodec_encode_video2(jpegContext, &packet, pFrame, &gotFrame) < 0) {
  1083. return -1;
  1084. }
  1085. sprintf(JPEGFName, "dvr-%06d.jpg", FrameNo);
  1086. JPEGFile = fopen(JPEGFName, "wb");
  1087. fwrite(packet.data, 1, packet.size, JPEGFile);
  1088. fclose(JPEGFile);
  1089. av_free_packet(&packet);
  1090. avcodec_close(jpegContext);
  1091. return 0;
  1092. }
  1093. */
  1094. MediaStateStatus videoAVLIBSource::getStatus(void)
  1095. {
  1096. if (m_pVideoState)
  1097. {
  1098. return m_pVideoState->m_bStatus;
  1099. }
  1100. return MS_UNDEFINED;
  1101. }
  1102. bool videoAVLIBSource::isFinished(void)
  1103. {
  1104. if (m_pVideoState)
  1105. {
  1106. if (m_pVideoState->m_bStatus < MS_CLOSING)
  1107. {
  1108. return false;
  1109. }
  1110. }
  1111. return true;
  1112. }
  1113. #undef LOG
  1114. #undef LOG_ERROR
  1115. //#undef DEBUG_EXTRA
  1116. #endif //! LRF_PROXY_ONLY