2fb7fca508078c5e46c0203f2c01596e73e5eb91
[vlc.git] / modules / gui / wxwindows / streamout.cpp
1 /*****************************************************************************
2  * streamout.cpp : wxWindows plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2001 VideoLAN
5  * $Id: streamout.cpp,v 1.5 2003/04/01 16:11:43 gbazin Exp $
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <string.h>                                            /* strerror() */
30 #include <stdio.h>
31
32 #include <vlc/vlc.h>
33
34 #ifdef WIN32                                                 /* mingw32 hack */
35 #undef Yield
36 #undef CreateDialog
37 #endif
38
39 /* Let vlc take care of the i18n stuff */
40 #define WXINTL_NO_GETTEXT_MACRO
41
42 #include <wx/wxprec.h>
43 #include <wx/wx.h>
44 #include <wx/notebook.h>
45 #include <wx/textctrl.h>
46 #include <wx/combobox.h>
47 #include <wx/spinctrl.h>
48 #include <wx/statline.h>
49
50 #include <vlc/intf.h>
51
52 #include "wxwindows.h"
53
54 #ifndef wxRB_SINGLE
55 #   define wxRB_SINGLE 0
56 #endif
57
58 enum
59 {
60     FILE_ACCESS_OUT = 0,
61     HTTP_ACCESS_OUT,
62     UDP_ACCESS_OUT,
63     RTP_ACCESS_OUT
64 };
65
66 enum
67 {
68     TS_ENCAPSULATION = 0,
69     PS_ENCAPSULATION,
70     AVI_ENCAPSULATION,
71     OGG_ENCAPSULATION
72 };
73
74 /*****************************************************************************
75  * Event Table.
76  *****************************************************************************/
77
78 /* IDs for the controls and the menu commands */
79 enum
80 {
81     Notebook_Event = wxID_HIGHEST,
82     MRL_Event,
83
84     FileBrowse_Event,
85     FileName_Event,
86
87     AccessType_Event,
88     AccessRadio1_Event, AccessRadio2_Event,
89     AccessRadio3_Event, AccessRadio4_Event,
90     NetPort_Event,
91     NetAddr_Event,
92
93     EncapsulationRadio1_Event, EncapsulationRadio2_Event,
94     EncapsulationRadio3_Event, EncapsulationRadio4_Event,
95 };
96
97 BEGIN_EVENT_TABLE(SoutDialog, wxDialog)
98     /* Button events */
99     EVT_BUTTON(wxID_OK, SoutDialog::OnOk)
100     EVT_BUTTON(wxID_CANCEL, SoutDialog::OnCancel)
101
102     /* Events generated by the access output panel */
103     EVT_RADIOBUTTON(AccessRadio1_Event, SoutDialog::OnAccessTypeChange)
104     EVT_RADIOBUTTON(AccessRadio2_Event, SoutDialog::OnAccessTypeChange)
105     EVT_RADIOBUTTON(AccessRadio3_Event, SoutDialog::OnAccessTypeChange)
106     EVT_RADIOBUTTON(AccessRadio4_Event, SoutDialog::OnAccessTypeChange)
107     EVT_TEXT(FileName_Event, SoutDialog::OnFileChange)
108     EVT_BUTTON(FileBrowse_Event, SoutDialog::OnFileBrowse)
109     EVT_TEXT(NetPort_Event, SoutDialog::OnNetChange)
110     EVT_SPINCTRL(NetPort_Event, SoutDialog::OnNetChange)
111     EVT_TEXT(NetAddr_Event, SoutDialog::OnNetChange)
112  
113     /* Events generated by the encapsulation panel */
114     EVT_RADIOBUTTON(EncapsulationRadio1_Event,
115                     SoutDialog::OnEncapsulationChange)
116     EVT_RADIOBUTTON(EncapsulationRadio2_Event,
117                     SoutDialog::OnEncapsulationChange)
118     EVT_RADIOBUTTON(EncapsulationRadio3_Event,
119                     SoutDialog::OnEncapsulationChange)
120     EVT_RADIOBUTTON(EncapsulationRadio4_Event,
121                     SoutDialog::OnEncapsulationChange)
122
123 END_EVENT_TABLE()
124
125 /*****************************************************************************
126  * Constructor.
127  *****************************************************************************/
128 SoutDialog::SoutDialog( intf_thread_t *_p_intf, Interface *_p_main_interface ):
129     wxDialog( _p_main_interface, -1, _("Stream output"), wxDefaultPosition,
130              wxDefaultSize, wxDEFAULT_FRAME_STYLE )
131 {
132     /* Initializations */
133     p_intf = _p_intf;
134     p_main_interface = _p_main_interface;
135     SetIcon( *p_intf->p_sys->p_icon );
136
137     /* Create a panel to put everything in */
138     wxPanel *panel = new wxPanel( this, -1 );
139     panel->SetAutoLayout( TRUE );
140
141     /* Create MRL combobox */
142     wxBoxSizer *mrl_sizer_sizer = new wxBoxSizer( wxHORIZONTAL );
143     wxStaticBox *mrl_box = new wxStaticBox( panel, -1,
144                                _("Stream output MRL") );
145     wxStaticBoxSizer *mrl_sizer = new wxStaticBoxSizer( mrl_box,
146                                                         wxHORIZONTAL );
147     wxStaticText *mrl_label = new wxStaticText( panel, -1,
148                                                 _("Destination Target:") );
149     mrl_combo = new wxComboBox( panel, MRL_Event, mrl,
150                                 wxPoint(20,25), wxSize(120, -1),
151                                 0, NULL );
152     mrl_sizer->Add( mrl_label, 0, wxEXPAND | wxALL, 5 );
153     mrl_sizer->Add( mrl_combo, 1, wxEXPAND | wxALL, 5 );
154     mrl_sizer_sizer->Add( mrl_sizer, 1, wxEXPAND | wxALL, 5 );
155
156     /* Create the output encapsulation panel */
157     encapsulation_panel = EncapsulationPanel( panel );
158
159     /* Create the access output panel */
160     access_panel = AccessPanel( panel );
161
162     /* Separation */
163     wxStaticLine *static_line = new wxStaticLine( panel, wxID_OK );
164
165     /* Create the buttons */
166     wxButton *ok_button = new wxButton( panel, wxID_OK, _("OK") );
167     ok_button->SetDefault();
168     wxButton *cancel_button = new wxButton( panel, wxID_CANCEL, _("Cancel") );
169
170     /* Place everything in sizers */
171     wxBoxSizer *button_sizer = new wxBoxSizer( wxHORIZONTAL );
172     button_sizer->Add( ok_button, 0, wxALL, 5 );
173     button_sizer->Add( cancel_button, 0, wxALL, 5 );
174     button_sizer->Layout();
175     wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
176     wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
177     panel_sizer->Add( mrl_sizer_sizer, 0, wxEXPAND, 5 );
178     panel_sizer->Add( access_panel, 1, wxEXPAND | wxALL, 5 );
179     panel_sizer->Add( encapsulation_panel, 0, wxEXPAND | wxALL, 5 );
180     panel_sizer->Add( static_line, 0, wxEXPAND | wxALL, 5 );
181     panel_sizer->Add( button_sizer, 0, wxALIGN_LEFT | wxALIGN_BOTTOM |
182                       wxALL, 5 );
183     panel_sizer->Layout();
184     panel->SetSizerAndFit( panel_sizer );
185     main_sizer->Add( panel, 1, wxGROW, 0 );
186     main_sizer->Layout();
187     SetSizerAndFit( main_sizer );
188
189     /* Update all the values */
190     ParseMRL();
191 }
192
193 SoutDialog::~SoutDialog()
194 {
195 }
196
197 /*****************************************************************************
198  * Private methods.
199  *****************************************************************************/
200 void SoutDialog::UpdateMRL()
201 {
202     wxString encapsulation;
203
204     switch( i_encapsulation_type )
205     {
206     case PS_ENCAPSULATION:
207         encapsulation = "/ps";
208         break;
209     case AVI_ENCAPSULATION:
210         encapsulation = "/avi";
211         break;
212     case OGG_ENCAPSULATION:
213         encapsulation = "/ogg";
214         break;
215     case TS_ENCAPSULATION:
216     default:
217         encapsulation = "/ts";
218         break;
219     }
220
221     switch( i_access_type )
222     {
223     case FILE_ACCESS_OUT:
224         mrl = "file" + encapsulation + ":" + file_combo->GetValue();
225         break;
226
227     case HTTP_ACCESS_OUT:
228         mrl = "http" + encapsulation + ":" + net_addr->GetLineText(0)
229               + wxString::Format( ":%d", net_port->GetValue() );
230         break;
231
232     case UDP_ACCESS_OUT:
233     case RTP_ACCESS_OUT:
234         mrl = ( i_access_type == UDP_ACCESS_OUT ) ? "udp" : "rtp";
235         mrl += encapsulation + ":" + net_addr->GetLineText(0);
236         if( net_port->GetValue() != config_GetInt( p_intf, "server-port" ) )
237         {
238             mrl += wxString::Format( ":%d", net_port->GetValue() );
239         }
240         break;
241     }
242
243     mrl_combo->SetValue( mrl );
244 }
245
246 wxPanel *SoutDialog::AccessPanel( wxWindow* parent )
247 {
248     int i;
249     wxPanel *panel = new wxPanel( parent, -1, wxDefaultPosition,
250                                   wxSize(200, 200) );
251
252     wxFlexGridSizer *sizer = new wxFlexGridSizer( 2, 4, 20 );
253     wxStaticBox *panel_box = new wxStaticBox( panel, -1, _("Output Method") );
254     wxStaticBoxSizer *panel_sizer = new wxStaticBoxSizer( panel_box,
255                                                           wxHORIZONTAL );
256
257     static const wxString access_output_array[] =
258     {
259         _("File"),
260         _("HTTP"),
261         _("UDP"),
262         _("RTP")
263     };
264
265     for( i=0; i<4; i++ )
266     {
267         access_radios[i] = new wxRadioButton( panel, AccessRadio1_Event + i,
268                                               access_output_array[i],
269                                               wxDefaultPosition, wxDefaultSize,
270                                               wxRB_SINGLE );
271
272         access_subpanels[i] = new wxPanel( panel, -1,
273                                            wxDefaultPosition, wxDefaultSize );
274     }
275
276     /* File row */
277     wxFlexGridSizer *subpanel_sizer;
278     wxStaticText *label;
279     subpanel_sizer = new wxFlexGridSizer( 3, 1, 20 );
280     label = new wxStaticText( access_subpanels[0], -1, _("Filename") );
281     file_combo = new wxComboBox( access_subpanels[0], FileName_Event, "",
282                                  wxPoint(20,25), wxSize(200, -1), 0, NULL );
283     wxButton *browse_button = new wxButton( access_subpanels[0],
284                                             FileBrowse_Event, _("Browse...") );
285     subpanel_sizer->Add( label, 0, wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL );
286     subpanel_sizer->Add( file_combo, 1,
287                          wxEXPAND | wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL );
288     subpanel_sizer->Add( browse_button, 0,
289                          wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL );
290
291     access_subpanels[0]->SetSizerAndFit( subpanel_sizer );
292
293     /* Net row */
294     subpanel_sizer = new wxFlexGridSizer( 4, 1, 20 );
295     label = new wxStaticText( access_subpanels[2], -1, _("Address") );
296     net_addr = new wxTextCtrl( access_subpanels[2], NetAddr_Event, "",
297                                wxDefaultPosition, wxSize( 200, -1 ),
298                                wxTE_PROCESS_ENTER);
299     subpanel_sizer->Add( label, 0, wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL );
300     subpanel_sizer->Add( net_addr, 1,
301                          wxEXPAND | wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL );
302
303     int val = config_GetInt( p_intf, "server-port" );
304     label = new wxStaticText( access_subpanels[2], -1, _("Port") );
305     net_port = new wxSpinCtrl( access_subpanels[2], NetPort_Event,
306                                wxString::Format("%d", val),
307                                wxDefaultPosition, wxDefaultSize,
308                                wxSP_ARROW_KEYS,
309                                0, 16000, val );
310
311     subpanel_sizer->Add( label, 0, wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL );
312     subpanel_sizer->Add( net_port, 0,
313                          wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL );
314
315     access_subpanels[2]->SetSizerAndFit( subpanel_sizer );
316
317
318     /* Stuff everything into the main panel */
319     for( i=0; i<4; i++ )
320     {
321         sizer->Add( access_radios[i], 0,
322                     wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL );
323         sizer->Add( access_subpanels[i], 1, wxEXPAND | wxALIGN_CENTER_VERTICAL
324                     | wxALIGN_LEFT );
325     }
326
327     panel_sizer->Add( sizer, 1, wxEXPAND, 0 );
328
329     panel->SetSizerAndFit( panel_sizer );
330
331     return panel;
332 }
333
334 wxPanel *SoutDialog::EncapsulationPanel( wxWindow* parent )
335 {
336     int i;
337     wxPanel *panel = new wxPanel( parent, -1, wxDefaultPosition,
338                                   wxSize(200, 200) );
339
340     wxStaticBox *panel_box = new wxStaticBox( panel, -1,
341                                               _("Encapsulation Method") );
342     wxStaticBoxSizer *panel_sizer = new wxStaticBoxSizer( panel_box,
343                                                           wxHORIZONTAL );
344
345     static const wxString encapsulation_array[] =
346     {
347         _("MPEG TS"),
348         _("MPEG PS"),
349         _("AVI"),
350         _("Ogg")
351     };
352
353     /* Stuff everything into the main panel */
354     for( i=0; i<4; i++ )
355     {
356         encapsulation_radios[i] =
357             new wxRadioButton( panel, EncapsulationRadio1_Event + i,
358                                encapsulation_array[i] );
359         panel_sizer->Add( encapsulation_radios[i], 0,
360                           wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL | wxALL, 5 );
361     }
362
363     panel->SetSizerAndFit( panel_sizer );
364
365     /* Update encapsulation panel */
366     encapsulation_radios[0]->Enable();
367     i_encapsulation_type = TS_ENCAPSULATION;
368
369     return panel;
370 }
371
372 void SoutDialog::ParseMRL()
373 {
374     /* Initialise MRL value */
375     char *psz_sout = config_GetPsz( p_intf, "sout" );
376     if( psz_sout )
377     {
378         mrl = psz_sout;
379         free( psz_sout );
380     }
381
382     /* Parse the MRL */
383     wxString access = mrl.BeforeFirst( '/' );
384     wxString encapsulation = mrl.BeforeFirst( ':' ).AfterFirst('/');
385
386     if( !access.Cmp( "http" ) )
387     {
388         i_access_type = HTTP_ACCESS_OUT;
389     }
390     else if( !access.Cmp( "udp" ) )
391     {
392         i_access_type = UDP_ACCESS_OUT;
393     }
394     else if( !access.Cmp( "rtp" ) )
395     {
396         i_access_type = RTP_ACCESS_OUT;
397     }
398     else
399     {
400         i_access_type = FILE_ACCESS_OUT;
401     }
402
403     if( !encapsulation.Cmp( "ps" ) )
404     {
405         i_encapsulation_type = PS_ENCAPSULATION;
406     }
407     else if( !encapsulation.Cmp( "avi" ) )
408     {
409         i_encapsulation_type = AVI_ENCAPSULATION;
410     }
411     else if( !encapsulation.Cmp( "ogg" ) )
412     {
413         i_encapsulation_type = OGG_ENCAPSULATION;
414     }
415     else
416     {
417         i_encapsulation_type = TS_ENCAPSULATION;
418     }
419
420     wxString second_part = mrl.AfterFirst( ':' );
421
422     if( i_access_type == FILE_ACCESS_OUT )
423     {
424         /* The whole second part of the MRL is the filename */
425         file_combo->SetValue( second_part );
426     }
427     else
428     {
429         /* we've got address:port */
430         wxString address = second_part.BeforeLast( ':' );
431         net_addr->SetValue( address );
432
433         long int i_port;
434         wxString port = second_part.AfterLast( ':' );
435         if( port.ToLong( &i_port ) )
436         {
437             net_port->SetValue( i_port );
438         }
439         else
440         {
441             net_port->SetValue( config_GetInt( p_intf, "server-port" ) );
442         }
443     }
444
445     /* Update access output panel */
446     wxCommandEvent dummy_event;
447     dummy_event.SetId( AccessRadio1_Event + i_access_type );
448     OnAccessTypeChange( dummy_event );
449
450     /* Update encapsulation output panel */
451     dummy_event.SetId( EncapsulationRadio1_Event + i_encapsulation_type );
452     OnEncapsulationChange( dummy_event );
453 }
454
455 /*****************************************************************************
456  * Events methods.
457  *****************************************************************************/
458 void SoutDialog::OnOk( wxCommandEvent& WXUNUSED(event) )
459 {
460     EndModal( wxID_OK );
461 }
462
463 void SoutDialog::OnCancel( wxCommandEvent& WXUNUSED(event) )
464 {
465     EndModal( wxID_CANCEL );
466 }
467
468 void SoutDialog::OnMRLChange( wxCommandEvent& event )
469 {
470     mrl = event.GetString();
471 }
472
473 /*****************************************************************************
474  * Access output panel event methods.
475  *****************************************************************************/
476 void SoutDialog::OnAccessTypeChange( wxCommandEvent& event )
477 {
478     int i;
479     i_access_type = event.GetId() - AccessRadio1_Event;
480
481     switch( i_access_type )
482     {
483     case FILE_ACCESS_OUT:
484         access_subpanels[0]->Enable();
485         access_subpanels[2]->Disable();
486         access_subpanels[3]->Disable();
487         for( i = 1; i < 4; i++ )
488         {
489             encapsulation_radios[i]->Enable();
490         }
491         break;
492
493     case HTTP_ACCESS_OUT:
494         access_subpanels[0]->Disable();
495         access_subpanels[2]->Enable();
496         access_subpanels[3]->Disable();
497         for( i = 1; i < 4; i++ )
498         {
499             encapsulation_radios[i]->Enable();
500         }
501         break;
502
503     case UDP_ACCESS_OUT:
504     case RTP_ACCESS_OUT:
505         access_subpanels[0]->Disable();
506         access_subpanels[2]->Enable();
507         access_subpanels[3]->Enable();
508         for( i = 1; i < 4; i++ )
509         {
510             encapsulation_radios[i]->Disable();
511         }
512         encapsulation_radios[TS_ENCAPSULATION]->SetValue(true);
513         i_encapsulation_type = TS_ENCAPSULATION;
514         break;
515     }
516
517     for( i = 0; i < 4; i++ )
518     {
519         access_radios[i]->SetValue( event.GetId() == (AccessRadio1_Event+i) );
520     }
521
522     UpdateMRL();
523 }
524
525 /*****************************************************************************
526  * File access output event methods.
527  *****************************************************************************/
528 void SoutDialog::OnFileChange( wxCommandEvent& WXUNUSED(event) )
529 {
530     UpdateMRL();
531 }
532
533 void SoutDialog::OnFileBrowse( wxCommandEvent& WXUNUSED(event) )
534 {
535     wxFileDialog dialog( this, _("Save file"), "", "", "*.*", wxSAVE );
536
537     if( dialog.ShowModal() == wxID_OK )
538     {
539         file_combo->SetValue( dialog.GetPath() );
540         UpdateMRL();
541     }
542 }
543
544 /*****************************************************************************
545  * Net access output event methods.
546  *****************************************************************************/
547 void SoutDialog::OnNetChange( wxCommandEvent& WXUNUSED(event) )
548 {
549     UpdateMRL();
550 }
551
552 /*****************************************************************************
553  * Encapsulation panel event methods.
554  *****************************************************************************/
555 void SoutDialog::OnEncapsulationChange( wxCommandEvent& event )
556 {
557     i_encapsulation_type = event.GetId() - EncapsulationRadio1_Event;
558     UpdateMRL();
559 }