media_channel_impl.h (5412B)
1 /* 2 * Copyright 2022 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #ifndef MEDIA_BASE_MEDIA_CHANNEL_IMPL_H_ 12 #define MEDIA_BASE_MEDIA_CHANNEL_IMPL_H_ 13 14 #include <stddef.h> 15 #include <stdint.h> 16 17 #include "api/array_view.h" 18 #include "api/call/transport.h" 19 #include "api/scoped_refptr.h" 20 #include "api/sequence_checker.h" 21 #include "api/task_queue/pending_task_safety_flag.h" 22 #include "api/task_queue/task_queue_base.h" 23 #include "media/base/media_channel.h" 24 #include "rtc_base/async_packet_socket.h" 25 #include "rtc_base/copy_on_write_buffer.h" 26 #include "rtc_base/dscp.h" 27 #include "rtc_base/socket.h" 28 #include "rtc_base/thread_annotations.h" 29 // This file contains the base classes for classes that implement 30 // the channel interfaces. 31 // These implementation classes used to be the exposed interface names, 32 // but this is in the process of being changed. 33 34 namespace webrtc { 35 36 // The `MediaChannelUtil` class provides functionality that is used by 37 // multiple MediaChannel-like objects, of both sending and receiving 38 // types. 39 class MediaChannelUtil { 40 public: 41 explicit MediaChannelUtil(TaskQueueBase* network_thread, 42 bool enable_dscp = false); 43 virtual ~MediaChannelUtil(); 44 // Returns the absolute sendtime extension id value from media channel. 45 virtual int GetRtpSendTimeExtnId() const; 46 47 Transport* transport() { return &transport_; } 48 49 // Base methods to send packet using MediaChannelNetworkInterface. 50 // These methods are used by some tests only. 51 bool SendPacket(CopyOnWriteBuffer* packet, 52 const AsyncSocketPacketOptions& options); 53 54 bool SendRtcp(CopyOnWriteBuffer* packet, 55 const AsyncSocketPacketOptions& options); 56 57 int SetOption(MediaChannelNetworkInterface::SocketType type, 58 Socket::Option opt, 59 int option); 60 61 // Functions that form part of one or more interface classes. 62 // Not marked override, since this class does not inherit from the 63 // interfaces. 64 65 // Corresponds to the SDP attribute extmap-allow-mixed, see RFC8285. 66 // Set to true if it's allowed to mix one- and two-byte RTP header extensions 67 // in the same stream. The setter and getter must only be called from 68 // worker_thread. 69 void SetExtmapAllowMixed(bool extmap_allow_mixed); 70 bool ExtmapAllowMixed() const; 71 72 void SetInterface(MediaChannelNetworkInterface* iface); 73 // Returns `true` if a non-null MediaChannelNetworkInterface pointer is held. 74 // Must be called on the network thread. 75 bool HasNetworkInterface() const; 76 77 protected: 78 bool DscpEnabled() const; 79 80 void SetPreferredDscp(DiffServCodePoint new_dscp); 81 82 private: 83 // Implementation of the Transport interface required 84 // by Call(). 85 class TransportForMediaChannels : public Transport { 86 public: 87 TransportForMediaChannels(TaskQueueBase* network_thread, bool enable_dscp); 88 89 virtual ~TransportForMediaChannels(); 90 91 // Implementation of Transport 92 bool SendRtp(ArrayView<const uint8_t> packet, 93 const PacketOptions& options) override; 94 bool SendRtcp(ArrayView<const uint8_t> packet, 95 const PacketOptions& options) override; 96 97 // Not implementation of Transport 98 void SetInterface(MediaChannelNetworkInterface* iface); 99 100 int SetOption(MediaChannelNetworkInterface::SocketType type, 101 Socket::Option opt, 102 int option); 103 AsyncSocketPacketOptions TranslatePacketOptions( 104 const PacketOptions& options); 105 106 bool DoSendPacket(CopyOnWriteBuffer* packet, 107 bool rtcp, 108 const AsyncSocketPacketOptions& options); 109 110 bool HasNetworkInterface() const { 111 RTC_DCHECK_RUN_ON(network_thread_); 112 return network_interface_ != nullptr; 113 } 114 bool DscpEnabled() const { return enable_dscp_; } 115 116 void SetPreferredDscp(DiffServCodePoint new_dscp); 117 118 private: 119 // This is the DSCP value used for both RTP and RTCP channels if DSCP is 120 // enabled. It can be changed at any time via `SetPreferredDscp`. 121 DiffServCodePoint PreferredDscp() const { 122 RTC_DCHECK_RUN_ON(network_thread_); 123 return preferred_dscp_; 124 } 125 126 // Apply the preferred DSCP setting to the underlying network interface RTP 127 // and RTCP channels. If DSCP is disabled, then apply the default DSCP 128 // value. 129 void UpdateDscp() RTC_RUN_ON(network_thread_); 130 131 int SetOptionLocked(MediaChannelNetworkInterface::SocketType type, 132 Socket::Option opt, 133 int option) RTC_RUN_ON(network_thread_); 134 135 const scoped_refptr<PendingTaskSafetyFlag> network_safety_ 136 RTC_PT_GUARDED_BY(network_thread_); 137 TaskQueueBase* const network_thread_; 138 const bool enable_dscp_; 139 MediaChannelNetworkInterface* network_interface_ 140 RTC_GUARDED_BY(network_thread_) = nullptr; 141 DiffServCodePoint preferred_dscp_ RTC_GUARDED_BY(network_thread_) = 142 DSCP_DEFAULT; 143 }; 144 145 bool extmap_allow_mixed_ = false; 146 TransportForMediaChannels transport_; 147 }; 148 149 } // namespace webrtc 150 151 #endif // MEDIA_BASE_MEDIA_CHANNEL_IMPL_H_