QTfrontend/net/recorder.cpp
author nemo
Wed, 06 Mar 2013 12:55:48 -0500
changeset 8676 c00f1df47cef
parent 8434 4821897a0f10
child 9080 9b42757d7e71
permissions -rw-r--r--
Add missing feedback button translation
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>
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    21
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    22
#include "recorder.h"
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    23
#include "gameuiconfig.h"
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    24
#include "hwconsts.h"
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    25
#include "game.h"
7897
5e7c0810f365 libav name refactor
koda
parents: 7868
diff changeset
    26
#include "LibavInteraction.h"
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    27
7507
3032a5739fe1 improve video uploading
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    28
// 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
    29
// of simultaneous encoders.
3032a5739fe1 improve video uploading
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    30
static const int maxRecorders = 3;
3032a5739fe1 improve video uploading
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    31
static int numRecorders = 0;
3032a5739fe1 improve video uploading
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    32
3032a5739fe1 improve video uploading
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    33
static QList<HWRecorder*> queue;
3032a5739fe1 improve video uploading
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    34
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
    35
HWRecorder::HWRecorder(GameUIConfig * config, const QString &prefix) :
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    36
    TCPBase(false)
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    37
{
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    38
    this->config = config;
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
    39
    this->prefix = prefix;
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
    40
    finished = false;
7897
5e7c0810f365 libav name refactor
koda
parents: 7868
diff changeset
    41
    name = prefix + "." + LibavInteraction::instance().getExtension(config->AVFormat());
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    42
}
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
HWRecorder::~HWRecorder()
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    45
{
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
    46
    emit encodingFinished(finished);
7507
3032a5739fe1 improve video uploading
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    47
    if (queue.empty())
3032a5739fe1 improve video uploading
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    48
        numRecorders--;
3032a5739fe1 improve video uploading
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    49
    else
8069
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 7897
diff changeset
    50
        queue.takeFirst()->Start(false);
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    51
}
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
void HWRecorder::onClientDisconnect()
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    54
{
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    55
}
7235
baa69bd025d9 1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents: 7198
diff changeset
    56
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    57
void HWRecorder::onClientRead()
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    58
{
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    59
    quint8 msglen;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    60
    quint32 bufsize;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    61
    while (!readbuffer.isEmpty() && ((bufsize = readbuffer.size()) > 0) &&
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    62
            ((msglen = readbuffer.data()[0]) < bufsize))
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    63
    {
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    64
        QByteArray msg = readbuffer.left(msglen + 1);
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    65
        readbuffer.remove(0, msglen + 1);
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
    66
        switch (msg.at(1))
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
    67
        {
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
    68
        case '?':
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    69
            SendIPC("!");
7280
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
    70
            break;
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
    71
        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
    72
            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
    73
            break;
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
    74
        case 'v':
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
    75
            finished = true;
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
    76
            break;
fd707afbc3a2 pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents: 7235
diff changeset
    77
        }
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    78
    }
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
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
    81
void HWRecorder::EncodeVideo(const QByteArray & record)
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    82
{
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    83
    toSendBuf = record;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    84
    toSendBuf.replace(QByteArray("\x02TD"), QByteArray("\x02TV"));
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    85
    toSendBuf.replace(QByteArray("\x02TL"), QByteArray("\x02TV"));
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    86
    toSendBuf.replace(QByteArray("\x02TN"), QByteArray("\x02TV"));
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    87
    toSendBuf.replace(QByteArray("\x02TS"), QByteArray("\x02TV"));
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    88
7507
3032a5739fe1 improve video uploading
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    89
    if (numRecorders < maxRecorders)
3032a5739fe1 improve video uploading
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    90
    {
3032a5739fe1 improve video uploading
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    91
        numRecorders++;
8069
bb7671829935 - Only allow one engine instance running at the moment
unc0rr
parents: 7897
diff changeset
    92
        Start(false); // run engine
7507
3032a5739fe1 improve video uploading
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    93
    }
3032a5739fe1 improve video uploading
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    94
    else
3032a5739fe1 improve video uploading
Stepan777 <stepik-777@mail.ru>
parents: 7376
diff changeset
    95
        queue.push_back(this);
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    96
}
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
QStringList HWRecorder::getArguments()
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
    99
{
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   100
    QStringList arguments;
7235
baa69bd025d9 1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents: 7198
diff changeset
   101
    QRect resolution = config->rec_Resolution();
8325
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8069
diff changeset
   102
    QString nick = config->netNick().toUtf8().toBase64();
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8069
diff changeset
   103
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8069
diff changeset
   104
    arguments << "--internal";
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8069
diff changeset
   105
    arguments << "--port";
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   106
    arguments << QString("%1").arg(ipc_port);
8325
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8069
diff changeset
   107
    arguments << "--prefix";
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8069
diff changeset
   108
    arguments << datadir->absolutePath();
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8069
diff changeset
   109
    arguments << "--user-prefix";
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8069
diff changeset
   110
    arguments << cfgdir->absolutePath();
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8069
diff changeset
   111
    arguments << "--locale";
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8069
diff changeset
   112
    arguments << HWGame::tr("en.txt");
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8069
diff changeset
   113
    arguments << "--frame-interval";
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   114
    arguments << QString::number(config->timerInterval());
8325
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8069
diff changeset
   115
    arguments << "--width";
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8069
diff changeset
   116
    arguments << QString::number(resolution.width());
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8069
diff changeset
   117
    arguments << "--height";
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8069
diff changeset
   118
    arguments << QString::number(resolution.height());
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8069
diff changeset
   119
    arguments << "--nosound";
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8069
diff changeset
   120
    arguments << "--raw-quality";
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   121
    arguments << QString::number(config->translateQuality());
8325
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8069
diff changeset
   122
    arguments << "--stereo";
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   123
    arguments << QString::number(config->stereoMode());
8325
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8069
diff changeset
   124
    arguments << "--nomusic";
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8069
diff changeset
   125
    arguments << "--volume";
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8069
diff changeset
   126
    arguments << "0";
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8069
diff changeset
   127
    if (config->isAltDamageEnabled())
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8069
diff changeset
   128
        arguments << "--altdmg";
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8069
diff changeset
   129
    if (!nick.isEmpty()) {
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8069
diff changeset
   130
        arguments << "--nick";
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8069
diff changeset
   131
        arguments << nick;
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8069
diff changeset
   132
    }
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8069
diff changeset
   133
    arguments << "--recorder";
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8069
diff changeset
   134
    arguments << QString::number(config->rec_Framerate()); //cVideoFramerateNum
ecd51650d5d8 GCI2012: Change Argument Passing Between Frontend and Engine
RowanD
parents: 8069
diff changeset
   135
    arguments << "1"; //cVideoFramerateDen
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   136
    arguments << prefix;
7235
baa69bd025d9 1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents: 7198
diff changeset
   137
    arguments << config->AVFormat();
baa69bd025d9 1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents: 7198
diff changeset
   138
    arguments << config->videoCodec();
8434
4821897a0f10 big indentation/whitespaces cleanup
sheepluva
parents: 8325
diff changeset
   139
// Could use a field to use quality instead. maybe quality could override bitrate - or just pass (and set) both.
7633
d4251e519062 Allow adjusting bitrate so that I can get a somewhat usable webm video. The audio is still tinny and unlistenable. Configuration option for that might be helpful, or just adjusting defaults in the wrapper.
nemo
parents: 7507
diff changeset
   140
// The library does support using both at once after all.
d4251e519062 Allow adjusting bitrate so that I can get a somewhat usable webm video. The audio is still tinny and unlistenable. Configuration option for that might be helpful, or just adjusting defaults in the wrapper.
nemo
parents: 7507
diff changeset
   141
    arguments << QString::number(config->rec_Bitrate()*1024);
7868
bfbc6aa87b7c disable fps widget when recording
koda
parents: 7794
diff changeset
   142
    arguments << (config->recordAudio() ? config->audioCodec() : "no");
7180
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   143
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   144
    return arguments;
53ffc8853008 here it is
Stepan777 <stepik-777@mail.ru>
parents:
diff changeset
   145
}