Cleanup: remove some unnecessary includes.
[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/constants.h"
35 #include "lib/copy_dcp_details_to_film.h"
36 #include "lib/dcp_content.h"
37 #include "lib/dcp_examiner.h"
38 #include "lib/examine_content_job.h"
39 #include "lib/exceptions.h"
40 #include "lib/ffmpeg_content.h"
41 #include "lib/film.h"
42 #include "lib/find_missing.h"
43 #include "lib/guess_crop.h"
44 #include "lib/image_content.h"
45 #include "lib/job_manager.h"
46 #include "lib/playlist.h"
47 #include "lib/video_content.h"
48 #include <dcp/cpl.h>
49 #include <dcp/decrypted_kdm.h>
50 #include <dcp/exceptions.h>
51 #include <dcp/search.h>
52 #include <dcp/warnings.h>
53 LIBDCP_DISABLE_WARNINGS
54 #include <wx/dirdlg.h>
55 #include <wx/wx.h>
56 LIBDCP_ENABLE_WARNINGS
57
58
59 using std::dynamic_pointer_cast;
60 using std::exception;
61 using std::list;
62 using std::make_shared;
63 using std::shared_ptr;
64 using std::vector;
65 using std::weak_ptr;
66 using boost::optional;
67 #if BOOST_VERSION >= 106100
68 using namespace boost::placeholders;
69 #endif
70 using namespace dcpomatic;
71
72
73 enum {
74         /* Start at 256 so we can have IDs on _cpl_menu from 1 to 255 */
75         ID_repeat = 256,
76         ID_join,
77         ID_find_missing,
78         ID_properties,
79         ID_advanced,
80         ID_re_examine,
81         ID_auto_crop,
82         ID_kdm,
83         ID_ov,
84         ID_choose_cpl,
85         ID_set_dcp_settings,
86         ID_remove
87 };
88
89
90 ContentMenu::ContentMenu(wxWindow* p, FilmViewer& viewer)
91         : _menu (new wxMenu)
92         , _parent (p)
93         , _pop_up_open (false)
94         , _viewer (viewer)
95 {
96         _repeat = _menu->Append (ID_repeat, _("Repeat..."));
97         _join = _menu->Append (ID_join, _("Join"));
98         _find_missing = _menu->Append (ID_find_missing, _("Find missing..."));
99         _re_examine = _menu->Append (ID_re_examine, _("Re-examine..."));
100         _auto_crop = _menu->Append (ID_auto_crop, _("Auto-crop..."));
101         _properties = _menu->Append (ID_properties, _("Properties..."));
102         _advanced = _menu->Append (ID_advanced, _("Advanced settings..."));
103         _menu->AppendSeparator ();
104         _kdm = _menu->Append (ID_kdm, _("Add KDM..."));
105         _ov = _menu->Append (ID_ov, _("Add OV..."));
106         _cpl_menu = new wxMenu ();
107         _choose_cpl = _menu->Append (ID_choose_cpl, _("Choose CPL..."), _cpl_menu);
108         _set_dcp_settings = _menu->Append (ID_set_dcp_settings, _("Set project DCP settings from this DCP"));
109         _menu->AppendSeparator ();
110         _remove = _menu->Append (ID_remove, _("Remove"));
111
112         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::repeat, this), ID_repeat);
113         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::join, this), ID_join);
114         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::find_missing, this), ID_find_missing);
115         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::properties, this), ID_properties);
116         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::advanced, this), ID_advanced);
117         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::re_examine, this), ID_re_examine);
118         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::auto_crop, this), ID_auto_crop);
119         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::kdm, this), ID_kdm);
120         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::ov, this), ID_ov);
121         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::set_dcp_settings, this), ID_set_dcp_settings);
122         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::remove, this), ID_remove);
123         _parent->Bind (wxEVT_MENU, boost::bind (&ContentMenu::cpl_selected, this, _1), 1, ID_repeat - 1);
124 }
125
126 void
127 ContentMenu::popup (weak_ptr<Film> film, ContentList c, TimelineContentViewList v, wxPoint p)
128 {
129         _film = film;
130         _content = c;
131         _views = v;
132
133         int const N = _cpl_menu->GetMenuItemCount();
134         for (int i = 1; i <= N; ++i) {
135                 _cpl_menu->Delete (i);
136         }
137
138         _repeat->Enable (!_content.empty ());
139
140         int n = 0;
141         for (auto i: _content) {
142                 if (dynamic_pointer_cast<FFmpegContent> (i)) {
143                         ++n;
144                 }
145         }
146
147         _join->Enable (n > 1);
148
149         _find_missing->Enable (_content.size() == 1 && !_content.front()->paths_valid ());
150         _properties->Enable (_content.size() == 1);
151         _advanced->Enable (_content.size() == 1);
152         _re_examine->Enable (!_content.empty ());
153         _auto_crop->Enable (_content.size() == 1);
154
155         if (_content.size() == 1) {
156                 auto dcp = dynamic_pointer_cast<DCPContent> (_content.front());
157                 if (dcp) {
158                         _kdm->Enable (dcp->encrypted ());
159                         _ov->Enable (dcp->needs_assets ());
160                         _set_dcp_settings->Enable (static_cast<bool>(dcp));
161                         try {
162                                 auto cpls = dcp::find_and_resolve_cpls (dcp->directories(), true);
163                                 _choose_cpl->Enable (cpls.size() > 1);
164                                 /* We can't have 0 as a menu item ID on OS X */
165                                 int id = 1;
166                                 for (auto i: cpls) {
167                                         auto item = _cpl_menu->AppendRadioItem (
168                                                 id++,
169                                                 wxString::Format (
170                                                         "%s (%s)",
171                                                         std_to_wx(i->annotation_text().get_value_or("")).data(),
172                                                         std_to_wx(i->id()).data()
173                                                         )
174                                                 );
175                                         item->Check (dcp->cpl() && dcp->cpl() == i->id());
176                                 }
177                         } catch (dcp::ReadError &) {
178                                 /* The DCP is probably missing */
179                         } catch (dcp::KDMDecryptionError &) {
180                                 /* We have an incorrect KDM */
181                         } catch (KDMError &) {
182                                 /* We have an incorrect KDM */
183                         }
184                 } else {
185                         _kdm->Enable (false);
186                         _ov->Enable (false);
187                         _choose_cpl->Enable (false);
188                         _set_dcp_settings->Enable (false);
189                 }
190         } else {
191                 _kdm->Enable (false);
192                 _set_dcp_settings->Enable (false);
193         }
194
195         _remove->Enable (!_content.empty ());
196
197         _pop_up_open = true;
198         _parent->PopupMenu (_menu, p);
199         _pop_up_open = false;
200 }
201
202
203 void
204 ContentMenu::set_dcp_settings ()
205 {
206         auto film = _film.lock ();
207         if (!film) {
208                 return;
209         }
210
211         DCPOMATIC_ASSERT (_content.size() == 1);
212         auto dcp = dynamic_pointer_cast<DCPContent>(_content.front());
213         DCPOMATIC_ASSERT (dcp);
214         copy_dcp_details_to_film (dcp, film);
215 }
216
217
218 void
219 ContentMenu::repeat ()
220 {
221         if (_content.empty ()) {
222                 return;
223         }
224
225         auto d = make_wx<RepeatDialog>(_parent);
226         if (d->ShowModal() != wxID_OK) {
227                 return;
228         }
229
230         auto film = _film.lock ();
231         if (!film) {
232                 return;
233         }
234
235         film->repeat_content (_content, d->number ());
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 = make_wx<wxDirDialog>(nullptr, _("Choose a folder"), wxT (""), wxDD_DIR_MUST_EXIST);
346                 r = d->ShowModal ();
347                 path = wx_to_std (d->GetPath());
348         } else {
349                 auto d = make_wx<wxFileDialog>(nullptr, _("Choose a file"), wxT (""), wxT (""), wxT ("*.*"));
350                 r = d->ShowModal ();
351                 path = wx_to_std (d->GetPath());
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 = make_wx<wxFileDialog>(_parent, _("Select KDM"));
383
384         if (d->ShowModal() != wxID_OK) {
385                 return;
386         }
387
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                 return;
394         }
395
396         /* Try to decrypt it to get an early preview of any errors */
397         try {
398                 decrypt_kdm_with_helpful_error (*kdm);
399         } catch (KDMError& e) {
400                 error_dialog (_parent, std_to_wx(e.summary()), std_to_wx(e.detail()));
401                 return;
402         } catch (exception& e) {
403                 error_dialog (_parent, e.what());
404                 return;
405         }
406
407         auto cpls = dcp::find_and_resolve_cpls (dcp->directories(), true);
408         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(); });
409         bool const kdm_matches_selected_cpl = dcp->cpl() || kdm->cpl_id() == dcp->cpl().get();
410
411         if (!kdm_matches_any_cpl) {
412                 error_dialog (_parent, _("This KDM was not made for this DCP.  You will need a different one."));
413                 return;
414         }
415
416         if (!kdm_matches_selected_cpl && kdm_matches_any_cpl) {
417                 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."));
418         }
419
420         dcp->add_kdm (*kdm);
421
422         auto film = _film.lock ();
423         DCPOMATIC_ASSERT (film);
424         JobManager::instance()->add (make_shared<ExamineContentJob>(film, dcp));
425 }
426
427 void
428 ContentMenu::ov ()
429 {
430         DCPOMATIC_ASSERT (!_content.empty ());
431         auto dcp = dynamic_pointer_cast<DCPContent> (_content.front());
432         DCPOMATIC_ASSERT (dcp);
433
434         auto d = make_wx<wxDirDialog>(_parent, _("Select OV"));
435
436         if (d->ShowModal() == wxID_OK) {
437                 dcp->add_ov (wx_to_std (d->GetPath()));
438                 shared_ptr<Film> film = _film.lock ();
439                 DCPOMATIC_ASSERT (film);
440                 JobManager::instance()->add (make_shared<ExamineContentJob>(film, dcp));
441         }
442 }
443
444 void
445 ContentMenu::properties ()
446 {
447         auto film = _film.lock ();
448         DCPOMATIC_ASSERT (film);
449         auto d = make_wx<ContentPropertiesDialog>(_parent, film, _content.front());
450         d->ShowModal ();
451 }
452
453
454 void
455 ContentMenu::advanced ()
456 {
457         DCPOMATIC_ASSERT(!_content.empty());
458
459         auto content = _content.front();
460         auto dialog = make_wx<ContentAdvancedDialog>(_parent, content);
461
462         if (dialog->ShowModal() == wxID_CANCEL) {
463                 return;
464         }
465
466         if (content->video) {
467                 content->video->set_use(!dialog->ignore_video());
468                 content->video->set_burnt_subtitle_language(dialog->burnt_subtitle_language());
469         }
470
471         auto ffmpeg = dynamic_pointer_cast<FFmpegContent>(content);
472         if (ffmpeg) {
473                 ffmpeg->set_filters(dialog->filters());
474         }
475
476         if (dialog->video_frame_rate()) {
477                 auto film = _film.lock();
478                 DCPOMATIC_ASSERT(film);
479                 content->set_video_frame_rate(film, *dialog->video_frame_rate());
480         } else {
481                 content->unset_video_frame_rate();
482         }
483 }
484
485
486 void
487 ContentMenu::cpl_selected (wxCommandEvent& ev)
488 {
489         if (!_pop_up_open) {
490                 return;
491         }
492
493         DCPOMATIC_ASSERT (!_content.empty ());
494         auto dcp = dynamic_pointer_cast<DCPContent> (_content.front());
495         DCPOMATIC_ASSERT (dcp);
496
497         auto cpls = dcp::find_and_resolve_cpls (dcp->directories(), true);
498         DCPOMATIC_ASSERT (ev.GetId() > 0);
499         DCPOMATIC_ASSERT (ev.GetId() <= int (cpls.size()));
500
501         auto i = cpls.begin ();
502         for (int j = 0; j < ev.GetId() - 1; ++j) {
503                 ++i;
504         }
505
506         dcp->set_cpl ((*i)->id ());
507         auto film = _film.lock ();
508         DCPOMATIC_ASSERT (film);
509         JobManager::instance()->add (make_shared<ExamineContentJob>(film, dcp));
510 }
511
512
513 void
514 ContentMenu::auto_crop ()
515 {
516         DCPOMATIC_ASSERT (_content.size() == 1);
517
518         auto film = _film.lock ();
519         DCPOMATIC_ASSERT (film);
520
521         auto update_viewer = [this](Crop crop) {
522                 auto film = _film.lock();
523                 DCPOMATIC_ASSERT (film);
524                 auto const content = _content.front();
525                 auto const current_crop = content->video->actual_crop();
526                 _viewer.set_crop_guess(
527                         dcpomatic::Rect<float>(
528                                 static_cast<float>(std::max(0, crop.left - current_crop.left)) / content->video->size().width,
529                                 static_cast<float>(std::max(0, crop.top - current_crop.top)) / content->video->size().height,
530                                 1.0f - (static_cast<float>(std::max(0, crop.left - current_crop.left + crop.right - current_crop.right)) / content->video->size().width),
531                                 1.0f - (static_cast<float>(std::max(0, crop.top - current_crop.top + crop.bottom - current_crop.bottom)) / content->video->size().height)
532                                 ));
533         };
534
535         auto guess_crop_for_content = [this, film]() {
536                 auto position = _viewer.position_in_content(_content.front()).get_value_or(
537                         ContentTime::from_frames(_content.front()->video->length(), _content.front()->video_frame_rate().get_value_or(24))
538                         );
539                 return guess_crop(film, _content.front(), Config::instance()->auto_crop_threshold(), position);
540         };
541
542         /* Make an initial guess in the view and open the dialog */
543
544         auto const crop = guess_crop_for_content ();
545         update_viewer (crop);
546
547         _auto_crop_dialog.reset(_parent, crop);
548         _auto_crop_dialog->Show ();
549
550         /* Update the dialog and view when the crop threshold changes */
551         _auto_crop_config_connection = Config::instance()->Changed.connect([this, guess_crop_for_content, update_viewer](Config::Property property) {
552                 auto film = _film.lock();
553                 DCPOMATIC_ASSERT (film);
554                 if (property == Config::AUTO_CROP_THRESHOLD) {
555                         auto const crop = guess_crop_for_content();
556                         _auto_crop_dialog->set(crop);
557                         update_viewer(crop);
558                 }
559         });
560
561         /* Also update the dialog and view when we're looking at a different frame */
562         _auto_crop_viewer_connection = _viewer.ImageChanged.connect([this, guess_crop_for_content, update_viewer](shared_ptr<PlayerVideo>) {
563                 auto const crop = guess_crop_for_content();
564                 _auto_crop_dialog->set(crop);
565                 update_viewer(crop);
566         });
567
568         /* Handle the user closing the dialog (with OK or cancel) */
569         _auto_crop_dialog->Bind(wxEVT_BUTTON, [this](wxCommandEvent& ev) {
570                 _auto_crop_config_connection.disconnect ();
571                 _auto_crop_viewer_connection.disconnect ();
572                 if (ev.GetId() == wxID_OK) {
573                         _content.front()->video->set_crop(_auto_crop_dialog->get());
574                 }
575                 _auto_crop_dialog->Show (false);
576                 _viewer.unset_crop_guess ();
577         });
578
579         /* Update the view when something in the dialog is changed */
580         _auto_crop_dialog->Changed.connect([update_viewer](Crop crop) {
581                 update_viewer (crop);
582         });
583 }