data_channel_observer.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #pragma once
  2. #include "callback.h"
  3. enum class DataChannelState : std::int32_t {
  4. kConnecting = 0,
  5. kOpen = 1,
  6. kClosing = 2,
  7. kClosed = 3
  8. };
  9. using DataChannelMessageCallback = Callback<const void*, const int32_t>;
  10. using DataChannelBufferingCallback =
  11. Callback<const uint64_t, const uint64_t, const uint64_t>;
  12. /// Callback fired when the data channel state changed.
  13. using DataChannelStateCallback = Callback</*DataChannelState*/ int, int>;
  14. class DataChannelObserver : public webrtc::DataChannelObserver {
  15. public:
  16. DataChannelObserver(
  17. rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel) ;
  18. webrtc::DataChannelInterface* data_channel() const {
  19. return data_channel_.get();
  20. }
  21. void SetMessageCallback(DataChannelMessageCallback callback) {
  22. std::lock_guard<std::mutex> lock(mutex_);
  23. message_callback_ = callback;
  24. }
  25. void SetBufferingCallback(
  26. DataChannelBufferingCallback callback) {
  27. std::lock_guard<std::mutex> lock(mutex_);
  28. buffering_callback_ = callback;
  29. }
  30. void SetStateCallback(DataChannelStateCallback callback) {
  31. std::lock_guard<std::mutex> lock(mutex_);
  32. state_callback_ = callback;
  33. }
  34. protected:
  35. // DataChannelObserver interface
  36. // The data channel state have changed.
  37. void OnStateChange() override;
  38. // A data buffer was successfully received.
  39. void OnMessage(const webrtc::DataBuffer& buffer) override;
  40. // The data channel's buffered_amount has changed.
  41. void OnBufferedAmountChange(uint64_t previous_amount) override;
  42. private:
  43. rtc::scoped_refptr<webrtc::DataChannelInterface> data_channel_;
  44. DataChannelMessageCallback message_callback_;
  45. DataChannelBufferingCallback buffering_callback_;
  46. DataChannelStateCallback state_callback_;
  47. std::mutex mutex_;
  48. };