* modules/gui/wxwindows/*: Added a stream output dialog box.
[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.1 2003/03/22 03:14:34 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     MulticastCheckbox_Event,
93
94     EncapsulationRadio1_Event, EncapsulationRadio2_Event,
95     EncapsulationRadio3_Event, EncapsulationRadio4_Event,
96 };
97
98 BEGIN_EVENT_TABLE(SoutDialog, wxDialog)
99     /* Button events */
100     EVT_BUTTON(wxID_OK, SoutDialog::OnOk)
101     EVT_BUTTON(wxID_CANCEL, SoutDialog::OnCancel)
102
103     /* Events generated by the access output panel */
104     EVT_RADIOBUTTON(AccessRadio1_Event, SoutDialog::OnAccessTypeChange)
105     EVT_RADIOBUTTON(AccessRadio2_Event, SoutDialog::OnAccessTypeChange)
106     EVT_RADIOBUTTON(AccessRadio3_Event, SoutDialog::OnAccessTypeChange)
107     EVT_RADIOBUTTON(AccessRadio4_Event, SoutDialog::OnAccessTypeChange)
108     EVT_TEXT(FileName_Event, SoutDialog::OnFileChange)
109     EVT_BUTTON(FileBrowse_Event, SoutDialog::OnFileBrowse)
110     EVT_TEXT(NetPort_Event, SoutDialog::OnNetChange)
111     EVT_SPINCTRL(NetPort_Event, SoutDialog::OnNetChange)
112     EVT_TEXT(NetAddr_Event, SoutDialog::OnNetChange)
113     EVT_CHECKBOX(MulticastCheckbox_Event, SoutDialog::OnMulticastChange)
114  
115     /* Events generated by the encapsulation panel */
116     EVT_RADIOBUTTON(EncapsulationRadio1_Event,
117                     SoutDialog::OnEncapsulationChange)
118     EVT_RADIOBUTTON(EncapsulationRadio2_Event,
119                     SoutDialog::OnEncapsulationChange)
120     EVT_RADIOBUTTON(EncapsulationRadio3_Event,
121                     SoutDialog::OnEncapsulationChange)
122     EVT_RADIOBUTTON(EncapsulationRadio4_Event,
123                     SoutDialog::OnEncapsulationChange)
124
125 END_EVENT_TABLE()
126
127 /*****************************************************************************
128  * Constructor.
129  *****************************************************************************/
130 SoutDialog::SoutDialog( intf_thread_t *_p_intf, Interface *_p_main_interface ):
131     wxDialog( _p_main_interface, -1, _("Stream Output"), wxDefaultPosition,
132              wxDefaultSize, wxDEFAULT_FRAME_STYLE )
133 {
134     /* Initializations */
135     p_intf = _p_intf;
136     p_main_interface = _p_main_interface;
137
138     /* Create a panel to put everything in */
139     wxPanel *panel = new wxPanel( this, -1 );
140     panel->SetAutoLayout( TRUE );
141
142     /* Initialise MRL value */
143     char *psz_sout = config_GetPsz( p_intf, "sout" );
144     if( psz_sout )
145     {
146         mrl = psz_sout;
147         free( psz_sout );
148     }
149
150     /* Create MRL combobox */
151     wxBoxSizer *mrl_sizer_sizer = new wxBoxSizer( wxHORIZONTAL );
152     wxStaticBox *mrl_box = new wxStaticBox( panel, -1,
153                                _("Stream Output MRL") );
154     wxStaticBoxSizer *mrl_sizer = new wxStaticBoxSizer( mrl_box,
155                                                         wxHORIZONTAL );
156     wxStaticText *mrl_label = new wxStaticText( panel, -1,
157                                                 _("Destination Target:") );
158     mrl_combo = new wxComboBox( panel, MRL_Event, mrl,
159                                 wxPoint(20,25), wxSize(120, -1),
160                                 0, NULL );
161     mrl_sizer->Add( mrl_label, 0, wxEXPAND | wxALL, 5 );
162     mrl_sizer->Add( mrl_combo, 1, wxEXPAND | wxALL, 5 );
163     mrl_sizer_sizer->Add( mrl_sizer, 1, wxEXPAND | wxALL, 5 );
164
165     /* Create the output encapsulation panel */
166     encapsulation_panel = EncapsulationPanel( panel );
167
168     /* Create the access output panel */
169     access_panel = AccessPanel( panel );
170
171     /* Separation */
172     wxStaticLine *static_line = new wxStaticLine( panel, wxID_OK );
173
174     /* Create the buttons */
175     wxButton *ok_button = new wxButton( panel, wxID_OK, _("OK") );
176     ok_button->SetDefault();
177     wxButton *cancel_button = new wxButton( panel, wxID_CANCEL, _("Cancel") );
178
179     /* Place everything in sizers */
180     wxBoxSizer *button_sizer = new wxBoxSizer( wxHORIZONTAL );
181     button_sizer->Add( ok_button, 0, wxALL, 5 );
182     button_sizer->Add( cancel_button, 0, wxALL, 5 );
183     button_sizer->Layout();
184     wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
185     wxBoxSizer *panel_sizer = new wxBoxSizer( wxVERTICAL );
186     panel_sizer->Add( mrl_sizer_sizer, 0, wxEXPAND, 5 );
187     panel_sizer->Add( access_panel, 1, wxEXPAND | wxALL, 5 );
188     panel_sizer->Add( encapsulation_panel, 0, wxEXPAND | wxALL, 5 );
189     panel_sizer->Add( static_line, 0, wxEXPAND | wxALL, 5 );
190     panel_sizer->Add( button_sizer, 0, wxALIGN_LEFT | wxALIGN_BOTTOM |
191                       wxALL, 5 );
192     panel_sizer->Layout();
193     panel->SetSizerAndFit( panel_sizer );
194     main_sizer->Add( panel, 1, wxGROW, 0 );
195     main_sizer->Layout();
196     SetSizerAndFit( main_sizer );
197 }
198
199 SoutDialog::~SoutDialog()
200 {
201 }
202
203 /*****************************************************************************
204  * Private methods.
205  *****************************************************************************/
206 void SoutDialog::UpdateMRL()
207 {
208     wxString encapsulation;
209
210     switch( i_encapsulation_type )
211     {
212     case PS_ENCAPSULATION:
213         encapsulation = "/ps";
214         break;
215     case AVI_ENCAPSULATION:
216         encapsulation = "/avi";
217         break;
218     case OGG_ENCAPSULATION:
219         encapsulation = "/ogg";
220         break;
221     case TS_ENCAPSULATION:
222     default:
223         encapsulation = "/ts";
224         break;
225     }
226
227     switch( i_access_type )
228     {
229     case FILE_ACCESS_OUT:
230         mrl = "file" + encapsulation + "://" + file_combo->GetValue();
231         break;
232
233     case HTTP_ACCESS_OUT:
234         mrl = "http" + encapsulation + "://" + net_addr->GetLineText(0)
235               + wxString::Format( ":%d", net_port->GetValue() );
236         break;
237
238     case UDP_ACCESS_OUT:
239         mrl = "udp" + encapsulation + (b_multicast ? "://@" : "://")
240               + net_addr->GetLineText(0)
241               + wxString::Format( ":%d", net_port->GetValue() );
242         break;
243
244     case RTP_ACCESS_OUT:
245         mrl = "rtp" + encapsulation + (b_multicast ? "://@" : "://")
246               + net_addr->GetLineText(0)
247               + wxString::Format( ":%d", net_port->GetValue() );
248         break;
249     }
250
251     mrl_combo->SetValue( mrl );
252 }
253
254 wxPanel *SoutDialog::AccessPanel( wxWindow* parent )
255 {
256     int i;
257     wxPanel *panel = new wxPanel( parent, -1, wxDefaultPosition,
258                                   wxSize(200, 200) );
259
260     wxFlexGridSizer *sizer = new wxFlexGridSizer( 2, 4, 20 );
261     wxStaticBox *panel_box = new wxStaticBox( panel, -1, _("Output Method") );
262     wxStaticBoxSizer *panel_sizer = new wxStaticBoxSizer( panel_box,
263                                                           wxHORIZONTAL );
264
265     static const wxString access_output_array[] =
266     {
267         _("File"),
268         _("HTTP"),
269         _("UDP"),
270         _("RTP")
271     };
272
273     for( i=0; i<4; i++ )
274     {
275         access_radios[i] = new wxRadioButton( panel, AccessRadio1_Event + i,
276                                               access_output_array[i],
277                                               wxDefaultPosition, wxDefaultSize,
278                                               wxRB_SINGLE );
279
280         access_subpanels[i] = new wxPanel( panel, -1,
281                                            wxDefaultPosition, wxDefaultSize );
282     }
283
284     /* File row */
285     wxFlexGridSizer *subpanel_sizer;
286     wxStaticText *label;
287     subpanel_sizer = new wxFlexGridSizer( 3, 1, 20 );
288     label = new wxStaticText( access_subpanels[0], -1, _("Filename") );
289     file_combo = new wxComboBox( access_subpanels[0], FileName_Event, "",
290                                  wxPoint(20,25), wxSize(200, -1), 0, NULL );
291     wxButton *browse_button = new wxButton( access_subpanels[0],
292                                             FileBrowse_Event, _("Browse...") );
293     subpanel_sizer->Add( label, 0, wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL );
294     subpanel_sizer->Add( file_combo, 1,
295                          wxEXPAND | wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL );
296     subpanel_sizer->Add( browse_button, 0,
297                          wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL );
298
299     access_subpanels[0]->SetSizerAndFit( subpanel_sizer );
300
301     /* Net row */
302     subpanel_sizer = new wxFlexGridSizer( 4, 1, 20 );
303     label = new wxStaticText( access_subpanels[2], -1, _("Address") );
304     net_addr = new wxTextCtrl( access_subpanels[2], NetAddr_Event, "",
305                                wxDefaultPosition, wxSize( 200, -1 ),
306                                wxTE_PROCESS_ENTER);
307     subpanel_sizer->Add( label, 0, wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL );
308     subpanel_sizer->Add( net_addr, 1,
309                          wxEXPAND | wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL );
310
311     label = new wxStaticText( access_subpanels[2], -1, _("Port") );
312     net_port = new wxSpinCtrl( access_subpanels[2], NetPort_Event,
313                                wxString::Format(_("%d"), 0/*val*/),
314                                wxDefaultPosition, wxDefaultSize,
315                                wxSP_ARROW_KEYS,
316                                0, 16000, 0/*val*/);
317
318     subpanel_sizer->Add( label, 0, wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL );
319     subpanel_sizer->Add( net_port, 0,
320                          wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL );
321
322     access_subpanels[2]->SetSizerAndFit( subpanel_sizer );
323
324     /* Multicast row */
325     subpanel_sizer = new wxFlexGridSizer( 1, 1, 20 );
326     b_multicast = VLC_FALSE;
327     multicast_checkbox = new wxCheckBox( access_subpanels[3],
328                                          MulticastCheckbox_Event,
329                                          _("Multicast") );
330     subpanel_sizer->Add( multicast_checkbox, 0, wxEXPAND |
331                          wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL );
332     access_subpanels[3]->SetSizerAndFit( subpanel_sizer );
333
334
335     /* Stuff everything into the main panel */
336     for( i=0; i<4; i++ )
337     {
338         sizer->Add( access_radios[i], 0,
339                     wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL );
340         sizer->Add( access_subpanels[i], 1, wxEXPAND | wxALIGN_CENTER_VERTICAL
341                     | wxALIGN_LEFT );
342     }
343
344     panel_sizer->Add( sizer, 1, wxEXPAND, 0 );
345
346     panel->SetSizerAndFit( panel_sizer );
347
348     /* Update access output panel */
349     net_addr->SetValue( "127.0.0.1" );
350     net_port->SetValue( config_GetInt( p_intf, "server-port" ) );
351     wxCommandEvent dummy_event;
352     dummy_event.SetId( AccessRadio1_Event );
353     OnAccessTypeChange( dummy_event );
354
355     return panel;
356 }
357
358 wxPanel *SoutDialog::EncapsulationPanel( wxWindow* parent )
359 {
360     int i;
361     wxPanel *panel = new wxPanel( parent, -1, wxDefaultPosition,
362                                   wxSize(200, 200) );
363
364     wxStaticBox *panel_box = new wxStaticBox( panel, -1,
365                                               _("Encapsulation Method") );
366     wxStaticBoxSizer *panel_sizer = new wxStaticBoxSizer( panel_box,
367                                                           wxHORIZONTAL );
368
369     static const wxString encapsulation_array[] =
370     {
371         _("MPEG TS"),
372         _("MPEG PS"),
373         _("AVI"),
374         _("Ogg")
375     };
376
377     /* Stuff everything into the main panel */
378     for( i=0; i<4; i++ )
379     {
380         encapsulation_radios[i] =
381             new wxRadioButton( panel, EncapsulationRadio1_Event + i,
382                                encapsulation_array[i] );
383         panel_sizer->Add( encapsulation_radios[i], 0,
384                           wxALIGN_LEFT | wxALIGN_CENTER_VERTICAL );
385     }
386
387     panel->SetSizerAndFit( panel_sizer );
388
389     /* Update encapsulation panel */
390     encapsulation_radios[0]->Enable();
391     i_encapsulation_type = TS_ENCAPSULATION;
392
393     return panel;
394 }
395
396 /*****************************************************************************
397  * Events methods.
398  *****************************************************************************/
399 void SoutDialog::OnOk( wxCommandEvent& WXUNUSED(event) )
400 {
401     EndModal( wxID_OK );
402 }
403
404 void SoutDialog::OnCancel( wxCommandEvent& WXUNUSED(event) )
405 {
406     EndModal( wxID_CANCEL );
407 }
408
409 void SoutDialog::OnMRLChange( wxCommandEvent& event )
410 {
411     mrl = event.GetString();
412 }
413
414 /*****************************************************************************
415  * Access output panel event methods.
416  *****************************************************************************/
417 void SoutDialog::OnAccessTypeChange( wxCommandEvent& event )
418 {
419     int i;
420     i_access_type = event.GetId() - AccessRadio1_Event;
421
422     switch( i_access_type )
423     {
424     case FILE_ACCESS_OUT:
425         access_subpanels[0]->Enable();
426         access_subpanels[2]->Disable();
427         access_subpanels[3]->Disable();
428         for( i = 1; i < 4; i++ )
429         {
430             encapsulation_radios[i]->Enable();
431         }
432         break;
433
434     case HTTP_ACCESS_OUT:
435         access_subpanels[0]->Disable();
436         access_subpanels[2]->Enable();
437         access_subpanels[3]->Disable();
438         for( i = 1; i < 4; i++ )
439         {
440             encapsulation_radios[i]->Enable();
441         }
442         break;
443
444     case UDP_ACCESS_OUT:
445     case RTP_ACCESS_OUT:
446         access_subpanels[0]->Disable();
447         access_subpanels[2]->Enable();
448         access_subpanels[3]->Enable();
449         for( i = 1; i < 4; i++ )
450         {
451             encapsulation_radios[i]->Disable();
452         }
453         encapsulation_radios[TS_ENCAPSULATION]->SetValue(true);
454         i_encapsulation_type = TS_ENCAPSULATION;
455         break;
456     }
457
458     for( i = 0; i < 4; i++ )
459     {
460         access_radios[i]->SetValue( event.GetId() == (AccessRadio1_Event+i) );
461     }
462
463     UpdateMRL();
464 }
465
466 /*****************************************************************************
467  * File access output event methods.
468  *****************************************************************************/
469 void SoutDialog::OnFileChange( wxCommandEvent& WXUNUSED(event) )
470 {
471     UpdateMRL();
472 }
473
474 void SoutDialog::OnFileBrowse( wxCommandEvent& WXUNUSED(event) )
475 {
476     wxFileDialog dialog( this, _("Save file"), "", "", "*.*", wxSAVE );
477
478     if( dialog.ShowModal() == wxID_OK )
479     {
480         file_combo->SetValue( dialog.GetPath() );      
481         UpdateMRL();
482     }
483 }
484
485 /*****************************************************************************
486  * Net access output event methods.
487  *****************************************************************************/
488 void SoutDialog::OnNetChange( wxCommandEvent& WXUNUSED(event) )
489 {
490     UpdateMRL();
491 }
492
493 void SoutDialog::OnMulticastChange( wxCommandEvent& event )
494 {
495     b_multicast = event.GetInt();
496     UpdateMRL();
497 }
498
499 /*****************************************************************************
500  * Encapsulation panel event methods.
501  *****************************************************************************/
502 void SoutDialog::OnEncapsulationChange( wxCommandEvent& event )
503 {
504     i_encapsulation_type = event.GetId() - EncapsulationRadio1_Event;
505     UpdateMRL();
506 }