media-session-p.h 12.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
/*
 * media-session-p.h
 * Copyright (C) 2017  Belledonne Communications SARL
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef _MEDIA_SESSION_P_H_
#define _MEDIA_SESSION_P_H_

#include <utility>

#include "call-session-p.h"

#include "media-session.h"
#include "port-config.h"
#include "nat/ice-agent.h"
#include "nat/stun-client.h"

#include "linphone/call_stats.h"
#include "sal/sal.h"

#include "private.h"

// =============================================================================

LINPHONE_BEGIN_NAMESPACE

class MediaSessionPrivate : public CallSessionPrivate {
public:
	MediaSessionPrivate (const Conference &conference, const std::shared_ptr<CallSessionParams> params, CallSessionListener *listener);
	virtual ~MediaSessionPrivate ();

public:
	static void stunAuthRequestedCb (void *userData, const char *realm, const char *nonce, const char **username, const char **password, const char **ha1);

	void accepted ();
	void ackReceived (LinphoneHeaders *headers);
	bool failure ();
	void pausedByRemote ();
	void remoteRinging ();
	void resumed ();
	void terminated ();
	void updated (bool isUpdate);
	void updating (bool isUpdate);

	void enableSymmetricRtp (bool value);
	void sendVfu ();

	void clearIceCheckList (IceCheckList *cl);
	void deactivateIce ();
	void prepareStreamsForIceGathering (bool hasVideo);
	void stopStreamsForIceGathering ();

	int getAf () const { return af; }
	bool getAudioMuted () const { return audioMuted; }
	LinphoneCore * getCore () const { return core; }
	IceSession * getIceSession () const { return iceAgent->getIceSession(); }
	SalMediaDescription * getLocalDesc () const { return localDesc; }
	MediaStream * getMediaStream (LinphoneStreamType type) const;
	LinphoneNatPolicy * getNatPolicy () const { return natPolicy; }
	int getRtcpPort (LinphoneStreamType type) const;
	int getRtpPort (LinphoneStreamType type) const;
	LinphoneCallStats * getStats (LinphoneStreamType type) const;
	int getStreamIndex (LinphoneStreamType type) const;
	int getStreamIndex (MediaStream *ms) const;
	SalOp * getOp () const { return op; }
	void setAudioMuted (bool value) { audioMuted = value; }

private:
	static OrtpJitterBufferAlgorithm jitterBufferNameToAlgo (const std::string &name);

#ifdef VIDEO_ENABLED
	static void videoStreamEventCb (void *userData, const MSFilter *f, const unsigned int eventId, const void *args);
#endif
#ifdef TEST_EXT_RENDERER
	static void extRendererCb (void *userData, const MSPicture *local, const MSPicture *remote);
#endif
	static void realTimeTextCharacterReceived (void *userData, MSFilter *f, unsigned int id, void *arg);

	static float aggregateQualityRatings (float audioRating, float videoRating);

	void setState (LinphoneCallState newState, const std::string &message);

	void computeStreamsIndexes (const SalMediaDescription *md);
	void fixCallParams (SalMediaDescription *rmd);
	void initializeParamsAccordingToIncomingCallParams ();
	void setCompatibleIncomingCallParams (SalMediaDescription *md);
	void updateBiggestDesc (SalMediaDescription *md);
	void updateRemoteSessionIdAndVer ();

	void initStats (LinphoneCallStats *stats, LinphoneStreamType type);
	void notifyStatsUpdated (int streamIndex) const;

	OrtpEvQueue * getEventQueue (int streamIndex) const;
	MediaStream * getMediaStream (int streamIndex) const;
	MSWebCam * getVideoDevice () const;

	void fillMulticastMediaAddresses ();
	int selectFixedPort (int streamIndex, std::pair<int, int> portRange);
	int selectRandomPort (int streamIndex, std::pair<int, int> portRange);
	void setPortConfig (int streamIndex, std::pair<int, int> portRange);
	void setPortConfigFromRtpSession (int streamIndex, RtpSession *session);
	void setRandomPortConfig (int streamIndex);

	void discoverMtu (const Address &remoteAddr);
	std::string getBindIpForStream (int streamIndex);
	void getLocalIp (const Address &remoteAddr);
	std::string getPublicIpForStream (int streamIndex);
	void runStunTestsIfNeeded ();
	void selectIncomingIpVersion ();
	void selectOutgoingIpVersion ();

	void forceStreamsDirAccordingToState (SalMediaDescription *md);
	bool generateB64CryptoKey (size_t keyLength, char *keyOut, size_t keyOutSize);
	void makeLocalMediaDescription ();
	int setupEncryptionKey (SalSrtpCryptoAlgo *crypto, MSCryptoSuite suite, unsigned int tag);
	void setupDtlsKeys (SalMediaDescription *md);
	void setupEncryptionKeys (SalMediaDescription *md);
	void setupRtcpFb (SalMediaDescription *md);
	void setupRtcpXr (SalMediaDescription *md);
	void setupZrtpHash (SalMediaDescription *md);
	void transferAlreadyAssignedPayloadTypes (SalMediaDescription *oldMd, SalMediaDescription *md);
	void updateLocalMediaDescriptionFromIce ();

	SalMulticastRole getMulticastRole (SalStreamType type);
	void joinMulticastGroup (int streamIndex, MediaStream *ms);

	int findCryptoIndexFromTag (const SalSrtpCryptoAlgo crypto[], unsigned char tag);
	void setDtlsFingerprint (MSMediaStreamSessions *sessions, const SalStreamDescription *sd, const SalStreamDescription *remote);
	void setDtlsFingerprintOnAllStreams ();
	void setupDtlsParams (MediaStream *ms);
	void setZrtpCryptoTypesParameters (MSZrtpParams *params);
	void startDtls (MSMediaStreamSessions *sessions, const SalStreamDescription *sd, const SalStreamDescription *remote);
	void startDtlsOnAllStreams ();
	void updateCryptoParameters (SalMediaDescription *oldMd, SalMediaDescription *newMd);
	bool updateStreamCryptoParameters (const SalStreamDescription *localStreamDesc, SalStreamDescription *oldStream, SalStreamDescription *newStream, MediaStream *ms);

	int getIdealAudioBandwidth (const SalMediaDescription *md, const SalStreamDescription *desc);
	int getVideoBandwidth (const SalMediaDescription *md, const SalStreamDescription *desc);
	RtpProfile * makeProfile (const SalMediaDescription *md, const SalStreamDescription *desc, int *usedPt);
	void unsetRtpProfile (int streamIndex);
	void updateAllocatedAudioBandwidth (const PayloadType *pt, int maxbw);

	void applyJitterBufferParams (RtpSession *session, LinphoneStreamType type);
	void clearEarlyMediaDestination (MediaStream *ms);
	void clearEarlyMediaDestinations ();
	void configureAdaptiveRateControl (MediaStream *ms, const OrtpPayloadType *pt, bool videoWillBeUsed);
	void configureRtpSessionForRtcpFb (const SalStreamDescription *stream);
	void configureRtpSessionForRtcpXr (SalStreamType type);
	RtpSession * createAudioRtpIoSession ();
	RtpSession * createVideoRtpIoSession ();
	void freeResources ();
	void handleIceEvents (OrtpEvent *ev);
	void handleStreamEvents (int streamIndex);
	void initializeAudioStream ();
	void initializeStreams ();
	void initializeTextStream ();
	void initializeVideoStream ();
	void parameterizeEqualizer (AudioStream *stream);
	void prepareEarlyMediaForking ();
	void postConfigureAudioStream (AudioStream *stream, bool muted);
	void postConfigureAudioStreams (bool muted);
	void setPlaybackGainDb (AudioStream *stream, float gain);
	void setSymmetricRtp (bool value);
	void startAudioStream (LinphoneCallState targetState, bool videoWillBeUsed);
	void startStreams (LinphoneCallState targetState);
	void startTextStream ();
	void startVideoStream (LinphoneCallState targetState);
	void stopAudioStream ();
	void stopStreams ();
	void stopTextStream ();
	void stopVideoStream ();
	void tryEarlyMediaForking (SalMediaDescription *md);
	void updateFrozenPayloads (SalMediaDescription *result);
	void updateStreams (SalMediaDescription *newMd, LinphoneCallState targetState);
	void updateStreamsDestinations (SalMediaDescription *oldMd, SalMediaDescription *newMd);

	bool allStreamsAvpfEnabled () const;
	bool allStreamsEncrypted () const;
	bool atLeastOneStreamStarted () const;
	void audioStreamAuthTokenReady (const std::string &authToken, bool verified);
	void audioStreamEncryptionChanged (bool encrypted);
	uint16_t getAvpfRrInterval () const;
	int getNbActiveStreams () const;
	bool isEncryptionMandatory () const;
	int mediaParametersChanged (SalMediaDescription *oldMd, SalMediaDescription *newMd);
	void propagateEncryptionChanged ();

	void fillLogStats (MediaStream *st);
	void updateLocalStats (LinphoneCallStats *stats, MediaStream *stream) const;
	void updateRtpStats (LinphoneCallStats *stats, int streamIndex);

	bool mediaReportEnabled (int statsType);
	bool qualityReportingEnabled () const;
	void updateReportingCallState ();
	void updateReportingMediaInfo (int statsType);

	void executeBackgroundTasks (bool oneSecondElapsed);
	void reportBandwidth ();
	void reportBandwidthForStream (MediaStream *ms, LinphoneStreamType type);

214
	void abort (const std::string &errorMsg);
215 216
	void handleIncomingReceivedStateInIncomingNotification ();
	bool isReadyForInvite () const;
217
	LinphoneStatus pause ();
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
	void setTerminated ();
	LinphoneStatus startAcceptUpdate (LinphoneCallState nextState, const std::string &stateInfo);
	LinphoneStatus startUpdate ();
	void terminate ();
	void updateCurrentParams ();

	void accept (const std::shared_ptr<MediaSessionParams> params);
	LinphoneStatus acceptUpdate (const std::shared_ptr<CallSessionParams> csp, LinphoneCallState nextState, const std::string &stateInfo);

#ifdef VIDEO_ENABLED
	void videoStreamEventCb (const MSFilter *f, const unsigned int eventId, const void *args);
#endif
	void realTimeTextCharacterReceived (MSFilter *f, unsigned int id, void *arg);

	void stunAuthRequestedCb (const char *realm, const char *nonce, const char **username, const char **password, const char **ha1);

private:
	static const std::string ecStateStore;
	static const int ecStateMaxLen;

	std::shared_ptr<MediaSessionParams> params = nullptr;
	std::shared_ptr<MediaSessionParams> currentParams = nullptr;
	std::shared_ptr<MediaSessionParams> remoteParams = nullptr;

	AudioStream *audioStream = nullptr;
	OrtpEvQueue *audioStreamEvQueue = nullptr;
	LinphoneCallStats *audioStats = nullptr;
	RtpProfile *audioProfile = nullptr;
	RtpProfile *rtpIoAudioProfile = nullptr;
	int mainAudioStreamIndex = LINPHONE_CALL_STATS_AUDIO;

	VideoStream *videoStream = nullptr;
	OrtpEvQueue *videoStreamEvQueue = nullptr;
	LinphoneCallStats *videoStats = nullptr;
	RtpProfile *rtpIoVideoProfile = nullptr;
	RtpProfile *videoProfile = nullptr;
	int mainVideoStreamIndex = LINPHONE_CALL_STATS_VIDEO;
	void *videoWindowId = nullptr;
	bool cameraEnabled = true;

	TextStream *textStream = nullptr;
	OrtpEvQueue *textStreamEvQueue = nullptr;
	LinphoneCallStats *textStats = nullptr;
	RtpProfile *textProfile = nullptr;
	int mainTextStreamIndex = LINPHONE_CALL_STATS_TEXT;

	StunClient *stunClient = nullptr;
	IceAgent *iceAgent = nullptr;

	int af; /* The address family to prefer for RTP path, guessed from signaling path */
	std::string mediaLocalIp;
	PortConfig mediaPorts[SAL_MEDIA_DESCRIPTION_MAX_STREAMS];
	bool needMediaLocalIpRefresh = false;
	MSMediaStreamSessions sessions[SAL_MEDIA_DESCRIPTION_MAX_STREAMS]; /* The rtp, srtp, zrtp contexts for each stream */
	SalMediaDescription *localDesc = nullptr;
	int localDescChanged = 0;
	SalMediaDescription *biggestDesc = nullptr;
	SalMediaDescription *resultDesc = nullptr;
	bool expectMediaInAck = false;
	unsigned int remoteSessionId = 0;
	unsigned int remoteSessionVer = 0;

	std::string authToken;
	bool authTokenVerified = false;
	std::string dtlsCertificateFingerprint;

	unsigned int nbMediaStarts = 0;
	int upBandwidth = 0; /* Upload bandwidth setting at the time the call is started. Used to detect if it changes during a call */
	int audioBandwidth = 0; /* Upload bandwidth used by audio */

	bool allMuted = false;
	bool audioMuted = false;
290
	bool automaticallyPaused = false;
291 292 293 294 295 296 297 298 299 300 301 302
	bool pausedByApp = false;
	bool playingRingbackTone = false;
	bool recordActive = false;

	std::string onHoldFile;

	L_DECLARE_PUBLIC(MediaSession);
};

LINPHONE_END_NAMESPACE

#endif // ifndef _MEDIA_SESSION_P_H_