]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/sout.cpp
Qt/Sout: avoid a possible crash
[vlc] / modules / gui / qt4 / dialogs / sout.cpp
1 /*****************************************************************************
2  * sout.cpp : Stream output dialog ( old-style )
3  ****************************************************************************
4  * Copyright (C) 2007-2009 the VideoLAN team
5  *
6  * $Id$
7  *
8  * Authors: Clément Stenac <zorglub@videolan.org>
9  *          Jean-Baptiste Kempf <jb@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * ( at your option ) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include "dialogs/sout.hpp"
31 #include "util/qt_dirs.hpp"
32 #include "components/sout/sout_widgets.hpp"
33
34 #include <QString>
35 #include <QFileDialog>
36 #include <QToolButton>
37 #include <assert.h>
38
39 SoutDialog::SoutDialog( QWidget *parent, intf_thread_t *_p_intf, const QString& inputMRL )
40            : QVLCDialog( parent,  _p_intf )
41 {
42     setWindowTitle( qtr( "Stream Output" ) );
43     setWindowRole( "vlc-stream-output" );
44
45     /* UI stuff */
46     ui.setupUi( this );
47     ui.inputBox->setMRL( inputMRL );
48     ui.helpEdit->setPlainText( qtr("This dialog will allow you to stream or "
49             "convert your media for use locally, on your private network, "
50             "or on the Internet.\n"
51             "You should start by checking that source matches what you want "
52             "your input to be and then press the \"Next\" "
53             "button to continue.\n") );
54
55     ui.mrlEdit->setToolTip ( qtr( "Stream output string.\n"
56                 "This is automatically generated "
57                  "when you change the above settings,\n"
58                  "but you can change it manually." ) ) ;
59
60 #if 0
61     /* This needs Qt4.5 to be cool */
62     ui.destTab->setTabsClosable( true );
63 #else
64     closeTabButton = new QToolButton( this );
65     ui.destTab->setCornerWidget( closeTabButton );
66     closeTabButton->hide();
67     closeTabButton->setAutoRaise( true );
68     closeTabButton->setIcon( QIcon( ":/toolbar/clear" ) );
69     BUTTONACT( closeTabButton, closeTab() );
70 #endif
71     CONNECT( ui.destTab, currentChanged( int ), this, tabChanged( int ) );
72     ui.destTab->setTabIcon( 0, QIcon( ":/buttons/playlist/playlist_add" ) );
73
74     ui.destBox->addItem( qtr( "File" ) );
75     ui.destBox->addItem( "HTTP" );
76     ui.destBox->addItem( "MS-WMSP (MMSH)" );
77     ui.destBox->addItem( "RTSP" );
78     ui.destBox->addItem( "RTP / MPEG Transport Stream" );
79     ui.destBox->addItem( "RTP Audio/Video Profile" );
80     ui.destBox->addItem( "UDP (legacy)" );
81     ui.destBox->addItem( "IceCast" );
82
83     BUTTONACT( ui.addButton, addDest() );
84
85 //     /* Connect everything to the updateMRL function */
86 #define CB( x ) CONNECT( ui.x, toggled( bool ), this, updateMRL() );
87 #define CT( x ) CONNECT( ui.x, textChanged( const QString& ), this, updateMRL() );
88 #define CS( x ) CONNECT( ui.x, valueChanged( int ), this, updateMRL() );
89 #define CC( x ) CONNECT( ui.x, currentIndexChanged( int ), this, updateMRL() );
90
91     /* Misc */
92     CB( soutAll );  CS( ttl ); CT( sapName ); CT( sapGroup );
93     CB( localOutput ); CB( transcodeBox );
94     CONNECT( ui.profileSelect, optionsChanged(), this, updateMRL() );
95
96     okButton = new QPushButton( qtr( "&Stream" ) );
97     QPushButton *cancelButton = new QPushButton( qtr( "&Cancel" ) );
98
99     okButton->setDefault( true );
100     ui.acceptButtonBox->addButton( okButton, QDialogButtonBox::AcceptRole );
101     ui.acceptButtonBox->addButton( cancelButton, QDialogButtonBox::RejectRole );
102
103     BUTTONACT( okButton, ok() );
104     BUTTONACT( cancelButton, cancel() );
105
106     BUTTONACT( ui.nextButton, next() );
107     BUTTONACT( ui.nextButton2, next() );
108     BUTTONACT( ui.prevButton, prev() );
109     BUTTONACT( ui.prevButton2, prev() );
110
111 #undef CC
112 #undef CS
113 #undef CT
114 #undef CB
115 }
116
117 void SoutDialog::next()
118 {
119     ui.toolBox->setCurrentIndex( ui.toolBox->currentIndex() + 1 );
120 }
121
122 void SoutDialog::prev()
123 {
124     ui.toolBox->setCurrentIndex( ui.toolBox->currentIndex() - 1 );
125 }
126
127 void SoutDialog::tabChanged( int i )
128 {
129     closeTabButton->setVisible( (i != 0) );
130 }
131
132 void SoutDialog::closeTab()
133 {
134     int i = ui.destTab->currentIndex();
135     if( i == 0 ) return;
136
137     QWidget *temp = ui.destTab->currentWidget();
138     ui.destTab->removeTab( i );
139     delete temp;
140     updateMRL();
141 }
142
143 void SoutDialog::addDest( )
144 {
145     VirtualDestBox *db;
146     QString caption;
147
148     switch( ui.destBox->currentIndex() )
149     {
150         case 0:
151             db = new FileDestBox( this );
152             caption = qtr( "File" );
153             break;
154         case 1:
155             db = new HTTPDestBox( this );
156             caption = qfu( "HTTP" );
157             break;
158         case 2:
159             db = new MMSHDestBox( this );
160             caption = qfu( "WMSP" );
161             break;
162         case 3:
163             db = new RTSPDestBox( this );
164             caption = qfu( "RTSP" );
165             break;
166         case 4:
167             db = new RTPDestBox( this, "ts" );
168             caption = "RTP/TS";
169             break;
170         case 5:
171             db = new RTPDestBox( this );
172             caption = "RTP/AVP";
173             break;
174         case 6:
175             db = new UDPDestBox( this );
176             caption = "UDP";
177             break;
178         case 7:
179             db = new ICEDestBox( this );
180             caption = "Icecast";
181             break;
182         default:
183             assert(0);
184     }
185
186     int index = ui.destTab->addTab( db, caption );
187     CONNECT( db, mrlUpdated(), this, updateMRL() );
188     ui.destTab->setCurrentIndex( index );
189     updateMRL();
190 }
191
192 void SoutDialog::ok()
193 {
194     mrl = ui.mrlEdit->toPlainText();
195     accept();
196 }
197
198 void SoutDialog::cancel()
199 {
200     mrl.clear();
201     reject();
202 }
203
204 void SoutDialog::updateMRL()
205 {
206     QString qs_mux = ui.profileSelect->getMux();
207
208     SoutMrl smrl( ":sout=#" );
209     if( !ui.profileSelect->getTranscode().isEmpty() && ui.transcodeBox->isChecked() )
210     {
211         smrl.begin( ui.profileSelect->getTranscode() );
212         smrl.end();
213     }
214
215     bool multi = false;
216
217     if( ui.destTab->count() >= 3 ||
218         ( ui.destTab->count() == 2 && ui.localOutput->isChecked() ) )
219         multi = true;
220
221     if( multi )
222         smrl.begin( "duplicate" );
223
224     for( int i = 1; i < ui.destTab->count(); i++ )
225     {
226         VirtualDestBox *vdb = qobject_cast<VirtualDestBox *>(ui.destTab->widget( i ));
227         if( !vdb )
228             continue;
229
230         QString tempMRL = vdb->getMRL( qs_mux );
231         if( tempMRL.isEmpty() ) continue;
232
233         if( multi )
234             smrl.option( "dst", tempMRL );
235         else
236         {
237             smrl.begin( tempMRL);
238             smrl.end();
239         }
240     }
241     if( ui.localOutput->isChecked() )
242     {
243         if( multi )
244             smrl.option( "dst", "display" );
245         else
246         {
247             smrl.begin( "display" );
248             smrl.end();
249         }
250     }
251
252     if ( multi ) smrl.end();
253
254     mrl = smrl.getMrl();
255
256     if( ui.sap->isChecked() )
257     {
258         QString group = ui.sapGroup->text();
259         QString name = ui.sapName->text();
260
261         /* FIXME: This sucks. We should really return a QStringList instead of
262          * (mis)quoting, concatainating and split input item paramters. */
263         name = name.replace( " ", " " );
264         group = group.replace( " ", " " );
265
266         /* We need to add options for both standard and rtp targets */
267         /* This is inelegant but simple and functional */
268         mrl.append( qfu( " :sout-rtp-sap" ) );
269         mrl.append( qfu( " :sout-rtp-name=" ) + name );
270         mrl.append( qfu( " :sout-standard-sap" ) );
271         mrl.append( qfu( " :sout-standard-name=" ) + name );
272         mrl.append( qfu( " :sout-standard-group=" ) + group );
273     }
274     else
275     {
276         mrl.append( qfu( " :no-sout-rtp-sap" ) );
277         mrl.append( qfu( " :no-sout-standard-sap" ) );
278     }
279
280     if( ui.soutAll->isChecked() )  mrl.append( " :sout-all" );
281
282     mrl.append( " :sout-keep" );
283
284     ui.mrlEdit->setPlainText( mrl );
285 }
286