stream.hpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * libdatachannel streamer example
  3. * Copyright (c) 2020 Filip Klembara (in2core)
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef stream_hpp
  19. #define stream_hpp
  20. #include "dispatchqueue.hpp"
  21. #include "rtc/rtc.hpp"
  22. class StreamSource {
  23. protected:
  24. uint64_t sampleTime_us = 0;
  25. rtc::binary sample = {};
  26. public:
  27. StreamSource() { }
  28. virtual void start() = 0;
  29. virtual void stop();
  30. virtual void loadNextSample() = 0;
  31. inline uint64_t getSampleTime_us() { return sampleTime_us; }
  32. inline rtc::binary getSample() { return sample; }
  33. ~StreamSource();
  34. };
  35. class Stream: std::enable_shared_from_this<Stream> {
  36. uint64_t startTime = 0;
  37. std::mutex mutex;
  38. DispatchQueue dispatchQueue = DispatchQueue("StreamQueue");
  39. bool _isRunning = false;
  40. public:
  41. const std::shared_ptr<StreamSource> audio;
  42. const std::shared_ptr<StreamSource> video;
  43. Stream(std::shared_ptr<StreamSource> video, std::shared_ptr<StreamSource> audio);
  44. enum class StreamSourceType {
  45. Audio,
  46. Video
  47. };
  48. ~Stream();
  49. private:
  50. rtc::synchronized_callback<StreamSourceType, uint64_t, rtc::binary> sampleHandler;
  51. std::pair<std::shared_ptr<StreamSource>, StreamSourceType> unsafePrepareForSample();
  52. void sendSample();
  53. public:
  54. void onSample(std::function<void (StreamSourceType, uint64_t, rtc::binary)> handler);
  55. void start();
  56. void stop();
  57. const bool & isRunning = _isRunning;
  58. };
  59. #endif /* stream_hpp */