summaryrefslogtreecommitdiff
path: root/src/tools/dcpomatic_processor.cc
blob: 460de87b6db4fbbf5cd9c0aef4dee0c8a8059545 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
    Copyright (C) 2025 Carl Hetherington <cth@carlh.net>

    This file is part of DCP-o-matic.

    DCP-o-matic is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    DCP-o-matic is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.

*/


#include "wx/about_dialog.h"
#include "wx/dcpomatic_button.h"
#include "wx/check_box.h"
#include "wx/editable_list.h"
#include "wx/i18n_setup.h"
#include "wx/wx_signal_manager.h"
#include "wx/wx_util.h"
#include "wx/wx_variant.h"
#include "lib/cross.h"
#include "lib/dcpomatic_log.h"
#include "lib/file_log.h"
#include "lib/fix_audio_levels_job.h"
#include "lib/job_manager.h"
#include "lib/util.h"
#include "lib/variant.h"
#include <dcp/search.h>
#include <dcp/warnings.h>
LIBDCP_DISABLE_WARNINGS
#include <wx/evtloop.h>
#include <wx/wx.h>
LIBDCP_ENABLE_WARNINGS
#ifdef __WXGTK__
#include <X11/Xlib.h>
#endif


using std::dynamic_pointer_cast;
using std::exception;
using std::make_shared;
using std::shared_ptr;
using std::string;
using std::vector;
#if BOOST_VERSION >= 106100
using namespace boost::placeholders;
#endif


/** @file  src/tools/dcpomatic_processor.cc
 *  @brief A tool to batch-process DCPs in some way (e.g. fix their sound levels)
 */


class DirDialogWrapper : public wxDirDialog
{
public:
	DirDialogWrapper(wxWindow* parent)
		: wxDirDialog(parent, _("Choose a folder"), {}, wxDD_DIR_MUST_EXIST)
	{

	}

	vector<boost::filesystem::path> get() const
	{
		return dcp::find_potential_dcps(boost::filesystem::path(wx_to_std(GetPath())));
	}

	void set(boost::filesystem::path)
	{
		/* Not used */
	}
};



class ProgressPanel : public wxPanel
{
public:
	ProgressPanel(wxWindow* parent)
	: wxPanel(parent, wxID_ANY)
	{
		auto overall_sizer = new wxBoxSizer(wxVERTICAL);

		_job_name = new wxStaticText(this, wxID_ANY, {});
		overall_sizer->Add(_job_name, 0, wxEXPAND | wxTOP | wxLEFT | wxRIGHT, DCPOMATIC_SIZER_GAP);

		_progress = new wxGauge(this, wxID_ANY, 100);
		overall_sizer->Add(_progress, 0, wxEXPAND | wxALL, DCPOMATIC_SIZER_GAP);

		SetSizerAndFit(overall_sizer);
	}

	void update(std::shared_ptr<const FixAudioLevelsJob> job)
	{
		auto const progress = job->progress();
		if (progress) {
			_progress->SetValue(*progress * 100);
		} else {
			_progress->Pulse();
		}
		string const sub = job->sub_name();
		size_t colon = sub.find(":");
		if (colon != string::npos) {
			_job_name->SetLabel(std_to_wx(sub.substr(0, colon)));
		} else {
			_job_name->SetLabel(std_to_wx(sub));
		}
	}

	void clear()
	{
		_job_name->SetLabel(wxT(""));
		_progress->SetValue(0);
	}

private:
	wxStaticText* _job_name;
	wxGauge* _progress;
};



class DOMFrame : public wxFrame
{
public:
	explicit DOMFrame(wxString const& title)
		: wxFrame(nullptr, -1, title)
		/* Use a panel as the only child of the Frame so that we avoid
		   the dark-grey background on Windows.
		*/
		, _overall_panel(new wxPanel(this, wxID_ANY))
	{
		auto bar = new wxMenuBar;
		setup_menu(bar);
		SetMenuBar(bar);

		Bind(wxEVT_MENU, boost::bind(&DOMFrame::file_exit, this), wxID_EXIT);
		Bind(wxEVT_MENU, boost::bind(&DOMFrame::help_about, this), wxID_ABOUT);

#ifdef DCPOMATIC_WINDOWS
		SetIcon(wxIcon(std_to_wx("id")));
#endif
		auto overall_sizer = new wxBoxSizer(wxVERTICAL);

		auto input_dcp_sizer = new wxBoxSizer(wxHORIZONTAL);
		add_label_to_sizer(input_dcp_sizer, _overall_panel, _("Input DCPs"), true, 0, wxALIGN_CENTER_VERTICAL);

		auto input_dcps = new EditableList<boost::filesystem::path, DirDialogWrapper>(
			_overall_panel,
			{ EditableListColumn(_("DCP"), 300, true) },
			boost::bind(&DOMFrame::dcp_paths, this),
			boost::bind(&DOMFrame::set_dcp_paths, this, _1),
			[](boost::filesystem::path p, int) { return p.filename().string(); },
			EditableListTitle::INVISIBLE,
			EditableListButton::NEW | EditableListButton::REMOVE
			);

		input_dcp_sizer->Add(input_dcps, 1, wxLEFT | wxEXPAND, DCPOMATIC_SIZER_GAP);
		overall_sizer->Add(input_dcp_sizer, 0, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);

		auto options_sizer = new wxBoxSizer(wxVERTICAL);
		_fix_audio_levels = new CheckBox(_overall_panel, _("Fix audio levels"));
		_fix_audio_levels->set(true);
		_fix_audio_levels->SetToolTip(
			_("Tick to change the audio levels to match the given LEQ(m) value")
		);
		options_sizer->Add(_fix_audio_levels, 0, wxBOTTOM, DCPOMATIC_SIZER_GAP);
		overall_sizer->Add(options_sizer, 0, wxLEFT, DCPOMATIC_DIALOG_BORDER);

		_progress_panel = new ProgressPanel(_overall_panel);
		overall_sizer->Add(_progress_panel, 0, wxEXPAND | wxALL, DCPOMATIC_DIALOG_BORDER);

		auto actions_sizer = new wxBoxSizer(wxHORIZONTAL);
		_cancel = new Button(_overall_panel, _("Cancel"));
		actions_sizer->Add(_cancel, 0, wxRIGHT, DCPOMATIC_SIZER_GAP);
		_process = new Button(_overall_panel, _("Process"));
		actions_sizer->Add(_process, 0, wxRIGHT, DCPOMATIC_SIZER_GAP);
		overall_sizer->Add(actions_sizer, 1, wxLEFT | wxRIGHT | wxALIGN_CENTER, DCPOMATIC_DIALOG_BORDER);

		_overall_panel->SetSizerAndFit(overall_sizer);

		_cancel->bind(&DOMFrame::cancel_clicked, this);
		_process->bind(&DOMFrame::process_clicked, this);

		setup_sensitivity();

		dcpomatic_log = make_shared<FileLog>("/home/carl/shite.log");
		dcpomatic_log->set_types(dcpomatic_log->types() | LogEntry::TYPE_DEBUG_ENCODE);
	}

private:
	void file_exit()
	{
		Close();
	}

	void help_about()
	{
		AboutDialog dialog(_overall_panel);
		dialog.ShowModal();
	}

	void setup_menu(wxMenuBar* m)
	{
		auto help = new wxMenu;
#ifdef DCPOMATIC_OSX
		/* This just needs to be appended somewhere, it seems - it magically
		 * gets moved to the right place.
		 */
		help->Append(wxID_EXIT, _("&Exit"));
		help->Append(wxID_ABOUT, variant::wx::insert_dcpomatic(_("About %s")));
#else
		auto file = new wxMenu;
		file->Append(wxID_EXIT, _("&Quit"));
		m->Append(file, _("&File"));

		help->Append(wxID_ABOUT, _("About"));
#endif
		m->Append(help, _("&Help"));
	}

	void setup_sensitivity()
	{
		auto const work = JobManager::instance()->work_to_do();
		_cancel->Enable(work);
		_process->Enable(!_dcp_paths.empty() && !work);
	}

	void cancel_clicked()
	{
		_cancel_pending = true;
	}

	void process_clicked()
	{
		auto job_manager = JobManager::instance();
		vector<shared_ptr<const FixAudioLevelsJob>> jobs;
		for (auto const& dcp: _dcp_paths) {
			auto job = make_shared<FixAudioLevelsJob>(dcp, boost::filesystem::path("/home/carl/tmp/wankz"), 60, true);
			job_manager->add(job);
			jobs.push_back(job);
		}

		setup_sensitivity();

		while (job_manager->work_to_do() && !_cancel_pending) {
			wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI | wxEVT_CATEGORY_USER_INPUT);
			dcpomatic_sleep_milliseconds(250);
			while (signal_manager->ui_idle()) {}
			auto last = job_manager->last_active_job();
			if (auto locked = last.lock()) {
				if (auto job = dynamic_pointer_cast<FixAudioLevelsJob>(locked)) {
					_progress_panel->update(job);
				}
			}
		}

		if (_cancel_pending) {
			_cancel_pending = false;
			JobManager::instance()->cancel_all_jobs();
			_progress_panel->clear();
			setup_sensitivity();
			return;
		}

		_progress_panel->clear();
		setup_sensitivity();

#if 0
		dcp::VerificationOptions options;
		options.check_picture_details = _check_picture_details->get();
		auto job_manager = JobManager::instance();
		vector<shared_ptr<const VerifyDCPJob>> jobs;
		for (auto const& dcp: _dcp_paths) {
			auto job = make_shared<VerifyDCPJob>(
				std::vector<boost::filesystem::path>{dcp},
				std::vector<boost::filesystem::path>(),
				options
			);
			job_manager->add(job);
			jobs.push_back(job);
		}

		setup_sensitivity();

		while (job_manager->work_to_do() && !_cancel_pending) {
			wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI | wxEVT_CATEGORY_USER_INPUT);
			dcpomatic_sleep_milliseconds(250);
			auto last = job_manager->last_active_job();
			if (auto locked = last.lock()) {
				if (auto dcp = dynamic_pointer_cast<VerifyDCPJob>(locked)) {
					_progress_panel->update(dcp);
				}
			}
		}

		if (_cancel_pending) {
			_cancel_pending = false;
			JobManager::instance()->cancel_all_jobs();
			_progress_panel->clear();
			setup_sensitivity();
			return;
		}

		DCPOMATIC_ASSERT(_dcp_paths.size() == jobs.size());
		_result_panel->add(jobs);
		if (_write_log->get()) {
			for (size_t i = 0; i < _dcp_paths.size(); ++i) {
				dcp::TextFormatter formatter(_dcp_paths[i] / "REPORT.txt");
				dcp::verify_report({ jobs[i]->result() }, formatter);
			}
		}

		_progress_panel->clear();
		setup_sensitivity();
#endif
	}

private:
	void set_dcp_paths (vector<boost::filesystem::path> dcps)
	{
		_dcp_paths = dcps;
		setup_sensitivity();
	}

	vector<boost::filesystem::path> dcp_paths() const
	{
		return _dcp_paths;
	}

	wxPanel* _overall_panel = nullptr;
	std::vector<boost::filesystem::path> _dcp_paths;
	CheckBox* _fix_audio_levels;
	Button* _cancel;
	Button* _process;
	ProgressPanel* _progress_panel;
	bool _cancel_pending = false;
};


/** @class App
 *  @brief The magic App class for wxWidgets.
 */
class App : public wxApp
{
public:
	App()
		: wxApp()
	{
		dcpomatic_setup_path_encoding();
#ifdef DCPOMATIC_LINUX
		XInitThreads();
#endif
	}

private:
	bool OnInit() override
	{
		try {
			SetAppName(variant::wx::dcpomatic_processor());

			if (!wxApp::OnInit()) {
				return false;
			}

#ifdef DCPOMATIC_LINUX
			unsetenv("UBUNTU_MENUPROXY");
#endif

#ifdef DCPOMATIC_OSX
			dcpomatic_sleep_seconds(1);
			make_foreground_application();
#endif

			/* Enable i18n; this will create a Config object
			   to look for a force-configured language.  This Config
			   object will be wrong, however, because dcpomatic_setup
			   hasn't yet been called and there aren't any filters etc.
			   set up yet.
			*/
			dcpomatic::wx::setup_i18n();

			/* Set things up, including filters etc.
			   which will now be internationalised correctly.
			*/
			dcpomatic_setup();

			/* Force the configuration to be re-loaded correctly next
			   time it is needed.
			*/
			Config::drop();

			signal_manager = new wxSignalManager(this);

			_frame = new DOMFrame(variant::wx::dcpomatic_processor());
			SetTopWindow(_frame);
			_frame->Maximize();
			_frame->Show();
		}
		catch (exception& e)
		{
			error_dialog(nullptr, variant::wx::insert_dcpomatic_processor(char_to_wx("%s could not start.")), std_to_wx(e.what()));
		}

		return true;
	}

	void report_exception()
	{
		try {
			throw;
		} catch (FileError& e) {
			error_dialog(
				nullptr,
				wxString::Format(
					_("An exception occurred: %s (%s)\n\n%s"),
					std_to_wx(e.what()),
					std_to_wx(e.file().string().c_str()),
					dcpomatic::wx::report_problem()
					)
				);
		} catch (boost::filesystem::filesystem_error& e) {
			error_dialog(
				nullptr,
				wxString::Format(
					_("An exception occurred: %s (%s) (%s)\n\n%s"),
					std_to_wx(e.what()),
					std_to_wx(e.path1().string()),
					std_to_wx(e.path2().string()),
					dcpomatic::wx::report_problem()
					)
				);
		} catch (exception& e) {
			error_dialog(
				nullptr,
				wxString::Format(
					_("An exception occurred: %s.\n\n%s"),
					std_to_wx(e.what()),
					dcpomatic::wx::report_problem()
					)
				);
		} catch (...) {
			error_dialog(nullptr, wxString::Format(_("An unknown exception occurred. %s"), dcpomatic::wx::report_problem()));
		}
	}

	/* An unhandled exception has occurred inside the main event loop */
	bool OnExceptionInMainLoop() override
	{
		report_exception();
		return false;
	}

	void OnUnhandledException() override
	{
		report_exception();
	}

	DOMFrame* _frame = nullptr;
};


IMPLEMENT_APP(App)