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