deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef API_ORTC_ORTCFACTORYINTERFACE_H_ |
| 12 | #define API_ORTC_ORTCFACTORYINTERFACE_H_ |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 13 | |
| 14 | #include <memory> |
| 15 | #include <string> |
| 16 | #include <utility> // For std::move. |
| 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "api/mediaconstraintsinterface.h" |
| 19 | #include "api/mediastreaminterface.h" |
| 20 | #include "api/mediatypes.h" |
| 21 | #include "api/ortc/ortcrtpreceiverinterface.h" |
| 22 | #include "api/ortc/ortcrtpsenderinterface.h" |
| 23 | #include "api/ortc/packettransportinterface.h" |
| 24 | #include "api/ortc/rtptransportcontrollerinterface.h" |
| 25 | #include "api/ortc/rtptransportinterface.h" |
| 26 | #include "api/ortc/srtptransportinterface.h" |
| 27 | #include "api/ortc/udptransportinterface.h" |
| 28 | #include "api/rtcerror.h" |
| 29 | #include "api/rtpparameters.h" |
| 30 | #include "p2p/base/packetsocketfactory.h" |
| 31 | #include "rtc_base/network.h" |
| 32 | #include "rtc_base/scoped_ref_ptr.h" |
| 33 | #include "rtc_base/thread.h" |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 34 | |
| 35 | namespace webrtc { |
| 36 | |
| 37 | // TODO(deadbeef): This should be part of /api/, but currently it's not and |
| 38 | // including its header violates checkdeps rules. |
| 39 | class AudioDeviceModule; |
| 40 | |
| 41 | // WARNING: This is experimental/under development, so use at your own risk; no |
| 42 | // guarantee about API stability is guaranteed here yet. |
| 43 | // |
| 44 | // This class is the ORTC analog of PeerConnectionFactory. It acts as a factory |
| 45 | // for ORTC objects that can be connected to each other. |
| 46 | // |
| 47 | // Some of these objects may not be represented by the ORTC specification, but |
| 48 | // follow the same general principles. |
| 49 | // |
| 50 | // If one of the factory methods takes another object as an argument, it MUST |
| 51 | // have been created by the same OrtcFactory. |
| 52 | // |
| 53 | // On object lifetimes: objects should be destroyed in this order: |
| 54 | // 1. Objects created by the factory. |
| 55 | // 2. The factory itself. |
| 56 | // 3. Objects passed into OrtcFactoryInterface::Create. |
| 57 | class OrtcFactoryInterface { |
| 58 | public: |
| 59 | // |network_thread| is the thread on which packets are sent and received. |
| 60 | // If null, a new rtc::Thread with a default socket server is created. |
| 61 | // |
| 62 | // |signaling_thread| is used for callbacks to the consumer of the API. If |
| 63 | // null, the current thread will be used, which assumes that the API consumer |
| 64 | // is running a message loop on this thread (either using an existing |
| 65 | // rtc::Thread, or by calling rtc::Thread::Current()->ProcessMessages). |
| 66 | // |
| 67 | // |network_manager| is used to determine which network interfaces are |
| 68 | // available. This is used for ICE, for example. If null, a default |
| 69 | // implementation will be used. Only accessed on |network_thread|. |
| 70 | // |
| 71 | // |socket_factory| is used (on the network thread) for creating sockets. If |
| 72 | // it's null, a default implementation will be used, which assumes |
| 73 | // |network_thread| is a normal rtc::Thread. |
| 74 | // |
| 75 | // |adm| is optional, and allows a different audio device implementation to |
| 76 | // be injected; otherwise a platform-specific module will be used that will |
| 77 | // use the default audio input. |
| 78 | // |
Karl Wiberg | 3e9e5b3 | 2017-11-06 05:01:56 +0100 | [diff] [blame^] | 79 | // |audio_encoder_factory| and |audio_decoder_factory| are used to |
| 80 | // instantiate audio codecs; they determine what codecs are supported. |
| 81 | // |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 82 | // Note that the OrtcFactoryInterface does not take ownership of any of the |
Karl Wiberg | 3e9e5b3 | 2017-11-06 05:01:56 +0100 | [diff] [blame^] | 83 | // objects passed in by raw pointer, and as previously stated, these objects |
| 84 | // can't be destroyed before the factory is. |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 85 | static RTCErrorOr<std::unique_ptr<OrtcFactoryInterface>> Create( |
| 86 | rtc::Thread* network_thread, |
| 87 | rtc::Thread* signaling_thread, |
| 88 | rtc::NetworkManager* network_manager, |
| 89 | rtc::PacketSocketFactory* socket_factory, |
Karl Wiberg | 3e9e5b3 | 2017-11-06 05:01:56 +0100 | [diff] [blame^] | 90 | AudioDeviceModule* adm, |
| 91 | rtc::scoped_refptr<AudioEncoderFactory> audio_encoder_factory, |
| 92 | rtc::scoped_refptr<AudioDecoderFactory> audio_decoder_factory); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 93 | |
Karl Wiberg | 3e9e5b3 | 2017-11-06 05:01:56 +0100 | [diff] [blame^] | 94 | // Constructor for convenience which uses default implementations where |
| 95 | // possible (though does still require that the current thread runs a message |
| 96 | // loop; see above). |
| 97 | static RTCErrorOr<std::unique_ptr<OrtcFactoryInterface>> Create( |
| 98 | rtc::scoped_refptr<AudioEncoderFactory> audio_encoder_factory, |
| 99 | rtc::scoped_refptr<AudioDecoderFactory> audio_decoder_factory) { |
| 100 | return Create(nullptr, nullptr, nullptr, nullptr, nullptr, |
| 101 | audio_encoder_factory, audio_decoder_factory); |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | virtual ~OrtcFactoryInterface() {} |
| 105 | |
| 106 | // Creates an RTP transport controller, which is used in calls to |
| 107 | // CreateRtpTransport methods. If your application has some notion of a |
| 108 | // "call", you should create one transport controller per call. |
| 109 | // |
| 110 | // However, if you only are using one RtpTransport object, this doesn't need |
| 111 | // to be called explicitly; CreateRtpTransport will create one automatically |
| 112 | // if |rtp_transport_controller| is null. See below. |
| 113 | // |
| 114 | // TODO(deadbeef): Add MediaConfig and RtcEventLog arguments? |
| 115 | virtual RTCErrorOr<std::unique_ptr<RtpTransportControllerInterface>> |
| 116 | CreateRtpTransportController() = 0; |
| 117 | |
| 118 | // Creates an RTP transport using the provided packet transports and |
| 119 | // transport controller. |
| 120 | // |
| 121 | // |rtp| will be used for sending RTP packets, and |rtcp| for RTCP packets. |
| 122 | // |
| 123 | // |rtp| can't be null. |rtcp| must be non-null if and only if |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 124 | // |rtp_parameters.rtcp.mux| is false, indicating that RTCP muxing isn't used. |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 125 | // Note that if RTCP muxing isn't enabled initially, it can still enabled |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 126 | // later through SetParameters. |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 127 | // |
| 128 | // If |transport_controller| is null, one will automatically be created, and |
| 129 | // its lifetime managed by the returned RtpTransport. This should only be |
| 130 | // done if a single RtpTransport is being used to communicate with the remote |
| 131 | // endpoint. |
| 132 | virtual RTCErrorOr<std::unique_ptr<RtpTransportInterface>> CreateRtpTransport( |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 133 | const RtpTransportParameters& rtp_parameters, |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 134 | PacketTransportInterface* rtp, |
| 135 | PacketTransportInterface* rtcp, |
| 136 | RtpTransportControllerInterface* transport_controller) = 0; |
| 137 | |
zhihuang | d3501ad | 2017-03-03 14:39:06 -0800 | [diff] [blame] | 138 | // Creates an SrtpTransport which is an RTP transport that uses SRTP. |
| 139 | virtual RTCErrorOr<std::unique_ptr<SrtpTransportInterface>> |
| 140 | CreateSrtpTransport( |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 141 | const RtpTransportParameters& rtp_parameters, |
zhihuang | d3501ad | 2017-03-03 14:39:06 -0800 | [diff] [blame] | 142 | PacketTransportInterface* rtp, |
| 143 | PacketTransportInterface* rtcp, |
| 144 | RtpTransportControllerInterface* transport_controller) = 0; |
| 145 | |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 146 | // Returns the capabilities of an RTP sender of type |kind|. These |
| 147 | // capabilities can be used to determine what RtpParameters to use to create |
| 148 | // an RtpSender. |
| 149 | // |
| 150 | // If for some reason you pass in MEDIA_TYPE_DATA, returns an empty structure. |
| 151 | virtual RtpCapabilities GetRtpSenderCapabilities( |
| 152 | cricket::MediaType kind) const = 0; |
| 153 | |
| 154 | // Creates an RTP sender with |track|. Will not start sending until Send is |
| 155 | // called. This is provided as a convenience; it's equivalent to calling |
| 156 | // CreateRtpSender with a kind (see below), followed by SetTrack. |
| 157 | // |
| 158 | // |track| and |transport| must not be null. |
| 159 | virtual RTCErrorOr<std::unique_ptr<OrtcRtpSenderInterface>> CreateRtpSender( |
| 160 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 161 | RtpTransportInterface* transport) = 0; |
| 162 | |
| 163 | // Overload of CreateRtpSender allows creating the sender without a track. |
| 164 | // |
| 165 | // |kind| must be MEDIA_TYPE_AUDIO or MEDIA_TYPE_VIDEO. |
| 166 | virtual RTCErrorOr<std::unique_ptr<OrtcRtpSenderInterface>> CreateRtpSender( |
| 167 | cricket::MediaType kind, |
| 168 | RtpTransportInterface* transport) = 0; |
| 169 | |
| 170 | // Returns the capabilities of an RTP receiver of type |kind|. These |
| 171 | // capabilities can be used to determine what RtpParameters to use to create |
| 172 | // an RtpReceiver. |
| 173 | // |
| 174 | // If for some reason you pass in MEDIA_TYPE_DATA, returns an empty structure. |
| 175 | virtual RtpCapabilities GetRtpReceiverCapabilities( |
| 176 | cricket::MediaType kind) const = 0; |
| 177 | |
| 178 | // Creates an RTP receiver of type |kind|. Will not start receiving media |
| 179 | // until Receive is called. |
| 180 | // |
| 181 | // |kind| must be MEDIA_TYPE_AUDIO or MEDIA_TYPE_VIDEO. |
| 182 | // |
| 183 | // |transport| must not be null. |
| 184 | virtual RTCErrorOr<std::unique_ptr<OrtcRtpReceiverInterface>> |
| 185 | CreateRtpReceiver(cricket::MediaType kind, |
| 186 | RtpTransportInterface* transport) = 0; |
| 187 | |
| 188 | // Create a UDP transport with IP address family |family|, using a port |
| 189 | // within the specified range. |
| 190 | // |
| 191 | // |family| must be AF_INET or AF_INET6. |
| 192 | // |
| 193 | // |min_port|/|max_port| values of 0 indicate no range restriction. |
| 194 | // |
| 195 | // Returns an error if the transport wasn't successfully created. |
| 196 | virtual RTCErrorOr<std::unique_ptr<UdpTransportInterface>> |
| 197 | CreateUdpTransport(int family, uint16_t min_port, uint16_t max_port) = 0; |
| 198 | |
| 199 | // Method for convenience that has no port range restrictions. |
| 200 | RTCErrorOr<std::unique_ptr<UdpTransportInterface>> CreateUdpTransport( |
| 201 | int family) { |
| 202 | return CreateUdpTransport(family, 0, 0); |
| 203 | } |
| 204 | |
| 205 | // NOTE: The methods below to create tracks/sources return scoped_refptrs |
| 206 | // rather than unique_ptrs, because these interfaces are also used with |
| 207 | // PeerConnection, where everything is ref-counted. |
| 208 | |
| 209 | // Creates a audio source representing the default microphone input. |
| 210 | // |options| decides audio processing settings. |
| 211 | virtual rtc::scoped_refptr<AudioSourceInterface> CreateAudioSource( |
| 212 | const cricket::AudioOptions& options) = 0; |
| 213 | |
| 214 | // Version of the above method that uses default options. |
| 215 | rtc::scoped_refptr<AudioSourceInterface> CreateAudioSource() { |
| 216 | return CreateAudioSource(cricket::AudioOptions()); |
| 217 | } |
| 218 | |
| 219 | // Creates a video source object wrapping and taking ownership of |capturer|. |
| 220 | // |
| 221 | // |constraints| can be used for selection of resolution and frame rate, and |
| 222 | // may be null if no constraints are desired. |
| 223 | virtual rtc::scoped_refptr<VideoTrackSourceInterface> CreateVideoSource( |
| 224 | std::unique_ptr<cricket::VideoCapturer> capturer, |
| 225 | const MediaConstraintsInterface* constraints) = 0; |
| 226 | |
| 227 | // Version of the above method that omits |constraints|. |
| 228 | rtc::scoped_refptr<VideoTrackSourceInterface> CreateVideoSource( |
| 229 | std::unique_ptr<cricket::VideoCapturer> capturer) { |
| 230 | return CreateVideoSource(std::move(capturer), nullptr); |
| 231 | } |
| 232 | |
| 233 | // Creates a new local video track wrapping |source|. The same |source| can |
| 234 | // be used in several tracks. |
| 235 | virtual rtc::scoped_refptr<VideoTrackInterface> CreateVideoTrack( |
| 236 | const std::string& id, |
| 237 | VideoTrackSourceInterface* source) = 0; |
| 238 | |
| 239 | // Creates an new local audio track wrapping |source|. |
| 240 | virtual rtc::scoped_refptr<AudioTrackInterface> CreateAudioTrack( |
| 241 | const std::string& id, |
| 242 | AudioSourceInterface* source) = 0; |
| 243 | }; |
| 244 | |
| 245 | } // namespace webrtc |
| 246 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 247 | #endif // API_ORTC_ORTCFACTORYINTERFACE_H_ |