New stereo mixing
[skype-call-recorder.git] / call.cpp
blob8532deb525e1350fb35b981a5340ef1be71b60e9
1 /*
2 Skype Call Recorder
3 Copyright (C) 2008 jlh (jlh at gmx dot ch)
5 This program is free software; you can redistribute it and/or modify it
6 under the terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2 of the License, version 3 of
8 the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 The GNU General Public License version 2 is included with the source of
20 this program under the file name COPYING. You can also get a copy on
21 http://www.fsf.org/
24 #include <QStringList>
25 #include <QList>
26 #include <QTcpServer>
27 #include <QTcpSocket>
28 #include <QMessageBox>
29 #include <cstdlib>
30 #include <cmath>
31 #include <cstring>
33 #include "call.h"
34 #include "common.h"
35 #include "skype.h"
36 #include "wavewriter.h"
37 #include "mp3writer.h"
38 #include "vorbiswriter.h"
39 #include "preferences.h"
40 #include "gui.h"
42 // AutoSync - automatic resynchronization of the two streams. this class has a
43 // circular buffer that keeps track of the delay between the two streams. it
44 // calculates the running average and deviation and then tells if and how much
45 // correction should be applied.
47 AutoSync::AutoSync(int s, long p) :
48 size(s),
49 index(0),
50 sum(0),
51 sum2(0),
52 precision(p),
53 suppress(s)
55 delays = new long[size];
56 std::memset(delays, 0, sizeof(long) * size);
59 AutoSync::~AutoSync() {
60 delete[] delays;
63 void AutoSync::add(long d) {
64 long old = delays[index];
65 sum += d - old;
66 sum2 += (qint64)d * (qint64)d - (qint64)old * (qint64)old;
67 delays[index++] = d;
68 if (index >= size)
69 index = 0;
70 if (suppress)
71 suppress--;
74 long AutoSync::getSync() {
75 if (suppress)
76 return 0;
78 float avg = (float)sum / (float)size;
79 float dev = std::sqrt(((float)sum2 - (float)sum * (float)sum / (float)size) / (float)size);
81 if (std::fabs(avg) > (float)precision && dev < (float)precision)
82 return (long)avg;
84 return 0;
87 void AutoSync::reset() {
88 suppress = size;
91 // Call class
93 Call::Call(QObject *p, Skype *sk, CallID i) :
94 QObject(p),
95 skype(sk),
96 id(i),
97 status("UNKNOWN"),
98 writer(NULL),
99 isRecording(false),
100 shouldRecord(1),
101 sync(100 * 2 * 3, 320) // approx 3 seconds
103 debug(QString("Call %1: Call object contructed").arg(id));
105 // Call objects track calls even before they are in progress and also
106 // when they are not being recorded.
108 // TODO check if we actually should record this call here
109 // and ask if we're unsure
111 skypeName = skype->getObject(QString("CALL %1 PARTNER_HANDLE").arg(id));
112 if (skypeName.isEmpty()) {
113 debug(QString("Call %1: cannot get partner handle").arg(id));
114 skypeName = "UnknownCaller";
117 displayName = skype->getObject(QString("CALL %1 PARTNER_DISPNAME").arg(id));
118 if (displayName.isEmpty()) {
119 debug(QString("Call %1: cannot get partner display name").arg(id));
120 displayName = "Unnamed Caller";
124 Call::~Call() {
125 debug(QString("Call %1: Call object destructed").arg(id));
127 if (isRecording)
128 stopRecording();
130 delete confirmation;
132 setStatus("UNKNOWN");
134 // QT takes care of deleting servers and sockets
137 bool Call::okToDelete() const {
138 // this is used for checking whether past calls may now be deleted.
139 // when a past call hasn't been decided yet whether it should have been
140 // recorded, then it may not be deleted until the decision has been
141 // made by the user.
143 if (isRecording)
144 return false;
146 if (confirmation)
147 /* confirmation dialog still open */
148 return false;
150 return true;
153 bool Call::statusActive() const {
154 return status == "INPROGRESS" ||
155 status == "ONHOLD" ||
156 status == "LOCALHOLD" ||
157 status == "REMOTEHOLD";
160 void Call::setStatus(const QString &s) {
161 bool wasActive = statusActive();
162 status = s;
163 bool nowActive = statusActive();
165 if (!wasActive && nowActive) {
166 emit startedCall(id, skypeName);
167 startRecording();
168 } else if (wasActive && !nowActive) {
169 // don't stop recording when we get "FINISHED". just wait for
170 // the connections to close so that we really get all the data
171 emit stoppedCall(id);
175 bool Call::statusDone() const {
176 return status == "BUSY" ||
177 status == "CANCELLED" ||
178 status == "FAILED" ||
179 status == "FINISHED" ||
180 status == "MISSED" ||
181 status == "REFUSED";
182 // TODO: see what the deal is with REDIAL_PENDING (protocol 8)
185 QString Call::constructFileName() const {
186 return getFileName(skypeName, displayName, skype->getSkypeName(),
187 skype->getObject("PROFILE FULLNAME"), timeStartRecording);
190 QString Call::constructCommentTag() const {
191 QString str("Skype call between %1%2 and %3%4.");
192 QString dn1, dn2;
193 if (!displayName.isEmpty())
194 dn1 = QString(" (") + displayName + ")";
195 dn2 = skype->getObject("PROFILE FULLNAME");
196 if (!dn2.isEmpty())
197 dn2 = QString(" (") + dn2 + ")";
198 return str.arg(skypeName, dn1, skype->getSkypeName(), dn2);
201 void Call::setShouldRecord() {
202 // this sets shouldRecord based on preferences. shouldRecord is 0 if
203 // the call should not be recorded, 1 if we should ask and 2 if we
204 // should record
206 QStringList list = preferences.get(Pref::AutoRecordYes).toList();
207 if (list.contains(skypeName)) {
208 shouldRecord = 2;
209 return;
212 list = preferences.get(Pref::AutoRecordAsk).toList();
213 if (list.contains(skypeName)) {
214 shouldRecord = 1;
215 return;
218 list = preferences.get(Pref::AutoRecordNo).toList();
219 if (list.contains(skypeName)) {
220 shouldRecord = 0;
221 return;
224 QString def = preferences.get(Pref::AutoRecordDefault).toString();
225 if (def == "yes")
226 shouldRecord = 2;
227 else if (def == "ask")
228 shouldRecord = 1;
229 else if (def == "no")
230 shouldRecord = 0;
231 else
232 shouldRecord = 1;
235 void Call::ask() {
236 confirmation = new RecordConfirmationDialog(skypeName, displayName);
237 connect(confirmation, SIGNAL(yes()), this, SLOT(confirmRecording()));
238 connect(confirmation, SIGNAL(no()), this, SLOT(denyRecording()));
241 void Call::hideConfirmation(int should) {
242 if (confirmation) {
243 delete confirmation;
244 shouldRecord = should;
248 void Call::confirmRecording() {
249 shouldRecord = 2;
250 emit showLegalInformation();
253 void Call::denyRecording() {
254 // note that the call might already be finished by now
255 shouldRecord = 0;
256 stopRecording(true);
257 removeFile();
260 void Call::removeFile() {
261 debug(QString("Removing '%1'").arg(fileName));
262 QFile::remove(fileName);
265 void Call::startRecording(bool force) {
266 if (force)
267 hideConfirmation(2);
269 if (isRecording)
270 return;
272 if (force) {
273 emit showLegalInformation();
274 } else {
275 setShouldRecord();
276 if (shouldRecord == 0)
277 return;
278 if (shouldRecord == 1)
279 ask();
280 else // shouldRecord == 2
281 emit showLegalInformation();
284 debug(QString("Call %1: start recording").arg(id));
286 // set up encoder for appropriate format
288 timeStartRecording = QDateTime::currentDateTime();
289 QString fn = constructFileName();
291 stereo = preferences.get(Pref::OutputStereo).toBool();
292 stereoMix = preferences.get(Pref::OutputStereoMix).toInt();
294 QString format = preferences.get(Pref::OutputFormat).toString();
296 if (format == "wav")
297 writer = new WaveWriter;
298 else if (format == "mp3")
299 writer = new Mp3Writer;
300 else /*if (format == "vorbis")*/
301 writer = new VorbisWriter;
303 if (preferences.get(Pref::OutputSaveTags).toBool())
304 writer->setTags(constructCommentTag(), timeStartRecording);
306 bool b = writer->open(fn, skypeSamplingRate, stereo);
307 fileName = writer->fileName();
309 if (!b) {
310 QMessageBox *box = new QMessageBox(QMessageBox::Critical, PROGRAM_NAME " - Error",
311 QString(PROGRAM_NAME " could not open the file %1. Please verify the output file pattern.").arg(fileName));
312 box->setWindowModality(Qt::NonModal);
313 box->setAttribute(Qt::WA_DeleteOnClose);
314 box->show();
315 removeFile();
316 delete writer;
317 return;
320 serverLocal = new QTcpServer(this);
321 serverLocal->listen();
322 connect(serverLocal, SIGNAL(newConnection()), this, SLOT(acceptLocal()));
323 serverRemote = new QTcpServer(this);
324 serverRemote->listen();
325 connect(serverRemote, SIGNAL(newConnection()), this, SLOT(acceptRemote()));
327 QString rep1 = skype->sendWithReply(QString("ALTER CALL %1 SET_CAPTURE_MIC PORT=\"%2\"").arg(id).arg(serverLocal->serverPort()));
328 QString rep2 = skype->sendWithReply(QString("ALTER CALL %1 SET_OUTPUT SOUNDCARD=\"default\" PORT=\"%2\"").arg(id).arg(serverRemote->serverPort()));
330 if (!rep1.startsWith("ALTER CALL ") || !rep2.startsWith("ALTER CALL")) {
331 QMessageBox *box = new QMessageBox(QMessageBox::Critical, PROGRAM_NAME " - Error",
332 QString(PROGRAM_NAME " could not obtain the audio streams from Skype and can thus not record this call.\n\n"
333 "The replies from Skype were:\n%1\n%2").arg(rep1, rep2));
334 box->setWindowModality(Qt::NonModal);
335 box->setAttribute(Qt::WA_DeleteOnClose);
336 box->show();
337 removeFile();
338 delete writer;
339 delete serverRemote;
340 delete serverLocal;
341 return;
344 if (preferences.get(Pref::DebugWriteSyncFile).toBool()) {
345 syncFile.setFileName(fn + ".sync");
346 syncFile.open(QIODevice::WriteOnly);
347 syncTime.start();
350 isRecording = true;
351 emit startedRecording(id);
354 void Call::acceptLocal() {
355 socketLocal = serverLocal->nextPendingConnection();
356 serverLocal->close();
357 // we don't delete the server, since it contains the socket.
358 // we could reparent, but that automatic stuff of QT is great
359 connect(socketLocal, SIGNAL(readyRead()), this, SLOT(readLocal()));
360 connect(socketLocal, SIGNAL(disconnected()), this, SLOT(checkConnections()));
363 void Call::acceptRemote() {
364 socketRemote = serverRemote->nextPendingConnection();
365 serverRemote->close();
366 connect(socketRemote, SIGNAL(readyRead()), this, SLOT(readRemote()));
367 connect(socketRemote, SIGNAL(disconnected()), this, SLOT(checkConnections()));
370 void Call::readLocal() {
371 bufferLocal += socketLocal->readAll();
372 if (isRecording)
373 tryToWrite();
376 void Call::readRemote() {
377 bufferRemote += socketRemote->readAll();
378 if (isRecording)
379 tryToWrite();
382 void Call::checkConnections() {
383 if (socketLocal->state() == QAbstractSocket::UnconnectedState && socketRemote->state() == QAbstractSocket::UnconnectedState) {
384 debug(QString("Call %1: both connections closed, stop recording").arg(id));
385 stopRecording();
389 void Call::mixToMono(long samples) {
390 qint16 *localData = reinterpret_cast<qint16 *>(bufferLocal.data());
391 qint16 *remoteData = reinterpret_cast<qint16 *>(bufferRemote.data());
393 for (long i = 0; i < samples; i++)
394 localData[i] = ((qint32)localData[i] + (qint32)remoteData[i]) / (qint32)2;
397 void Call::mixToStereo(long samples, int pan) {
398 qint16 *localData = reinterpret_cast<qint16 *>(bufferLocal.data());
399 qint16 *remoteData = reinterpret_cast<qint16 *>(bufferRemote.data());
401 qint32 fl = 100 - pan;
402 qint32 fr = pan;
404 for (long i = 0; i < samples; i++) {
405 qint16 newLocal = ((qint32)localData[i] * fl + (qint32)remoteData[i] * fr) / (qint32)100;
406 qint16 newRemote = ((qint32)localData[i] * fr + (qint32)remoteData[i] * fl) / (qint32)100;
407 localData[i] = newLocal;
408 remoteData[i] = newRemote;
412 long Call::padBuffers() {
413 // pads the shorter buffer with silence, so they are both the same
414 // length afterwards. returns the new number of samples in each buffer
416 long l = bufferLocal.size();
417 long r = bufferRemote.size();
419 if (l < r) {
420 long amount = r - l;
421 bufferLocal.append(QByteArray(amount, 0));
422 debug(QString("Call %1: padding %2 samples on local buffer").arg(id).arg(amount / 2));
423 return r / 2;
424 } else if (l > r) {
425 long amount = l - r;
426 bufferRemote.append(QByteArray(amount, 0));
427 debug(QString("Call %1: padding %2 samples on remote buffer").arg(id).arg(amount / 2));
428 return l / 2;
431 return l / 2;
434 void Call::doSync(long s) {
435 if (s > 0) {
436 bufferLocal.append(QByteArray(s * 2, 0));
437 debug(QString("Call %1: padding %2 samples on local buffer").arg(id).arg(s));
438 } else {
439 bufferRemote.append(QByteArray(s * -2, 0));
440 debug(QString("Call %1: padding %2 samples on remote buffer").arg(id).arg(-s));
444 void Call::tryToWrite(bool flush) {
445 //debug(QString("Situation: %3, %4").arg(bufferLocal.size()).arg(bufferRemote.size()));
447 long samples; // number of samples to write
449 if (flush) {
450 // when flushing, we pad the shorter buffer, so that all
451 // available data is written. this shouldn't usually be a
452 // significant amount, but it might be if there was an audio
453 // I/O error in Skype.
454 samples = padBuffers();
455 } else {
456 long l = bufferLocal.size() / 2;
457 long r = bufferRemote.size() / 2;
459 sync.add(r - l);
461 long syncAmount = sync.getSync();
462 syncAmount = (syncAmount / 160) * 160;
464 if (syncAmount) {
465 doSync(syncAmount);
466 sync.reset();
467 l = bufferLocal.size() / 2;
468 r = bufferRemote.size() / 2;
471 if (syncFile.isOpen())
472 syncFile.write(QString("%1 %2 %3\n").arg(syncTime.elapsed()).arg(r - l).arg(syncAmount).toAscii().constData());
474 if (std::labs(r - l) > skypeSamplingRate * 20) {
475 // more than 20 seconds out of sync, something went
476 // wrong. avoid eating memory by accumulating data
477 long s = (r - l) / skypeSamplingRate;
478 debug(QString("Call %1: WARNING: seriously out of sync by %2s; padding").arg(id).arg(s));
479 samples = padBuffers();
480 sync.reset();
481 } else {
482 samples = l < r ? l : r;
484 // skype usually sends new PCM data every 10ms (160
485 // samples at 16kHz). let's accumulate at least 100ms
486 // of data before bothering to write it to disk
487 if (samples < skypeSamplingRate / 10)
488 return;
492 // got new samples to write to file, or have to flush. note that we
493 // have to flush even if samples == 0
495 bool success;
497 if (!stereo) {
498 // mono
499 mixToMono(samples);
500 QByteArray dummy;
501 success = writer->write(bufferLocal, dummy, samples, flush);
502 bufferRemote.remove(0, samples * 2);
503 } else if (stereoMix == 0) {
504 // local left, remote right
505 success = writer->write(bufferLocal, bufferRemote, samples, flush);
506 } else if (stereoMix == 100) {
507 // local right, remote left
508 success = writer->write(bufferRemote, bufferLocal, samples, flush);
509 } else {
510 mixToStereo(samples, stereoMix);
511 success = writer->write(bufferLocal, bufferRemote, samples, flush);
514 if (!success) {
515 QMessageBox *box = new QMessageBox(QMessageBox::Critical, PROGRAM_NAME " - Error",
516 QString(PROGRAM_NAME " encountered an error while writing this call to disk. Recording terminated."));
517 box->setWindowModality(Qt::NonModal);
518 box->setAttribute(Qt::WA_DeleteOnClose);
519 box->show();
520 stopRecording(false);
521 return;
524 // the writer will remove the samples from the buffers
525 //debug(QString("Call %1: wrote %2 samples").arg(id).arg(samples));
527 // TODO: handle the case where the two streams get out of sync (buffers
528 // not equally fulled by a significant amount). does skype document
529 // whether we always get two nice, equal, in-sync streams, even if
530 // there have been transmission errors? perl-script behavior: if out
531 // of sync by more than 6.4ms, then remove 1ms from the stream that's
532 // ahead.
535 void Call::stopRecording(bool flush) {
536 if (!isRecording)
537 return;
539 debug(QString("Call %1: stop recording").arg(id));
541 // NOTE: we don't delete the sockets here, because we may be here as a
542 // reaction to their disconnected() signals; and they don't like being
543 // deleted during their signals. we don't delete the servers either,
544 // since they own the sockets and we're too lazy to reparent. it's
545 // easiest to let QT handle all this on its own. there will be some
546 // memory wasted if you start/stop recording within the same call a few
547 // times, but unless you do it thousands of times, the waste is more
548 // than acceptable.
550 // flush data to writer
551 if (flush)
552 tryToWrite(true);
553 writer->close();
554 delete writer;
556 if (syncFile.isOpen())
557 syncFile.close();
559 // we must disconnect all signals from the sockets first, so that upon
560 // closing them it won't call checkConnections() and we don't land here
561 // recursively again
562 disconnect(socketLocal, 0, this, 0);
563 disconnect(socketRemote, 0, this, 0);
564 socketLocal->close();
565 socketRemote->close();
567 isRecording = false;
568 emit stoppedRecording(id);
571 // ---- CallHandler ----
573 CallHandler::CallHandler(QObject *parent, Skype *s) : QObject(parent), skype(s) {
576 CallHandler::~CallHandler() {
577 prune();
579 QList<Call *> list = calls.values();
580 if (!list.isEmpty()) {
581 debug(QString("Destroying CallHandler, these calls still exist:"));
582 for (int i = 0; i < list.size(); i++) {
583 Call *c = list.at(i);
584 debug(QString(" call %1, status=%2, okToDelete=%3").arg(c->getID()).arg(c->getStatus()).arg(c->okToDelete()));
588 delete legalInformationDialog;
591 void CallHandler::callCmd(const QStringList &args) {
592 CallID id = args.at(0).toInt();
594 if (ignore.contains(id))
595 return;
597 bool newCall = false;
599 Call *call;
601 if (calls.contains(id)) {
602 call = calls[id];
603 } else {
604 call = new Call(this, skype, id);
605 calls[id] = call;
606 newCall = true;
608 connect(call, SIGNAL(startedCall(int, const QString &)), this, SIGNAL(startedCall(int, const QString &)));
609 connect(call, SIGNAL(stoppedCall(int)), this, SIGNAL(stoppedCall(int)));
610 connect(call, SIGNAL(startedRecording(int)), this, SIGNAL(startedRecording(int)));
611 connect(call, SIGNAL(stoppedRecording(int)), this, SIGNAL(stoppedRecording(int)));
612 connect(call, SIGNAL(showLegalInformation()), this, SLOT(showLegalInformation()));
615 QString subCmd = args.at(1);
617 if (subCmd == "STATUS")
618 call->setStatus(args.at(2));
619 else if (newCall && subCmd == "DURATION")
620 // this is where we start recording calls that are already
621 // running, for example if the user starts this program after
622 // the call has been placed
623 call->setStatus("INPROGRESS");
625 prune();
628 void CallHandler::prune() {
629 QList<Call *> list = calls.values();
630 for (int i = 0; i < list.size(); i++) {
631 Call *c = list.at(i);
632 if (c->statusDone() && c->okToDelete()) {
633 // we ignore this call from now on, because Skype might still send
634 // us information about it, like "SEEN" or "VAA_INPUT_STATUS"
635 calls.remove(c->getID());
636 ignore.insert(c->getID());
637 delete c;
642 void CallHandler::startRecording(int id) {
643 if (!calls.contains(id))
644 return;
646 calls[id]->startRecording(true);
649 void CallHandler::stopRecording(int id) {
650 if (!calls.contains(id))
651 return;
653 Call *call = calls[id];
654 call->stopRecording();
655 call->hideConfirmation(2);
658 void CallHandler::stopRecordingAndDelete(int id) {
659 if (!calls.contains(id))
660 return;
662 Call *call = calls[id];
663 call->stopRecording();
664 call->removeFile();
665 call->hideConfirmation(0);
668 void CallHandler::showLegalInformation() {
669 if (preferences.get(Pref::SuppressLegalInformation).toBool())
670 return;
672 if (!legalInformationDialog)
673 legalInformationDialog = new LegalInformationDialog;
675 legalInformationDialog->raise();
676 legalInformationDialog->activateWindow();