track.hpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * Copyright (c) 2020-2021 Paul-Louis Ageneau
  3. *
  4. * This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. */
  8. #ifndef RTC_IMPL_TRACK_H
  9. #define RTC_IMPL_TRACK_H
  10. #include "channel.hpp"
  11. #include "common.hpp"
  12. #include "description.hpp"
  13. #include "mediahandler.hpp"
  14. #include "queue.hpp"
  15. #if RTC_ENABLE_MEDIA
  16. #include "dtlssrtptransport.hpp"
  17. #endif
  18. #include <atomic>
  19. #include <shared_mutex>
  20. namespace rtc::impl {
  21. struct PeerConnection;
  22. class Track final : public std::enable_shared_from_this<Track>, public Channel {
  23. public:
  24. Track(weak_ptr<PeerConnection> pc, Description::Media desc);
  25. ~Track();
  26. void close();
  27. void incoming(message_ptr message);
  28. bool outgoing(message_ptr message);
  29. optional<message_variant> receive() override;
  30. optional<message_variant> peek() override;
  31. size_t availableAmount() const override;
  32. void flushPendingMessages() override;
  33. message_variant trackMessageToVariant(message_ptr message);
  34. void onFrame(std::function<void(binary data, FrameInfo frame)> callback);
  35. bool isOpen() const;
  36. bool isClosed() const;
  37. size_t maxMessageSize() const;
  38. string mid() const;
  39. Description::Direction direction() const;
  40. Description::Media description() const;
  41. void setDescription(Description::Media desc);
  42. shared_ptr<MediaHandler> getMediaHandler();
  43. void setMediaHandler(shared_ptr<MediaHandler> handler);
  44. #if RTC_ENABLE_MEDIA
  45. void open(shared_ptr<DtlsSrtpTransport> transport);
  46. #endif
  47. bool transportSend(message_ptr message);
  48. private:
  49. const weak_ptr<PeerConnection> mPeerConnection;
  50. #if RTC_ENABLE_MEDIA
  51. weak_ptr<DtlsSrtpTransport> mDtlsSrtpTransport;
  52. #endif
  53. Description::Media mMediaDescription;
  54. shared_ptr<MediaHandler> mMediaHandler;
  55. mutable std::shared_mutex mMutex;
  56. std::atomic<bool> mIsClosed = false;
  57. Queue<message_ptr> mRecvQueue;
  58. synchronized_callback<binary, FrameInfo> frameCallback;
  59. };
  60. } // namespace rtc::impl
  61. #endif