media-session-p.h 13.8 KB
Newer Older
1 2
/*
 * media-session-p.h
Ronan's avatar
Ronan committed
3
 * Copyright (C) 2010-2018 Belledonne Communications SARL
4
 *
Ghislain MARY's avatar
Ghislain MARY committed
5 6 7 8
 * 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 2
 * of the License, or (at your option) any later version.
9 10 11 12 13 14 15
 *
 * 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
Ghislain MARY's avatar
Ghislain MARY committed
16 17
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 19
 */

20 21
#ifndef _L_MEDIA_SESSION_P_H_
#define _L_MEDIA_SESSION_P_H_
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

#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"

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

LINPHONE_BEGIN_NAMESPACE

class MediaSessionPrivate : public CallSessionPrivate {
public:
38 39
	static int resumeAfterFailedTransfer (void *userData, unsigned int);
	static bool_t startPendingRefer (void *userData);
40 41
	static void stunAuthRequestedCb (void *userData, const char *realm, const char *nonce, const char **username, const char **password, const char **ha1);

42 43
	void accepted () override;
	void ackReceived (LinphoneHeaders *headers) override;
Ghislain MARY's avatar
Ghislain MARY committed
44
	void dtmfReceived (char dtmf);
45
	bool failure () override;
46
	void pauseForTransfer ();
47
	void pausedByRemote ();
48
	void remoteRinging () override;
49
	void replaceOp (SalCallOp *newOp) override;
50
	int resumeAfterFailedTransfer ();
51
	void resumed ();
52
	void startPendingRefer ();
Ghislain MARY's avatar
Ghislain MARY committed
53
	void telephoneEventReceived (int event);
54
	void terminated () override;
55
	void updated (bool isUpdate);
56
	void updating (bool isUpdate) override;
57 58

	void enableSymmetricRtp (bool value);
Ghislain MARY's avatar
Ghislain MARY committed
59
	void oglRender () const;
60 61 62 63 64 65 66
	void sendVfu ();

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

67
	int getAf () const { return af; }
68

69
	bool getAudioMuted () const { return audioMuted; }
70

71 72 73
	MediaSessionParams *getCurrentParams () const { return static_cast<MediaSessionParams *>(currentParams); }
	MediaSessionParams *getParams () const { return static_cast<MediaSessionParams *>(params); }
	MediaSessionParams *getRemoteParams () const { return static_cast<MediaSessionParams *>(remoteParams); }
74
	void setCurrentParams (MediaSessionParams *msp);
75
	void setParams (MediaSessionParams *msp);
76
	void setRemoteParams (MediaSessionParams *msp);
77

78
	IceSession *getIceSession () const { return iceAgent ? iceAgent->getIceSession() : nullptr; }
79

80
	SalMediaDescription *getLocalDesc () const { return localDesc; }
81

82
	unsigned int getMediaStartCount () const;
83
	MediaStream *getMediaStream (LinphoneStreamType type) const;
84
	LinphoneNatPolicy *getNatPolicy () const { return natPolicy; }
85

86 87
	int getRtcpPort (LinphoneStreamType type) const;
	int getRtpPort (LinphoneStreamType type) const;
88
	LinphoneCallStats *getStats (LinphoneStreamType type) const;
89 90
	int getStreamIndex (LinphoneStreamType type) const;
	int getStreamIndex (MediaStream *ms) const;
91
	SalCallOp * getOp () const { return op; }
92
	MSWebCam *getVideoDevice () const;
93
	void setAudioMuted (bool value) { audioMuted = value; }
94

Ghislain MARY's avatar
Ghislain MARY committed
95 96 97
	void initializeStreams ();
	void stopStreams ();

98 99 100 101 102 103 104 105 106
	// Methods used by testers
	void addLocalDescChangedFlag (int flag) { localDescChanged |= flag; }
	belle_sip_source_t *getDtmfTimer () const { return dtmfTimer; }
	const std::string &getDtmfSequence () const { return dtmfSequence; }
	int getMainAudioStreamIndex () const { return mainAudioStreamIndex; }
	int getMainTextStreamIndex () const { return mainTextStreamIndex; }
	int getMainVideoStreamIndex () const { return mainVideoStreamIndex; }
	SalMediaDescription *getResultDesc () const { return resultDesc; }

107
	// CoreListener
Ghislain MARY's avatar
Ghislain MARY committed
108
	void onNetworkReachable (bool sipNetworkReachable, bool mediaNetworkReachable) override;
109

110 111 112
	// Call listener
	void snapshotTakenCb(void *userdata, struct _MSFilter *f, unsigned int id, void *arg);

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

Ghislain MARY's avatar
Ghislain MARY committed
116 117 118 119 120 121
#ifdef VIDEO_ENABLED
	static void videoStreamEventCb (void *userData, const MSFilter *f, const unsigned int eventId, const void *args);
#endif // ifdef VIDEO_ENABLED
#ifdef TEST_EXT_RENDERER
	static void extRendererCb (void *userData, const MSPicture *local, const MSPicture *remote);
#endif // ifdef TEST_EXT_RENDERER
122
	static void realTimeTextCharacterReceived (void *userData, MSFilter *f, unsigned int id, void *arg);
Ghislain MARY's avatar
Ghislain MARY committed
123
	static int sendDtmf (void *data, unsigned int revents);
124 125
	static float aggregateQualityRatings (float audioRating, float videoRating);

126
	std::shared_ptr<Participant> getMe () const;
127
	void setState (CallSession::State newState, const std::string &message) override;
128 129 130

	void computeStreamsIndexes (const SalMediaDescription *md);
	void fixCallParams (SalMediaDescription *rmd);
131
	void initializeParamsAccordingToIncomingCallParams () override;
132 133 134 135 136
	void setCompatibleIncomingCallParams (SalMediaDescription *md);
	void updateBiggestDesc (SalMediaDescription *md);
	void updateRemoteSessionIdAndVer ();

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

139 140
	OrtpEvQueue *getEventQueue (int streamIndex) const;
	MediaStream *getMediaStream (int streamIndex) const;
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

	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);
166
	void setupLimeIdentityKey (SalMediaDescription *md);
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
	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);
185
	RtpProfile *makeProfile (const SalMediaDescription *md, const SalStreamDescription *desc, int *usedPt);
186 187 188 189 190 191 192 193 194
	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);
195 196
	RtpSession *createAudioRtpIoSession ();
	RtpSession *createVideoRtpIoSession ();
197 198 199 200 201 202 203 204 205
	void freeResources ();
	void handleIceEvents (OrtpEvent *ev);
	void handleStreamEvents (int streamIndex);
	void initializeAudioStream ();
	void initializeTextStream ();
	void initializeVideoStream ();
	void prepareEarlyMediaForking ();
	void postConfigureAudioStreams (bool muted);
	void setSymmetricRtp (bool value);
206
	void setupRingbackPlayer ();
207 208
	void startAudioStream (CallSession::State targetState, bool videoWillBeUsed);
	void startStreams (CallSession::State targetState);
209
	void startTextStream ();
210
	void startVideoStream (CallSession::State targetState);
211 212 213 214 215
	void stopAudioStream ();
	void stopTextStream ();
	void stopVideoStream ();
	void tryEarlyMediaForking (SalMediaDescription *md);
	void updateFrozenPayloads (SalMediaDescription *result);
216
	void updateStreams (SalMediaDescription *newMd, CallSession::State targetState);
217 218 219 220 221 222 223 224
	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;
Benjamin REIS's avatar
Benjamin REIS committed
225
	unsigned int getNbActiveStreams () const;
226 227
	bool isEncryptionMandatory () const;
	int mediaParametersChanged (SalMediaDescription *oldMd, SalMediaDescription *newMd);
228
	void addSecurityEventInChatrooms (const IdentityAddress &faultyDevice, ConferenceSecurityEvent::SecurityEventType securityEventType);
229 230 231 232 233 234 235 236 237 238
	void propagateEncryptionChanged ();

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

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

239 240 241
	void abort (const std::string &errorMsg) override;
	void handleIncomingReceivedStateInIncomingNotification () override;
	bool isReadyForInvite () const override;
242
	LinphoneStatus pause ();
243
	int restartInvite () override;
244
	void setTerminated () override;
245
	LinphoneStatus startAcceptUpdate (CallSession::State nextState, const std::string &stateInfo) override;
246
	LinphoneStatus startUpdate (const std::string &subject = "") override;
247
	void terminate () override;
248
	void updateCurrentParams () const override;
249

250
	void accept (const MediaSessionParams *params, bool wasRinging);
251
	LinphoneStatus acceptUpdate (const CallSessionParams *csp, CallSession::State nextState, const std::string &stateInfo) override;
252

253 254
	void refreshSockets ();
	void reinviteToRecoverFromConnectionLoss () override;
255
	void repairByInviteWithReplaces () override;
256

Ghislain MARY's avatar
Ghislain MARY committed
257 258 259
#ifdef VIDEO_ENABLED
	void videoStreamEventCb (const MSFilter *f, const unsigned int eventId, const void *args);
#endif // ifdef VIDEO_ENABLED
260
	void realTimeTextCharacterReceived (MSFilter *f, unsigned int id, void *arg);
Ghislain MARY's avatar
Ghislain MARY committed
261
	int sendDtmf ();
262 263 264 265 266 267

	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;

268 269
	std::weak_ptr<Participant> me;

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
	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;

292
	LinphoneNatPolicy *natPolicy = nullptr;
293 294
	std::unique_ptr<StunClient> stunClient;
	std::unique_ptr<IceAgent> iceAgent;
295

296 297 298
	// The address family to prefer for RTP path, guessed from signaling path.
	int af;

Ghislain MARY's avatar
Ghislain MARY committed
299 300 301
	std::string dtmfSequence;
	belle_sip_source_t *dtmfTimer = nullptr;

302 303 304
	std::string mediaLocalIp;
	PortConfig mediaPorts[SAL_MEDIA_DESCRIPTION_MAX_STREAMS];
	bool needMediaLocalIpRefresh = false;
305 306 307 308

	// The rtp, srtp, zrtp contexts for each stream.
	MSMediaStreamSessions sessions[SAL_MEDIA_DESCRIPTION_MAX_STREAMS];

309 310 311 312 313 314 315 316 317 318 319 320
	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;

321
	unsigned int mediaStartCount = 0;
322 323 324 325 326 327

	// Upload bandwidth setting at the time the call is started. Used to detect if it changes during a call.
	int upBandwidth = 0;

	// Upload bandwidth used by audio.
	int audioBandwidth = 0;
328 329 330

	bool allMuted = false;
	bool audioMuted = false;
331
	bool automaticallyPaused = false;
332 333
	bool pausedByApp = false;
	bool recordActive = false;
334
	bool incomingIceReinvitePending = false;
335 336 337 338 339 340 341 342

	std::string onHoldFile;

	L_DECLARE_PUBLIC(MediaSession);
};

LINPHONE_END_NAMESPACE

343
#endif // ifndef _L_MEDIA_SESSION_P_H_