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