webrtc_data_stream_adapter.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef REMOTING_PROTOCOL_WEBRTC_DATA_STREAM_ADAPTER_H_
  5. #define REMOTING_PROTOCOL_WEBRTC_DATA_STREAM_ADAPTER_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/callback.h"
  9. #include "base/containers/queue.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "remoting/protocol/message_pipe.h"
  13. #include "third_party/webrtc/api/peer_connection_interface.h"
  14. #include "third_party/webrtc/rtc_base/ref_count.h"
  15. namespace remoting {
  16. namespace protocol {
  17. // WebrtcDataStreamAdapter implements MessagePipe for WebRTC data channels.
  18. class WebrtcDataStreamAdapter : public MessagePipe,
  19. public webrtc::DataChannelObserver {
  20. public:
  21. explicit WebrtcDataStreamAdapter(
  22. rtc::scoped_refptr<webrtc::DataChannelInterface> channel);
  23. WebrtcDataStreamAdapter(const WebrtcDataStreamAdapter&) = delete;
  24. WebrtcDataStreamAdapter& operator=(const WebrtcDataStreamAdapter&) = delete;
  25. ~WebrtcDataStreamAdapter() override;
  26. std::string name() { return channel_->label(); }
  27. // MessagePipe interface.
  28. void Start(EventHandler* event_handler) override;
  29. void Send(google::protobuf::MessageLite* message,
  30. base::OnceClosure done) override;
  31. private:
  32. enum class State { CONNECTING, OPEN, CLOSED };
  33. struct PendingMessage {
  34. PendingMessage(webrtc::DataBuffer buffer, base::OnceClosure done_callback);
  35. PendingMessage(PendingMessage&&);
  36. ~PendingMessage();
  37. PendingMessage& operator=(PendingMessage&&);
  38. webrtc::DataBuffer buffer;
  39. base::OnceClosure done_callback;
  40. };
  41. void SendMessagesIfReady();
  42. // webrtc::DataChannelObserver interface.
  43. void OnStateChange() override;
  44. void OnMessage(const webrtc::DataBuffer& buffer) override;
  45. void OnBufferedAmountChange(uint64_t previous_amount) override;
  46. // Helpers for calling EventHandler methods asynchronously.
  47. // webrtc::DataChannelObserver can be called synchronously mid-operation
  48. // (e.g., in the middle of a Send operation). As such, it is important to let
  49. // the stack unwind before doing any real work. Since this class doesn't
  50. // control the EventHandler implementation, the safest approach is always to
  51. // call the latter's methods asynchronously.
  52. void InvokeOpenEvent();
  53. void InvokeClosedEvent();
  54. void InvokeMessageEvent(std::unique_ptr<CompoundBuffer> buffer);
  55. rtc::scoped_refptr<webrtc::DataChannelInterface> channel_;
  56. raw_ptr<EventHandler> event_handler_ = nullptr;
  57. State state_ = State::CONNECTING;
  58. // The data and done callbacks for queued but not yet sent messages.
  59. base::queue<PendingMessage> pending_messages_;
  60. base::WeakPtrFactory<WebrtcDataStreamAdapter> weak_ptr_factory_{this};
  61. };
  62. } // namespace protocol
  63. } // namespace remoting
  64. #endif // REMOTING_PROTOCOL_WEBRTC_DATA_STREAM_ADAPTER_H_