Supporters update.
[dcpomatic.git] / src / wx / content_menu.cc
1 /*
2     Copyright (C) 2013-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic 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     DCP-o-matic 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 DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #include "auto_crop_dialog.h"
23 #include "content_advanced_dialog.h"
24 #include "content_menu.h"
25 #include "content_properties_dialog.h"
26 #include "dir_dialog.h"
27 #include "file_dialog.h"
28 #include "film_viewer.h"
29 #include "id.h"
30 #include "repeat_dialog.h"
31 #include "timeline_video_content_view.h"
32 #include "timeline_audio_content_view.h"
33 #include "wx_util.h"
34 #include "lib/audio_content.h"
35 #include "lib/config.h"
36 #include "lib/content_factory.h"
37 #include "lib/constants.h"
38 #include "lib/copy_dcp_details_to_film.h"
39 #include "lib/dcp_content.h"
40 #include "lib/dcp_examiner.h"
41 #include "lib/examine_content_job.h"
42 #include "lib/exceptions.h"
43 #include "lib/ffmpeg_content.h"
44 #include "lib/film.h"
45 #include "lib/film_util.h"
46 #include "lib/find_missing.h"
47 #include "lib/guess_crop.h"
48 #include "lib/image_content.h"
49 #include "lib/job_manager.h"
50 #include "lib/playlist.h"
51 #include "lib/video_content.h"
52 #include <dcp/cpl.h>
53 #include <dcp/decrypted_kdm.h>
54 #include <dcp/exceptions.h>
55 #include <dcp/search.h>
56 #include <dcp/warnings.h>
57 LIBDCP_DISABLE_WARNINGS
58 #include <wx/dirdlg.h>
59 #include <wx/wx.h>
60 LIBDCP_ENABLE_WARNINGS
61
62
63 using std::dynamic_pointer_cast;
64 using std::exception;
65 using std::list;
66 using std::make_shared;
67 using std::shared_ptr;
68 using std::vector;
69 using std::weak_ptr;
70 using boost::optional;
71 #if BOOST_VERSION >= 106100
72 using namespace boost::placeholders;
73 #endif
74 using namespace dcpomatic;
75
76
77 enum {
78         ID_repeat = DCPOMATIC_CONTENT_MENU,
79         ID_join,
80         ID_find_missing,
81         ID_properties,
82         ID_advanced,
83         ID_re_examine,
84         ID_auto_crop,
85         ID_kdm,
86         ID_ov,
87         ID_choose_cpl,
88         ID_set_dcp_settings,
89         ID_set_dcp_markers,
90         ID_remove
91 };
92
93
94 ContentMenu::ContentMenu(wxWindow* p, FilmViewer& viewer)
95         : _menu (new wxMenu)
96         , _parent (p)
97         , _pop_up_open (false)
98         , _viewer (viewer)
99 {
100         _repeat = _menu->Append (ID_repeat, _("Repeat..."));
101         _join = _menu->Append (ID_join, _("Join"));
102         _find_missing = _menu->Append (ID_find_missing, _("Find missing..."));
103         _re_examine = _menu->Append (ID_re_examine, _("Re-examine..."));
104         _auto_crop = _menu->Append (ID_auto_crop, _("Auto-crop..."));
105         _properties = _menu->Append (ID_properties, _("Properties..."));
106         _advanced = _menu->Append (ID_advanced, _("Advanced settings..."));
107         _menu->AppendSeparator ();
108         _kdm = _menu->Append (ID_kdm, _("Add KDM..."));
109         _ov = _menu->Append (ID_ov, _("Add OV..."));
110         _cpl_menu = new wxMenu ();
111         _choose_cpl = _menu->Append (ID_choose_cpl, _("Choose CPL..."), _cpl_menu);
112         _set_dcp_settings = _menu->Append (ID_set_dcp_settings, _("Set project DCP settings from this DCP"));
113         _set_dcp_markers = _menu->Append(ID_set_dcp_markers, _("Set project markers from this DCP"));
114         _menu->AppendSeparator ();
115         _remove = _menu->Append (ID_remove, _("Remove"));
116
117         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::repeat, this), ID_repeat);
118         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::join, this), ID_join);
119         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::find_missing, this), ID_find_missing);
120         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::properties, this), ID_properties);
121         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::advanced, this), ID_advanced);
122         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::re_examine, this), ID_re_examine);
123         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::auto_crop, this), ID_auto_crop);
124         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::kdm, this), ID_kdm);
125         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::ov, this), ID_ov);
126         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::set_dcp_settings, this), ID_set_dcp_settings);
127         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::set_dcp_markers, this), ID_set_dcp_markers);
128         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::remove, this), ID_remove);
129         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::cpl_selected, this, _1), 1, ID_repeat - 1);
130 }
131
132 void
133 ContentMenu::popup (weak_ptr<Film> film, ContentList c, TimelineContentViewList v, wxPoint p)
134 {
135         _film = film;
136         _content = c;
137         _views = v;
138
139         int const N = _cpl_menu->GetMenuItemCount();
140         for (int i = DCPOMATIC_CPL_MENU; i < DCPOMATIC_CPL_MENU + N; ++i) {
141                 _cpl_menu->Delete (i);
142         }
143
144         _repeat->Enable (!_content.empty ());
145
146         int n = 0;
147         for (auto i: _content) {
148                 if (dynamic_pointer_cast<FFmpegContent> (i)) {
149                         ++n;
150                 }
151         }
152
153         _join->Enable (n > 1);
154
155         _find_missing->Enable (_content.size() == 1 && !_content.front()->paths_valid ());
156         _properties->Enable (_content.size() == 1);
157         _advanced->Enable (_content.size() == 1);
158         _re_examine->Enable (!_content.empty ());
159         _auto_crop->Enable (_content.size() == 1);
160
161         if (_content.size() == 1) {
162                 auto dcp = dynamic_pointer_cast<DCPContent> (_content.front());
163                 if (dcp) {
164                         _kdm->Enable (dcp->encrypted ());
165                         _ov->Enable (dcp->needs_assets ());
166                         _set_dcp_settings->Enable (static_cast<bool>(dcp));
167                         _set_dcp_markers->Enable(static_cast<bool>(dcp));
168                         try {
169                                 auto cpls = dcp::find_and_resolve_cpls (dcp->directories(), true);
170                                 _choose_cpl->Enable (cpls.size() > 1);
171                                 int id = DCPOMATIC_CPL_MENU;
172                                 for (auto i: cpls) {
173                                         auto item = _cpl_menu->AppendRadioItem (
174                                                 id++,
175                                                 wxString::Format (
176                                                         "%s (%s)",
177                                                         std_to_wx(i->annotation_text().get_value_or("")).data(),
178                                                         std_to_wx(i->id()).data()
179                                                         )
180                                                 );
181                                         item->Check (dcp->cpl() && dcp->cpl() == i->id());
182                                 }
183                         } catch (dcp::ReadError &) {
184                                 /* The DCP is probably missing */
185                         } catch (dcp::KDMDecryptionError &) {
186                                 /* We have an incorrect KDM */
187                         } catch (KDMError &) {
188                                 /* We have an incorrect KDM */
189                         }
190                 } else {
191                         _kdm->Enable (false);
192                         _ov->Enable (false);
193                         _choose_cpl->Enable (false);
194                         _set_dcp_settings->Enable (false);
195                         _set_dcp_markers->Enable(false);
196                 }
197         } else {
198                 _kdm->Enable (false);
199                 _set_dcp_settings->Enable (false);
200                 _set_dcp_markers->Enable(false);
201         }
202
203         _remove->Enable (!_content.empty ());
204
205         _pop_up_open = true;
206         _parent->PopupMenu (_menu, p);
207         _pop_up_open = false;
208 }
209
210
211 void
212 ContentMenu::set_dcp_settings ()
213 {
214         auto film = _film.lock ();
215         if (!film) {
216                 return;
217         }
218
219         DCPOMATIC_ASSERT (_content.size() == 1);
220         auto dcp = dynamic_pointer_cast<DCPContent>(_content.front());
221         DCPOMATIC_ASSERT (dcp);
222         copy_dcp_settings_to_film(dcp, film);
223 }
224
225
226 void
227 ContentMenu::set_dcp_markers()
228 {
229         auto film = _film.lock();
230         if (!film) {
231                 return;
232         }
233
234         DCPOMATIC_ASSERT(_content.size() == 1);
235         auto dcp = dynamic_pointer_cast<DCPContent>(_content.front());
236         DCPOMATIC_ASSERT(dcp);
237         copy_dcp_markers_to_film(dcp, film);
238 }
239
240
241 void
242 ContentMenu::repeat ()
243 {
244         if (_content.empty ()) {
245                 return;
246         }
247
248         auto d = make_wx<RepeatDialog>(_parent);
249         if (d->ShowModal() != wxID_OK) {
250                 return;
251         }
252
253         auto film = _film.lock ();
254         if (!film) {
255                 return;
256         }
257
258         film->repeat_content (_content, d->number ());
259
260         _content.clear ();
261         _views.clear ();
262 }
263
264
265 void
266 ContentMenu::join ()
267 {
268         vector<shared_ptr<Content>> fc;
269         for (auto i: _content) {
270                 auto f = dynamic_pointer_cast<FFmpegContent> (i);
271                 if (f) {
272                         fc.push_back (f);
273                 }
274         }
275
276         DCPOMATIC_ASSERT (fc.size() > 1);
277
278         auto film = _film.lock ();
279         if (!film) {
280                 return;
281         }
282
283         try {
284                 auto joined = make_shared<FFmpegContent>(fc);
285                 film->remove_content (_content);
286                 film->examine_and_add_content (joined);
287         } catch (JoinError& e) {
288                 error_dialog (_parent, std_to_wx (e.what ()));
289         }
290 }
291
292
293 void
294 ContentMenu::remove ()
295 {
296         if (_content.empty ()) {
297                 return;
298         }
299
300         auto film = _film.lock ();
301         if (!film) {
302                 return;
303         }
304
305         /* We are removing from the timeline if _views is not empty */
306         bool handled = false;
307         if (!_views.empty ()) {
308                 /* Special case: we only remove FFmpegContent if its video view is selected;
309                    if not, and its audio view is selected, we unmap the audio.
310                 */
311                 for (auto i: _content) {
312                         auto fc = dynamic_pointer_cast<FFmpegContent> (i);
313                         if (!fc) {
314                                 continue;
315                         }
316
317                         shared_ptr<TimelineVideoContentView> video;
318                         shared_ptr<TimelineAudioContentView> audio;
319
320                         for (auto j: _views) {
321                                 auto v = dynamic_pointer_cast<TimelineVideoContentView>(j);
322                                 auto a = dynamic_pointer_cast<TimelineAudioContentView>(j);
323                                 if (v && v->content() == fc) {
324                                         video = v;
325                                 } else if (a && a->content() == fc) {
326                                         audio = a;
327                                 }
328                         }
329
330                         if (!video && audio) {
331                                 auto m = fc->audio->mapping ();
332                                 m.unmap_all ();
333                                 fc->audio->set_mapping (m);
334                                 handled = true;
335                         }
336                 }
337         }
338
339         if (!handled) {
340                 film->remove_content (_content);
341         }
342
343         _content.clear ();
344         _views.clear ();
345 }
346
347
348 void
349 ContentMenu::find_missing ()
350 {
351         if (_content.size() != 1) {
352                 return;
353         }
354
355         auto film = _film.lock ();
356         if (!film) {
357                 return;
358         }
359
360         /* XXX: a bit nasty */
361         auto ic = dynamic_pointer_cast<ImageContent> (_content.front());
362         auto dc = dynamic_pointer_cast<DCPContent> (_content.front());
363
364         int r = wxID_CANCEL;
365         boost::filesystem::path path;
366
367         if ((ic && !ic->still ()) || dc) {
368                 wxDirDialog dialog(nullptr, _("Choose a folder"), wxT(""), wxDD_DIR_MUST_EXIST);
369                 r = dialog.ShowModal();
370                 path = wx_to_std(dialog.GetPath());
371         } else {
372                 wxFileDialog dialog(nullptr, _("Choose a file"), wxT(""), wxT(""), wxT("*.*"));
373                 r = dialog.ShowModal();
374                 path = wx_to_std(dialog.GetPath());
375         }
376
377         if (r == wxID_CANCEL) {
378                 return;
379         }
380
381         dcpomatic::find_missing (film->content(), path);
382 }
383
384 void
385 ContentMenu::re_examine ()
386 {
387         auto film = _film.lock ();
388         if (!film) {
389                 return;
390         }
391
392         for (auto i: _content) {
393                 JobManager::instance()->add(make_shared<ExamineContentJob>(film, i));
394         }
395 }
396
397
398 void
399 ContentMenu::kdm ()
400 {
401         DCPOMATIC_ASSERT (!_content.empty ());
402         auto dcp = dynamic_pointer_cast<DCPContent> (_content.front());
403         DCPOMATIC_ASSERT (dcp);
404
405         FileDialog dialog(_parent, _("Select KDM"), wxT("XML files|*.xml|All files|*.*"), 0, "AddKDMPath");
406
407         if (!dialog.show()) {
408                 return;
409         }
410
411         optional<dcp::EncryptedKDM> kdm;
412         try {
413                 kdm = dcp::EncryptedKDM(dcp::file_to_string(dialog.path(), MAX_KDM_SIZE));
414         } catch (exception& e) {
415                 error_dialog (_parent, _("Could not load KDM"), std_to_wx(e.what()));
416                 return;
417         }
418
419         /* Try to decrypt it to get an early preview of any errors */
420         try {
421                 decrypt_kdm_with_helpful_error (*kdm);
422         } catch (KDMError& e) {
423                 error_dialog (_parent, std_to_wx(e.summary()), std_to_wx(e.detail()));
424                 return;
425         } catch (exception& e) {
426                 error_dialog (_parent, e.what());
427                 return;
428         }
429
430         auto cpls = dcp::find_and_resolve_cpls (dcp->directories(), true);
431         bool const kdm_matches_any_cpl = std::any_of(cpls.begin(), cpls.end(), [kdm](shared_ptr<const dcp::CPL> cpl) { return cpl->id() == kdm->cpl_id(); });
432         bool const kdm_matches_selected_cpl = dcp->cpl() || kdm->cpl_id() == dcp->cpl().get();
433
434         if (!kdm_matches_any_cpl) {
435                 error_dialog (_parent, _("This KDM was not made for this DCP.  You will need a different one."));
436                 return;
437         }
438
439         if (!kdm_matches_selected_cpl && kdm_matches_any_cpl) {
440                 message_dialog (_parent, _("This KDM was made for one of the CPLs in this DCP, but not the currently selected one.  To play the currently-selected CPL you will need a different KDM."));
441         }
442
443         dcp->add_kdm (*kdm);
444
445         auto film = _film.lock ();
446         DCPOMATIC_ASSERT (film);
447         JobManager::instance()->add (make_shared<ExamineContentJob>(film, dcp));
448 }
449
450 void
451 ContentMenu::ov ()
452 {
453         DCPOMATIC_ASSERT (!_content.empty ());
454         auto dcp = dynamic_pointer_cast<DCPContent> (_content.front());
455         DCPOMATIC_ASSERT (dcp);
456
457         auto film = _film.lock();
458         DCPOMATIC_ASSERT(film);
459         DirDialog dialog(_parent, _("Select OV"), wxDD_DIR_MUST_EXIST, "AddFilesPath", add_files_override_path(film));
460
461         if (dialog.show()) {
462                 dcp->add_ov(dialog.path());
463                 auto film = _film.lock();
464                 DCPOMATIC_ASSERT (film);
465                 JobManager::instance()->add (make_shared<ExamineContentJob>(film, dcp));
466         }
467 }
468
469 void
470 ContentMenu::properties ()
471 {
472         auto film = _film.lock ();
473         DCPOMATIC_ASSERT (film);
474         auto d = make_wx<ContentPropertiesDialog>(_parent, film, _content.front());
475         d->ShowModal ();
476 }
477
478
479 void
480 ContentMenu::advanced ()
481 {
482         DCPOMATIC_ASSERT(!_content.empty());
483
484         auto content = _content.front();
485         auto dialog = make_wx<ContentAdvancedDialog>(_parent, content);
486
487         if (dialog->ShowModal() == wxID_CANCEL) {
488                 return;
489         }
490
491         if (content->video) {
492                 content->video->set_use(!dialog->ignore_video());
493                 content->video->set_burnt_subtitle_language(dialog->burnt_subtitle_language());
494         }
495
496         auto ffmpeg = dynamic_pointer_cast<FFmpegContent>(content);
497         if (ffmpeg) {
498                 ffmpeg->set_filters(dialog->filters());
499         }
500
501         if (dialog->video_frame_rate()) {
502                 auto film = _film.lock();
503                 DCPOMATIC_ASSERT(film);
504                 content->set_video_frame_rate(film, *dialog->video_frame_rate());
505         } else {
506                 content->unset_video_frame_rate();
507         }
508 }
509
510
511 void
512 ContentMenu::cpl_selected (wxCommandEvent& ev)
513 {
514         if (!_pop_up_open) {
515                 return;
516         }
517
518         DCPOMATIC_ASSERT (!_content.empty ());
519         auto dcp = dynamic_pointer_cast<DCPContent> (_content.front());
520         DCPOMATIC_ASSERT (dcp);
521
522         auto cpls = dcp::find_and_resolve_cpls (dcp->directories(), true);
523
524         DCPOMATIC_ASSERT(ev.GetId() >= DCPOMATIC_CPL_MENU);
525         DCPOMATIC_ASSERT(ev.GetId() < int(DCPOMATIC_CPL_MENU + cpls.size()));
526         dcp->set_cpl(cpls[ev.GetId() - DCPOMATIC_CPL_MENU]->id());
527
528         auto film = _film.lock ();
529         DCPOMATIC_ASSERT (film);
530         JobManager::instance()->add (make_shared<ExamineContentJob>(film, dcp));
531 }
532
533
534 void
535 ContentMenu::auto_crop ()
536 {
537         DCPOMATIC_ASSERT (_content.size() == 1);
538
539         auto film = _film.lock ();
540         DCPOMATIC_ASSERT (film);
541
542         auto update_viewer = [this](Crop crop) {
543                 auto film = _film.lock();
544                 DCPOMATIC_ASSERT (film);
545                 auto const content = _content.front();
546                 auto const current_crop = content->video->actual_crop();
547                 auto const video_size_guess = content->video->size().get_value_or(dcp::Size(1998, 1080));
548                 _viewer.set_crop_guess(
549                         dcpomatic::Rect<float>(
550                                 static_cast<float>(std::max(0, crop.left - current_crop.left)) / video_size_guess.width,
551                                 static_cast<float>(std::max(0, crop.top - current_crop.top)) / video_size_guess.height,
552                                 1.0f - (static_cast<float>(std::max(0, crop.left - current_crop.left + crop.right - current_crop.right)) / video_size_guess.width),
553                                 1.0f - (static_cast<float>(std::max(0, crop.top - current_crop.top + crop.bottom - current_crop.bottom)) / video_size_guess.height)
554                                 ));
555         };
556
557         auto guess_crop_for_content = [this, film]() {
558                 auto position = _viewer.position_in_content(_content.front()).get_value_or(
559                         ContentTime::from_frames(_content.front()->video->length(), _content.front()->video_frame_rate().get_value_or(24))
560                         );
561                 return guess_crop(film, _content.front(), Config::instance()->auto_crop_threshold(), position);
562         };
563
564         /* Make an initial guess in the view and open the dialog */
565
566         auto const crop = guess_crop_for_content ();
567         update_viewer (crop);
568
569         _auto_crop_dialog.reset(_parent, crop);
570         _auto_crop_dialog->Show ();
571
572         /* Update the dialog and view when the crop threshold changes */
573         _auto_crop_config_connection = Config::instance()->Changed.connect([this, guess_crop_for_content, update_viewer](Config::Property property) {
574                 auto film = _film.lock();
575                 DCPOMATIC_ASSERT (film);
576                 if (property == Config::AUTO_CROP_THRESHOLD) {
577                         auto const crop = guess_crop_for_content();
578                         _auto_crop_dialog->set(crop);
579                         update_viewer(crop);
580                 }
581         });
582
583         /* Also update the dialog and view when we're looking at a different frame */
584         _auto_crop_viewer_connection = _viewer.ImageChanged.connect([this, guess_crop_for_content, update_viewer](shared_ptr<PlayerVideo>) {
585                 auto const crop = guess_crop_for_content();
586                 _auto_crop_dialog->set(crop);
587                 update_viewer(crop);
588         });
589
590         /* Handle the user closing the dialog (with OK or cancel) */
591         _auto_crop_dialog->Bind(wxEVT_BUTTON, [this](wxCommandEvent& ev) {
592                 _auto_crop_config_connection.disconnect ();
593                 _auto_crop_viewer_connection.disconnect ();
594                 if (ev.GetId() == wxID_OK) {
595                         _content.front()->video->set_crop(_auto_crop_dialog->get());
596                 }
597                 _auto_crop_dialog->Show (false);
598                 _viewer.unset_crop_guess ();
599         });
600
601         /* Update the view when something in the dialog is changed */
602         _auto_crop_dialog->Changed.connect([update_viewer](Crop crop) {
603                 update_viewer (crop);
604         });
605 }