runner.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*************************************************************************
  2. * Copyright (C) [2020] by Cambricon, Inc. All rights reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. #include "runner.h"
  21. #include <chrono>
  22. #include <iostream>
  23. #include <memory>
  24. #include <string>
  25. #include <thread>
  26. #include <utility>
  27. #include <vector>
  28. #include "cxxutil/log.h"
  29. StreamRunner::StreamRunner(const std::string& data_path) : demux_event_handle_(this), data_path_(data_path) {
  30. parser_.reset(new VideoParser(&demux_event_handle_));
  31. if (!parser_->Open(data_path.c_str())) {
  32. THROW_EXCEPTION(edk::Exception::INIT_FAILED, "Open video source failed");
  33. }
  34. // set mlu environment
  35. env_.SetDeviceId(0);
  36. env_.BindDevice();
  37. const VideoInfo& info = parser_->GetVideoInfo();
  38. // create decoder
  39. edk::EasyDecode::Attr attr;
  40. attr.frame_geometry.w = info.width;
  41. attr.frame_geometry.h = info.height;
  42. attr.codec_type = info.codec_type;
  43. // attr.interlaced = info.progressive ? false : true;
  44. attr.pixel_format = edk::PixelFmt::NV21;
  45. attr.dev_id = 0;
  46. attr.frame_callback = std::bind(&StreamRunner::ReceiveFrame, this, std::placeholders::_1);
  47. attr.eos_callback = std::bind(&StreamRunner::ReceiveEos, this);
  48. attr.silent = false;
  49. attr.input_buffer_num = 6;
  50. attr.output_buffer_num = 6;
  51. decode_ = edk::EasyDecode::New(attr);
  52. }
  53. StreamRunner::~StreamRunner() {
  54. Stop();
  55. WaitForRunLoopExit();
  56. }
  57. void StreamRunner::DemuxLoop(const uint32_t repeat_time) {
  58. bool is_rtsp = parser_->IsRtsp();
  59. uint32_t loop_time = 0;
  60. try {
  61. while (Running()) {
  62. // frame rate control, 25 frame per second for local video
  63. int ret = parser_->ParseLoop(is_rtsp ? 0 : 40);
  64. if (ret == -1) {
  65. THROW_EXCEPTION(edk::Exception::UNAVAILABLE, "no video source");
  66. }
  67. if (ret == 1) {
  68. // eos
  69. if (repeat_time > loop_time++) {
  70. parser_->Close();
  71. if (!parser_->Open(data_path_.c_str())) {
  72. THROW_EXCEPTION(edk::Exception::INIT_FAILED, "Open video source failed");
  73. }
  74. std::cout << "Loop..." << std::endl;
  75. continue;
  76. } else {
  77. demux_event_handle_.SendEos();
  78. std::cout << "End Of Stream" << std::endl;
  79. break;
  80. }
  81. }
  82. }
  83. } catch (edk::Exception& e) {
  84. LOGE(SAMPLES) << e.what();
  85. Stop();
  86. }
  87. if (Running()) demux_event_handle_.SendEos();
  88. parser_->Close();
  89. std::this_thread::sleep_for(std::chrono::duration<float, std::milli>(1000));
  90. std::unique_lock<std::mutex> lk(mut_);
  91. if (frames_.empty()) Stop();
  92. }
  93. bool StreamRunner::RunLoop() {
  94. in_loop_.store(true);
  95. try {
  96. while (running_.load()) {
  97. // inference
  98. std::unique_lock<std::mutex> lk(mut_);
  99. if (!cond_.wait_for(lk, std::chrono::milliseconds(100), [this] { return !frames_.empty(); })) {
  100. continue;
  101. }
  102. edk::CnFrame frame = frames_.front();
  103. frames_.pop();
  104. lk.unlock();
  105. Process(std::move(frame));
  106. lk.lock();
  107. if (frames_.size() == 0 && receive_eos_.load()) {
  108. break;
  109. }
  110. lk.unlock();
  111. }
  112. } catch (edk::Exception& err) {
  113. LOGE(SAMPLES) << err.what();
  114. running_.store(false);
  115. in_loop_.store(false);
  116. return false;
  117. }
  118. // uninitialize
  119. running_.store(false);
  120. in_loop_.store(false);
  121. return true;
  122. }