File: k3bexternalencoder.cpp

package info (click to toggle)
k3b 2.0.3a-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 50,416 kB
  • ctags: 12,996
  • sloc: cpp: 100,125; sh: 67; xml: 10; makefile: 8
file content (318 lines) | stat: -rw-r--r-- 9,033 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
 * Copyright (C) 2003-2009 Sebastian Trueg <trueg@k3b.org>
 * Copyright (C) 2010 Michal Malek <michalm@jabster.pl>
 *
 * This file is part of the K3b project.
 * Copyright (C) 1998-2009 Sebastian Trueg <trueg@k3b.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * See the file "COPYING" for the exact licensing terms.
 */

#include "k3bexternalencoder.h"
#include "k3bexternalencodercommand.h"

#include <config-k3b.h>

#include "k3bcore.h"
#include "k3bprocess.h"

#include <KDebug>
#include <KConfig>
#include <KLocale>
#include <KStandardDirs>

#include <QFile>
#include <QList>
#include <QRegExp>

#include <sys/types.h>


K3B_EXPORT_PLUGIN(k3bexternalencoder, K3bExternalEncoder)

Q_DECLARE_METATYPE( QProcess::ExitStatus )


static const unsigned char s_riffHeader[] =
{
    0x52, 0x49, 0x46, 0x46, // 0  "RIFF"
    0x00, 0x00, 0x00, 0x00, // 4  wavSize
    0x57, 0x41, 0x56, 0x45, // 8  "WAVE"
    0x66, 0x6d, 0x74, 0x20, // 12 "fmt "
    0x10, 0x00, 0x00, 0x00, // 16
    0x01, 0x00, 0x02, 0x00, // 20
    0x44, 0xac, 0x00, 0x00, // 24
    0x10, 0xb1, 0x02, 0x00, // 28
    0x04, 0x00, 0x10, 0x00, // 32
    0x64, 0x61, 0x74, 0x61, // 36 "data"
    0x00, 0x00, 0x00, 0x00  // 40 byteCount
};





static K3bExternalEncoderCommand commandByExtension( const QString& extension )
{
    QList<K3bExternalEncoderCommand> cmds( K3bExternalEncoderCommand::readCommands() );
    for( QList<K3bExternalEncoderCommand>::iterator it = cmds.begin(); it != cmds.end(); ++it )
        if( (*it).extension == extension )
            return *it;

    kDebug() << "(K3bExternalEncoder) could not find command for extension " << extension;

    return K3bExternalEncoderCommand();
}


class K3bExternalEncoder::Private
{
public:
    K3b::Process* process;
    QString fileName;
    K3b::Msf length;

    K3bExternalEncoderCommand cmd;

    bool initialized;
};


K3bExternalEncoder::K3bExternalEncoder( QObject* parent, const QVariantList& )
    : K3b::AudioEncoder( parent ),
      d( new Private() )
{
    d->process = 0;
    d->initialized = false;

    qRegisterMetaType<QProcess::ExitStatus>();
}


K3bExternalEncoder::~K3bExternalEncoder()
{
    if( d->process ) {
        disconnect( d->process );
        d->process->deleteLater();
    }
    delete d;
}


void K3bExternalEncoder::finishEncoderInternal()
{
    if( d->process && d->process->state() == QProcess::Running ) {
        d->process->closeWriteChannel();

        // this is kind of evil...
        // but we need to be sure the process exited when this method returnes
        d->process->waitForFinished(-1);
    }
    d->initialized = false;
}


void K3bExternalEncoder::slotExternalProgramFinished( int exitCode, QProcess::ExitStatus exitStatus )
{
    if( (exitStatus != QProcess::NormalExit) || (exitCode != 0) )
        kDebug() << "(K3bExternalEncoder) program exited with error.";
}


bool K3bExternalEncoder::openFile( const QString& ext, const QString& filename, const K3b::Msf& length, const MetaData& metaData )
{
    d->fileName = filename;
    d->length = length;

    // delete existing files as some programs (like flac for example) might refuse to overwrite files
    if( QFile::exists( filename ) )
        QFile::remove( filename );

    return initEncoderInternal( ext, length, metaData );
}


bool K3bExternalEncoder::isOpen() const
{
    return d->initialized;
}


void K3bExternalEncoder::closeFile()
{
    finishEncoderInternal();
}


bool K3bExternalEncoder::initEncoderInternal( const QString& extension, const K3b::Msf& /*length*/, const MetaData& metaData )
{
    // find the correct command
    d->cmd = commandByExtension( extension );

    if( !d->cmd.command.isEmpty() ) {
        // create the commandline
        QStringList params = d->cmd.command.split( ' ' );
        for( QStringList::iterator it = params.begin(); it != params.end(); ++it ) {
            (*it).replace( "%f", d->fileName );
            (*it).replace( "%a", metaData[META_TRACK_ARTIST].toString() );
            (*it).replace( "%t", metaData[META_TRACK_TITLE].toString() );
            (*it).replace( "%c", metaData[META_TRACK_COMMENT].toString() );
            (*it).replace( "%y", metaData[META_YEAR].toString() );
            (*it).replace( "%m", metaData[META_ALBUM_TITLE].toString() );
            (*it).replace( "%r", metaData[META_ALBUM_ARTIST].toString() );
            (*it).replace( "%x", metaData[META_ALBUM_COMMENT].toString() );
            (*it).replace( "%n", metaData[META_TRACK_NUMBER].toString() );
            (*it).replace( "%g", metaData[META_GENRE].toString() );
        }


        kDebug() << "***** external parameters:";
        kDebug() << params.join( " " ) << flush;

        // set one general error message
        setLastError( i18n("Command failed: %1", params.join( " " ) ) );

        // always create a new process since we are called in a separate thread
        if( d->process ) {
            disconnect( d->process );
            d->process->deleteLater();
        }
        d->process = new K3b::Process();
        d->process->setSplitStdout( true );
        connect( d->process, SIGNAL(finished(int, QProcess::ExitStatus)),
                this, SLOT(slotExternalProgramFinished(int, QProcess::ExitStatus)) );
        connect( d->process, SIGNAL(stderrLine(QString)),
                 this, SLOT(slotExternalProgramOutput(QString)) );
        connect( d->process, SIGNAL(stdoutLine(QString)),
                 this, SLOT(slotExternalProgramOutput(QString)) );

        d->process->setProgram( params );
        d->process->start( KProcess::SeparateChannels );

        if( d->process->waitForStarted() ) {
            if( d->cmd.writeWaveHeader )
                d->initialized = writeWaveHeader();
            else
                d->initialized = true;
        }
        else {
            QString commandName = d->cmd.command.section( QRegExp("\\s+"), 0 );
            if( !KStandardDirs::findExe( commandName ).isEmpty() )
                setLastError( i18n("Could not find program '%1'",commandName) );

            d->initialized = false;
        }

    }
    else {
        setLastError( i18n("Invalid command: the command is empty.") );
        d->initialized = false;
    }

    return d->initialized;
}


bool K3bExternalEncoder::writeWaveHeader()
{
    kDebug() << "(K3bExternalEncoder) writing wave header";

    // write the RIFF thing
    if( d->process->write( (const char*) s_riffHeader, 4 ) != 4 ) {
        kDebug() << "(K3bExternalEncoder) failed to write riff header.";
        return false;
    }

    // write the wave size
    qint32 dataSize( d->length.audioBytes() );
    qint32 wavSize( dataSize + 44 - 8 );
    char c[4];

    c[0] = (wavSize   >> 0 ) & 0xff;
    c[1] = (wavSize   >> 8 ) & 0xff;
    c[2] = (wavSize   >> 16) & 0xff;
    c[3] = (wavSize   >> 24) & 0xff;

    if( d->process->write( c, 4 ) != 4 ) {
        kDebug() << "(K3bExternalEncoder) failed to write wave size.";
        return false;
    }

    // write static part of the header
    if( d->process->write( (const char*) s_riffHeader + 8, 32 ) != 32 ) {
        kDebug() << "(K3bExternalEncoder) failed to write wave header.";
        return false;
    }

    c[0] = (dataSize   >> 0 ) & 0xff;
    c[1] = (dataSize   >> 8 ) & 0xff;
    c[2] = (dataSize   >> 16) & 0xff;
    c[3] = (dataSize   >> 24) & 0xff;

    if( d->process->write( c, 4 ) != 4 ) {
        kDebug() << "(K3bExternalEncoder) failed to write data size.";
        return false;
    }

    return d->process->waitForBytesWritten( -1 );
}


long K3bExternalEncoder::encodeInternal( const char* data, Q_ULONG len )
{
    if( !d->initialized )
        return -1;

    if( d->process->state() == QProcess::Running ) {

        long written = 0;

        if( d->cmd.swapByteOrder ) {
            char* buffer = new char[len];
            for( unsigned int i = 0; i < len-1; i+=2 ) {
                buffer[i] = data[i+1];
                buffer[i+1] = data[i];
            }

            written = d->process->write( buffer, len );
            delete [] buffer;
        }
        else
            written = d->process->write( data, len );

        d->process->waitForBytesWritten( -1 );

        return written;
    }
    else
        return -1;
}


void K3bExternalEncoder::slotExternalProgramOutput( const QString& line )
{
    kDebug() << "(" << d->cmd.name << ") " << line;
}


QStringList K3bExternalEncoder::extensions() const
{
    QStringList el;
    QList<K3bExternalEncoderCommand> cmds( K3bExternalEncoderCommand::readCommands() );
    for( QList<K3bExternalEncoderCommand>::iterator it = cmds.begin(); it != cmds.end(); ++it )
        el.append( (*it).extension );

    return el;
}


QString K3bExternalEncoder::fileTypeComment( const QString& ext ) const
{
    return commandByExtension( ext ).name;
}

#include "k3bexternalencoder.moc"