You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. MediaMtx (C++) - WebRTC (WHEP) reader session (MediaMTX: internal/servers/webrtc/session.go)
  3. The browser POSTs its SDP offer (recvonly video); the answer contains one H264 send-only track and
  4. all local ICE candidates (the la_mojo dashboard does not trickle). The camera's H264 is sent as it is
  5. (no transcoding), starting at the next IDR.
  6. */
  7. #pragma once
  8. #include <atomic>
  9. #include <condition_variable>
  10. #include <memory>
  11. #include <mutex>
  12. #include <string>
  13. #include <rtc/rtc.hpp>
  14. #include "../../conf/serverConf.h"
  15. #include "../../core/mediaPath.h"
  16. #include "../../logger/logger.h"
  17. #include "../../stream/stream.h"
  18. namespace mtx {
  19. class WebrtcSession : public PathReader, public LogParent
  20. {
  21. public:
  22. WebrtcSession(const std::string & secret, std::shared_ptr<MediaPath> path, const ServerConf & global, const std::string & remoteAddr);
  23. ~WebrtcSession() override;
  24. // Returns the answer SDP; on failure false + http status + error
  25. bool create(const std::string & offer, std::string & answer, int & status, std::string & err);
  26. bool addRemoteCandidates(const std::string & sdpFrag, std::string & err);
  27. void close();
  28. // The server removes sessions that are closing, failed or never connected
  29. bool isClosing() const { return m_bClosing; }
  30. bool connectTimedOut(int64_t nowMsValue) const { return ! m_bConnected && nowMsValue - m_tCreated > 20000; }
  31. const std::string & secret() const { return m_sSecret; }
  32. const std::string & pathName() const { return m_pPath->name(); }
  33. // PathReader
  34. void push(const UnitPtr & unit) override { m_reader.push(unit); }
  35. void pathNotReady() override;
  36. private:
  37. void onUnit(const UnitPtr & unit);
  38. std::string m_sSecret;
  39. std::shared_ptr<MediaPath> m_pPath;
  40. ServerConf m_global;
  41. std::string m_sRemote;
  42. AsyncReader m_reader;
  43. std::shared_ptr<rtc::PeerConnection> m_pPc;
  44. std::shared_ptr<rtc::Track> m_pTrack;
  45. std::shared_ptr<rtc::RtpPacketizationConfig> m_pRtpConfig;
  46. std::shared_ptr<rtc::RtcpSrReporter> m_pSrReporter;
  47. StreamDesc m_desc;
  48. std::mutex m_mutex;
  49. std::condition_variable m_cond;
  50. bool m_bGathered = false;
  51. std::atomic<bool> m_bTrackOpen{false};
  52. std::atomic<bool> m_bConnected{false};
  53. std::atomic<bool> m_bClosing{false};
  54. bool m_bReading = false;
  55. bool m_bClosed = false;
  56. bool m_bStarted = false; // first IDR sent
  57. int64_t m_nFirstPts = 0;
  58. int64_t m_tCreated = 0;
  59. int64_t m_tLastSr = 0;
  60. uint64_t m_nBytesSent = 0;
  61. };
  62. } // namespace mtx