source: verona/phcpp/phapipp.cpp @ 499:8844ce6b2c20

Last change on this file since 499:8844ce6b2c20 was 499:8844ce6b2c20, checked in by Vadim Lebedev <vadim@…>, 12 months ago

implement phAnswerMessage to allow explicity generateion of answers to incoming MESSAGE requests

File size: 14.8 KB
Line 
1/*
2  The phapipp module implements thin C++ wrapper around phapi
3  Copyright (C) 2007  Vadim Lebedev  <vadim@mbdsys.com>
4
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20
21#include <assert.h>
22#include <stdlib.h>
23#include <string.h>
24#include <stdio.h>
25
26#include "phapipp.h"
27
28#if defined(_WIN32)
29#include <windows.h>
30# ifdef _WIN32_WCE
31#  include "connmgr_status.h"
32# endif
33#define strdup _strdup
34#endif
35
36namespace verona {
37
38static phapi *uniqueobj;
39
40phapi::phapi()
41{
42        assert(uniqueobj == 0);
43        uniqueobj = this;
44}
45
46
47phapi::~phapi()
48{
49        assert(uniqueobj == this);
50        uniqueobj = 0;
51}
52
53
54
55int phapi::init(bool asyncMode)
56{
57        return phInit(&callbacks, 0, asyncMode);
58}
59
60
61void phapi::terminate()
62{
63        phTerminate();
64}
65
66
67
68int phapi::addAuthInfo(const char* username, const char* userid,  const char* passwd,
69                const char *realm, const char* ha1)
70{
71        return phAddAuthInfo(username, userid, passwd, ha1, realm);
72}
73
74int phapi::listenAddr(int local_sip_port)
75{
76        return phListenAddr(local_sip_port);
77}
78
79int phapi::addVline(const char* displayname, const char* username, const char* host,
80                const char*  proxy, int regTimeout, int mobility)
81{
82        return phAddVline3(displayname, username, host, proxy, regTimeout, mobility);
83}
84
85int phapi::delVline(int vlid)
86{
87        return phDelVline(vlid);
88}
89
90int phapi::vlRegister(int vlid)
91{
92        return phvlRegister(vlid);
93}
94
95int phapi::sendOptions(int vlid, const char *to)
96{
97        return phLineSendOptions(vlid, to);
98}
99
100int phapi::placeCall(int vlid, const char*  uri, void* userdata, int rcid, int streams,
101                const char* adev, const char* audio_addr, const char* video_addr)
102{
103        return phLinePlaceCall4(vlid, uri, userdata, rcid, streams, adev, audio_addr, video_addr);
104}
105
106
107int phapi::acceptCall(int cid, void* userData, int streams,
108                const char* audio_addr, const char* video_addr)
109{
110        return phAcceptCall4(cid, userData, streams, audio_addr, video_addr);
111}
112
113
114int phapi::rejectCall(int cid, int reason, const char *uri)
115{
116        return uri ? phRejectCall(cid, reason) : phRejectCall2(cid, uri, reason);
117}
118
119
120int phapi::ringingCall(int cid)
121{
122        return phRingingCall(cid);
123}
124
125int phapi::closeCall(int cid)
126{
127        return phCloseCall(cid);
128
129}
130
131int phapi::holdCall(int cid)
132{
133        return phHoldCall(cid);
134}
135
136int phapi::resumeCall(int cid)
137{
138        return phResumeCall(cid);
139}
140
141int phapi::blindTransferCall(int cid, const char *uri)
142{
143        return phBlindTransferCall(cid, uri);
144}
145
146int phapi::sendDtmf(int cid, int dtmfChar, int mode)
147{
148        return phSendDtmf(cid, dtmfChar, mode);
149}
150
151int phapi::messageInCall(int cid, const char *mime,  const char *buff)
152{
153        return phCallSendMessage(cid, (char*) mime, (char*) buff);
154}
155
156
157int phapi::getSipCallID(int cid, char *idbuf,  int bufsize)
158{
159        return phCallGetSipCallID(cid, idbuf, bufsize);
160}
161
162int phapi::setFollowMe(int vlid, const char *uri)
163{
164        return phLineSetFollowMe(vlid, uri);
165}
166
167int phapi::setBusy(int vlid, bool busy)
168{
169        return phLineSetBusy(vlid, busy);
170}
171
172int phapi::sendMessage(int vlid, const char* to,  const char* buff, const char* mime,
173                const char* target, const char* sipcid)
174{
175        return phLineSendMessage2(vlid, target, to, buff, mime, sipcid);
176}
177
178int phapi::sendMessage3(int vlid, const char* to,  const char* buff, const char* mime,
179                const std::vector<std::pair<const char*, const char* > >& hdrs, const char* target)
180{
181        return phLineSendMessage3(vlid, target, to, buff, mime, hdrs.size(), (const struct ph_hdr_val*) &hdrs.front() );
182}
183
184int phapi::answerMessage(int tid, int code)
185{
186        return phAnswerMessage(tid, code);
187}
188
189
190int phapi::subscribe(int vlid, const char* to, int winfo, int use_proxy, int expire,
191                const std::vector<std::pair<const char*, const char* > >* hdrs)
192{
193        return phLineSubscribe3(vlid, to, winfo, use_proxy, expire, hdrs ? hdrs->size() : 0,
194                        hdrs ? (const struct ph_hdr_val*) &hdrs->front() : 0);
195}
196
197
198int phapi::unsubscribe(int sid, int winfo)
199{
200        return phLineUnsubscribe(sid, winfo);
201}
202
203
204int phapi::publish(int vlid, const char* to, const char* evt, const char* ctt, const char* content,
205                int expires)
206{
207        if (expires == -1)
208                expires = 600;
209
210        return phLinePublish2(vlid, to, evt, ctt, content, expires);
211}
212
213
214int phapi::poll()
215{
216        return phPoll();
217}
218
219void phapi::refresh()
220{
221        phRefresh();
222}
223
224
225void phapi::setLogFile(const char* logFile)
226{
227        phSetLogFileName(logFile);
228}
229
230
231int phapi::mobility(int vlid)
232{
233        return phLineGetMobility(vlid);
234
235}
236
237int phapi::setUsername2(int vlid, const char* username)
238{
239        return phLineSetUsername2(vlid, username);
240}
241
242
243
244int phapi::getLineSipAddress(int vlid, char *buf, int size)
245{
246        return phLineGetSipAddress(vlid, buf, size);
247}
248
249void phapi::setUaString(const char *uastr)
250{
251        phSetUaString(uastr);
252}
253
254void phapi::changeAudioCodecList(const char *audio_codecs)
255{
256        ph_config_s *phcfg = ph_get_config();
257        const size_t S = sizeof(phcfg->audio_codecs);
258
259        strncpy(phcfg->audio_codecs, audio_codecs, S);
260        phcfg->audio_codecs[S-1] = 0;
261}
262
263void phapi::changeVideoCodecList(const char *video_codecs)
264{
265        ph_config_s *phcfg = ph_get_config();
266        const size_t S = sizeof(phcfg->video_codecs);
267       
268        strncpy(phcfg->video_codecs, video_codecs, S);
269        phcfg->video_codecs[S-1] = 0;
270}
271       
272#if defined(_WIN32_WCE)
273char * phapi::getLocalIpFromDescription(const char *description)
274{
275        DWORD pcbBufferSize = 0;
276        ConnMgrQueryDetailedStatus(0,&pcbBufferSize);
277        CONNMGR_CONNECTION_DETAILED_STATUS *  pStatusBuffer = (CONNMGR_CONNECTION_DETAILED_STATUS *)calloc(pcbBufferSize, 1);
278
279        if(!ConnMgrQueryDetailedStatus(pStatusBuffer,&pcbBufferSize))
280        {
281                for (CONNMGR_CONNECTION_DETAILED_STATUS * curItem = pStatusBuffer; curItem != 0; curItem = curItem->pNext)
282                {
283                        WCHAR NameBuffer[512];
284                        mbstowcs(NameBuffer,description,512);
285
286                        if (curItem->pIPAddr != NULL && (curItem->szAdapterName && !wcscmp(NameBuffer,curItem->szAdapterName) || curItem->szDescription && !wcscmp(NameBuffer,curItem->szDescription)))
287                        {
288                                WCHAR addressAsString[128];
289                                DWORD addressSize = sizeof(addressAsString);
290
291                                WSAAddressToString((LPSOCKADDR)curItem->pIPAddr->IPAddr,128,0,addressAsString,&addressSize);
292
293                                char *local_ip = new char[128];
294                                WideCharToMultiByte(CP_ACP, 0, addressAsString, -1,local_ip, 128, NULL, NULL);
295                                return local_ip;
296                        }
297                }
298        }
299        return "";
300}
301#endif
302
303void phapi::changeLocalIP(const char *local_ip)
304{
305        ph_config_s *phcfg = ph_get_config();
306        const size_t S = sizeof(phcfg->local_ip_addr);
307        strncpy(phcfg->local_ip_addr, local_ip, S);
308        phcfg->local_ip_addr[S-1] = 0;
309}
310
311std::map<char*, char*> phapi::getAudioCaptureDevices()
312{
313        ph_audio_card_desc_t* device_tab = NULL;
314        int pos = 0;
315        int i = 0;
316        std::map<char*, char*> device_map;
317
318        pos = phAudioCaptureCardList(&device_tab);
319
320        for (i = 0; i < pos; i++)
321        {
322                device_map.insert(std::pair<char*, char*>(device_tab[i].id, device_tab[i].name));
323                printf("MSSndCard id:%s name:%s !!!!\n", device_tab[i].id, device_tab[i].name);
324        }
325       
326        if(device_tab)
327                delete device_tab;
328       
329        return device_map;
330}
331
332std::map<char*, char*> phapi::getVideoWebcamDevices()
333{
334        ph_video_web_cam_desc_t* device_tab = NULL;
335        int pos =0;
336        int i = 0;
337        std::map<char *, char*> device_map;
338               
339        pos = phVideoWebcamList(&device_tab);
340               
341        for (i = 0; i < pos; i++)
342        {
343                device_map.insert(std::pair<char*, char*>(device_tab[i].id, device_tab[i].name));
344                printf("webcam id:%s name:%s !!!!\n", device_tab[i].id, device_tab[i].name);
345        }
346       
347        if(device_tab)
348                delete device_tab;
349       
350        return device_map;
351}
352       
353std::map<char*, char*> phapi::getAudioPlaybackDevices()
354{
355        ph_audio_card_desc_t* device_tab = NULL;
356        int pos = 0;
357        int i = 0;
358        std::map<char*, char*> device_map;
359
360        pos = phAudioPlaybackCardList(&device_tab);
361
362        for (i = 0; i < pos; i++)
363        {
364                device_map.insert(std::pair<char*, char*>(device_tab[i].id, device_tab[i].name));
365        }
366       
367        if(device_tab)
368                delete device_tab;
369       
370        return device_map;
371}
372
373void phapi::changeAudioDeviceIn(const char *devstr)
374{
375        phChangeAudioDeviceIn(devstr);
376}
377
378void phapi::changeAudioDeviceOut(const char *devstr)
379{
380        phChangeAudioDeviceOut(devstr);
381}
382       
383void phapi::changeVideoDevices(const char *devstr)
384{
385        phChangeVideoDevices(devstr);
386}
387       
388int phapi::setSoftRecvVolumeGain(int cid, float gain)
389{
390        return phAudioSoftRecvVolumeGain(cid, gain);
391}
392
393int phapi::setSoftSendVolumeGain(int cid, float gain)
394{
395        return phAudioSoftSendVolumeGain(cid, gain);
396}
397
398int phapi::setRecvVolumeLevel(int cid, int level)
399{
400        return phAudioRecvVolumeLevel(cid, level);
401}
402
403int phapi::setSendVolumeLevel(int cid, int level)
404{
405        return phAudioSendVolumeLevel(cid, level);
406}
407
408int phapi::mutePlayback(int cid, int val)
409{
410        return phAudioMutePlayback(cid, val);
411}
412
413int phapi::muteCapture(int cid, int val)
414{
415        return phAudioMuteCapture(cid, val);
416}
417
418int phapi::setSoundFileGain(phStream* phstream,float gain)
419{
420        return phSetSoundFileGain(phstream,gain);
421}
422       
423phStream * phapi::playSoundFile(const char * filename, int interval, const char* deviceid)
424{
425        return phPlaySoundFile(filename,interval,deviceid);
426}
427       
428int phapi::stopSoundFile(phStream * phstream)
429{
430        return phStopSoundFile(phstream);
431}
432
433phStream * phapi::videoStartPreview(const char* deviceid)
434{
435        phStream * stream = phVideoStartPreview(deviceid);
436        return stream;
437}
438       
439int phapi::videoStopPreview(phStream * phstream)
440{
441        return phVideoStopPreview(phstream);
442}
443
444int phapi::videoSetDefaultImage(const char* path)
445{
446        return phVideoSetDefaultImage(path);
447}
448
449void phapi::onCallProgress(int cid, const phCallStateInfo_t *info)
450{
451        if(info->event == phDTMF)
452                onDtmfProgress(cid,info->dtmfDigit);
453        else if(info->event == phCALLCLOSED || info->event == phCALLERROR) {
454#if defined(_WIN32_WCE)
455                onCallProgress2(cid,info->event,(wchar_t*)"",info->errorCode);
456#else
457                onCallProgress2(cid,info->event,(char*)"",info->errorCode);
458#endif
459        }else {
460#if defined(_WIN32_WCE)
461                int utfsize = MultiByteToWideChar(CP_ACP, 0, info->remoteUri, -1, NULL, 0);
462                wchar_t *tmp = (wchar_t *)alloca((utfsize + 1) * sizeof(*tmp));
463                       
464                MultiByteToWideChar(CP_ACP, 0, info->remoteUri, -1,tmp, utfsize);
465                onCallProgress2(cid,info->event, tmp,info->errorCode);
466#else
467                onCallProgress2(cid,info->event,(char*)info->remoteUri,info->errorCode);
468#endif
469        }
470}
471
472#if defined(_WIN32_WCE)
473void phapi::onCallProgress2(int cid, phCallStateEvent status, wchar_t * uri, int error)
474#else
475void phapi::onCallProgress2(int cid, phCallStateEvent status, char * uri, int error)
476#endif
477{
478
479}
480
481void phapi::onDtmfProgress(int cid, int)
482{
483
484}
485
486void phapi::onTransferProgress(int cid, const phTransferStateInfo_t *info)  { }
487void phapi::onConfProgress(int cfid, const phConfStateInfo_t *info)  { }
488void phapi::onRegProgress(int regid, int status)  { }
489void phapi::onMsgProgress(int mid,  const phMsgStateInfo *info)
490{
491        onMsgProgress2(mid,     (char *)info->rawctt, (char *)info->content);
492}
493void phapi::onMsgProgress2(int mid, char * type, char * content) { }
494void phapi::onConnectionLost(const char* host, int port) { }
495#if defined(_WIN32_WCE)
496void phapi::onNotifyProgress (const char* event, const char* from, const char *ctt, wchar_t* content)  { }
497#else
498void phapi::onNotifyProgress (const char* event, const char* from, const char *ctt, const char* content)  { }
499#endif
500void phapi::onSubscriptionProgress(int sid,  const phSubscriptionStateInfo_t *info)  { }
501void phapi::onFrameReady(int cid, phVideoFrameReceivedEvent_t *ev) { }
502void phapi::onPhLogCsl(const char *msg){ }
503
504void phapi::onSoundFileStopped(void *userdata , unsigned int _id, void *arg){}
505       
506void phapi::onQosEvent(int cid, const phQosInfo_t * info){}
507       
508void phapi::callProgress(int cid, const phCallStateInfo_t *info)
509{
510        if (uniqueobj)
511                uniqueobj->onCallProgress(cid, info);
512}
513
514void phapi::transferProgress(int cid, const phTransferStateInfo_t *info)
515{
516        if (uniqueobj)
517                uniqueobj->onTransferProgress(cid, info);
518
519}
520
521
522void phapi::confProgress(int cfid, const phConfStateInfo_t *info)
523{
524        if (uniqueobj)
525                uniqueobj->onConfProgress(cfid, info);
526
527}
528
529void phapi::regProgress(int regid, int status)
530{
531        if (uniqueobj)
532                uniqueobj->onRegProgress(regid, status);
533
534}
535
536void phapi::msgProgress(int mid,  const phMsgStateInfo_t *info)
537{
538        if (uniqueobj) {
539                uniqueobj->onMsgProgress(mid, info);
540        }
541
542}
543
544void phapi::connectionLost(const char *host, int port)
545{
546        if (uniqueobj)
547                uniqueobj->onConnectionLost(host, port);
548}
549
550void phapi::notifyProgress (const char* event, const char* from, const char *ctt, const char* content)
551{
552#if defined(_WIN32_WCE)
553        int utfsize = MultiByteToWideChar(CP_ACP, 0, content, -1, NULL, 0);
554        wchar_t *tmp = (wchar_t *)alloca((utfsize + 1) * sizeof(*tmp));
555                       
556        MultiByteToWideChar(CP_ACP, 0, content, -1,tmp, utfsize);
557
558        if (uniqueobj)
559                uniqueobj->onNotifyProgress(event, from, ctt, tmp);
560#else
561        if (uniqueobj)
562                uniqueobj->onNotifyProgress(event, from, ctt, content);
563#endif
564
565}
566
567void phapi::subscriptionProgress(int sid,  const phSubscriptionStateInfo_t *info)
568{
569        if (uniqueobj)
570                uniqueobj->onSubscriptionProgress(sid, info);
571
572}
573
574
575void phapi::frameReady(int cid, phVideoFrameReceivedEvent_t *ev)
576{
577        if (uniqueobj)
578                uniqueobj->onFrameReady(cid, ev);
579
580}
581
582void phapi::phLogCsl(const char *msg)
583{
584        if (uniqueobj)
585                uniqueobj->onPhLogCsl(msg);
586}
587
588void phapi::soundFileStopped(void *userdata , unsigned int _id, void *arg)
589{
590        if (uniqueobj)
591                uniqueobj->onSoundFileStopped(userdata,_id,arg);
592}
593       
594void phapi::qosEvent(int cid, const phQosInfo_t * info)
595{
596        if (uniqueobj)
597                uniqueobj->onQosEvent(cid, info);
598}
599
600phCallbacks_t phapi::callbacks = {
601                phapi::callProgress,
602                phapi::transferProgress,
603                phapi::confProgress,
604                phapi::regProgress,
605                phapi::msgProgress,
606                phapi::connectionLost,
607                phapi::notifyProgress,
608                phapi::subscriptionProgress,
609                phapi::frameReady,
610                0,
611#ifdef HAVE_CSL
612                phapi::phLogCsl,
613#endif /*!HAVE_CSL*/
614                phapi::soundFileStopped,
615                phapi::qosEvent
616};
617
618
619}
620/*
621
622
623#ifdef TEST
624
625using namespace verona;
626
627stdphapi* myphapi = new stdphapi;
628
629
630void testAuth(const std::string& name, const std::string& id, const std::string& pw, const std::string& realm)
631{
632  myphapi->addAuthInfo(name, id, pw, realm);
633}
634
635
636int main(int argc, char *argv[])
637{
638
639}
640
641#endif
642 */
Note: See TracBrowser for help on using the repository browser.