runner.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #ifndef EDK_SAMPLES_RUNNER_H_
  21. #define EDK_SAMPLES_RUNNER_H_
  22. #include <atomic>
  23. #include <condition_variable>
  24. #include <memory>
  25. #include <mutex>
  26. #include <queue>
  27. #include <string>
  28. #include "cxxutil/log.h"
  29. #include "device/mlu_context.h"
  30. #include "easycodec/easy_decode.h"
  31. #include "video_parser.h"
  32. class StreamRunner {
  33. public:
  34. explicit StreamRunner(const std::string& data_path);
  35. virtual ~StreamRunner();
  36. void Start() { running_.store(true); }
  37. void Stop() {
  38. running_.store(false);
  39. cond_.notify_one();
  40. }
  41. bool RunLoop();
  42. virtual void Process(edk::CnFrame frame) = 0;
  43. void DemuxLoop(const uint32_t repeat_time);
  44. void ReceiveEos() { receive_eos_ = true; }
  45. void ReceiveFrame(const edk::CnFrame& info) {
  46. std::unique_lock<std::mutex> lk(mut_);
  47. frames_.push(info);
  48. cond_.notify_one();
  49. }
  50. bool Running() { return running_.load(); }
  51. protected:
  52. void WaitForRunLoopExit() {
  53. while (in_loop_.load()) {}
  54. }
  55. edk::MluContext env_;
  56. std::unique_ptr<edk::EasyDecode> decode_{nullptr};
  57. private:
  58. StreamRunner() = delete;
  59. class DemuxEventHandle : public IDemuxEventHandle {
  60. public:
  61. explicit DemuxEventHandle(StreamRunner* runner): runner_(runner) {}
  62. bool OnPacket(const edk::CnPacket& packet) override { return runner_->decode_->FeedData(packet, true); }
  63. void OnEos() override { LOGI(SAMPLES) << "capture EOS"; }
  64. void SendEos() {
  65. if (!send_eos_) {
  66. runner_->decode_->FeedEos();
  67. send_eos_ = true;
  68. }
  69. }
  70. bool Running() override {
  71. return runner_->Running();
  72. }
  73. private:
  74. StreamRunner* runner_;
  75. bool send_eos_{false};
  76. } demux_event_handle_;
  77. std::unique_ptr<VideoParser> parser_;
  78. std::queue<edk::CnFrame> frames_;
  79. std::mutex mut_;
  80. std::condition_variable cond_;
  81. std::string data_path_;
  82. std::atomic<bool> receive_eos_{false};
  83. std::atomic<bool> running_{false};
  84. std::atomic<bool> in_loop_{false};
  85. };
  86. #endif // EDK_SAMPLES_RUNNER_H_