]> git.sesse.net Git - vlc/blob - modules/gui/qt4/components/sout/profile_selector.cpp
Qt: Sout Profiles: be sure to set a mux.
[vlc] / modules / gui / qt4 / components / sout / profile_selector.cpp
1 /*****************************************************************************
2  * profile_selector.cpp : A small profile selector and editor
3  ****************************************************************************
4  * Copyright (C) 2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jean-Baptiste Kempf <jb@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24
25 #include "components/sout/profile_selector.hpp"
26 #include "components/sout/profiles.hpp"
27 #include "dialogs/sout.hpp"
28
29 #include <QHBoxLayout>
30 #include <QToolButton>
31 #include <QComboBox>
32 #include <QLabel>
33 #include <QMessageBox>
34
35 VLCProfileSelector::VLCProfileSelector( QWidget *_parent ): QWidget( _parent )
36 {
37     QHBoxLayout *layout = new QHBoxLayout( this );
38
39     QLabel *prLabel = new QLabel( qtr( "Profile"), this );
40     layout->addWidget( prLabel );
41
42     profileBox = new QComboBox( this );
43     layout->addWidget( profileBox );
44
45     QToolButton *editButton = new QToolButton( this );
46     editButton->setIcon( QIcon( ":/preferences" ) );
47     editButton->setToolTip( qtr( "Edit selected profile" ) );
48     layout->addWidget( editButton );
49
50     QToolButton *deleteButton = new QToolButton( this );
51     deleteButton->setIcon( QIcon( ":/clear" ) );
52     deleteButton->setToolTip( qtr( "Delete selected profile" ) );
53     layout->addWidget( deleteButton );
54
55     QToolButton *newButton = new QToolButton( this );
56     newButton->setIcon( QIcon( ":/new" ) );
57     newButton->setToolTip( qtr( "Create a new profile" ) );
58     layout->addWidget(newButton);
59
60     BUTTONACT( newButton, newProfile() );
61     BUTTONACT( editButton, editProfile() );
62     BUTTONACT( deleteButton, deleteProfile() );
63     fillProfilesCombo();
64
65     CONNECT( profileBox, activated( int ),
66              this, updateOptions( int ) );
67
68     updateOptions( 0 );
69 }
70
71 inline void VLCProfileSelector::fillProfilesCombo()
72 {
73     QSettings settings(
74 #ifdef WIN32
75             QSettings::IniFormat,
76 #else
77             QSettings::NativeFormat,
78 #endif
79             QSettings::UserScope, "vlc", "vlc-qt-interface" );
80
81     int i_size = settings.beginReadArray( "codecs-profiles" );
82
83     for( int i = 0; i < i_size; i++ )
84     {
85         settings.setArrayIndex( i );
86         if( settings.value( "Profile-Name" ).toString().isEmpty() ) continue;
87         profileBox->addItem( settings.value( "Profile-Name" ).toString(),
88                 settings.value( "Profile-Value" ) );
89     }
90     if( i_size == 0 )
91     {
92         for( int i = 0; i < NB_PROFILE; i++ )
93         {
94             profileBox->addItem( video_profile_name_list[i],
95                                  video_profile_value_list[i] );
96         }
97     }
98     settings.endArray();
99 }
100
101 void VLCProfileSelector::newProfile()
102 {
103     editProfile( "", "" );
104 }
105
106 void VLCProfileSelector::editProfile()
107 {
108     editProfile( profileBox->currentText(),
109                  profileBox->itemData( profileBox->currentIndex() ).toString() );
110 }
111
112 void VLCProfileSelector::editProfile( QString qs, QString value )
113 {
114     VLCProfileEditor *editor = new VLCProfileEditor( qs, value, this );
115
116     if( QDialog::Accepted == editor->exec() )
117     {
118         if( qs.isEmpty() )
119             profileBox->addItem( editor->name, QVariant( editor->transcodeValue() ) );
120         else
121         {
122             int i_profile = profileBox->findText( qs );
123             profileBox->setItemText( i_profile, editor->name );
124             profileBox->setItemData( i_profile, QVariant( editor->transcodeValue() ) );
125         }
126     }
127     delete editor;
128
129     saveProfiles();
130     emit optionsChanged();
131 }
132
133 void VLCProfileSelector::deleteProfile()
134 {
135     profileBox->removeItem( profileBox->currentIndex() );
136 }
137
138 void VLCProfileSelector::saveProfiles()
139 {
140     QSettings settings(
141 #ifdef WIN32
142             QSettings::IniFormat,
143 #else
144             QSettings::NativeFormat,
145 #endif
146             QSettings::UserScope, "vlc", "vlc-qt-interface" );
147
148     settings.beginWriteArray( "codecs-profiles" );
149     for( int i = 0; i < profileBox->count(); i++ )
150     {
151         settings.setArrayIndex( i );
152         settings.setValue( "Profile-Name", profileBox->itemText( i ) );
153         settings.setValue( "Profile-Value", profileBox->itemData( i ).toString() );
154     }
155     settings.endArray();
156 }
157
158 void VLCProfileSelector::updateOptions( int i )
159 {
160     QStringList options = profileBox->itemData( i ).toString().split( ";" );
161     if( options.size() < 16 )
162         return;
163
164     mux = options[0];
165
166     SoutMrl smrl;
167     if( options[1].toInt() || options[2].toInt() )
168     {
169         smrl.begin( "transcode" );
170
171         if( options[1].toInt() )
172         {
173             smrl.option( "vcodec", options[4] );
174             smrl.option( "vb", options[5].toInt() );
175             smrl.option( "scale", options[6] );
176             smrl.option( "fps", options[7] );
177             smrl.option( "width", options[8].toInt() );
178             smrl.option( "height", options[9].toInt() );
179         }
180
181         if( options[2].toInt() )
182         {
183             smrl.option( "acodec", options[10] );
184             smrl.option( "ab", options[11].toInt() );
185             smrl.option( "channels", options[12].toInt() );
186             smrl.option( "samplerate", options[13].toInt() );
187         }
188
189         if( options[3].toInt() )
190         {
191             smrl.option( "scodec", options[14] );
192             if( options[15].toInt() )
193                 smrl.option( "soverlay" );
194         }
195
196         smrl.end();
197
198         transcode = smrl.getMrl();
199     }
200     else
201         transcode = "";
202     emit optionsChanged();
203 }
204
205
206 /**
207  * VLCProfileEditor
208  **/
209 VLCProfileEditor::VLCProfileEditor( QString qs_name, QString value,
210         QWidget *_parent )
211                  : QVLCDialog( _parent, NULL )
212 {
213     ui.setupUi( this );
214     if( !qs_name.isEmpty() )
215     {
216         ui.profileLine->setText( qs_name );
217         ui.profileLine->setReadOnly( true );
218     }
219     registerCodecs();
220     CONNECT( ui.transcodeVideo, toggled( bool ),
221             this, setVTranscodeOptions( bool ) );
222     CONNECT( ui.transcodeAudio, toggled( bool ),
223             this, setATranscodeOptions( bool ) );
224     CONNECT( ui.transcodeSubs, toggled( bool ),
225             this, setSTranscodeOptions( bool ) );
226     setVTranscodeOptions( false );
227     setATranscodeOptions( false );
228     setSTranscodeOptions( false );
229
230     QPushButton *saveButton = new QPushButton( qtr( "Save" ) );
231     ui.buttonBox->addButton( saveButton, QDialogButtonBox::AcceptRole );
232     BUTTONACT( saveButton, close() );
233     QPushButton *cancelButton = new QPushButton( qtr( "Cancel" ) );
234     ui.buttonBox->addButton( cancelButton, QDialogButtonBox::RejectRole );
235     BUTTONACT( cancelButton, reject() );
236
237     fillProfile( value );
238 }
239
240 inline void VLCProfileEditor::registerCodecs()
241 {
242
243 #define ADD_VCODEC( name, fourcc ) ui.vCodecBox->addItem( name, QVariant( fourcc ) );
244     ADD_VCODEC( "MPEG-1", "mp1v" )
245     ADD_VCODEC( "MPEG-2", "mp2v" )
246     ADD_VCODEC( "MPEG-4", "mp4v" )
247     ADD_VCODEC( "DIVX 1" , "DIV1" )
248     ADD_VCODEC( "DIVX 2" , "DIV2" )
249     ADD_VCODEC( "DIVX 3" , "DIV3" )
250     ADD_VCODEC( "H-263", "H263" )
251     ADD_VCODEC( "H-264", "h264" )
252     ADD_VCODEC( "WMV1", "WMV1" )
253     ADD_VCODEC( "WMV2" , "WMV2" )
254     ADD_VCODEC( "M-JPEG", "MJPG" )
255     ADD_VCODEC( "Theora", "theo" )
256     ADD_VCODEC( "Dirac", "drac" )
257
258 #define ADD_ACODEC( name, fourcc ) ui.aCodecBox->addItem( name, QVariant( fourcc ) );
259     ADD_ACODEC( "MPEG Audio", "mpga" )
260     ADD_ACODEC( "MP3", "mp3" )
261     ADD_ACODEC( "MPEG 4 Audio ( AAC )", "mp4a" )
262     ADD_ACODEC( "A52/AC-3", "a52" )
263     ADD_ACODEC( "Vorbis", "vorb" )
264     ADD_ACODEC( "Flac", "flac" )
265     ADD_ACODEC( "Speex", "spx" )
266     ADD_ACODEC( "WAV", "s16l" )
267     ADD_ACODEC( "WMA", "wma" )
268
269 #define ADD_SCALING( factor ) ui.vScaleBox->addItem( factor );
270     ADD_SCALING( "1" )
271     ADD_SCALING( "0.25" )
272     ADD_SCALING( "0.5" )
273     ADD_SCALING( "0.75" )
274     ADD_SCALING( "1.25" )
275     ADD_SCALING( "1.5" )
276     ADD_SCALING( "1.75" )
277     ADD_SCALING( "2" )
278
279 #define ADD_SAMPLERATE( sample ) ui.aSampleBox->addItem( sample );
280     ADD_SAMPLERATE( "11250" )
281     ADD_SAMPLERATE( "22500" )
282     ADD_SAMPLERATE( "44100" )
283     ADD_SAMPLERATE( "48000" )
284
285 #define ADD_SCODEC( name, fourcc ) ui.subsCodecBox->addItem( name, QVariant( fourcc ) );
286     ADD_SCODEC( "DVB subtitle", "dvbs" )
287     ADD_SCODEC( "T.140", "t140" )
288 }
289
290 void VLCProfileEditor::fillProfile( QString qs )
291 {
292     QStringList options = qs.split( ";" );
293     if( options.size() < 16 )
294         return;
295
296     ui.transcodeVideo->setChecked( options[1].toInt() );
297     ui.transcodeAudio->setChecked( options[2].toInt() );
298     ui.transcodeSubs->setChecked( options[3].toInt() );
299
300     ui.vCodecBox->setCurrentIndex( ui.vCodecBox->findData( options[4] ) );
301     ui.vBitrateSpin->setValue( options[5].toInt() );
302     ui.vScaleBox->setEditText( options[6] );
303     ui.vFrameBox->setValue( options[7].toDouble() );
304     ui.widthBox->setValue( options[8].toInt() );
305     ui.heightBox->setValue( options[9].toInt() );
306
307     ui.aCodecBox->setCurrentIndex( ui.aCodecBox->findData( options[10] ) );
308     ui.aBitrateSpin->setValue( options[11].toInt() );
309     ui.aChannelsSpin->setValue( options[12].toInt() );
310     ui.aSampleBox->setCurrentIndex( ui.aSampleBox->findText( options[13] ) );
311
312     ui.subsCodecBox->setCurrentIndex( ui.subsCodecBox->findData( options[14] ) );
313     ui.subsOverlay->setChecked( options[15].toInt() );
314 }
315
316 void VLCProfileEditor::setVTranscodeOptions( bool b_trans )
317 {
318     ui.vCodecLabel->setEnabled( b_trans );
319     ui.vCodecBox->setEnabled( b_trans );
320     ui.vBitrateLabel->setEnabled( b_trans );
321     ui.vBitrateSpin->setEnabled( b_trans );
322     ui.vScaleLabel->setEnabled( b_trans );
323     ui.vScaleBox->setEnabled( b_trans );
324     ui.heightBox->setEnabled( b_trans );
325     ui.heightLabel->setEnabled( b_trans );
326     ui.widthBox->setEnabled( b_trans );
327     ui.widthLabel->setEnabled( b_trans );
328     ui.vFrameBox->setEnabled( b_trans );
329     ui.vFrameLabel->setEnabled( b_trans );
330 }
331
332 void VLCProfileEditor::setATranscodeOptions( bool b_trans )
333 {
334     ui.aCodecLabel->setEnabled( b_trans );
335     ui.aCodecBox->setEnabled( b_trans );
336     ui.aBitrateLabel->setEnabled( b_trans );
337     ui.aBitrateSpin->setEnabled( b_trans );
338     ui.aChannelsLabel->setEnabled( b_trans );
339     ui.aChannelsSpin->setEnabled( b_trans );
340     ui.aSampleLabel->setEnabled( b_trans );
341     ui.aSampleBox->setEnabled( b_trans );
342 }
343
344 void VLCProfileEditor::setSTranscodeOptions( bool b_trans )
345 {
346     ui.subsCodecBox->setEnabled( b_trans );
347     ui.subsOverlay->setEnabled( b_trans );
348 }
349
350 void VLCProfileEditor::close()
351 {
352     if( ui.profileLine->text().isEmpty() )
353     {
354         QMessageBox::warning( this, qtr(" Profile Name Missing" ),
355                 qtr( "You must set a name for the profile." ) );
356         ui.profileLine->setFocus();
357         return;
358     }
359     name = ui.profileLine->text();
360
361     accept();
362 }
363
364 QString VLCProfileEditor::transcodeValue()
365 {
366 #define SMUX( x, txt ) if( ui.x->isChecked() ) muxValue =  txt; else
367     SMUX( PSMux, "ps" )
368     SMUX( TSMux, "ts" )
369     SMUX( MPEG1Mux, "mpeg1" )
370     SMUX( OggMux, "ogg" )
371     SMUX( ASFMux, "asf" )
372     SMUX( MOVMux, "mp4" )
373     SMUX( WAVMux, "wav" )
374     SMUX( RAWMux, "raw" )
375     SMUX( FLVMux, "flv" )
376     SMUX( MKVMux, "mkv" )
377     SMUX( AVIMux, "avi" )
378     SMUX( MJPEGMux, "mjpg" );
379
380 #define currentData( box ) box->itemData( box->currentIndex() )
381     QStringList transcodeMRL;
382     transcodeMRL
383             << muxValue
384
385             << QString::number( ui.transcodeVideo->isChecked() )
386             << QString::number( ui.transcodeAudio->isChecked() )
387             << QString::number( ui.transcodeSubs->isChecked() )
388
389             << currentData( ui.vCodecBox ).toString()
390             << QString::number( ui.vBitrateSpin->value() )
391             << ui.vScaleBox->currentText()
392             << QString::number( ui.vFrameBox->value() )
393             << QString::number( ui.widthBox->value() )
394             << QString::number( ui.heightBox->value() )
395
396             << currentData( ui.aCodecBox ).toString()
397             << QString::number( ui.aBitrateSpin->value() )
398             << QString::number( ui.aChannelsSpin->value() )
399             << ui.aSampleBox->currentText()
400
401             << currentData( ui.subsCodecBox ).toString()
402             << QString::number( ui.subsOverlay->isChecked() );
403
404     return transcodeMRL.join( ";" );
405 }
406