rtspclientdown.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include "rtspclientdown.h"
  2. #include <iostream>
  3. rtspclientdown::rtspclientdown(std::string strrtspserver)
  4. {
  5. mpthread = new std::thread(&rtspclientdown::threadrtspdown,this,strrtspserver);
  6. }
  7. rtspclientdown::~rtspclientdown()
  8. {
  9. mbthreadrun = false;
  10. mpthread->join();
  11. }
  12. int rtspclientdown::Getretrycount()
  13. {
  14. return mretrycount;
  15. }
  16. void rtspclientdown::threadrtspdown(std::string strrtspserver)
  17. {
  18. AVFormatContext *i_fmt_ctx;
  19. /* should set to NULL so that avformat_open_input() allocate a new one */
  20. i_fmt_ctx = NULL;
  21. AVDictionary *avdic=NULL;
  22. char option_key[]="rtsp_transport";
  23. char option_value[]="tcp";
  24. av_dict_set(&avdic,option_key,option_value,0);
  25. bool bConnected = false;
  26. while(mbthreadrun)
  27. {
  28. if(bConnected == false)
  29. {
  30. i_fmt_ctx = NULL;
  31. if (avformat_open_input(&i_fmt_ctx, strrtspserver.data(), NULL, &avdic)!=0)
  32. {
  33. fprintf(stderr, " = could not open input file\n");
  34. std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  35. mretrycount++;
  36. continue ;
  37. }
  38. if (avformat_find_stream_info(i_fmt_ctx,NULL)<0)
  39. {
  40. fprintf(stderr, " = could not find stream info\n");
  41. std::this_thread::sleep_for(std::chrono::milliseconds(1000));
  42. continue ;
  43. }
  44. bConnected = true;
  45. }
  46. AVPacket i_pkt;
  47. av_init_packet(&i_pkt);
  48. i_pkt.size = 0;
  49. i_pkt.data = NULL;
  50. if (av_read_frame(i_fmt_ctx, &i_pkt) <0 )
  51. {
  52. bConnected = false;
  53. std::cout<<"connect fail. retry."<<std::endl;
  54. continue;
  55. }
  56. iv::h264rawframedata xframe;
  57. xframe.mdatasize = i_pkt.size;
  58. xframe.mpstr_ptr = std::shared_ptr<char>(new char[xframe.mdatasize]);
  59. memcpy(xframe.mpstr_ptr.get(),i_pkt.data,xframe.mdatasize);
  60. mmutexframe.lock();
  61. while(mvectorframe.size()>900)
  62. {
  63. std::cout<<" rtspclientdown::threadrtspdown. erase frame."<<std::endl;
  64. mvectorframe.erase(mvectorframe.begin());
  65. }
  66. mvectorframe.push_back(xframe);
  67. mmutexframe.unlock();
  68. mcv.notify_all();
  69. av_packet_unref(&i_pkt);
  70. }
  71. }
  72. int rtspclientdown::Getrtspframe(iv::h264rawframedata &xframe, int nwaitms)
  73. {
  74. int nrtn = 0;
  75. if(mvectorframe.size()>0)
  76. {
  77. mmutexframe.lock();
  78. if(mvectorframe.size()>0)
  79. {
  80. xframe = mvectorframe[0];
  81. mvectorframe.erase(mvectorframe.begin());
  82. nrtn = 1;
  83. }
  84. else
  85. {
  86. nrtn = 0;
  87. }
  88. mmutexframe.unlock();
  89. return nrtn;
  90. }
  91. if(nwaitms == 0)return nrtn;
  92. std::unique_lock<std::mutex> lk(mmutexcv);
  93. if(mcv.wait_for(lk, std::chrono::milliseconds(nwaitms)) == std::cv_status::timeout)
  94. {
  95. lk.unlock();
  96. }
  97. else
  98. {
  99. lk.unlock();
  100. }
  101. mmutexframe.lock();
  102. if(mvectorframe.size()>0)
  103. {
  104. xframe = mvectorframe[0];
  105. mvectorframe.erase(mvectorframe.begin());
  106. nrtn = 1;
  107. }
  108. else
  109. {
  110. nrtn = 0;
  111. }
  112. mmutexframe.unlock();
  113. return nrtn;
  114. }