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