rtspclientdown.cpp 3.1 KB

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