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