1 /*
2 * jabberjinglecontent.cpp - A Jingle content.
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 //Kopete
19 #include "jabberjinglecontent.h"
20 #include "jabberjinglesession.h"
21 #include "mediamanager.h"
22 #include "mediasession.h"
23 #include "jinglertpsession.h"
24
25 //Iris
26 #include "jinglecontent.h"
27 #include "jinglesession.h"
28
29 #include "jabber_protocol_debug.h"
30 #include <QMessageBox>
31
JabberJingleContent(JabberJingleSession * parent,XMPP::JingleContent * c)32 JabberJingleContent::JabberJingleContent(JabberJingleSession *parent, XMPP::JingleContent *c)
33 : m_content(c)
34 , m_jabberSession(parent)
35 {
36 m_rtpInSession = 0;
37 m_rtpOutSession = 0;
38 m_mediaSession = 0;
39 m_mediaManager = m_jabberSession->mediaManager();
40 if (!m_mediaManager) {
41 kDebug(KDE_DEFAULT_DEBUG_AREA) << "m_mediaManager is Null !";
42 }
43 if (c == 0) {
44 return;
45 }
46
47 kDebug(KDE_DEFAULT_DEBUG_AREA) << "Created a new JabberJingleContent with" << c->name();
48 }
49
~JabberJingleContent()50 JabberJingleContent::~JabberJingleContent()
51 {
52 qCDebug(JABBER_PROTOCOL_LOG) << "destroyed";
53 delete m_content;
54 delete m_rtpInSession;
55 delete m_rtpOutSession;
56 delete m_mediaSession;
57 }
58
setContent(XMPP::JingleContent * content)59 void JabberJingleContent::setContent(XMPP::JingleContent *content)
60 {
61 m_content = content;
62 }
63
prepareRtpInSession()64 void JabberJingleContent::prepareRtpInSession()
65 {
66 kDebug(KDE_DEFAULT_DEBUG_AREA) << "Prepare RTP IN session";
67 if (m_rtpInSession == 0) {
68 if (!m_content->inSocket()) {
69 qCDebug(JABBER_PROTOCOL_LOG) << "Fatal : Invalid Socket !";
70 return;
71 }
72 m_rtpInSession = new JingleRtpSession(JingleRtpSession::In);
73 m_rtpInSession->setMediaSession(m_mediaSession);
74 m_rtpInSession->setPayload(m_content->bestPayload());
75 m_rtpInSession->setRtpSocket(m_content->inSocket()); // This will set rtcp port = rtp port + 1. Maybe we don't want that for ice-udp.
76 qCDebug(JABBER_PROTOCOL_LOG) << "Connecting m_rtpInSession readyRead signal.";
77 connect(m_rtpInSession, SIGNAL(readyRead(QByteArray)), this, SLOT(slotIncomingData(QByteArray)));
78 } else {
79 qCDebug(JABBER_PROTOCOL_LOG) << "RTP IN session already set !";
80 }
81 }
82
prepareRtpOutSession()83 void JabberJingleContent::prepareRtpOutSession()
84 {
85 kDebug(KDE_DEFAULT_DEBUG_AREA) << "Prepare RTP OUT session";
86 if (m_rtpOutSession == 0) {
87 if (!m_content->outSocket()) {
88 qCDebug(JABBER_PROTOCOL_LOG) << "Fatal : Invalid Socket !";
89 return;
90 }
91 m_rtpOutSession = new JingleRtpSession(JingleRtpSession::Out);
92 m_rtpOutSession->setMediaSession(m_mediaSession);
93 m_rtpOutSession->setPayload(m_content->bestPayload());
94 m_rtpOutSession->setRtpSocket(m_content->outSocket()); // This will set rtcp port = rtp port + 1. Maybe we don't want that for ice-udp.
95 if (m_jabberSession->session()->state() == XMPP::JingleSession::Pending) {
96 m_rtpOutSession->setPayload(m_content->bestPayload());
97 }
98 } else {
99 qCDebug(JABBER_PROTOCOL_LOG) << "RTP OUT session already set !";
100 }
101 }
102
slotIncomingData(const QByteArray & data)103 void JabberJingleContent::slotIncomingData(const QByteArray &data)
104 {
105 //qCDebug(JABBER_PROTOCOL_LOG) << "Receiving ! (" << data.size() << "bytes)";
106 m_mediaSession->write(data);
107 }
108
startStreaming()109 void JabberJingleContent::startStreaming()
110 {
111 qCDebug(JABBER_PROTOCOL_LOG) << "Start Streaming";
112
113 if (m_content->type() == XMPP::JingleContent::Audio) {
114 m_mediaSession = new MediaSession(m_mediaManager, "speex" /*FIXME:use m_content->bestPayload()*/);
115 if (m_mediaSession == 0) {
116 qCDebug(JABBER_PROTOCOL_LOG) << "Media Session is NULL!";
117 return;
118 }
119 connect(m_mediaSession, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
120 m_mediaSession->setSamplingRate(8000 /*FIXME:use m_content->bestPayload()*/);
121
122 prepareRtpOutSession();
123 prepareRtpInSession();
124
125 if (!m_mediaSession->start()) {
126 QMessageBox::warning(0, tr("Jingle audio"), tr("Unable to start you audio device, the session will start anyway."));
127 }
128 }
129 }
130
slotReadyRead()131 void JabberJingleContent::slotReadyRead()
132 {
133 m_rtpOutSession->send(m_mediaSession->read());
134 }
135
elementToSdp(const QDomElement & elem)136 QString JabberJingleContent::elementToSdp(const QDomElement &elem)
137 {
138 Q_UNUSED(elem)
139 return QString();
140 }
141
slotSendRtpData()142 /*DEPRECATED*/ void JabberJingleContent::slotSendRtpData()
143 {
144 //qCDebug(JABBER_PROTOCOL_LOG) << "Send RTP data.";
145 m_rtpOutSession->send(m_mediaSession->read());
146 }
147
contentName()148 QString JabberJingleContent::contentName()
149 {
150 if (!m_content) {
151 return "";
152 }
153 return m_content->name();
154 }
155