1 /*
2 * mediasession.cpp - A helper class to easily manage media data.
3 *
4 * Copyright (c) 2008 by Detlev Casanova <detlev.casanova@gmail.com>
5 *
6 * Kopete (c) by the Kopete developers <kopete-devel@kde.org>
7 *
8 * *************************************************************************
9 * * *
10 * * This program is free software; you can redistribute it and/or modify *
11 * * it under the terms of the GNU General Public License as published by *
12 * * the Free Software Foundation; either version 2 of the License, or *
13 * * (at your option) any later version. *
14 * * *
15 * *************************************************************************
16 */
17
18 #include "mediasession.h"
19 #include "mediamanager.h"
20 #include "abstractio.h"
21 #include "speexio.h"
22
23 #include "jabber_protocol_debug.h"
24 #include <QTime>
25
26 class MediaSession::Private
27 {
28 public:
29 AbstractIO *plugin;
30 MediaManager *mediaManager;
31 QString codecName;
32 QByteArray encodedData;
33 QTime startTime;
34 int tsValue;
35 int ts;
36 };
37
MediaSession(MediaManager * mm,const QString & codecName)38 MediaSession::MediaSession(MediaManager *mm, const QString &codecName)
39 : d(new Private())
40 {
41 d->mediaManager = mm;
42 d->codecName = codecName;
43
44 if (d->codecName == "speex") {
45 d->plugin = new SpeexIO();
46 }
47
48 d->ts = 0;
49
50 qCDebug(JABBER_PROTOCOL_LOG) << "Created Media Session for codec" << codecName;
51 }
52
~MediaSession()53 MediaSession::~MediaSession()
54 {
55 d->mediaManager->removeSession(this);
56 delete d->plugin;
57 delete d;
58 qCDebug(JABBER_PROTOCOL_LOG) << "Deleted Media Session";
59 }
60
setSamplingRate(int sr)61 void MediaSession::setSamplingRate(int sr)
62 {
63 static_cast<SpeexIO *>(d->plugin)->setSamplingRate(sr);
64 d->tsValue = d->plugin->tsValue();
65 }
66
setQuality(int q)67 void MediaSession::setQuality(int q)
68 {
69 static_cast<SpeexIO *>(d->plugin)->setQuality(q);
70 }
71
start()72 bool MediaSession::start()
73 {
74 d->startTime = QTime::currentTime();
75 bool managerOk = d->mediaManager->addSession(this); //Tell the media manager the session is being started.
76 bool pluginOk = d->plugin->start();
77
78 connect((QObject *)d->mediaManager->alsaIn(), SIGNAL(readyRead()), (QObject *)this, SLOT(slotReadyRead()));
79 connect((QObject *)d->plugin, SIGNAL(encoded()), (QObject *)this, SLOT(slotEncoded()));
80 connect((QObject *)d->plugin, SIGNAL(decoded()), (QObject *)this, SLOT(slotDecoded()));
81
82 return managerOk && pluginOk;
83 }
84
write(const QByteArray & sData)85 void MediaSession::write(const QByteArray &sData)
86 {
87 //decoding speex data.
88 //qCDebug(JABBER_PROTOCOL_LOG) << "Receiving ! (" << sData.size() << "bytes)";
89 d->plugin->decode(sData);
90 }
91
slotReadyRead()92 void MediaSession::slotReadyRead()
93 {
94 //qCDebug(JABBER_PROTOCOL_LOG) << "Ready read";
95 d->plugin->encode(d->mediaManager->read());
96 }
97
slotEncoded()98 void MediaSession::slotEncoded()
99 {
100 //d->encodedData.clear();
101 d->encodedData = d->plugin->encodedData();
102
103 emit readyRead(); // Encoded data is ready to be read and sent over the network.
104 }
105
read() const106 QByteArray MediaSession::read() const
107 {
108 return d->encodedData;
109 }
110
slotDecoded()111 void MediaSession::slotDecoded()
112 {
113 //qCDebug(JABBER_PROTOCOL_LOG) << "Decoded !";
114
115 QByteArray rawData = d->plugin->decodedData(); //FIXME:what about this QByteArray lifetime ?
116 if (rawData.isNull()) {
117 qCDebug(JABBER_PROTOCOL_LOG) << "rawData is NULL !";
118 return;
119 }
120
121 //qCDebug(JABBER_PROTOCOL_LOG) << "rawData =" << rawData.toBase64() << "(" << rawData.size() << "bytes)";
122
123 //FIXME: write should become writeAudio, a write Video should be created.
124 d->mediaManager->write(rawData);
125 }
126
timeStamp()127 int MediaSession::timeStamp()
128 {
129 int ret = d->startTime.msecsTo(QTime::currentTime());
130 //qCDebug(JABBER_PROTOCOL_LOG) << "Return value :" << ret;
131 return ret;
132 }
133