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