Some basics of AudioMapping.
[dcpomatic.git] / src / wx / ffmpeg_content_dialog.cc
1 /* -*- c-basic-offset: 8; default-tab-width: 8; -*- */
2
3 /*
4     Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20 */
21
22 #include <boost/lexical_cast.hpp>
23 #include "lib/ffmpeg_content.h"
24 #include "ffmpeg_content_dialog.h"
25 #include "wx_util.h"
26 #include "audio_mapping_view.h"
27
28 using std::vector;
29 using std::string;
30 using boost::shared_ptr;
31 using boost::lexical_cast;
32
33 FFmpegContentDialog::FFmpegContentDialog (wxWindow* parent, shared_ptr<FFmpegContent> content)
34         : wxDialog (parent, wxID_ANY, _("Video"))
35 {
36         wxFlexGridSizer* grid = new wxFlexGridSizer (3, 6, 6);
37         grid->AddGrowableCol (1, 1);
38
39         add_label_to_sizer (grid, this, _("Audio Stream"));
40         _audio_stream = new wxChoice (this, wxID_ANY);
41         grid->Add (_audio_stream, 1, wxEXPAND | wxALL, 6);
42         _audio_description = new wxStaticText (this, wxID_ANY, wxT (""));
43         grid->Add (_audio_description, 1, wxALIGN_CENTER_VERTICAL | wxLEFT, 8);
44         
45         add_label_to_sizer (grid, this, _("Subtitle stream"));
46         _subtitle_stream = new wxChoice (this, wxID_ANY);
47         grid->Add (_subtitle_stream, 1, wxEXPAND | wxALL, 6);
48         grid->AddSpacer (0);
49
50         _audio_mapping = new AudioMappingView (this);
51         grid->Add (_audio_mapping, 1, wxEXPAND | wxALL, 6);
52         grid->AddSpacer (0);
53
54         _audio_stream->Clear ();
55         vector<FFmpegAudioStream> a = content->audio_streams ();
56         for (vector<FFmpegAudioStream>::iterator i = a.begin(); i != a.end(); ++i) {
57                 _audio_stream->Append (std_to_wx (i->name), new wxStringClientData (std_to_wx (lexical_cast<string> (i->id))));
58         }
59         
60         if (content->audio_stream()) {
61                 checked_set (_audio_stream, lexical_cast<string> (content->audio_stream()->id));
62         }
63
64         _subtitle_stream->Clear ();
65         vector<FFmpegSubtitleStream> s = content->subtitle_streams ();
66         if (s.empty ()) {
67                 _subtitle_stream->Enable (false);
68         }
69         for (vector<FFmpegSubtitleStream>::iterator i = s.begin(); i != s.end(); ++i) {
70                 _subtitle_stream->Append (std_to_wx (i->name), new wxStringClientData (std_to_wx (lexical_cast<string> (i->id))));
71         }
72         
73         if (content->subtitle_stream()) {
74                 checked_set (_subtitle_stream, lexical_cast<string> (content->subtitle_stream()->id));
75         } else {
76                 _subtitle_stream->SetSelection (wxNOT_FOUND);
77         }
78
79         _audio_mapping->set_mapping (content->audio_mapping ());
80
81         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
82         overall_sizer->Add (grid, 1, wxEXPAND | wxALL, 6);
83
84         wxSizer* buttons = CreateSeparatedButtonSizer (wxOK);
85         if (buttons) {
86                 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
87         }
88
89         SetSizer (overall_sizer);
90         overall_sizer->Layout ();
91         overall_sizer->SetSizeHints (this);
92
93         _audio_stream->Connect    (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED, wxCommandEventHandler (FFmpegContentDialog::audio_stream_changed), 0, this);
94         _subtitle_stream->Connect (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED, wxCommandEventHandler (FFmpegContentDialog::subtitle_stream_changed), 0, this);
95 }
96
97 void
98 FFmpegContentDialog::audio_stream_changed (wxCommandEvent &)
99 {
100         shared_ptr<FFmpegContent> c = _content.lock ();
101         if (!c) {
102                 return;
103         }
104         
105         vector<FFmpegAudioStream> a = c->audio_streams ();
106         vector<FFmpegAudioStream>::iterator i = a.begin ();
107         string const s = string_client_data (_audio_stream->GetClientObject (_audio_stream->GetSelection ()));
108         while (i != a.end() && lexical_cast<string> (i->id) != s) {
109                 ++i;
110         }
111
112         if (i != a.end ()) {
113                 c->set_audio_stream (*i);
114         }
115
116         if (!c->audio_stream ()) {
117                 _audio_description->SetLabel (wxT (""));
118         } else {
119                 wxString s;
120                 if (c->audio_channels() == 1) {
121                         s << _("1 channel");
122                 } else {
123                         s << c->audio_channels() << wxT (" ") << _("channels");
124                 }
125                 s << wxT (", ") << c->content_audio_frame_rate() << _("Hz");
126                 _audio_description->SetLabel (s);
127         }
128 }
129
130
131
132 void
133 FFmpegContentDialog::subtitle_stream_changed (wxCommandEvent &)
134 {
135         shared_ptr<FFmpegContent> c = _content.lock ();
136         if (!c) {
137                 return;
138         }
139         
140         vector<FFmpegSubtitleStream> a = c->subtitle_streams ();
141         vector<FFmpegSubtitleStream>::iterator i = a.begin ();
142         string const s = string_client_data (_subtitle_stream->GetClientObject (_subtitle_stream->GetSelection ()));
143         while (i != a.end() && lexical_cast<string> (i->id) != s) {
144                 ++i;
145         }
146
147         if (i != a.end ()) {
148                 c->set_subtitle_stream (*i);
149         }
150 }