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.

959 lines
37 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
7 years ago
​
​
7 years ago
​
​
​
​
​
​
7 years ago
​
​
​
​
7 years ago
​
​
​
​
​
​
7 years ago
​
​
​
7 years ago
​
​
​
​
​
​
7 years ago
​
​
​
2 years ago
​
2 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
7 years ago
​
7 years ago
​
​
​
2 years ago
​
7 years ago
​
2 years ago
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
7 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
2 years ago
7 years ago
7 years ago
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
7 years ago
7 years ago
​
7 years ago
​
7 years ago
​
​
7 years ago
7 years ago
​
7 years ago
​
​
7 years ago
7 years ago
​
​
7 years ago
7 years ago
​
​
7 years ago
​
7 years ago
​
​
​
​
​
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
7 years ago
​
7 years ago
​
7 years ago
​
7 years ago
​
​
7 years ago
7 years ago
​
​
7 years ago
​
​
​
7 years ago
​
​
​
​
​
​
7 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
7 years ago
7 years ago
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
​
​
​
​
7 years ago
2 years ago
​
7 years ago
2 years ago
4 years ago
7 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
  1. #include "videoAVLIBSource.h"
  2. #include "libavutil/error.h"
  3. #define LOG( X ) m_pLog->Log( "LibAV_SRC", this, X )
  4. #ifdef DEBUG
  5. #define LOG_ERROR( X ) {\
  6. char _tBfrErrMSG2w34[256];\
  7. sprintf( _tBfrErrMSG2w34, "In file %s[%d] in func:%s %s",__FILE__, __LINE__, __func__, X );\
  8. m_pLog->Log( "LIBAV_SRC", this, _LOG_ERROR, _tBfrErrMSG2w34 );\
  9. }
  10. #else
  11. #define LOG_ERROR( X ) m_pLog->Log( "LIBAV_SRC", this, _LOG_ERROR, X );
  12. #endif
  13. static int interrupt_cb( void *ctx ){
  14. basicLogCollection::getInstance()->Log( "LibAV_CB", NULL, "INTERUPT!" );
  15. // do something
  16. return 0;
  17. }
  18. static const AVIOInterruptCB int_cb = { interrupt_cb, NULL };
  19. //https://ffmpeg.org/doxygen/3.4/ffplay_8c_source.html#l02732
  20. videoAVLIBSource::videoAVLIBSource( void *opaque ): basicMediaSource(), basicThread() {
  21. m_pLog = basicLogCollection::getInstance();
  22. m_bStreamingSource = true;
  23. m_bSizeIsAutomatic = false;
  24. m_nOutWidth = m_nInWidth = m_nOutHeight = m_nInHeight = -1;
  25. m_bSrcType = AVLIB_SOURCE_NOTDEF;
  26. if( opaque ) {
  27. m_pOpaque = opaque;
  28. } else {
  29. m_pOpaque = (void *)this;
  30. };
  31. if( !initLibAV() ) return;
  32. m_pVideoState = (VideoState*)av_mallocz(sizeof(VideoState));
  33. if (m_pVideoState == NULL){
  34. LOG_ERROR("VideoState Memory Allocation Error");
  35. return;
  36. };
  37. m_eMediaStatus = m_pVideoState->m_bStatus = MS_UNDEFINED;
  38. m_pVideoState->m_iVideoStreamID = -1;
  39. #ifndef _NOAUDIO_
  40. m_pVideoState->m_iAudioStreamID = -1;
  41. #endif
  42. m_pVideoState->m_bStatus = MS_INIT;
  43. };
  44. videoAVLIBSource::videoAVLIBSource( char *Source, void *opaque ) : basicMediaSource(), basicThread() {
  45. m_pLog = basicLogCollection::getInstance();
  46. m_bStreamingSource = true;
  47. m_bSizeIsAutomatic = false;
  48. m_nOutWidth = m_nInWidth = m_nOutHeight = m_nInHeight = -1;
  49. m_bSrcType = AVLIB_SOURCE_NOTDEF;
  50. if( opaque ) {
  51. m_pOpaque = opaque;
  52. } else {
  53. m_pOpaque = (void *)this;
  54. };
  55. if( !initLibAV() ) return;
  56. m_pVideoState = (VideoState*)av_mallocz(sizeof(VideoState));
  57. if (m_pVideoState == NULL){
  58. LOG_ERROR("VideoState Memory Allocation Error");
  59. return;
  60. };
  61. m_eMediaStatus = m_pVideoState->m_bStatus = MS_UNDEFINED;
  62. m_pVideoState->m_pFormatCtx = avformat_alloc_context();
  63. if (m_pVideoState->m_pFormatCtx == NULL){
  64. LOG_ERROR("Video Format Context Memory Allocation Error");
  65. return;
  66. };
  67. m_pVideoState->m_iVideoStreamID = -1;
  68. #ifndef _NOAUDIO_
  69. m_pVideoState->m_iAudioStreamID = -1;
  70. #endif
  71. m_pVideoState->m_bStatus = MS_INIT;
  72. _open( Source );
  73. };
  74. videoAVLIBSource::~videoAVLIBSource(){
  75. LOG("Destructor");
  76. if ( m_pVideoState != NULL ){
  77. _close();
  78. av_free(m_pVideoState);
  79. m_pVideoState = NULL;
  80. };
  81. };
  82. bool videoAVLIBSource::setOutputSize( int Width, int Height ){
  83. if( m_pVideoState ){
  84. if( m_pVideoState->m_bStatus == MS_INIT ) {
  85. if( Width && Height ) {
  86. m_bSizeIsAutomatic = false;
  87. m_nOutWidth = Width;
  88. m_nOutHeight = Height;
  89. return true;
  90. };
  91. };
  92. };
  93. return false;
  94. };
  95. bool videoAVLIBSource::setProportionalByWidth(int Width){
  96. if( m_pVideoState ){
  97. if( m_pVideoState->m_bStatus == MS_INIT ) {
  98. if( Width > 0 ) {
  99. m_bSizeIsAutomatic = true;
  100. m_nOutWidth = Width;
  101. return true;
  102. };
  103. };
  104. };
  105. return false;
  106. };
  107. bool videoAVLIBSource::setProportionalByHeight(int Height){
  108. if( m_pVideoState ){
  109. if( m_pVideoState->m_bStatus == MS_INIT ) {
  110. if( Height > 0 ) {
  111. m_bSizeIsAutomatic = true;
  112. m_nOutHeight = Height;
  113. return true;
  114. };
  115. };
  116. };
  117. return false;
  118. };
  119. bool videoAVLIBSource::setFrameAspectRatio( FrameAspectRatio AspectRatio ){
  120. return true;
  121. };
  122. bool videoAVLIBSource::_close(void){
  123. LOG("CLOSE");
  124. //set Close Status - to prevent continue
  125. m_eMediaStatus = m_pVideoState->m_bStatus = MS_CLOSED;
  126. if ( m_pVideoState == NULL) return true;
  127. m_pMediaDestination = NULL;//??
  128. //_stop(); //stops the Thread
  129. basicThread::waitThreadEnd( true );
  130. if ( m_pVideoState->m_pFormatOpts ) {
  131. av_dict_free( &m_pVideoState->m_pFormatOpts );
  132. m_pVideoState->m_pFormatOpts = NULL;
  133. };
  134. if ( m_pVideoState->m_pFormatCtx ) {
  135. avformat_close_input( &m_pVideoState->m_pFormatCtx );
  136. m_pVideoState->m_iVideoStreamID = -1;
  137. };
  138. if( m_pVideoState->m_bStatus > MS_INIT ){
  139. #if ( LIBAVCODEC_VERSION_MAJOR < 58 ) // && ( LIBAVCODEC_VERSION_MINOR < 50 )
  140. #ifndef _NOAUDIO_
  141. if (m_pVideoState->m_pAudioCtx != NULL){
  142. avcodec_close(m_pVideoState->m_pAudioCtx);
  143. avcodec_free_context(&m_pVideoState->m_pAudioCtx);
  144. };
  145. #endif
  146. if (m_pVideoState->m_pCodecCtx != NULL){
  147. avcodec_close(m_pVideoState->m_pCodecCtx);
  148. m_pVideoState->m_iVideoStreamID = -1;
  149. };
  150. #else
  151. #ifndef _NOAUDIO_
  152. if (m_pVideoState->m_pAudioCtx != NULL){
  153. avcodec_close(m_pVideoState->m_pAudioCtx);
  154. avcodec_free_context(&m_pVideoState->m_pAudioCtx);
  155. };
  156. #endif
  157. if (m_pVideoState->m_pCodecCtx != NULL){
  158. avcodec_close(m_pVideoState->m_pCodecCtx);
  159. avcodec_free_context(&m_pVideoState->m_pCodecCtx);
  160. };
  161. #endif
  162. m_eMediaStatus = m_pVideoState->m_bStatus = MS_INIT;
  163. };
  164. sws_freeContext( m_pVideoState->m_pSwsCtx );
  165. return true;
  166. };
  167. bool videoAVLIBSource::_continue( void ){
  168. if( m_pVideoState->m_bStatus == MS_STOPED ){
  169. return basicThread::init();
  170. };
  171. return false;
  172. };
  173. bool videoAVLIBSource::_stop(void){
  174. LOG("STOP");
  175. return basicThread::uninit();
  176. };
  177. //https://www.youtube.com/watch?v=Lu3LZmhC_DY
  178. bool videoAVLIBSource::_restart( void ){
  179. if (m_pVideoState == NULL) return false;
  180. if (m_pVideoState->m_bStatus < MS_OPENED) return false;
  181. if (m_pVideoState->m_pFormatCtx == NULL){
  182. LOG_ERROR("Video Format Context Memory Allocation Error");
  183. return false;
  184. };
  185. // Stop Thread
  186. basicThread::uninit();
  187. int RetVal;
  188. switch( m_bSrcType ){
  189. case AVLIB_SOURCE_DDSHOW:{
  190. AVInputFormat *iformat = av_find_input_format("dshow");
  191. if( ( RetVal = avformat_open_input(&m_pVideoState->m_pFormatCtx, m_pVideoState->m_pFileName, iformat, &m_pVideoState->m_pFormatOpts) ) != 0 ){
  192. char tB[1024];
  193. //av_free( iformat );
  194. char tBuffer[1024];
  195. av_strerror( RetVal, tBuffer, 1024 );
  196. sprintf(tB, "VideoSource Couldn't open input device[%s](Code:%d): %s", tBuffer, RetVal, m_pVideoState->m_pFileName );
  197. LOG_ERROR(tB);
  198. m_eMediaStatus = m_pVideoState->m_bStatus = MS_INIT;
  199. return false;
  200. };
  201. break;
  202. };
  203. case AVLIB_SOURCE_V4L2:
  204. case AVLIB_SOURCE_IPSTREAM:{
  205. if( ( RetVal = avformat_open_input(&m_pVideoState->m_pFormatCtx, m_pVideoState->m_pFileName, NULL, &m_pVideoState->m_pFormatOpts) ) != 0 ){
  206. //av_dict_free(&options);
  207. char tB[1024];
  208. char tBuffer[1024];
  209. av_strerror( RetVal, tBuffer, 1024 );
  210. sprintf( tB, "VideoSource Couldn't open input stream[%s](Code:%d): %s", tBuffer, RetVal, m_pVideoState->m_pFileName );
  211. LOG_ERROR( tB );
  212. m_eMediaStatus = m_pVideoState->m_bStatus = MS_INIT;
  213. return false;
  214. };
  215. break;
  216. };
  217. };
  218. #ifdef DEBUG
  219. LOG("DataStream Opened");
  220. #endif
  221. m_eMediaStatus = m_pVideoState->m_bStatus = MS_OPENED;
  222. if( avformat_find_stream_info( m_pVideoState->m_pFormatCtx, NULL )<0 ){
  223. char tB[1024];
  224. sprintf( tB, "Couldn't find stream information: %s", m_pVideoState->m_pFileName );
  225. LOG_ERROR( tB );
  226. return false;
  227. };
  228. #if ( LIBAVCODEC_VERSION_MAJOR < 58 ) // && ( LIBAVCODEC_VERSION_MINOR < 50 )
  229. for( unsigned int i=0; i < m_pVideoState->m_pFormatCtx->nb_streams; i++ ){
  230. //if( m_pVideoState->m_pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ){
  231. if( m_pVideoState->m_pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO ){
  232. LOG( "VIDEO stream detected." );
  233. if( m_pVideoState->m_iVideoStreamID <0 ){
  234. LOG( "VIDEO stream assigned." );
  235. m_pVideoState->m_iVideoStreamID=i;
  236. };
  237. };
  238. # ifndef _NOAUDIO_
  239. if( m_pVideoState->m_pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO ) {
  240. LOG( "AUDIO stream detected." );
  241. if( m_pVideoState->m_iAudioStreamID < 0 ) {
  242. LOG( "AUDIO stream assigned." );
  243. m_pVideoState->m_iAudioStreamID=i;
  244. }
  245. };
  246. # endif
  247. };
  248. if( m_pVideoState->m_iVideoStreamID == -1 ){
  249. LOG_ERROR( "Didn't find a video stream.");
  250. return false;
  251. };
  252. # ifndef _NOAUDIO_
  253. if( m_pVideoState->m_iAudioStreamID == -1 ){
  254. LOG_ERROR("Didn't find a audio stream.");
  255. return false;
  256. };
  257. # endif
  258. // Stream found
  259. m_pVideoState->m_pCodecCtx = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID]->codec;
  260. //m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID]->codecpar->codec_id
  261. m_pVideoState->m_pVideoCodec = avcodec_find_decoder( m_pVideoState->m_pCodecCtx->codec_id );
  262. #else
  263. # ifdef DEBUG_EXTRA
  264. LOG("YABABA-2!");
  265. # endif
  266. /* find the video stream information */
  267. m_pVideoState->m_iVideoStreamID = av_find_best_stream(m_pVideoState->m_pFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, &m_pVideoState->m_pVideoCodec, 0);
  268. if (m_pVideoState->m_iVideoStreamID == -1){
  269. LOG_ERROR("Didn't find a video stream.");
  270. return false;
  271. };
  272. # ifndef _NOAUDIO_
  273. if (m_pVideoState->m_iAudioStreamID == -1){
  274. LOG_ERROR("Didn't find a audio stream.");
  275. return false;
  276. };
  277. # endif
  278. if (!(m_pVideoState->m_pCodecCtx = avcodec_alloc_context3(m_pVideoState->m_pVideoCodec))){
  279. LOG_ERROR("Can not create decoder context!");
  280. return false;
  281. };
  282. m_pVideoState->m_pVideoStr = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID];
  283. if (avcodec_parameters_to_context(m_pVideoState->m_pCodecCtx, m_pVideoState->m_pVideoStr->codecpar) < 0){
  284. LOG_ERROR("Can not set Context parameters!");
  285. return false;
  286. };
  287. #endif
  288. if( m_pVideoState->m_pVideoCodec == NULL ){
  289. char tB[1024];
  290. //sprintf(Buffer, "Codec [%d] not found for %s", m_pVideoState->m_pCodecCtx->codec_id, m_pVideoState->m_pFileName);
  291. sprintf( tB, "Codec [%d] not found for %s", m_pVideoState->m_pCodecCtx->codec_id, m_pVideoState->m_pFileName );
  292. LOG_ERROR( tB );
  293. return false;
  294. };
  295. /*
  296. End-user application may configure the number of threads to utilize (via AVCodecContext::thread_count property set before avcodec_open2())
  297. and threaded mode (AVCodecContext::thread_type set to FF_THREAD_FRAME or FF_THREAD_SLICE).
  298. Thread pool will be managed by FFmpeg itself, although some answers say that it is also possible using application-provided pool.
  299. */
  300. m_pVideoState->m_pCodecCtx->thread_count = 4;
  301. //m_pVideoState->m_pCodecCtx->thread_type = FF_THREAD_FRAME;
  302. //if (is_live)
  303. m_pVideoState->m_pCodecCtx->thread_count = FF_THREAD_SLICE;
  304. //else
  305. //m_pVideoState->m_pCodecCtx->thread_count = FF_THREAD_SLICE | FF_THREAD_FRAME;
  306. #ifdef DEBUG_EXTRA
  307. LOG("Before Open2");
  308. #endif
  309. if( avcodec_open2( m_pVideoState->m_pCodecCtx, m_pVideoState->m_pVideoCodec, NULL ) < 0 ){
  310. char tB[1024];
  311. sprintf( tB, "Could not open Video codec[%d] for %s", m_pVideoState->m_pCodecCtx->codec_id, m_pVideoState->m_pFileName );
  312. LOG_ERROR( tB );
  313. return false;
  314. };
  315. /* we do not send complete frames */
  316. //if( m_pVideoState->m_pVideoCodec->capabilities & AV_CODEC_CAP_TRUNCATED ) m_pVideoState->m_pCodecCtx->flags |= AV_CODEC_CAP_TRUNCATED;
  317. #ifndef _NOAUDIO_
  318. m_pVideoState->m_pAudioCtx = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iAudioStreamID]->codec;
  319. m_pVideoState->m_pAudioCodec = avcodec_find_decoder( m_pVideoState->m_pAudioCtx->codec_id );
  320. if( !m_pVideoState->m_pAudioCodec ){
  321. LOG_ERROR( "Unsupported Audio codec!" );
  322. return false;
  323. };
  324. if( avcodec_open2( m_pVideoState->m_pAudioCtx, m_pVideoState->m_pAudioCodec, NULL ) < 0 ){
  325. //SDL_LogError( SDL_LOG_CATEGORY_AUDIO, "Could not open Audio codec[%d].", m_pVideoState->m_pAudioCtx->codec_id );
  326. LOG_ERROR( "Could not open Audio codec." );
  327. return false;
  328. };
  329. #endif
  330. return basicThread::init();
  331. };
  332. bool videoAVLIBSource::_open( char *URI, bool ShowDeviceMenu, float framerate, int inputPin, int width, int height, int s_width, int s_height ){
  333. if (URI == NULL) return false;
  334. if (m_pVideoState == NULL) return false;
  335. if (m_bThreadStatus > TH_PRE_INIT ) return false;
  336. m_pVideoState->m_pFormatCtx = avformat_alloc_context();
  337. if (m_pVideoState->m_pFormatCtx == NULL){
  338. LOG_ERROR("Video Format Context Memory Allocation Error");
  339. return false;
  340. };
  341. if (m_pVideoState->m_bStatus == MS_INIT) {
  342. char Buffer[1024] = {0};
  343. char tChar[128] = {0};
  344. strncpy( m_pVideoState->m_pFileName, URI, MAX_PATH_LEN );
  345. sprintf( m_pCookie, "%s", MD5String(m_pVideoState->m_pFileName) );
  346. m_eMediaStatus = m_pVideoState->m_bStatus = MS_OPENING;
  347. if( strncmp( m_pVideoState->m_pFileName, "video=", 6 ) == 0 ){
  348. m_bSrcType = AVLIB_SOURCE_DDSHOW;
  349. #ifdef DEBUG
  350. LOG("MS-Video Device");
  351. #endif
  352. //AVDictionary *options = NULL;
  353. if( width != -1 && height != -1 ){
  354. m_bSizeIsAutomatic = false;
  355. char tChar[10];
  356. sprintf(tChar, "%dx%d", width, height);
  357. av_dict_set(&m_pVideoState->m_pFormatOpts, "video_size", tChar, 0);
  358. } else {
  359. //m_bSizeIsAutomatic = true;
  360. };
  361. //av_dict_set(&m_pVideoState->m_pFormatOpts, "pixel_format", "yuyv422", 0 );
  362. av_dict_set(&m_pVideoState->m_pFormatOpts, "video_codec", "mjpeg", 0 );
  363. if( inputPin != -1 ){
  364. sprintf(tChar, "%d", inputPin);
  365. av_dict_set(&m_pVideoState->m_pFormatOpts, "crossbar_video_input_pin_number", tChar, 0);
  366. };
  367. if( framerate != -1 ){
  368. sprintf(tChar, "%f", framerate);
  369. av_dict_set(&m_pVideoState->m_pFormatOpts, "framerate", tChar, 0);
  370. };
  371. av_dict_set(&m_pVideoState->m_pFormatOpts, "rtbufsize", "500000k", 0);
  372. if( ShowDeviceMenu ){
  373. av_dict_set(&m_pVideoState->m_pFormatOpts, "show_video_device_dialog", "true", 0);
  374. av_dict_set(&m_pVideoState->m_pFormatOpts, "show_video_crossbar_connection_dialog", "true", 0);
  375. //sprintf(Buffer, "data/%s.settings", m_pCookie);
  376. //av_dict_set(&options, "video_device_save", Buffer, 0);
  377. #ifndef _NOAUDIO_
  378. av_dict_set(&m_pVideoState->m_pFormatOpts, "show_audio_crossbar_connection_dialog", "true", 0);
  379. sprintf(Buffer, "%s-a.settings", URI);
  380. av_dict_set(&m_pVideoState->m_pFormatOpts, "audio_device_save", Buffer, 0);
  381. #endif
  382. } else {
  383. sprintf(Buffer, "data/%s.settings", m_pCookie );
  384. //if (!fileExist(Buffer)) av_dict_set(&m_pVideoState->m_pFormatOpts, "video_device_load", Buffer, 0);
  385. };
  386. //av_dict_set(&m_pVideoState->m_pFormatOpts, "crossbar_video_input_pin_number", "1", 0);
  387. //av_dict_set(&m_pVideoState->m_pFormatOpts, "show_audio_crossbar_connection_dialog", "true", 0);
  388. //av_dict_set(&m_pVideoState->m_pFormatOpts, "show_video_crossbar_connection_dialog", "true", 0);
  389. //AVInputFormat * is Internal Const in CoDec Lib
  390. AVInputFormat *iformat = av_find_input_format("dshow");
  391. //av_dict_set(&options, "pixel_format", "bgr24", 0);
  392. av_dict_set(&m_pVideoState->m_pFormatOpts, "timeout", "5000", 0);
  393. //AVDeviceInfoList *device_list;
  394. //m_pVideoState->m_pFormatCtx->iformat = iformat;
  395. //avdevice_list_devices(m_pVideoState->m_pFormatCtx, &device_list);
  396. //avdevice_free_list_devices(&device_list);
  397. if( avformat_open_input(&m_pVideoState->m_pFormatCtx, m_pVideoState->m_pFileName, iformat, &m_pVideoState->m_pFormatOpts) != 0 ){
  398. av_dict_free( &m_pVideoState->m_pFormatOpts );
  399. m_pVideoState->m_pFormatOpts = NULL;
  400. sprintf(Buffer, "VideoSource Couldn't open input device: %s", URI);
  401. LOG_ERROR(Buffer);
  402. m_eMediaStatus = m_pVideoState->m_bStatus = MS_INIT;
  403. return false;
  404. };
  405. /*AVDictionaryEntry *e;
  406. if (e = av_dict_get(options, "", NULL, AV_DICT_IGNORE_SUFFIX)) {
  407. sprintf(Buffer, "Option %s not recognized by the demuxer", e->key);
  408. LOG_ERROR(Buffer);
  409. };*/
  410. //if (iformat!=NULL)
  411. //if (iformat->priv_class != NULL){
  412. // const AVOption *cOption = NULL;
  413. // while((cOption = av_opt_next(&iformat->priv_class, cOption))!= NULL )
  414. // serializeOption(cOption);
  415. //};
  416. //av_dict_free(&options);
  417. //AVOption *mpOption = NULL;
  418. } else {
  419. if( strncmp( URI, "/dev/video", 10 ) == 0 ) {
  420. m_bSrcType = AVLIB_SOURCE_V4L2;
  421. } else {
  422. m_bSrcType = AVLIB_SOURCE_IPSTREAM;
  423. };
  424. //AVDictionary *options = NULL;
  425. //av_dict_set(&m_pVideoState->m_pFormatOpts, "movflags", "faststart", 0);
  426. if( m_bSrcType == AVLIB_SOURCE_IPSTREAM ) {
  427. #ifdef DEBUG
  428. LOG("IP Stream Source");
  429. #endif
  430. av_dict_set(&m_pVideoState->m_pFormatOpts, "rtsp_transport", "tcp", 0);
  431. av_dict_set(&m_pVideoState->m_pFormatOpts, "stimeout", "5000000", 0);
  432. av_dict_set(&m_pVideoState->m_pFormatOpts, "buffer_size", "4096000", 0);
  433. /*
  434. av_dict_set_int(&opts, "rw_timeout", 5000000, 0);
  435. av_dict_set_int(&opts, "tcp_nodelay", 1, 0);
  436. av_dict_set_int(&opts, "stimeout", 10000000, 0);
  437. av_dict_set(&opts, "user_agent", "Mozilla/5.0", 0);
  438. av_dict_set(&opts, "rtsp_transport", "tcp", 0);
  439. av_dict_set(&opts, "rtsp_flags", "prefer_tcp", 0);
  440. av_dict_set_int(&opts, "buffer_size", BUFSIZE, 0);
  441. pFormatCtx->flags |= AVFMT_FLAG_NONBLOCK;
  442. pFormatCtx->flags |= AVFMT_FLAG_DISCARD_CORRUPT;
  443. pFormatCtx->flags |= AVFMT_FLAG_NOBUFFER;
  444. */
  445. } else {
  446. #ifdef DEBUG
  447. LOG("Linux Video Device");
  448. #endif
  449. if( width != -1 && height != -1 ){
  450. m_bSizeIsAutomatic = false;
  451. sprintf(tChar, "%dx%d", width, height);
  452. av_dict_set(&m_pVideoState->m_pFormatOpts, "video_size", tChar, 0);
  453. switch ( inputPin ) {
  454. case 0: LOG("PIX YUYV"); av_dict_set(&m_pVideoState->m_pFormatOpts, "pixel_format", "yuv420p", 0); break;
  455. case 1: LOG("PIX MJPG"); av_dict_set(&m_pVideoState->m_pFormatOpts, "pixel_format", "mjpeg", 0); break;
  456. default: av_dict_set(&m_pVideoState->m_pFormatOpts, "pixel_format", "rgb24", 0); break;
  457. };
  458. } else {
  459. //m_bSizeIsAutomatic = true;
  460. };
  461. av_dict_set(&m_pVideoState->m_pFormatOpts, "fflags", "nobuffer", 0);
  462. };
  463. av_dict_set(&m_pVideoState->m_pFormatOpts, "max_delay", "100", 0);
  464. //av_dict_set(&options, "analyzeduration", "1000000", 0);
  465. if( framerate != -1 ){
  466. sprintf(tChar, "%0.2f", framerate);
  467. //sprintf(tChar, "120" );
  468. av_dict_set(&m_pVideoState->m_pFormatOpts, "framerate", tChar, 0);
  469. //av_dict_set(&options, "frame_rate", tChar, 0);
  470. };
  471. //av_dict_set(&options, "timeout", "8000", 0);
  472. int RetVal = 0;
  473. //LOG("Before Open1");
  474. if( ( RetVal = avformat_open_input(&m_pVideoState->m_pFormatCtx, m_pVideoState->m_pFileName, NULL, &m_pVideoState->m_pFormatOpts) ) != 0 ){
  475. av_dict_free( &m_pVideoState->m_pFormatOpts);
  476. m_pVideoState->m_pFormatOpts = NULL;
  477. char tBuffer[1024];
  478. av_strerror( RetVal, tBuffer, 1024 );
  479. sprintf(Buffer, "VideoSource Couldn't open input stream[%s](Code:%d): %s", tBuffer, RetVal, m_pVideoState->m_pFileName );
  480. LOG_ERROR(Buffer);
  481. return false;
  482. };
  483. //LOG("After Open1");
  484. AVDictionaryEntry *e;
  485. if( e = av_dict_get( m_pVideoState->m_pFormatOpts, "", NULL, AV_DICT_IGNORE_SUFFIX )){
  486. sprintf(Buffer, "Option %s not recognized by the demuxer", e->key);
  487. LOG_ERROR(Buffer);
  488. };
  489. // e is pointer to entry!
  490. //av_dict_free( &e );
  491. };
  492. #ifdef DEBUG
  493. LOG("DataStream Opened");
  494. #endif
  495. m_eMediaStatus = m_pVideoState->m_bStatus = MS_OPENED;
  496. if( avformat_find_stream_info( m_pVideoState->m_pFormatCtx, NULL )<0 ){
  497. sprintf(Buffer, "Couldn't find stream information: %s", URI);
  498. LOG_ERROR(Buffer);
  499. return false;
  500. };
  501. #if ( LIBAVCODEC_VERSION_MAJOR < 58 ) // && ( LIBAVCODEC_VERSION_MINOR < 50 )
  502. for( unsigned int i=0; i < m_pVideoState->m_pFormatCtx->nb_streams; i++ ){
  503. //if( m_pVideoState->m_pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ){
  504. if( m_pVideoState->m_pFormatCtx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO ){
  505. LOG( "VIDEO stream detected." );
  506. if( m_pVideoState->m_iVideoStreamID <0 ){
  507. LOG( "VIDEO stream assigned." );
  508. m_pVideoState->m_iVideoStreamID=i;
  509. };
  510. };
  511. # ifndef _NOAUDIO_
  512. if( m_pVideoState->m_pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO ) {
  513. LOG( "AUDIO stream detected." );
  514. if( m_pVideoState->m_iAudioStreamID < 0 ) {
  515. LOG( "AUDIO stream assigned." );
  516. m_pVideoState->m_iAudioStreamID=i;
  517. }
  518. };
  519. # endif
  520. };
  521. if( m_pVideoState->m_iVideoStreamID == -1 ){
  522. LOG_ERROR( "Didn't find a video stream.");
  523. return false;
  524. };
  525. // Video Stream found
  526. m_pVideoState->m_pCodecCtx = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID]->codec;
  527. // Static pointer
  528. m_pVideoState->m_pVideoCodec = avcodec_find_decoder( m_pVideoState->m_pCodecCtx->codec_id );
  529. # ifndef _NOAUDIO_
  530. if( m_pVideoState->m_iAudioStreamID == -1 ){
  531. LOG_ERROR("Didn't find a audio stream.");
  532. return false;
  533. };
  534. //ToDo: Bind Audio Codec
  535. # endif
  536. #else
  537. # ifdef DEBUG_EXTRA
  538. LOG("YABABA-2!");
  539. # endif
  540. /* find the video stream information */
  541. m_pVideoState->m_iVideoStreamID = av_find_best_stream(m_pVideoState->m_pFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, &m_pVideoState->m_pVideoCodec, 0);
  542. if (m_pVideoState->m_iVideoStreamID == -1){
  543. LOG_ERROR("Didn't find a video stream.");
  544. return false;
  545. };
  546. # ifndef _NOAUDIO_
  547. if (m_pVideoState->m_iAudioStreamID == -1){
  548. LOG_ERROR("Didn't find a audio stream.");
  549. return false;
  550. };
  551. # endif
  552. if (!(m_pVideoState->m_pCodecCtx = avcodec_alloc_context3(m_pVideoState->m_pVideoCodec))){
  553. LOG_ERROR("Can not create decoder context!");
  554. return false;
  555. };
  556. m_pVideoState->m_pVideoStr = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iVideoStreamID];
  557. if (avcodec_parameters_to_context(m_pVideoState->m_pCodecCtx, m_pVideoState->m_pVideoStr->codecpar) < 0){
  558. LOG_ERROR("Can not set Context parameters!");
  559. return false;
  560. };
  561. #endif
  562. if( m_pVideoState->m_pVideoCodec == NULL ){
  563. //sprintf(Buffer, "Codec [%d] not found for %s", m_pVideoState->m_pCodecCtx->codec_id, URI);
  564. sprintf(Buffer, "Codec [%d] not found for %s", m_pVideoState->m_pCodecCtx->codec_id, URI);
  565. LOG_ERROR( Buffer );
  566. return false;
  567. };
  568. /*
  569. End-user application may configure the number of threads to utilize (via AVCodecContext::thread_count property set before avcodec_open2())
  570. and threaded mode (AVCodecContext::thread_type set to FF_THREAD_FRAME or FF_THREAD_SLICE).
  571. Thread pool will be managed by FFmpeg itself, although some answers say that it is also possible using application-provided pool.
  572. */
  573. m_pVideoState->m_pCodecCtx->thread_count = 4;
  574. //m_pVideoState->m_pCodecCtx->thread_type = FF_THREAD_FRAME;
  575. //if (is_live)
  576. m_pVideoState->m_pCodecCtx->thread_count = FF_THREAD_SLICE;
  577. //else
  578. //m_pVideoState->m_pCodecCtx->thread_count = FF_THREAD_SLICE | FF_THREAD_FRAME;
  579. #ifdef DEBUG_EXTRA
  580. LOG("Before Open2");
  581. #endif
  582. if( avcodec_open2( m_pVideoState->m_pCodecCtx, m_pVideoState->m_pVideoCodec, NULL ) < 0 ){
  583. sprintf(Buffer, "Could not open Video codec[%d] for %s", m_pVideoState->m_pCodecCtx->codec_id, URI );
  584. LOG_ERROR( Buffer );
  585. return false;
  586. };
  587. /* we do not send complete frames */
  588. if( m_pVideoState->m_pVideoCodec->capabilities & AV_CODEC_CAP_TRUNCATED ) m_pVideoState->m_pCodecCtx->flags |= AV_CODEC_CAP_TRUNCATED;
  589. #ifndef _NOAUDIO_
  590. m_pVideoState->m_pAudioCtx = m_pVideoState->m_pFormatCtx->streams[m_pVideoState->m_iAudioStreamID]->codec;
  591. m_pVideoState->m_pAudioCodec = avcodec_find_decoder( m_pVideoState->m_pAudioCtx->codec_id );
  592. if( !m_pVideoState->m_pAudioCodec ){
  593. LOG_ERROR( "Unsupported Audio codec!" );
  594. return false;
  595. };
  596. if( avcodec_open2( m_pVideoState->m_pAudioCtx, m_pVideoState->m_pAudioCodec, NULL ) < 0 ){
  597. //SDL_LogError( SDL_LOG_CATEGORY_AUDIO, "Could not open Audio codec[%d].", m_pVideoState->m_pAudioCtx->codec_id );
  598. LOG_ERROR( "Could not open Audio codec." );
  599. return false;
  600. };
  601. #endif
  602. #ifdef DEBUG_EXTRA
  603. char tB[256];
  604. sprintf( tB, "Setting Frame [%dx%d]", s_width, s_height );
  605. LOG( tB );
  606. sprintf( tB, "Source Frame [%dx%d]", m_pVideoState->m_pCodecCtx->width, m_pVideoState->m_pCodecCtx->height );
  607. LOG( tB );
  608. #endif
  609. if( ( s_width > 0 ) && ( s_height > 0 ) ){
  610. m_nOutWidth = s_width;
  611. m_nOutHeight = s_height;
  612. } else {
  613. if( m_bSizeIsAutomatic ){
  614. #ifdef DEBUG_EXTRA
  615. LOG("Size is Automatic");
  616. #endif
  617. if(( m_nOutWidth == -1 )&&( m_nOutHeight == -1 )){
  618. m_nOutWidth = m_pVideoState->m_pCodecCtx->width;
  619. m_nOutHeight = m_pVideoState->m_pCodecCtx->height;
  620. } else {
  621. // Proportional
  622. if( m_nOutHeight == -1 ){
  623. float k = ((float)m_pVideoState->m_pCodecCtx->height) / ((float)m_pVideoState->m_pCodecCtx->width);
  624. m_nOutHeight = (int)(((float)m_nOutWidth ) * k );
  625. } else {
  626. float k = ((float)m_pVideoState->m_pCodecCtx->width) / ((float)m_pVideoState->m_pCodecCtx->height);
  627. m_nOutWidth = (int)(((float)m_nOutHeight ) * k );
  628. };
  629. };
  630. } else {
  631. #ifdef DEBUG_EXTRA
  632. LOG("Size is Manual");
  633. #endif
  634. if( ( width > 0 ) && ( height > 0 ) ){
  635. m_nOutWidth = width;
  636. m_nOutHeight = height;
  637. } else {
  638. m_nOutWidth = m_pVideoState->m_pCodecCtx->width;
  639. m_nOutHeight = m_pVideoState->m_pCodecCtx->height;
  640. };
  641. };
  642. };
  643. #ifdef DEBUG_EXTRA
  644. sprintf( tB, "Using Size [%dx%d]", m_nOutWidth, m_nOutHeight );
  645. LOG( tB );
  646. sprintf(tB,"swsPar: IN: %dx%d [%d] OUT: %dx%d [%d] {%d}", m_pVideoState->m_pCodecCtx->width,
  647. m_pVideoState->m_pCodecCtx->height,
  648. m_pVideoState->m_pCodecCtx->pix_fmt,
  649. m_nOutWidth,
  650. m_nOutHeight,
  651. AV_PIX_FMT_BGR24,
  652. SWS_BICUBIC );
  653. LOG( tB );
  654. #endif
  655. m_pVideoState->m_pSwsCtx = sws_getContext(
  656. m_pVideoState->m_pCodecCtx->width,
  657. m_pVideoState->m_pCodecCtx->height,
  658. m_pVideoState->m_pCodecCtx->pix_fmt,
  659. m_nOutWidth,
  660. m_nOutHeight,
  661. AV_PIX_FMT_BGR24,
  662. SWS_BICUBIC, //SWS_FAST_BILINEAR,// | SWS_ACCURATE_RND,//SWS_BILINEAR,//SWS_BICUBIC,
  663. NULL,
  664. NULL,
  665. NULL
  666. );
  667. if ( m_pVideoState->m_pSwsCtx == NULL ){
  668. LOG_ERROR("Error Allocating SwsCtx!");
  669. return false;
  670. };
  671. #ifdef DEBUG
  672. LOG("Allocation Done->Start Thread!");
  673. #endif
  674. return init();
  675. };
  676. return false;
  677. };
  678. void videoAVLIBSource::m_fThreadProc( void ){
  679. AVPacket * pPacket = NULL;
  680. cv::Mat mRGB( m_nOutHeight, m_nOutWidth, CV_8UC3, cv::Scalar( 255, 0, 255 ) );
  681. AVFrame * pFrame = NULL;
  682. AVFrame * pFrameBGR = NULL;
  683. uint8_t * pBGRbuffer = NULL;
  684. int dec_ret;
  685. char tB[256];
  686. m_bThreadStatus = TH_WORKING;
  687. pPacket = (AVPacket *) av_malloc( sizeof(AVPacket) );
  688. if ( pPacket == NULL ) {
  689. m_bThreadStatus = TH_ENDED;
  690. LOG_ERROR("Error allocating AVPacket!");
  691. } else {
  692. av_init_packet( pPacket );
  693. pFrame = av_frame_alloc();
  694. if ( pFrame == NULL ) {
  695. m_bThreadStatus = TH_ENDED;
  696. LOG_ERROR("Error allocating SRC AVFrame!");
  697. } else {
  698. pFrameBGR = av_frame_alloc();
  699. if ( pFrameBGR == NULL ) {
  700. m_bThreadStatus = TH_ENDED;
  701. LOG_ERROR("Error allocating BGR AVFrame!");
  702. } else {
  703. dec_ret = av_image_alloc ( pFrameBGR->data, pFrameBGR->linesize, m_nOutWidth, m_nOutHeight, AV_PIX_FMT_BGR24, BUFFER_ALIGN );
  704. sprintf( tB, "Allocated: %d bytes to Frame", dec_ret );
  705. LOG( tB );
  706. if ( dec_ret < 0 ) {
  707. m_bThreadStatus = TH_ENDED;
  708. LOG_ERROR("Error allocating BGR AVFrame Buffer!");
  709. } else {
  710. //pBGRbuffer = (uint8_t *) av_malloc( av_image_get_buffer_size( AV_PIX_FMT_BGR24, m_nWidth , m_nHeight , BUFFER_ALIGN ));
  711. //if ( pBGRbuffer == NULL ) {
  712. // m_bThreadStatus = TH_ENDED;
  713. // LOG_ERROR("Error allocating BGR AVFrame Buffer!");
  714. //} else {
  715. // av_image_fill_arrays ( pFrameBGR->data, pFrameBGR->linesize, pBGRbuffer, AV_PIX_FMT_BGR24, m_nWidth, m_nHeight, BUFFER_ALIGN );
  716. //
  717. // pFrameBGR->width = m_nWidth;
  718. // pFrameBGR->height = m_nHeight;
  719. // m_pVideoState->m_bStatus = MS_ALOCATED;
  720. #ifdef DEBUG
  721. LOG("Video Reader Thread Started.");
  722. #endif
  723. };
  724. };
  725. };
  726. };
  727. //cv::setBreakOnError( true );
  728. m_eMediaStatus = m_pVideoState->m_bStatus = MS_STOPED;
  729. while( m_pMediaDestination == NULL && m_bThreadStatus == TH_WORKING ){
  730. SLEEP(20);
  731. #ifdef DEBUG_EXTRA
  732. LOG("Waiting Destination");
  733. #endif
  734. };
  735. m_eMediaStatus = m_pVideoState->m_bStatus = MS_PRESENT;
  736. if( m_bSrcType == AVLIB_SOURCE_IPSTREAM ){
  737. av_read_play( m_pVideoState->m_pFormatCtx );
  738. };
  739. #ifdef DEBUG
  740. LOG("Presentation Started.");
  741. #endif
  742. while( m_pVideoState->m_bStatus < MS_CLOSING && m_bThreadStatus != TH_ENDED ){
  743. if ( m_pMediaDestination != NULL ){
  744. dec_ret = av_read_frame( m_pVideoState->m_pFormatCtx, pPacket );
  745. if( dec_ret >= 0 ){
  746. if( pPacket->stream_index == m_pVideoState->m_iVideoStreamID ){
  747. m_pVideoState->m_bStatus = MS_PRESENT;
  748. dec_ret = avcodec_send_packet( m_pVideoState->m_pCodecCtx, pPacket );
  749. if( dec_ret < 0 && dec_ret != AVERROR(EAGAIN) /* && dec_ret != AVERROR_EOF */ ){
  750. LOG_ERROR( "Error sending video package to decoder" );
  751. m_eMediaStatus = m_pVideoState->m_bStatus = MS_CLOSING;
  752. m_bThreadStatus = TH_ENDED;
  753. } else {
  754. while( dec_ret >= 0 && m_bThreadStatus != TH_ENDED ) {
  755. dec_ret = avcodec_receive_frame( m_pVideoState->m_pCodecCtx, pFrame );
  756. if( dec_ret == AVERROR(EAGAIN) || dec_ret == AVERROR_EOF ){
  757. break;//Skip to next data
  758. } else if( dec_ret < 0 ){
  759. LOG_ERROR("Error decoding video frame");
  760. m_eMediaStatus = m_pVideoState->m_bStatus = MS_CLOSING;
  761. m_bThreadStatus = TH_ENDED;
  762. break;
  763. };
  764. sws_scale( m_pVideoState->m_pSwsCtx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, m_pVideoState->m_pCodecCtx->height, pFrameBGR->data, pFrameBGR->linesize );
  765. try{
  766. if ( m_pMediaDestination ) {
  767. memcpy( mRGB.data, pFrameBGR->data[0], pFrameBGR->linesize[0] * m_nOutHeight );
  768. if (!m_pMediaDestination->pushFrame(mRGB, m_pOpaque)) {
  769. m_pMediaDestination = NULL;
  770. LOG_ERROR("Error Consume Frame by Media Destination!");
  771. m_eMediaStatus = m_pVideoState->m_bStatus = MS_CLOSING;
  772. m_bThreadStatus = TH_ENDED;
  773. };
  774. };
  775. } catch (cv::Exception & e) {
  776. LOG_ERROR( e.msg.c_str() );
  777. m_eMediaStatus = m_pVideoState->m_bStatus = MS_CLOSING;
  778. m_pMediaDestination = NULL;
  779. m_bThreadStatus = TH_ENDED;
  780. break;
  781. };
  782. };
  783. };
  784. };
  785. } else if( dec_ret != AVERROR(EAGAIN) && dec_ret != AVERROR_EOF ){
  786. LOG_ERROR( "Error reading video package" );
  787. char tBuffer[1024];
  788. av_strerror( dec_ret, tBuffer, 1024 );
  789. LOG_ERROR( tBuffer );
  790. m_eMediaStatus = m_pVideoState->m_bStatus = MS_CLOSING;
  791. m_bThreadStatus = TH_ENDED;
  792. } else {
  793. if( dec_ret == AVERROR_EOF ){
  794. avformat_close_input( &m_pVideoState->m_pFormatCtx );
  795. if( ( dec_ret = avformat_open_input(&m_pVideoState->m_pFormatCtx, m_pVideoState->m_pFileName, NULL, &m_pVideoState->m_pFormatOpts) ) != 0 ){
  796. char tBuffer[1024];
  797. av_strerror( dec_ret, tBuffer, 1024 );
  798. sprintf( tB, "VideoSource Couldn't open input stream[%s](Code:%d): %s", tBuffer, dec_ret, m_pVideoState->m_pFileName );
  799. LOG_ERROR( tB );
  800. m_pVideoState->m_bStatus = MS_CLOSING;
  801. m_bThreadStatus = TH_ENDED;
  802. };
  803. };
  804. //m_pVideoState->m_bStatus = MS_STOPED;
  805. SLEEP( 5 );
  806. };
  807. av_packet_unref( pPacket );
  808. SLEEP( 5 );
  809. } else SLEEP( 5 );
  810. };
  811. #ifdef DEBUG
  812. LOG("Video Source Ended!");
  813. #endif
  814. if ( pFrame != NULL ) {
  815. av_frame_free( &pFrame );
  816. };
  817. if ( pPacket != NULL ) {
  818. av_free( pPacket );
  819. };
  820. if( pBGRbuffer != NULL ) {
  821. av_free( pBGRbuffer );
  822. };
  823. m_bThreadStatus = TH_ENDED;
  824. m_eMediaStatus = m_pVideoState->m_bStatus = MS_STOPED;
  825. #ifdef DEBUG
  826. LOG("Video Structures Cleared!");
  827. #endif
  828. };
  829. /*
  830. 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.
  831. int save_frame_as_jpeg( AVCodecContext *pCodecCtx, AVFrame *pFrame, int FrameNo) {
  832. AVCodec *jpegCodec = avcodec_find_encoder(AV_CODEC_ID_JPEG2000);
  833. if (!jpegCodec) {
  834. return -1;
  835. }
  836. AVCodecContext *jpegContext = avcodec_alloc_context3(jpegCodec);
  837. if (!jpegContext) {
  838. return -1;
  839. }
  840. jpegContext->pix_fmt = pCodecCtx->pix_fmt;
  841. jpegContext->height = pFrame->height;
  842. jpegContext->width = pFrame->width;
  843. if (avcodec_open2(jpegContext, jpegCodec, NULL) < 0) {
  844. return -1;
  845. }
  846. FILE *JPEGFile;
  847. char JPEGFName[256];
  848. AVPacket packet = {.data = NULL, .size = 0};
  849. av_init_packet(&packet);
  850. int gotFrame;
  851. if (avcodec_encode_video2(jpegContext, &packet, pFrame, &gotFrame) < 0) {
  852. return -1;
  853. }
  854. sprintf(JPEGFName, "dvr-%06d.jpg", FrameNo);
  855. JPEGFile = fopen(JPEGFName, "wb");
  856. fwrite(packet.data, 1, packet.size, JPEGFile);
  857. fclose(JPEGFile);
  858. av_free_packet(&packet);
  859. avcodec_close(jpegContext);
  860. return 0;
  861. }
  862. */
  863. MediaStateStatus videoAVLIBSource::getStatus( void ){
  864. if( m_pVideoState ) return m_pVideoState->m_bStatus;
  865. return MS_UNDEFINED;
  866. };
  867. bool videoAVLIBSource::isFinished( void ){
  868. if( m_pVideoState ) if( m_pVideoState->m_bStatus < MS_CLOSING ) return false;
  869. return true;
  870. };
  871. #undef LOG
  872. #undef LOG_ERROR