stream.hpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * libdatachannel streamer example
  3. * Copyright (c) 2020 Filip Klembara (in2core)
  4. *
  5. * This Source Code Form is subject to the terms of the Mozilla Public
  6. * License, v. 2.0. If a copy of the MPL was not distributed with this
  7. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  8. */
  9. #ifndef stream_hpp
  10. #define stream_hpp
  11. #include "dispatchqueue.hpp"
  12. #include "rtc/rtc.hpp"
  13. class StreamSource {
  14. protected:
  15. public:
  16. virtual void start() = 0;
  17. virtual void stop() = 0;
  18. virtual void loadNextSample() = 0;
  19. virtual uint64_t getSampleTime_us() = 0;
  20. virtual uint64_t getSampleDuration_us() = 0;
  21. virtual rtc::binary getSample() = 0;
  22. };
  23. class Stream: public std::enable_shared_from_this<Stream> {
  24. uint64_t startTime = 0;
  25. std::mutex mutex;
  26. DispatchQueue dispatchQueue = DispatchQueue("StreamQueue");
  27. bool _isRunning = false;
  28. public:
  29. const std::shared_ptr<StreamSource> audio;
  30. const std::shared_ptr<StreamSource> video;
  31. Stream(std::shared_ptr<StreamSource> video, std::shared_ptr<StreamSource> audio);
  32. ~Stream();
  33. enum class StreamSourceType {
  34. Audio,
  35. Video
  36. };
  37. private:
  38. rtc::synchronized_callback<StreamSourceType, uint64_t, rtc::binary> sampleHandler;
  39. std::pair<std::shared_ptr<StreamSource>, StreamSourceType> unsafePrepareForSample();
  40. void sendSample();
  41. public:
  42. void onSample(std::function<void (StreamSourceType, uint64_t, rtc::binary)> handler);
  43. void start();
  44. void stop();
  45. const bool & isRunning = _isRunning;
  46. };
  47. #endif /* stream_hpp */