~ubuntu-branches/ubuntu/quantal/psi/quantal

« back to all changes in this revision

Viewing changes to src/avcall/avcall.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2009-09-25 17:49:51 UTC
  • mfrom: (6.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20090925174951-lvm7kdap82o8xhn3
Tags: 0.13-1
* Updated to upstream version 0.13
* Set Standards-Version to 3.8.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2009  Barracuda Networks, Inc.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation, either version 2 of the License, or
 
7
 * (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
 *
 
17
 */
 
18
 
 
19
#include "avcall.h"
 
20
 
 
21
#include <stdio.h>
 
22
#include <stdlib.h>
 
23
#include <QCoreApplication>
 
24
#include <QLibrary>
 
25
#include <QDir>
 
26
#include <QtCrypto>
 
27
#include "xmpp_jid.h"
 
28
#include "jinglertp.h"
 
29
#include "../psimedia/psimedia.h"
 
30
#include "applicationinfo.h"
 
31
#include "psiaccount.h"
 
32
 
 
33
#define USE_THREAD
 
34
 
 
35
class Configuration
 
36
{
 
37
public:
 
38
        bool liveInput;
 
39
        QString audioOutDeviceId, audioInDeviceId, videoInDeviceId;
 
40
        QString file;
 
41
        bool loopFile;
 
42
        PsiMedia::AudioParams audioParams;
 
43
        PsiMedia::VideoParams videoParams;
 
44
 
 
45
        int basePort;
 
46
        QString extHost;
 
47
 
 
48
        Configuration() :
 
49
                liveInput(false),
 
50
                loopFile(false),
 
51
                basePort(-1)
 
52
        {
 
53
        }
 
54
};
 
55
 
 
56
// get default settings
 
57
static Configuration getDefaultConfiguration()
 
58
{
 
59
        Configuration config;
 
60
        config.liveInput = true;
 
61
        config.loopFile = true;
 
62
        return config;
 
63
}
 
64
 
 
65
static Configuration *g_config = 0;
 
66
 
 
67
static void ensureConfig()
 
68
{
 
69
        if(!g_config)
 
70
        {
 
71
                g_config = new Configuration;
 
72
                *g_config = getDefaultConfiguration();
 
73
        }
 
74
}
 
75
 
 
76
#ifdef GSTPROVIDER_STATIC
 
77
Q_IMPORT_PLUGIN(gstprovider)
 
78
#endif
 
79
 
 
80
#ifndef GSTPROVIDER_STATIC
 
81
static QString findPlugin(const QString &relpath, const QString &basename)
 
82
{
 
83
        QDir dir(QCoreApplication::applicationDirPath());
 
84
        if(!dir.cd(relpath))
 
85
                return QString();
 
86
        foreach(const QString &fileName, dir.entryList())
 
87
        {
 
88
                if(fileName.contains(basename))
 
89
                {
 
90
                        QString filePath = dir.filePath(fileName);
 
91
                        if(QLibrary::isLibrary(filePath))
 
92
                                return filePath;
 
93
                }
 
94
        }
 
95
        return QString();
 
96
}
 
97
#endif
 
98
 
 
99
static bool g_loaded = false;
 
100
 
 
101
static void ensureLoaded()
 
102
{
 
103
        if(!g_loaded)
 
104
        {
 
105
#ifndef GSTPROVIDER_STATIC
 
106
                QString pluginFile;
 
107
                QString resourcePath;
 
108
 
 
109
                pluginFile = qgetenv("PSI_MEDIA_PLUGIN");
 
110
                if(pluginFile.isEmpty())
 
111
                {
 
112
#if defined(Q_OS_WIN)
 
113
                        pluginFile = findPlugin(".", "gstprovider");
 
114
                        resourcePath = QCoreApplication::applicationDirPath() + "/gstreamer-0.10";
 
115
#elif defined(Q_OS_MAC)
 
116
                        pluginFile = findPlugin("../Plugins", "gstprovider");
 
117
                        resourcePath = QCoreApplication::applicationDirPath() + "/../Frameworks/gstreamer-0.10";
 
118
#else
 
119
                        pluginFile = findPlugin(ApplicationInfo::libDir() + "/plugins", "gstprovider");
 
120
#endif
 
121
                }
 
122
 
 
123
                PsiMedia::PluginResult r = PsiMedia::loadPlugin(pluginFile, resourcePath);
 
124
                if(r == PsiMedia::PluginSuccess)
 
125
                        g_loaded = true;
 
126
#else
 
127
                g_loaded = true;
 
128
#endif
 
129
                if(g_loaded)
 
130
                        ensureConfig();
 
131
        }
 
132
}
 
133
 
 
134
static JingleRtpPayloadType payloadInfoToPayloadType(const PsiMedia::PayloadInfo &pi)
 
135
{
 
136
        JingleRtpPayloadType out;
 
137
        out.id = pi.id();
 
138
        out.name = pi.name();
 
139
        out.clockrate = pi.clockrate();
 
140
        out.channels = pi.channels();
 
141
        out.ptime = pi.ptime();
 
142
        out.maxptime = pi.maxptime();
 
143
        foreach(const PsiMedia::PayloadInfo::Parameter &pip, pi.parameters())
 
144
        {
 
145
                JingleRtpPayloadType::Parameter ptp;
 
146
                ptp.name = pip.name;
 
147
                ptp.value = pip.value;
 
148
                out.parameters += ptp;
 
149
        }
 
150
        return out;
 
151
}
 
152
 
 
153
static PsiMedia::PayloadInfo payloadTypeToPayloadInfo(const JingleRtpPayloadType &pt)
 
154
{
 
155
        PsiMedia::PayloadInfo out;
 
156
        out.setId(pt.id);
 
157
        out.setName(pt.name);
 
158
        out.setClockrate(pt.clockrate);
 
159
        out.setChannels(pt.channels);
 
160
        out.setPtime(pt.ptime);
 
161
        out.setMaxptime(pt.maxptime);
 
162
        QList<PsiMedia::PayloadInfo::Parameter> list;
 
163
        foreach(const JingleRtpPayloadType::Parameter &ptp, pt.parameters)
 
164
        {
 
165
                PsiMedia::PayloadInfo::Parameter pip;
 
166
                pip.name = ptp.name;
 
167
                pip.value = ptp.value;
 
168
                list += pip;
 
169
        }
 
170
        out.setParameters(list);
 
171
        return out;
 
172
}
 
173
 
 
174
class AvTransmit : public QObject
 
175
{
 
176
        Q_OBJECT
 
177
 
 
178
public:
 
179
        PsiMedia::RtpChannel *audio, *video;
 
180
        JingleRtpChannel *transport;
 
181
 
 
182
        AvTransmit(PsiMedia::RtpChannel *_audio, PsiMedia::RtpChannel *_video, JingleRtpChannel *_transport, QObject *parent = 0) :
 
183
                QObject(parent),
 
184
                audio(_audio),
 
185
                video(_video),
 
186
                transport(_transport)
 
187
        {
 
188
                if(audio)
 
189
                {
 
190
                        audio->setParent(this);
 
191
                        connect(audio, SIGNAL(readyRead()), SLOT(audio_readyRead()));
 
192
                }
 
193
 
 
194
                if(video)
 
195
                {
 
196
                        video->setParent(this);
 
197
                        connect(video, SIGNAL(readyRead()), SLOT(video_readyRead()));
 
198
                }
 
199
 
 
200
                transport->setParent(this);
 
201
                connect(transport, SIGNAL(readyRead()), SLOT(transport_readyRead()));
 
202
                connect(transport, SIGNAL(packetsWritten(int)), SLOT(transport_packetsWritten(int)));
 
203
        }
 
204
 
 
205
        ~AvTransmit()
 
206
        {
 
207
                if(audio)
 
208
                        audio->setParent(0);
 
209
                if(video)
 
210
                        video->setParent(0);
 
211
                transport->setParent(0);
 
212
        }
 
213
 
 
214
private slots:
 
215
        void audio_readyRead()
 
216
        {
 
217
                while(audio->packetsAvailable() > 0)
 
218
                {
 
219
                        PsiMedia::RtpPacket packet = audio->read();
 
220
 
 
221
                        JingleRtp::RtpPacket jpacket;
 
222
                        jpacket.type = JingleRtp::Audio;
 
223
                        jpacket.portOffset = packet.portOffset();
 
224
                        jpacket.value = packet.rawValue();
 
225
 
 
226
                        transport->write(jpacket);
 
227
                }
 
228
        }
 
229
 
 
230
        void video_readyRead()
 
231
        {
 
232
                while(video->packetsAvailable() > 0)
 
233
                {
 
234
                        PsiMedia::RtpPacket packet = video->read();
 
235
 
 
236
                        JingleRtp::RtpPacket jpacket;
 
237
                        jpacket.type = JingleRtp::Video;
 
238
                        jpacket.portOffset = packet.portOffset();
 
239
                        jpacket.value = packet.rawValue();
 
240
 
 
241
                        transport->write(jpacket);
 
242
                }
 
243
        }
 
244
 
 
245
        void transport_readyRead()
 
246
        {
 
247
                while(transport->packetsAvailable())
 
248
                {
 
249
                        JingleRtp::RtpPacket jpacket = transport->read();
 
250
 
 
251
                        if(jpacket.type == JingleRtp::Audio)
 
252
                                audio->write(PsiMedia::RtpPacket(jpacket.value, jpacket.portOffset));
 
253
                        else if(jpacket.type == JingleRtp::Video)
 
254
                                video->write(PsiMedia::RtpPacket(jpacket.value, jpacket.portOffset));
 
255
                }
 
256
        }
 
257
 
 
258
        void transport_packetsWritten(int count)
 
259
        {
 
260
                Q_UNUSED(count);
 
261
 
 
262
                // nothing
 
263
        }
 
264
};
 
265
 
 
266
class AvTransmitHandler : public QObject
 
267
{
 
268
        Q_OBJECT
 
269
 
 
270
public:
 
271
        AvTransmit *avTransmit;
 
272
        QThread *previousThread;
 
273
 
 
274
        AvTransmitHandler(QObject *parent = 0) :
 
275
                QObject(parent),
 
276
                avTransmit(0)
 
277
        {
 
278
        }
 
279
 
 
280
        ~AvTransmitHandler()
 
281
        {
 
282
                if(avTransmit)
 
283
                        releaseAvTransmit();
 
284
        }
 
285
 
 
286
        // NOTE: the handler never touches these variables except here
 
287
        //   and on destruction, so it's safe to call this function from
 
288
        //   another thread if you know what you're doing.
 
289
        void setAvTransmit(AvTransmit *_avTransmit)
 
290
        {
 
291
                avTransmit = _avTransmit;
 
292
                previousThread = avTransmit->thread();
 
293
                avTransmit->moveToThread(thread());
 
294
        }
 
295
 
 
296
        void releaseAvTransmit()
 
297
        {
 
298
                Q_ASSERT(avTransmit);
 
299
                avTransmit->moveToThread(previousThread);
 
300
                avTransmit = 0;
 
301
        }
 
302
};
 
303
 
 
304
class AvTransmitThread : public QCA::SyncThread
 
305
{
 
306
        Q_OBJECT
 
307
 
 
308
public:
 
309
        AvTransmitHandler *handler;
 
310
 
 
311
        AvTransmitThread(QObject *parent = 0) :
 
312
                QCA::SyncThread(parent),
 
313
                handler(0)
 
314
        {
 
315
        }
 
316
 
 
317
        ~AvTransmitThread()
 
318
        {
 
319
                stop();
 
320
        }
 
321
 
 
322
protected:
 
323
        virtual void atStart()
 
324
        {
 
325
                handler = new AvTransmitHandler;
 
326
        }
 
327
 
 
328
        virtual void atEnd()
 
329
        {
 
330
                delete handler;
 
331
        }
 
332
};
 
333
 
 
334
//----------------------------------------------------------------------------
 
335
// AvCall
 
336
//----------------------------------------------------------------------------
 
337
class AvCallManagerPrivate : public QObject
 
338
{
 
339
        Q_OBJECT
 
340
 
 
341
public:
 
342
        AvCallManager *q;
 
343
        PsiAccount *pa;
 
344
        JingleRtpManager *rtpManager;
 
345
        QList<AvCall*> sessions;
 
346
        QList<AvCall*> pending;
 
347
 
 
348
        AvCallManagerPrivate(PsiAccount *_pa, AvCallManager *_q);
 
349
        ~AvCallManagerPrivate();
 
350
 
 
351
        void unlink(AvCall *call);
 
352
 
 
353
private slots:
 
354
        void rtp_incomingReady();
 
355
};
 
356
 
 
357
class AvCallPrivate : public QObject
 
358
{
 
359
        Q_OBJECT
 
360
 
 
361
public:
 
362
        AvCall *q;
 
363
        AvCallManagerPrivate *manager;
 
364
        bool incoming;
 
365
        JingleRtp *sess;
 
366
        PsiMedia::RtpSession rtp;
 
367
        XMPP::Jid peer;
 
368
        AvCall::Mode mode;
 
369
        int bitrate;
 
370
        bool allowVideo;
 
371
        QString errorString;
 
372
        bool transmitAudio;
 
373
        bool transmitVideo;
 
374
        bool transmitting;
 
375
        AvTransmit *avTransmit;
 
376
        AvTransmitThread *avTransmitThread;
 
377
 
 
378
        AvCallPrivate(AvCall *_q) :
 
379
                QObject(_q),
 
380
                q(_q),
 
381
                manager(0),
 
382
                sess(0),
 
383
                transmitAudio(false),
 
384
                transmitVideo(false),
 
385
                transmitting(false),
 
386
                avTransmit(0),
 
387
                avTransmitThread(0)
 
388
        {
 
389
                allowVideo = AvCallManager::isVideoSupported();
 
390
 
 
391
                connect(&rtp, SIGNAL(started()), SLOT(rtp_started()));
 
392
                connect(&rtp, SIGNAL(preferencesUpdated()), SLOT(rtp_preferencesUpdated()));
 
393
                connect(&rtp, SIGNAL(stopped()), SLOT(rtp_stopped()));
 
394
                connect(&rtp, SIGNAL(error()), SLOT(rtp_error()));
 
395
        }
 
396
 
 
397
        ~AvCallPrivate()
 
398
        {
 
399
                rtp.disconnect(this);
 
400
                cleanup();
 
401
                unlink();
 
402
        }
 
403
 
 
404
        void unlink()
 
405
        {
 
406
                if(manager)
 
407
                {
 
408
                        // note that the object remains active, just
 
409
                        //   dissociated from the manager
 
410
                        manager->unlink(q);
 
411
                        manager = 0;
 
412
                }
 
413
        }
 
414
 
 
415
        void startOutgoing()
 
416
        {
 
417
                if(!manager)
 
418
                        return;
 
419
 
 
420
                manager->rtpManager->setBasePort(g_config->basePort);
 
421
                manager->rtpManager->setExternalAddress(g_config->extHost);
 
422
 
 
423
                start_rtp();
 
424
        }
 
425
 
 
426
        bool initIncoming()
 
427
        {
 
428
                setup_sess();
 
429
 
 
430
                // JingleRtp guarantees there will be at least one of audio or video
 
431
                bool offeredAudio = false;
 
432
                bool offeredVideo = false;
 
433
                if(!sess->remoteAudioPayloadTypes().isEmpty())
 
434
                        offeredAudio = true;
 
435
                if(allowVideo && !sess->remoteVideoPayloadTypes().isEmpty())
 
436
                        offeredVideo = true;
 
437
 
 
438
                if(offeredAudio && offeredVideo)
 
439
                        mode = AvCall::Both;
 
440
                else if(offeredAudio)
 
441
                        mode = AvCall::Audio;
 
442
                else if(offeredVideo)
 
443
                        mode = AvCall::Video;
 
444
                else
 
445
                {
 
446
                        // this could happen if only video is offered but
 
447
                        //   we don't allow it
 
448
                        return false;
 
449
                }
 
450
 
 
451
                return true;
 
452
        }
 
453
 
 
454
        void accept()
 
455
        {
 
456
                if(!manager)
 
457
                        return;
 
458
 
 
459
                manager->rtpManager->setBasePort(g_config->basePort);
 
460
                manager->rtpManager->setExternalAddress(g_config->extHost);
 
461
 
 
462
                // kick off the acceptance negotiation while simultaneously
 
463
                //   initializing the rtp engine.  note that session-accept
 
464
                //   won't actually get sent to the peer until we call
 
465
                //   localMediaUpdated()
 
466
                int types;
 
467
                if(mode == AvCall::Both)
 
468
                        types = JingleRtp::Audio | JingleRtp::Video;
 
469
                else if(mode == AvCall::Audio)
 
470
                        types = JingleRtp::Audio;
 
471
                else // Video
 
472
                        types = JingleRtp::Video;
 
473
 
 
474
                sess->accept(types);
 
475
                start_rtp();
 
476
        }
 
477
 
 
478
        void reject()
 
479
        {
 
480
                if(sess)
 
481
                        sess->reject();
 
482
                cleanup();
 
483
        }
 
484
 
 
485
private:
 
486
        static QString rtpSessionErrorToString(PsiMedia::RtpSession::Error e)
 
487
        {
 
488
                QString str;
 
489
                switch(e)
 
490
                {
 
491
                        case PsiMedia::RtpSession::ErrorSystem:
 
492
                                str = tr("System error"); break;
 
493
                        case PsiMedia::RtpSession::ErrorCodec:
 
494
                                str = tr("Codec error"); break;
 
495
                        default: // generic
 
496
                                str = tr("Generic error"); break;
 
497
                }
 
498
                return str;
 
499
        }
 
500
 
 
501
        void cleanup()
 
502
        {
 
503
                // if we had a thread, this will move the object back
 
504
                delete avTransmitThread;
 
505
                avTransmitThread = 0;
 
506
 
 
507
                delete avTransmit;
 
508
                avTransmit = 0;
 
509
 
 
510
                rtp.reset();
 
511
 
 
512
                delete sess;
 
513
                sess = 0;
 
514
        }
 
515
 
 
516
        void start_rtp()
 
517
        {
 
518
                Configuration &config = *g_config;
 
519
 
 
520
                transmitAudio = false;
 
521
                transmitVideo = false;
 
522
 
 
523
                if(config.liveInput)
 
524
                {
 
525
                        if(config.audioInDeviceId.isEmpty() && config.videoInDeviceId.isEmpty())
 
526
                        {
 
527
                                errorString = tr("Cannot call without selecting a device.  Do you have a microphone?  Check the Psi options.");
 
528
                                cleanup();
 
529
                                emit q->error();
 
530
                                return;
 
531
                        }
 
532
 
 
533
                        if((mode == AvCall::Audio || mode == AvCall::Both) && !config.audioInDeviceId.isEmpty())
 
534
                        {
 
535
                                rtp.setAudioInputDevice(config.audioInDeviceId);
 
536
                                transmitAudio = true;
 
537
                        }
 
538
                        else
 
539
                                rtp.setAudioInputDevice(QString());
 
540
 
 
541
                        if((mode == AvCall::Video || mode == AvCall::Both) && !config.videoInDeviceId.isEmpty() && allowVideo)
 
542
                        {
 
543
                                rtp.setVideoInputDevice(config.videoInDeviceId);
 
544
                                transmitVideo = true;
 
545
                        }
 
546
                        else
 
547
                                rtp.setVideoInputDevice(QString());
 
548
                }
 
549
                else // non-live (file) input
 
550
                {
 
551
                        rtp.setFileInput(config.file);
 
552
                        rtp.setFileLoopEnabled(config.loopFile);
 
553
 
 
554
                        // we just assume the file has both audio and video.
 
555
                        //   if it doesn't, no big deal, it'll still work.
 
556
                        //   update: after starting, we can correct these
 
557
                        //   variables.
 
558
                        transmitAudio = true;
 
559
                        transmitVideo = true;
 
560
                }
 
561
 
 
562
                if(!config.audioOutDeviceId.isEmpty())
 
563
                        rtp.setAudioOutputDevice(config.audioOutDeviceId);
 
564
 
 
565
                // media types are flagged by params, even if empty
 
566
                QList<PsiMedia::AudioParams> audioParamsList;
 
567
                if(transmitAudio)
 
568
                        audioParamsList += PsiMedia::AudioParams();
 
569
                rtp.setLocalAudioPreferences(audioParamsList);
 
570
 
 
571
                QList<PsiMedia::VideoParams> videoParamsList;
 
572
                if(transmitVideo)
 
573
                        videoParamsList += PsiMedia::VideoParams();
 
574
                rtp.setLocalVideoPreferences(videoParamsList);
 
575
 
 
576
                // for incoming sessions, we have the remote media info at
 
577
                //   the start, so use it
 
578
                if(incoming)
 
579
                        setup_remote_media();
 
580
 
 
581
                if(bitrate != -1)
 
582
                        rtp.setMaximumSendingBitrate(bitrate);
 
583
 
 
584
                transmitting = false;
 
585
                rtp.start();
 
586
        }
 
587
 
 
588
        void setup_sess()
 
589
        {
 
590
                connect(sess, SIGNAL(rejected()), SLOT(sess_rejected()));
 
591
                connect(sess, SIGNAL(error()), SLOT(sess_error()));
 
592
                connect(sess, SIGNAL(activated()), SLOT(sess_activated()));
 
593
                connect(sess, SIGNAL(remoteMediaUpdated()), SLOT(sess_remoteMediaUpdated()));
 
594
        }
 
595
 
 
596
        void setup_remote_media()
 
597
        {
 
598
                if(transmitAudio)
 
599
                {
 
600
                        QList<JingleRtpPayloadType> payloadTypes = sess->remoteAudioPayloadTypes();
 
601
                        QList<PsiMedia::PayloadInfo> list;
 
602
                        if(!payloadTypes.isEmpty())
 
603
                                list += payloadTypeToPayloadInfo(payloadTypes.first());
 
604
                        rtp.setRemoteAudioPreferences(list);
 
605
                }
 
606
 
 
607
                if(transmitVideo)
 
608
                {
 
609
                        QList<JingleRtpPayloadType> payloadTypes = sess->remoteVideoPayloadTypes();
 
610
                        QList<PsiMedia::PayloadInfo> list;
 
611
                        if(!payloadTypes.isEmpty())
 
612
                                list += payloadTypeToPayloadInfo(payloadTypes.first());
 
613
                        rtp.setRemoteVideoPreferences(list);
 
614
                }
 
615
 
 
616
                // FIXME: if the remote side doesn't support a media type,
 
617
                //   then we need to downgrade locally
 
618
        }
 
619
 
 
620
private slots:
 
621
        void rtp_started()
 
622
        {
 
623
                if(!manager)
 
624
                        return;
 
625
 
 
626
                printf("rtp_started\n");
 
627
 
 
628
                PsiMedia::PayloadInfo audio, *pAudio;
 
629
                PsiMedia::PayloadInfo video, *pVideo;
 
630
 
 
631
                pAudio = 0;
 
632
                pVideo = 0;
 
633
                if(transmitAudio)
 
634
                {
 
635
                        // confirm transmitting of audio is actually possible,
 
636
                        //   in the case that a file is used as input
 
637
                        if(rtp.canTransmitAudio())
 
638
                        {
 
639
                                audio = rtp.localAudioPayloadInfo().first();
 
640
                                pAudio = &audio;
 
641
                        }
 
642
                        else
 
643
                                transmitAudio = false;
 
644
                }
 
645
                if(transmitVideo)
 
646
                {
 
647
                        // same for video
 
648
                        if(rtp.canTransmitVideo())
 
649
                        {
 
650
                                video = rtp.localVideoPayloadInfo().first();
 
651
                                pVideo = &video;
 
652
                        }
 
653
                        else
 
654
                                transmitVideo = false;
 
655
                }
 
656
 
 
657
                if(transmitAudio && transmitVideo)
 
658
                        mode = AvCall::Both;
 
659
                else if(transmitAudio && !transmitVideo)
 
660
                        mode = AvCall::Audio;
 
661
                else if(transmitVideo && !transmitAudio)
 
662
                        mode = AvCall::Video;
 
663
                else
 
664
                {
 
665
                        // can't happen?
 
666
                        Q_ASSERT(0);
 
667
                }
 
668
 
 
669
                if(!incoming)
 
670
                {
 
671
                        sess = manager->rtpManager->createOutgoing();
 
672
                        setup_sess();
 
673
                }
 
674
 
 
675
                if(pAudio)
 
676
                {
 
677
                        JingleRtpPayloadType pt = payloadInfoToPayloadType(*pAudio);
 
678
                        sess->setLocalAudioPayloadTypes(QList<JingleRtpPayloadType>() << pt);
 
679
                }
 
680
 
 
681
                if(pVideo)
 
682
                {
 
683
                        JingleRtpPayloadType pt = payloadInfoToPayloadType(*pVideo);
 
684
                        sess->setLocalVideoPayloadTypes(QList<JingleRtpPayloadType>() << pt);
 
685
                }
 
686
 
 
687
                if(!incoming)
 
688
                        sess->connectToJid(peer);
 
689
                else
 
690
                        sess->localMediaUpdate();
 
691
        }
 
692
 
 
693
        void rtp_preferencesUpdated()
 
694
        {
 
695
                // nothing?
 
696
        }
 
697
 
 
698
        void rtp_stopped()
 
699
        {
 
700
                // nothing for now, until we do async shutdown
 
701
        }
 
702
 
 
703
        void rtp_error()
 
704
        {
 
705
                errorString = tr("An error occurred while trying to send:\n%1.").arg(rtpSessionErrorToString(rtp.errorCode()));
 
706
                reject();
 
707
                emit q->error();
 
708
        }
 
709
 
 
710
        void sess_rejected()
 
711
        {
 
712
                errorString = tr("Call was rejected or terminated.");
 
713
                cleanup();
 
714
                emit q->error();
 
715
        }
 
716
 
 
717
        void sess_error()
 
718
        {
 
719
                JingleRtp::Error e = sess->errorCode();
 
720
                if(e == JingleRtp::ErrorTimeout)
 
721
                {
 
722
                        errorString = tr("Call negotiation timed out.");
 
723
                        cleanup();
 
724
                }
 
725
                else if(e == JingleRtp::ErrorICE)
 
726
                {
 
727
                        errorString = tr("Unable to establish peer-to-peer connection.");
 
728
                        reject();
 
729
                }
 
730
                else
 
731
                {
 
732
                        errorString = tr("Call negotiation failed.");
 
733
                        cleanup();
 
734
                }
 
735
 
 
736
                emit q->error();
 
737
        }
 
738
 
 
739
        void sess_activated()
 
740
        {
 
741
                PsiMedia::RtpChannel *audio = 0;
 
742
                PsiMedia::RtpChannel *video = 0;
 
743
 
 
744
                if(transmitAudio)
 
745
                        audio = rtp.audioRtpChannel();
 
746
                if(transmitVideo)
 
747
                        video = rtp.videoRtpChannel();
 
748
 
 
749
                avTransmit = new AvTransmit(audio, video, sess->rtpChannel());
 
750
#ifdef USE_THREAD
 
751
                avTransmitThread = new AvTransmitThread(this);
 
752
                avTransmitThread->start();
 
753
                avTransmitThread->handler->setAvTransmit(avTransmit);
 
754
#endif
 
755
 
 
756
                if(transmitAudio)
 
757
                        rtp.transmitAudio();
 
758
                if(transmitVideo)
 
759
                        rtp.transmitVideo();
 
760
 
 
761
                transmitting = true;
 
762
                emit q->activated();
 
763
        }
 
764
 
 
765
        void sess_remoteMediaUpdated()
 
766
        {
 
767
                setup_remote_media();
 
768
                rtp.updatePreferences();
 
769
        }
 
770
};
 
771
 
 
772
AvCall::AvCall()
 
773
{
 
774
        d = new AvCallPrivate(this);
 
775
}
 
776
 
 
777
AvCall::AvCall(const AvCall &from) :
 
778
        QObject(0)
 
779
{
 
780
        Q_UNUSED(from);
 
781
        fprintf(stderr, "AvCall copy not supported\n");
 
782
        abort();
 
783
}
 
784
 
 
785
AvCall::~AvCall()
 
786
{
 
787
        delete d;
 
788
}
 
789
 
 
790
XMPP::Jid AvCall::jid() const
 
791
{
 
792
        if(d->sess)
 
793
                return d->sess->jid();
 
794
        else
 
795
                return XMPP::Jid();
 
796
}
 
797
 
 
798
AvCall::Mode AvCall::mode() const
 
799
{
 
800
        return d->mode;
 
801
}
 
802
 
 
803
void AvCall::connectToJid(const XMPP::Jid &jid, Mode mode, int kbps)
 
804
{
 
805
        d->peer = jid;
 
806
        d->mode = mode;
 
807
        d->bitrate = kbps;
 
808
        d->startOutgoing();
 
809
}
 
810
 
 
811
void AvCall::accept(Mode mode, int kbps)
 
812
{
 
813
        d->mode = mode;
 
814
        d->bitrate = kbps;
 
815
        d->accept();
 
816
}
 
817
 
 
818
void AvCall::reject()
 
819
{
 
820
        d->reject();
 
821
}
 
822
 
 
823
void AvCall::setIncomingVideo(PsiMedia::VideoWidget *widget)
 
824
{
 
825
        d->rtp.setVideoOutputWidget(widget);
 
826
}
 
827
 
 
828
QString AvCall::errorString() const
 
829
{
 
830
        return d->errorString;
 
831
}
 
832
 
 
833
void AvCall::unlink()
 
834
{
 
835
        d->unlink();
 
836
}
 
837
 
 
838
//----------------------------------------------------------------------------
 
839
// AvCallManager
 
840
//----------------------------------------------------------------------------
 
841
AvCallManagerPrivate::AvCallManagerPrivate(PsiAccount *_pa, AvCallManager *_q) :
 
842
        QObject(_q),
 
843
        q(_q),
 
844
        pa(_pa)
 
845
{
 
846
        rtpManager = new JingleRtpManager(pa->client());
 
847
        connect(rtpManager, SIGNAL(incomingReady()), SLOT(rtp_incomingReady()));
 
848
}
 
849
 
 
850
AvCallManagerPrivate::~AvCallManagerPrivate()
 
851
{
 
852
        delete rtpManager;
 
853
}
 
854
 
 
855
void AvCallManagerPrivate::unlink(AvCall *call)
 
856
{
 
857
        sessions.removeAll(call);
 
858
}
 
859
 
 
860
void AvCallManagerPrivate::rtp_incomingReady()
 
861
{
 
862
        AvCall *call = new AvCall;
 
863
        call->d->manager = this;
 
864
        call->d->incoming = true;
 
865
        call->d->sess = rtpManager->takeIncoming();
 
866
        sessions += call;
 
867
        if(!call->d->initIncoming())
 
868
        {
 
869
                call->d->sess->reject();
 
870
                delete call->d->sess;
 
871
                call->d->sess = 0;
 
872
                delete call;
 
873
                return;
 
874
        }
 
875
 
 
876
        pending += call;
 
877
        emit q->incomingReady();
 
878
}
 
879
 
 
880
AvCallManager::AvCallManager(PsiAccount *pa) :
 
881
        QObject(0)
 
882
{
 
883
        d = new AvCallManagerPrivate(pa, this);
 
884
}
 
885
 
 
886
AvCallManager::~AvCallManager()
 
887
{
 
888
        delete d;
 
889
}
 
890
 
 
891
AvCall *AvCallManager::createOutgoing()
 
892
{
 
893
        AvCall *call = new AvCall;
 
894
        call->d->manager = d;
 
895
        call->d->incoming = false;
 
896
        return call;
 
897
}
 
898
 
 
899
AvCall *AvCallManager::takeIncoming()
 
900
{
 
901
        return d->pending.takeFirst();
 
902
}
 
903
 
 
904
void AvCallManager::config()
 
905
{
 
906
        // TODO: remove this function?
 
907
}
 
908
 
 
909
bool AvCallManager::isSupported()
 
910
{
 
911
        ensureLoaded();
 
912
        if(!QCA::isSupported("hmac(sha1)"))
 
913
        {
 
914
                printf("hmac support missing for voice calls, install qca-ossl\n");
 
915
                return false;
 
916
        }
 
917
        return PsiMedia::isSupported();
 
918
}
 
919
 
 
920
bool AvCallManager::isVideoSupported()
 
921
{
 
922
        if(!isSupported())
 
923
                return false;
 
924
 
 
925
        if(!QString::fromLatin1(qgetenv("PSI_ENABLE_VIDEO")).isEmpty())
 
926
                return true;
 
927
        else
 
928
                return false;
 
929
}
 
930
 
 
931
void AvCallManager::setSelfAddress(const QHostAddress &addr)
 
932
{
 
933
        d->rtpManager->setSelfAddress(addr);
 
934
}
 
935
 
 
936
void AvCallManager::setStunHost(const QString &host, int port)
 
937
{
 
938
        d->rtpManager->setStunHost(host, port);
 
939
}
 
940
 
 
941
void AvCallManager::setBasePort(int port)
 
942
{
 
943
        if(port == 0)
 
944
                port = -1;
 
945
        g_config->basePort = port;
 
946
}
 
947
 
 
948
void AvCallManager::setExternalAddress(const QString &host)
 
949
{
 
950
        g_config->extHost = host;
 
951
}
 
952
 
 
953
void AvCallManager::setAudioOutDevice(const QString &id)
 
954
{
 
955
        g_config->audioOutDeviceId = id;
 
956
}
 
957
 
 
958
void AvCallManager::setAudioInDevice(const QString &id)
 
959
{
 
960
        g_config->audioInDeviceId = id;
 
961
}
 
962
 
 
963
void AvCallManager::setVideoInDevice(const QString &id)
 
964
{
 
965
        g_config->videoInDeviceId = id;
 
966
}
 
967
 
 
968
#include "avcall.moc"