Home2L - C/C++ API v1.4-0-g38cc (2024-05-25)
Smart Tools for a Private Home
phone.H
Go to the documentation of this file.
1/*
2 * This file is part of the Home2L project.
3 *
4 * (C) 2015-2021 Gundolf Kiefer
5 *
6 * Home2L is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Home2L is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Home2L. If not, see <https://www.gnu.org/licenses/>.
18 *
19 */
20
21
22#ifndef _PHONE_
23#define _PHONE_
24
25
45/* Unfortunately, 'liblinphone' and friends make some definitions (e.g. for bool)
46 * that conflict with other libraries and the main program. Hence, this module also acts
47 * as a demilitarized zone. The header must not include any home2l-related basic headers.
48 */
49
50
51#include <stdint.h>
52#include <pthread.h>
53
54
55
56
57// *************************** Environment settings ****************************
58
59
60// (read-only; primarily needed by the phone drivers themselves)
61extern const char *envPhoneLinphonerc;
62
63extern int envPhoneSipPort;
64
65extern const char *envPhoneAudioDriver;
66extern const char *envPhoneAudioDevice;
67extern const char *envPhoneAudioInDevice;
68extern const char *envPhoneAudioOutDevice;
69extern float envPhoneAudioInGain;
70extern float envPhoneAudioOutGain;
71extern const char *envPhoneAudioCodec;
72
73extern int envPhoneEchoTail;
74extern int envPhoneEchoAlgo;
75extern int envPhoneEchoAggressiveness;
76extern bool envPhoneEchoNoiseSuppression;
77
78extern const char *envPhoneVideoDriver;
79extern const char *envPhoneVideoDevice;
80extern const char *envPhoneVideoCodec;
81
82extern const char *envPhoneRegister;
83extern const char *envPhoneSecret;
84
85extern const char *envPhoneRingbackFile;
86extern const char *envPhoneRingbackFileKey;
87extern float envPhoneRingbackLevel;
88
89extern int envPhoneRotation;
90
91
92
93
94
95// *************************** Video driver(s) *********************************
96
97
102 pvfNone = 0,
103 pvfABGR8888,
104 pvfBGR24,
105 pvfARGB8888,
106 pvfRGB24,
107 pvfYUY2,
108 pvfUYVY,
109 pvfYVYU,
110 pvfIYUV,
111 pvfYV12
112};
113
114
117
118
121 bool changed;
122
127 int w, h;
129
136 uint8_t *data;
137 int pitch;
138
139 uint8_t *planeY, *planeU, *planeV;
140 int pitchY, pitchU, pitchV;
142};
143
144
145static inline void PhoneVideoFrameInit (TPhoneVideoFrame *vf) {
146 vf->changed = false;
147
148 vf->format = pvfNone;
149 vf->w = vf->h = 0;
150
151 vf->data = NULL;
152 vf->pitch = 0;
153
154 vf->planeY = vf->planeU = vf->planeV = NULL;
155 vf->pitchY = vf->pitchU = vf->pitchV = 0;
156}
157
158
159
160
161
162// *************************** CPhone ******************************************
163
164// The class 'CPhone' represents a telephone. It contains two "Call"
165// objects (#0 and #1). This is to support the following scenarios:
166//
167// a) Simple calls: only call #0 is used, #1 remains inactive.
168//
169// b) Transfer calls: #0 is the new call (or none), #1 the paused one.
170// Swapping between slots #0 and #1 happens on pausing/resuming.
171//
172// c) Camera preview for the door phone using a second call is presently
173// unsupported.
174// Current solution for door cam: Use primary call (no second call),
175// accept with mic muted. Drawback: Visitor recognizes accept action.
176//
177// d) Machines with multiple logical phones (door/gate phone) may use multiple
178// 'CPhone' objects.
179//
180// NOTE on the use of audio / camera devices by other program modules / threads
181// (e.g. application-programmed ringing, music playback):
182//
183// 1. To avoid conflicts, other modules must ensure that during the invocation
184// of any action ('Dial', 'AcceptCall', enabling auto-acceptance),
185// such devices are closed by the other modules.
186//
187// 2. Devices may be used by the other modules only after a "device-permitting"
188// state has been reported again via 'ReportState ()'. The "device-permitting"
189// states are: 'psIdle', 'psRinging'.
190//
191// To avoid race conditions, the following contract applies:
192// - After returning from 'CPhone::Dial ()', the state is already 'psDialing'
193// (i.e. 'ReportState ()' is called from 'CPhone::Dial ()').
194// - 'AcceptCall ()' is only called when in state 'psRinging'.
195// - If 'SetIncomingCallAction (psInCall)' is called, the application does
196// not use any device at all.
197
198
201 psNone = 0,
211
212
213static inline bool PhoneStateIsDevicePermitting (EPhoneState s) { return s == psIdle || s == psRinging; }
216
217
220 pmNone = 0,
226 pmVideo = 12,
227 pmAll = 15
229#define pmOff pmNone
230#define pmOn pmAll
231
232
236class CPhone {
237 public:
238 CPhone () { Init (); }
239 ~CPhone () { Done (); }
240 void Init ();
241 void Done ();
242
245
246 void Iterate ();
247
249
250
253
254 void Setup (const char *agentName, int _mediaSelected, int withLogging,
255 const char *tmpDir);
274
275 bool Register (const char *identity = NULL, const char *secret = NULL);
285
287
288
298
299 bool Dial (const char *url);
310 bool AcceptCall ();
317 bool Hangup ();
330
338
339 bool SendDtmf (const char *dtmfSequence);
341
343
344
347
348 void SetIncomingCallAction (EPhoneState _incomingAction) { incomingAction = _incomingAction; }
356 EPhoneState GetIncomingCallAction () { return incomingAction; }
357
358 void SetAutoAccept () { SetIncomingCallAction (psInCall); }
359 void SetAutoReject () { SetIncomingCallAction (psIdle); }
360 void SetManualAccept () { SetIncomingCallAction (psRinging); }
361
363
364
367
368 void SelectMedia (unsigned selected, unsigned mask = pmAll);
369 unsigned GetMediaSelected () { return mediaSelected; }
370
371 void SetMicOn (bool on) { SelectMedia (on ? pmOn : pmOff, pmAudioIn); }
372 bool GetMicOn () { return GetMediaSelected () & pmAudioIn ? true : false; }
373 void SetCamOn (bool on) { SelectMedia (on ? pmOn : pmOff, pmVideoIn); }
374 bool GetCamOn () { return GetMediaSelected () & pmVideoIn ? true : false; }
375
377
378
386
387 // Phone state...
388 EPhoneState GetState () { return state; }
389
390 // Callbacks...
391 virtual void OnPhoneStateChanged (EPhoneState oldState);
392 void SetCbPhoneStateChanged (void (*_cbPhoneStateChanged) (void *, EPhoneState), void *data = NULL) { cbPhoneStateChanged = _cbPhoneStateChanged; cbPhoneStateChangedData = data; }
394
395 virtual void OnInfo (const char *msg);
396 void SetCbInfo (void (*_cbInfo) (void *, const char *), void *data = NULL) { cbInfo = _cbInfo; cbInfoData = data; }
398
399 virtual void OnDtmfReceived (char dtmf);
400 void SetCbDtmfReceived (void (*_cbDtmfReceived) (void *, char), void *data = NULL) { cbDtmfReceived = _cbDtmfReceived; cbDtmfReceivedData = data; }
402
403 // Information...
404 int GetCallDuration (int callId = 0);
407 const char *GetPeerUrl (int callId = 0);
414
416
417
429 void VideoUnlock ();
432
433
439 void ReportState (EPhoneState _state);
440 void ReportInfo (const char *fmt, ...);
441 void *GetLibData () { return libData; }
442 const int GetLibDataSize () { return sizeof (libData); }
444
445 protected:
446
447 // Various state fields...
448 EPhoneState state;
449 int mediaSelected;
450 EPhoneState incomingAction;
451
452 // Callbacks...
453 void *cbPhoneStateChangedData, *cbInfoData, *cbDtmfReceivedData;
454 void (*cbPhoneStateChanged) (void *, EPhoneState);
455 void (*cbInfo) (void *, const char *);
456 void (*cbDtmfReceived) (void *, char);
457
458 // Video stream...
459 TPhoneVideoFrame picInfo; // contains 'updated' field
460
461 // Library-specific data...
462#define LIBDATA_SIZE 64
463 uint8_t libData [LIBDATA_SIZE];
464};
465
466
468
469
470#endif
IP Telephone.
Definition: phone.H:236
TPhoneVideoFrame * VideoLockFrame(int streamId)
Lock the video stream and return info about the current picture.
bool AcceptCall()
Green button on the phone: Accept an incoming call.
void VideoUnlock()
Unlock the video stream.
bool SendDtmf(const char *dtmfSequence)
Send a sequence of DTMF characters.
void SetCbPhoneStateChanged(void(*_cbPhoneStateChanged)(void *, EPhoneState), void *data=NULL)
Callback on a phone state change; the second parameter is the previous state.
Definition: phone.H:392
void SetIncomingCallAction(EPhoneState _incomingAction)
Set decision on what to do with an incoming call in form of a desired next state:
Definition: phone.H:348
bool Hangup()
Red button on the phone: Cancel a call or a dialing process. If there is a paused call,...
bool CancelAllCalls()
Cancel all active calls, try to reach the idle state as soon as possible.
int GetCallDuration(int callId=0)
Get call duration in seconds.
void SetCbInfo(void(*_cbInfo)(void *, const char *), void *data=NULL)
Callback on the receipt of a new info message (should be displayed on the UI).
Definition: phone.H:396
EPhoneState GetState()
Get current phone state.
Definition: phone.H:388
void SetCbDtmfReceived(void(*_cbDtmfReceived)(void *, char), void *data=NULL)
Callback on the receipt of a DTMF character.
Definition: phone.H:400
void Setup(const char *agentName, int _mediaSelected, int withLogging, const char *tmpDir)
Setup phone object.
void Iterate()
Iterate the backend and update the phone state; must be called regularly.
bool PrepareTransfer()
Pause (hold) the current call and set phone into the state 'psTransferIdle' to initiate an attended t...
bool Dial(const char *url)
Dial the number or URL.
bool CompleteTransfer()
Transfer the paused call to the active one (complete the transfer). If called when still dialing,...
bool Register(const char *identity=NULL, const char *secret=NULL)
Register phone with a registrar.
const char * GetPeerUrl(int callId=0)
Get the URL of the peer.
EPhoneState
Current phone state.
Definition: phone.H:200
EPhoneMedia
Media mask.
Definition: phone.H:219
const char * StrPhoneVideoFormat(EPhoneVideoFormat x)
Get a readable string for the format.
EPhoneVideoFormat
Video frame pixel format. The values refer to the respective SDL pixel format types of a similar name...
Definition: phone.H:101
static bool PhoneStateIsDevicePermitting(EPhoneState s)
Return whether the caller is allowed to use the media (audio/video) devices that may otherwise be occ...
Definition: phone.H:213
@ psTransferIdle
Primary call is paused, UI may query a new number to transfer to.
Definition: phone.H:206
@ psInCall
A call is active.
Definition: phone.H:205
@ psRinging
This phone is ringing.
Definition: phone.H:204
@ psTransferAutoComplete
Same as 'psTransferDialing', but on remote pickup the transfer will complete automatically.
Definition: phone.H:208
@ psDialing
Phone is dialing, remote end may be ringing.
Definition: phone.H:203
@ psNone
No state (e.g. phone not initialized).
Definition: phone.H:201
@ psTransferDialing
Primary call is paused, a new call is in the dialing state.
Definition: phone.H:207
@ psTransferInCall
Primary call is paused, secondary call is active; hanging up results in completing the transfer.
Definition: phone.H:209
@ psIdle
Phone is idle.
Definition: phone.H:202
@ pmNone
Nothing.
Definition: phone.H:220
@ pmAudio
Microphone and Speaker.
Definition: phone.H:223
@ pmVideoOut
Display.
Definition: phone.H:225
@ pmAudioOut
Speaker.
Definition: phone.H:222
@ pmAudioIn
Microphone.
Definition: phone.H:221
@ pmVideo
Camera and Display.
Definition: phone.H:226
@ pmAll
Everything.
Definition: phone.H:227
@ pmVideoIn
Camera.
Definition: phone.H:224
Data structure to pass video frames to the UI.
Definition: phone.H:120
int pitch
number of bytes between rows (for 'data != NULL')
Definition: phone.H:137
bool changed
Set on change, reset on fetching.
Definition: phone.H:121
uint8_t * planeV
color planes (Y, U, V) (for 'SDL_UpdateYUVTexture()')
Definition: phone.H:139
uint8_t * data
Data, if all planes contiguous in memory (for 'SDL_UpdateTexture()')
Definition: phone.H:136
int h
Image dimensions.
Definition: phone.H:127
int pitchV
number of bytes between rows (for 'SDL_UpdateYUVTexture()')
Definition: phone.H:140
EPhoneVideoFormat format
Pixel format.
Definition: phone.H:126