QTfrontend/net/recorder.cpp
author sheepluva
Tue, 28 Aug 2012 21:59:49 +0200
changeset 7618 4d3a32d91028
parent 7507 3032a5739fe1
child 7633 d4251e519062
permissions -rw-r--r--
fix build for me
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
     1
/*
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
     2
 * Hedgewars, a free turn based strategy game
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
     3
 * Copyright (c) 2004-2012 Andrey Korotaev <unC0Rr@gmail.com>
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
     4
 *
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
     8
 *
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    12
 * GNU General Public License for more details.
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    13
 *
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    17
 */
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    18
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    19
#include <QString>
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    20
#include <QByteArray>
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
    21
//#include <QMessageBox>
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    22
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    23
#include "recorder.h"
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    24
#include "gameuiconfig.h"
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    25
#include "hwconsts.h"
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    26
#include "game.h"
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
    27
#include "libav_iteraction.h"
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    28
7507
3032a5739fe1 improve video uploading
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    29
// Encoding is memory expensive process, so we need to limit maximum number
3032a5739fe1 improve video uploading
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    30
// of simultaneous encoders.
3032a5739fe1 improve video uploading
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    31
static const int maxRecorders = 3;
3032a5739fe1 improve video uploading
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    32
static int numRecorders = 0;
3032a5739fe1 improve video uploading
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    33
3032a5739fe1 improve video uploading
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    34
static QList<HWRecorder*> queue;
3032a5739fe1 improve video uploading
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    35
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
    36
HWRecorder::HWRecorder(GameUIConfig * config, const QString &prefix) :
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    37
    TCPBase(false)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    38
{
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    39
    this->config = config;
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
    40
    this->prefix = prefix;
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
    41
    finished = false;
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
    42
    name = prefix + "." + LibavIteraction::instance().getExtension(config->AVFormat());
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    43
}
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    44
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    45
HWRecorder::~HWRecorder()
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    46
{
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
    47
    emit encodingFinished(finished);
7507
3032a5739fe1 improve video uploading
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    48
    if (queue.empty())
3032a5739fe1 improve video uploading
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    49
        numRecorders--;
3032a5739fe1 improve video uploading
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    50
    else
3032a5739fe1 improve video uploading
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    51
        queue.takeFirst()->Start();
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    52
}
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    53
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    54
void HWRecorder::onClientDisconnect()
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    55
{
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    56
}
7235
baa69bd025d9 1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents: 7198
diff changeset
    57
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    58
void HWRecorder::onClientRead()
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    59
{
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    60
    quint8 msglen;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    61
    quint32 bufsize;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    62
    while (!readbuffer.isEmpty() && ((bufsize = readbuffer.size()) > 0) &&
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    63
            ((msglen = readbuffer.data()[0]) < bufsize))
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    64
    {
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    65
        QByteArray msg = readbuffer.left(msglen + 1);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    66
        readbuffer.remove(0, msglen + 1);
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
    67
        switch (msg.at(1))
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
    68
        {
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
    69
        case '?':
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    70
            SendIPC("!");
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
    71
            break;
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
    72
        case 'p':
7376
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7280
diff changeset
    73
            emit onProgress((quint8(msg.at(2))*256.0 + quint8(msg.at(3)))*0.0001);
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
    74
            break;
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
    75
        case 'v':
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
    76
            finished = true;
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
    77
            break;
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
    78
        }
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    79
    }
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    80
}
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    81
7376
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7280
diff changeset
    82
void HWRecorder::EncodeVideo(const QByteArray & record)
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    83
{
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    84
    toSendBuf = record;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    85
    toSendBuf.replace(QByteArray("\x02TD"), QByteArray("\x02TV"));
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    86
    toSendBuf.replace(QByteArray("\x02TL"), QByteArray("\x02TV"));
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    87
    toSendBuf.replace(QByteArray("\x02TN"), QByteArray("\x02TV"));
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    88
    toSendBuf.replace(QByteArray("\x02TS"), QByteArray("\x02TV"));
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    89
7507
3032a5739fe1 improve video uploading
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    90
    if (numRecorders < maxRecorders)
3032a5739fe1 improve video uploading
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    91
    {
3032a5739fe1 improve video uploading
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    92
        numRecorders++;
3032a5739fe1 improve video uploading
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    93
        Start(); // run engine
3032a5739fe1 improve video uploading
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    94
    }
3032a5739fe1 improve video uploading
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    95
    else
3032a5739fe1 improve video uploading
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    96
        queue.push_back(this);
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    97
}
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    98
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    99
QStringList HWRecorder::getArguments()
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   100
{
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   101
    QStringList arguments;
7235
baa69bd025d9 1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents: 7198
diff changeset
   102
    QRect resolution = config->rec_Resolution();
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   103
    arguments << cfgdir->absolutePath();
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   104
    arguments << QString::number(resolution.width());
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   105
    arguments << QString::number(resolution.height());
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   106
    arguments << "32"; // bpp
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   107
    arguments << QString("%1").arg(ipc_port);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   108
    arguments << "0"; // fullscreen
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   109
    arguments << "0"; // sound
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   110
    arguments << "0"; // music
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   111
    arguments << "0"; // sound volume
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   112
    arguments << QString::number(config->timerInterval());
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   113
    arguments << datadir->absolutePath();
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   114
    arguments << (config->isShowFPSEnabled() ? "1" : "0");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   115
    arguments << (config->isAltDamageEnabled() ? "1" : "0");
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   116
    arguments << config->netNick().toUtf8().toBase64();
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   117
    arguments << QString::number(config->translateQuality());
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   118
    arguments << QString::number(config->stereoMode());
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   119
    arguments << HWGame::tr("en.txt");
7376
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7280
diff changeset
   120
    arguments << QString::number(config->rec_Framerate()); // framerate numerator
48b79b3ca592 rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents: 7280
diff changeset
   121
    arguments << "1";  // framerate denominator
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   122
    arguments << prefix;
7235
baa69bd025d9 1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents: 7198
diff changeset
   123
    arguments << config->AVFormat();
baa69bd025d9 1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents: 7198
diff changeset
   124
    arguments << config->videoCodec();
7198
5debd5fe526e 1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents: 7180
diff changeset
   125
    arguments << "5"; // video quality
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
   126
    arguments << (config->recordAudio()? config->audioCodec() : "no");
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   127
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   128
    return arguments;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   129
}