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 | |
| 11 | #ifndef WEBRTC_API_ORTC_ORTCFACTORYINTERFACE_H_ |
| 12 | #define WEBRTC_API_ORTC_ORTCFACTORYINTERFACE_H_ |
| 13 | |
| 14 | #include <memory> |
| 15 | #include <string> |
| 16 | #include <utility> // For std::move. |
| 17 | |
| 18 | #include "webrtc/api/mediaconstraintsinterface.h" |
| 19 | #include "webrtc/api/mediastreaminterface.h" |
| 20 | #include "webrtc/api/mediatypes.h" |
| 21 | #include "webrtc/api/ortc/ortcrtpreceiverinterface.h" |
| 22 | #include "webrtc/api/ortc/ortcrtpsenderinterface.h" |
| 23 | #include "webrtc/api/ortc/packettransportinterface.h" |
| 24 | #include "webrtc/api/ortc/rtptransportcontrollerinterface.h" |
| 25 | #include "webrtc/api/ortc/rtptransportinterface.h" |
zhihuang | d3501ad | 2017-03-03 14:39:06 -0800 | [diff] [blame] | 26 | #include "webrtc/api/ortc/srtptransportinterface.h" |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 27 | #include "webrtc/api/ortc/udptransportinterface.h" |
| 28 | #include "webrtc/api/rtcerror.h" |
| 29 | #include "webrtc/api/rtpparameters.h" |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 30 | #include "webrtc/p2p/base/packetsocketfactory.h" |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 31 | #include "webrtc/rtc_base/network.h" |
| 32 | #include "webrtc/rtc_base/scoped_ref_ptr.h" |
| 33 | #include "webrtc/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 | // |
| 79 | // Note that the OrtcFactoryInterface does not take ownership of any of the |
| 80 | // objects passed in, and as previously stated, these objects can't be |
| 81 | // destroyed before the factory is. |
| 82 | static RTCErrorOr<std::unique_ptr<OrtcFactoryInterface>> Create( |
| 83 | rtc::Thread* network_thread, |
| 84 | rtc::Thread* signaling_thread, |
| 85 | rtc::NetworkManager* network_manager, |
| 86 | rtc::PacketSocketFactory* socket_factory, |
| 87 | AudioDeviceModule* adm); |
| 88 | |
| 89 | // Constructor for convenience which uses default implementations of |
| 90 | // everything (though does still require that the current thread runs a |
| 91 | // message loop; see above). |
| 92 | static RTCErrorOr<std::unique_ptr<OrtcFactoryInterface>> Create() { |
| 93 | return Create(nullptr, nullptr, nullptr, nullptr, nullptr); |
| 94 | } |
| 95 | |
| 96 | virtual ~OrtcFactoryInterface() {} |
| 97 | |
| 98 | // Creates an RTP transport controller, which is used in calls to |
| 99 | // CreateRtpTransport methods. If your application has some notion of a |
| 100 | // "call", you should create one transport controller per call. |
| 101 | // |
| 102 | // However, if you only are using one RtpTransport object, this doesn't need |
| 103 | // to be called explicitly; CreateRtpTransport will create one automatically |
| 104 | // if |rtp_transport_controller| is null. See below. |
| 105 | // |
| 106 | // TODO(deadbeef): Add MediaConfig and RtcEventLog arguments? |
| 107 | virtual RTCErrorOr<std::unique_ptr<RtpTransportControllerInterface>> |
| 108 | CreateRtpTransportController() = 0; |
| 109 | |
| 110 | // Creates an RTP transport using the provided packet transports and |
| 111 | // transport controller. |
| 112 | // |
| 113 | // |rtp| will be used for sending RTP packets, and |rtcp| for RTCP packets. |
| 114 | // |
| 115 | // |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] | 116 | // |rtp_parameters.rtcp.mux| is false, indicating that RTCP muxing isn't used. |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 117 | // Note that if RTCP muxing isn't enabled initially, it can still enabled |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 118 | // later through SetParameters. |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 119 | // |
| 120 | // If |transport_controller| is null, one will automatically be created, and |
| 121 | // its lifetime managed by the returned RtpTransport. This should only be |
| 122 | // done if a single RtpTransport is being used to communicate with the remote |
| 123 | // endpoint. |
| 124 | virtual RTCErrorOr<std::unique_ptr<RtpTransportInterface>> CreateRtpTransport( |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 125 | const RtpTransportParameters& rtp_parameters, |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 126 | PacketTransportInterface* rtp, |
| 127 | PacketTransportInterface* rtcp, |
| 128 | RtpTransportControllerInterface* transport_controller) = 0; |
| 129 | |
zhihuang | d3501ad | 2017-03-03 14:39:06 -0800 | [diff] [blame] | 130 | // Creates an SrtpTransport which is an RTP transport that uses SRTP. |
| 131 | virtual RTCErrorOr<std::unique_ptr<SrtpTransportInterface>> |
| 132 | CreateSrtpTransport( |
sprang | db2a9fc | 2017-08-09 06:42:32 -0700 | [diff] [blame] | 133 | const RtpTransportParameters& rtp_parameters, |
zhihuang | d3501ad | 2017-03-03 14:39:06 -0800 | [diff] [blame] | 134 | PacketTransportInterface* rtp, |
| 135 | PacketTransportInterface* rtcp, |
| 136 | RtpTransportControllerInterface* transport_controller) = 0; |
| 137 | |
deadbeef | e814a0d | 2017-02-25 18:15:09 -0800 | [diff] [blame] | 138 | // Returns the capabilities of an RTP sender of type |kind|. These |
| 139 | // capabilities can be used to determine what RtpParameters to use to create |
| 140 | // an RtpSender. |
| 141 | // |
| 142 | // If for some reason you pass in MEDIA_TYPE_DATA, returns an empty structure. |
| 143 | virtual RtpCapabilities GetRtpSenderCapabilities( |
| 144 | cricket::MediaType kind) const = 0; |
| 145 | |
| 146 | // Creates an RTP sender with |track|. Will not start sending until Send is |
| 147 | // called. This is provided as a convenience; it's equivalent to calling |
| 148 | // CreateRtpSender with a kind (see below), followed by SetTrack. |
| 149 | // |
| 150 | // |track| and |transport| must not be null. |
| 151 | virtual RTCErrorOr<std::unique_ptr<OrtcRtpSenderInterface>> CreateRtpSender( |
| 152 | rtc::scoped_refptr<MediaStreamTrackInterface> track, |
| 153 | RtpTransportInterface* transport) = 0; |
| 154 | |
| 155 | // Overload of CreateRtpSender allows creating the sender without a track. |
| 156 | // |
| 157 | // |kind| must be MEDIA_TYPE_AUDIO or MEDIA_TYPE_VIDEO. |
| 158 | virtual RTCErrorOr<std::unique_ptr<OrtcRtpSenderInterface>> CreateRtpSender( |
| 159 | cricket::MediaType kind, |
| 160 | RtpTransportInterface* transport) = 0; |
| 161 | |
| 162 | // Returns the capabilities of an RTP receiver of type |kind|. These |
| 163 | // capabilities can be used to determine what RtpParameters to use to create |
| 164 | // an RtpReceiver. |
| 165 | // |
| 166 | // If for some reason you pass in MEDIA_TYPE_DATA, returns an empty structure. |
| 167 | virtual RtpCapabilities GetRtpReceiverCapabilities( |
| 168 | cricket::MediaType kind) const = 0; |
| 169 | |
| 170 | // Creates an RTP receiver of type |kind|. Will not start receiving media |
| 171 | // until Receive is called. |
| 172 | // |
| 173 | // |kind| must be MEDIA_TYPE_AUDIO or MEDIA_TYPE_VIDEO. |
| 174 | // |
| 175 | // |transport| must not be null. |
| 176 | virtual RTCErrorOr<std::unique_ptr<OrtcRtpReceiverInterface>> |
| 177 | CreateRtpReceiver(cricket::MediaType kind, |
| 178 | RtpTransportInterface* transport) = 0; |
| 179 | |
| 180 | // Create a UDP transport with IP address family |family|, using a port |
| 181 | // within the specified range. |
| 182 | // |
| 183 | // |family| must be AF_INET or AF_INET6. |
| 184 | // |
| 185 | // |min_port|/|max_port| values of 0 indicate no range restriction. |
| 186 | // |
| 187 | // Returns an error if the transport wasn't successfully created. |
| 188 | virtual RTCErrorOr<std::unique_ptr<UdpTransportInterface>> |
| 189 | CreateUdpTransport(int family, uint16_t min_port, uint16_t max_port) = 0; |
| 190 | |
| 191 | // Method for convenience that has no port range restrictions. |
| 192 | RTCErrorOr<std::unique_ptr<UdpTransportInterface>> CreateUdpTransport( |
| 193 | int family) { |
| 194 | return CreateUdpTransport(family, 0, 0); |
| 195 | } |
| 196 | |
| 197 | // NOTE: The methods below to create tracks/sources return scoped_refptrs |
| 198 | // rather than unique_ptrs, because these interfaces are also used with |
| 199 | // PeerConnection, where everything is ref-counted. |
| 200 | |
| 201 | // Creates a audio source representing the default microphone input. |
| 202 | // |options| decides audio processing settings. |
| 203 | virtual rtc::scoped_refptr<AudioSourceInterface> CreateAudioSource( |
| 204 | const cricket::AudioOptions& options) = 0; |
| 205 | |
| 206 | // Version of the above method that uses default options. |
| 207 | rtc::scoped_refptr<AudioSourceInterface> CreateAudioSource() { |
| 208 | return CreateAudioSource(cricket::AudioOptions()); |
| 209 | } |
| 210 | |
| 211 | // Creates a video source object wrapping and taking ownership of |capturer|. |
| 212 | // |
| 213 | // |constraints| can be used for selection of resolution and frame rate, and |
| 214 | // may be null if no constraints are desired. |
| 215 | virtual rtc::scoped_refptr<VideoTrackSourceInterface> CreateVideoSource( |
| 216 | std::unique_ptr<cricket::VideoCapturer> capturer, |
| 217 | const MediaConstraintsInterface* constraints) = 0; |
| 218 | |
| 219 | // Version of the above method that omits |constraints|. |
| 220 | rtc::scoped_refptr<VideoTrackSourceInterface> CreateVideoSource( |
| 221 | std::unique_ptr<cricket::VideoCapturer> capturer) { |
| 222 | return CreateVideoSource(std::move(capturer), nullptr); |
| 223 | } |
| 224 | |
| 225 | // Creates a new local video track wrapping |source|. The same |source| can |
| 226 | // be used in several tracks. |
| 227 | virtual rtc::scoped_refptr<VideoTrackInterface> CreateVideoTrack( |
| 228 | const std::string& id, |
| 229 | VideoTrackSourceInterface* source) = 0; |
| 230 | |
| 231 | // Creates an new local audio track wrapping |source|. |
| 232 | virtual rtc::scoped_refptr<AudioTrackInterface> CreateAudioTrack( |
| 233 | const std::string& id, |
| 234 | AudioSourceInterface* source) = 0; |
| 235 | }; |
| 236 | |
| 237 | } // namespace webrtc |
| 238 | |
| 239 | #endif // WEBRTC_API_ORTC_ORTCFACTORYINTERFACE_H_ |