1 /*
2   * mediamanager.cpp - A manager for the media sessions.
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 "mediamanager.h"
19 #include "mediasession.h"
20 #include "alsaio.h"
21 
22 #include "jabber_protocol_debug.h"
23 
24 class MediaManager::Private
25 {
26 public:
27     AlsaIO *alsaIn;
28     AlsaIO *alsaOut;
29     QString inputDev;
30     QString outputDev;
31 
32     QList<MediaSession *> sessions;
33 
34     bool started;
35 };
36 
MediaManager(QString inputDev,QString outputDev)37 MediaManager::MediaManager(QString inputDev, QString outputDev)
38     : d(new Private)
39 {
40     d->inputDev = inputDev;
41     d->outputDev = outputDev;
42 
43     d->alsaIn = 0;
44     d->alsaOut = 0;
45 
46     d->started = false;
47 
48     qCDebug(JABBER_PROTOCOL_LOG) << "Created Media Manager.";
49 }
50 
~MediaManager()51 MediaManager::~MediaManager()
52 {
53     stop();
54     delete d;
55     qCDebug(JABBER_PROTOCOL_LOG) << "Deleted Media Manager.";
56 }
57 
alsaIn() const58 AlsaIO *MediaManager::alsaIn() const
59 {
60     return d->alsaIn;
61 }
62 
alsaOut() const63 AlsaIO *MediaManager::alsaOut() const
64 {
65     return d->alsaOut;
66 }
67 
start()68 bool MediaManager::start()
69 {
70     if (d->started) {
71         return true;
72     }
73 
74     bool in = false, out = false;
75 
76     d->alsaIn = new AlsaIO(AlsaIO::Capture, d->inputDev, AlsaIO::Signed16Le);
77     d->alsaOut = new AlsaIO(AlsaIO::Playback, d->outputDev, AlsaIO::Signed16Le);
78 
79     if (d->alsaIn->start()) {
80         in = true;
81     }
82     if (d->alsaOut->start()) {
83         out = true;
84     }
85 
86     return d->started = (in && out);
87 }
88 
stop()89 void MediaManager::stop()
90 {
91     delete d->alsaIn;
92     d->alsaIn = 0;
93 
94     delete d->alsaOut;
95     d->alsaOut = 0;
96 
97     d->started = false;
98 }
99 
read()100 QByteArray MediaManager::read()
101 {
102     if (alsaIn()) {
103         return alsaIn()->data();
104     }
105     return 0;
106 }
107 
write(const QByteArray & data)108 void MediaManager::write(const QByteArray &data)
109 {
110     //qCDebug(JABBER_PROTOCOL_LOG) << "Writin on alsa device !";
111     if (alsaOut()) {
112         alsaOut()->write(data);
113     }
114 }
115 
addSession(MediaSession * sess)116 bool MediaManager::addSession(MediaSession *sess)
117 {
118     bool ret = true;
119     if (d->sessions.count() == 0) {
120         ret = start();
121     }
122     d->sessions << sess;
123 
124     return ret;
125 }
126 
removeSession(MediaSession * sess)127 void MediaManager::removeSession(MediaSession *sess)
128 {
129     for (int i = 0; i < d->sessions.count(); i++) {
130         if (d->sessions.at(i) == sess) {
131             d->sessions.removeAt(i);
132             break;
133         }
134     }
135 
136     if (d->sessions.count() == 0) {
137         stop();
138     }
139 }
140