1 // Copyright 2018 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 
5 #ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_PEERCONNECTION_TRANSCEIVER_STATE_SURFACER_H_
6 #define THIRD_PARTY_BLINK_RENDERER_MODULES_PEERCONNECTION_TRANSCEIVER_STATE_SURFACER_H_
7 
8 #include "third_party/blink/renderer/modules/modules_export.h"
9 #include "third_party/blink/renderer/modules/peerconnection/rtc_rtp_transceiver_impl.h"
10 #include "third_party/blink/renderer/modules/peerconnection/webrtc_media_stream_track_adapter_map.h"
11 #include "third_party/blink/renderer/platform/peerconnection/rtc_peer_connection_handler_client.h"
12 #include "third_party/webrtc/api/rtp_transceiver_interface.h"
13 #include "third_party/webrtc/api/sctp_transport_interface.h"
14 #include "third_party/webrtc/rtc_base/ref_count.h"
15 #include "third_party/webrtc/rtc_base/ref_counted_object.h"
16 
17 namespace blink {
18 
19 // Takes care of creating and initializing transceiver states (including sender
20 // and receiver states). This is usable for both blocking and non-blocking calls
21 // to the webrtc signaling thread.
22 //
23 // The surfacer is initialized on the signaling thread and states are obtained
24 // on the main thread. It is designed to be initialized and handed off; it is
25 // not thread safe for concurrent thread usage.
26 class MODULES_EXPORT TransceiverStateSurfacer {
27  public:
28   TransceiverStateSurfacer(
29       scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
30       scoped_refptr<base::SingleThreadTaskRunner> signaling_task_runner);
31   TransceiverStateSurfacer(TransceiverStateSurfacer&&);
32   TransceiverStateSurfacer(const TransceiverStateSurfacer&) = delete;
33   ~TransceiverStateSurfacer();
34 
35   // This is intended to be used for moving the object from the signaling thread
36   // to the main thread and as such has no thread checks. Once moved to the main
37   // this should only be invoked on the main thread.
38   TransceiverStateSurfacer& operator=(TransceiverStateSurfacer&&);
39   TransceiverStateSurfacer& operator=(const TransceiverStateSurfacer&) = delete;
40 
is_initialized()41   bool is_initialized() const { return is_initialized_; }
42 
43   // Must be invoked on the signaling thread.
44   void Initialize(
45       scoped_refptr<webrtc::PeerConnectionInterface> native_peer_connection,
46       scoped_refptr<blink::WebRtcMediaStreamTrackAdapterMap> track_adapter_map,
47       std::vector<rtc::scoped_refptr<webrtc::RtpTransceiverInterface>>
48           transceivers);
49 
50   // Must be invoked on the main thread.
51   blink::WebRTCSctpTransportSnapshot SctpTransportSnapshot();
52   std::vector<blink::RtpTransceiverState> ObtainStates();
53 
54  protected:
55   scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
56   scoped_refptr<base::SingleThreadTaskRunner> signaling_task_runner_;
57   bool is_initialized_;
58   bool states_obtained_;
59   blink::WebRTCSctpTransportSnapshot sctp_transport_snapshot_;
60   std::vector<blink::RtpTransceiverState> transceiver_states_;
61 };
62 
63 // A dummy implementation of a transceiver used to surface sender state
64 // information only. It is not thread safe, only designed to be passed on to
65 // TransceiverStateSurfacer::Initialize().
66 class MODULES_EXPORT SurfaceSenderStateOnly
67     : public rtc::RefCountedObject<webrtc::RtpTransceiverInterface> {
68  public:
69   SurfaceSenderStateOnly(rtc::scoped_refptr<webrtc::RtpSenderInterface> sender);
70   ~SurfaceSenderStateOnly() override;
71 
72   cricket::MediaType media_type() const override;
73   absl::optional<std::string> mid() const override;
74   rtc::scoped_refptr<webrtc::RtpSenderInterface> sender() const override;
75   rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver() const override;
76   bool stopped() const override;
77   webrtc::RtpTransceiverDirection direction() const override;
78   void SetDirection(webrtc::RtpTransceiverDirection new_direction) override;
79   absl::optional<webrtc::RtpTransceiverDirection> current_direction()
80       const override;
81   void Stop() override;
82 
83  private:
84   rtc::scoped_refptr<webrtc::RtpSenderInterface> sender_;
85 };
86 
87 // A dummy implementation of a transceiver used to surface receiver state
88 // information only. It is not thread safe, only designed to be passed on to
89 // TransceiverStateSurfacer::Initialize().
90 class MODULES_EXPORT SurfaceReceiverStateOnly
91     : public rtc::RefCountedObject<webrtc::RtpTransceiverInterface> {
92  public:
93   SurfaceReceiverStateOnly(
94       rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver);
95   ~SurfaceReceiverStateOnly() override;
96 
97   cricket::MediaType media_type() const override;
98   absl::optional<std::string> mid() const override;
99   rtc::scoped_refptr<webrtc::RtpSenderInterface> sender() const override;
100   rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver() const override;
101   bool stopped() const override;
102   webrtc::RtpTransceiverDirection direction() const override;
103   void SetDirection(webrtc::RtpTransceiverDirection new_direction) override;
104   absl::optional<webrtc::RtpTransceiverDirection> current_direction()
105       const override;
106   void Stop() override;
107 
108  private:
109   rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver_;
110 };
111 
112 }  // namespace blink
113 
114 #endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_PEERCONNECTION_TRANSCEIVER_STATE_SURFACER_H_
115