Fix slider position updates post seek (#1085).
[dcpomatic.git] / src / wx / film_viewer.cc
1 /*
2     Copyright (C) 2012-2016 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 /** @file  src/film_viewer.cc
22  *  @brief A wx widget to view a preview of a Film.
23  */
24
25 #include "film_viewer.h"
26 #include "playhead_to_timecode_dialog.h"
27 #include "playhead_to_frame_dialog.h"
28 #include "wx_util.h"
29 #include "lib/film.h"
30 #include "lib/ratio.h"
31 #include "lib/util.h"
32 #include "lib/job_manager.h"
33 #include "lib/image.h"
34 #include "lib/exceptions.h"
35 #include "lib/examine_content_job.h"
36 #include "lib/filter.h"
37 #include "lib/player.h"
38 #include "lib/player_video.h"
39 #include "lib/video_content.h"
40 #include "lib/video_decoder.h"
41 #include "lib/timer.h"
42 #include "lib/butler.h"
43 #include "lib/log.h"
44 #include "lib/config.h"
45 extern "C" {
46 #include <libavutil/pixfmt.h>
47 }
48 #include <dcp/exceptions.h>
49 #include <wx/tglbtn.h>
50 #include <iostream>
51 #include <iomanip>
52
53 using std::string;
54 using std::pair;
55 using std::min;
56 using std::max;
57 using std::cout;
58 using std::list;
59 using std::bad_alloc;
60 using std::make_pair;
61 using std::exception;
62 using boost::shared_ptr;
63 using boost::dynamic_pointer_cast;
64 using boost::weak_ptr;
65 using boost::optional;
66 using dcp::Size;
67
68 static
69 int
70 rtaudio_callback (void* out, void *, unsigned int frames, double, RtAudioStreamStatus, void* data)
71 {
72         return reinterpret_cast<FilmViewer*>(data)->audio_callback (out, frames);
73 }
74
75 FilmViewer::FilmViewer (wxWindow* p)
76         : wxPanel (p)
77         , _panel (new wxPanel (this))
78         , _outline_content (new wxCheckBox (this, wxID_ANY, _("Outline content")))
79         , _left_eye (new wxRadioButton (this, wxID_ANY, _("Left eye"), wxDefaultPosition, wxDefaultSize, wxRB_GROUP))
80         , _right_eye (new wxRadioButton (this, wxID_ANY, _("Right eye")))
81         , _jump_to_selected (new wxCheckBox (this, wxID_ANY, _("Jump to selected content")))
82         , _slider (new wxSlider (this, wxID_ANY, 0, 0, 4096))
83         , _back_button (new wxButton (this, wxID_ANY, wxT("<")))
84         , _forward_button (new wxButton (this, wxID_ANY, wxT(">")))
85         , _frame_number (new wxStaticText (this, wxID_ANY, wxT("")))
86         , _timecode (new wxStaticText (this, wxID_ANY, wxT("")))
87         , _play_button (new wxToggleButton (this, wxID_ANY, _("Play")))
88         , _coalesce_player_changes (false)
89         , _pending_player_change (false)
90         , _last_seek_accurate (true)
91         , _audio (DCPOMATIC_RTAUDIO_API)
92         , _audio_channels (0)
93         , _audio_block_size (1024)
94         , _playing (false)
95         , _latency_history_count (0)
96 {
97 #ifndef __WXOSX__
98         _panel->SetDoubleBuffered (true);
99 #endif
100
101         _panel->SetBackgroundStyle (wxBG_STYLE_PAINT);
102
103         _v_sizer = new wxBoxSizer (wxVERTICAL);
104         SetSizer (_v_sizer);
105
106         _v_sizer->Add (_panel, 1, wxEXPAND);
107
108         wxBoxSizer* view_options = new wxBoxSizer (wxHORIZONTAL);
109         view_options->Add (_outline_content, 0, wxRIGHT, DCPOMATIC_SIZER_GAP);
110         view_options->Add (_left_eye, 0, wxLEFT | wxRIGHT, DCPOMATIC_SIZER_GAP);
111         view_options->Add (_right_eye, 0, wxLEFT | wxRIGHT, DCPOMATIC_SIZER_GAP);
112         view_options->Add (_jump_to_selected, 0, wxLEFT | wxRIGHT, DCPOMATIC_SIZER_GAP);
113         _v_sizer->Add (view_options, 0, wxALL, DCPOMATIC_SIZER_GAP);
114
115         wxBoxSizer* h_sizer = new wxBoxSizer (wxHORIZONTAL);
116
117         wxBoxSizer* time_sizer = new wxBoxSizer (wxVERTICAL);
118         time_sizer->Add (_frame_number, 0, wxEXPAND);
119         time_sizer->Add (_timecode, 0, wxEXPAND);
120
121         h_sizer->Add (_back_button, 0, wxALL, 2);
122         h_sizer->Add (time_sizer, 0, wxEXPAND);
123         h_sizer->Add (_forward_button, 0, wxALL, 2);
124         h_sizer->Add (_play_button, 0, wxEXPAND);
125         h_sizer->Add (_slider, 1, wxEXPAND);
126
127         _v_sizer->Add (h_sizer, 0, wxEXPAND | wxALL, 6);
128
129         _frame_number->SetMinSize (wxSize (84, -1));
130         _back_button->SetMinSize (wxSize (32, -1));
131         _forward_button->SetMinSize (wxSize (32, -1));
132
133         _panel->Bind            (wxEVT_PAINT,             boost::bind (&FilmViewer::paint_panel,     this));
134         _panel->Bind            (wxEVT_SIZE,              boost::bind (&FilmViewer::panel_sized,     this, _1));
135         _outline_content->Bind  (wxEVT_CHECKBOX,          boost::bind (&FilmViewer::refresh_panel,   this));
136         _left_eye->Bind         (wxEVT_RADIOBUTTON,       boost::bind (&FilmViewer::refresh,         this));
137         _right_eye->Bind        (wxEVT_RADIOBUTTON,       boost::bind (&FilmViewer::refresh,         this));
138         _slider->Bind           (wxEVT_SCROLL_CHANGED,    boost::bind (&FilmViewer::slider_moved,    this));
139         _play_button->Bind      (wxEVT_TOGGLEBUTTON,      boost::bind (&FilmViewer::play_clicked,    this));
140         _timer.Bind             (wxEVT_TIMER,             boost::bind (&FilmViewer::timer,           this));
141         _back_button->Bind      (wxEVT_LEFT_DOWN,         boost::bind (&FilmViewer::back_clicked,    this, _1));
142         _forward_button->Bind   (wxEVT_LEFT_DOWN,         boost::bind (&FilmViewer::forward_clicked, this, _1));
143         _frame_number->Bind     (wxEVT_LEFT_DOWN,         boost::bind (&FilmViewer::frame_number_clicked, this));
144         _timecode->Bind         (wxEVT_LEFT_DOWN,         boost::bind (&FilmViewer::timecode_clicked, this));
145         _jump_to_selected->Bind (wxEVT_CHECKBOX,          boost::bind (&FilmViewer::jump_to_selected_clicked, this));
146
147         _jump_to_selected->SetValue (Config::instance()->jump_to_selected ());
148
149         set_film (shared_ptr<Film> ());
150
151         JobManager::instance()->ActiveJobsChanged.connect (
152                 bind (&FilmViewer::active_jobs_changed, this, _2)
153                 );
154
155         setup_sensitivity ();
156
157         _config_changed_connection = Config::instance()->Changed.connect (bind (&FilmViewer::config_changed, this, _1));
158         config_changed (Config::PREVIEW_SOUND_OUTPUT);
159 }
160
161 FilmViewer::~FilmViewer ()
162 {
163         stop ();
164 }
165
166 void
167 FilmViewer::set_film (shared_ptr<Film> film)
168 {
169         if (_film == film) {
170                 return;
171         }
172
173         _film = film;
174
175         _frame.reset ();
176
177         update_position_slider ();
178         update_position_label ();
179
180         if (!_film) {
181                 return;
182         }
183
184         try {
185                 _player.reset (new Player (_film, _film->playlist ()));
186                 _player->set_fast ();
187         } catch (bad_alloc) {
188                 error_dialog (this, _("There is not enough free memory to do that."));
189                 _film.reset ();
190                 return;
191         }
192
193         /* Always burn in subtitles, even if content is set not to, otherwise we won't see them
194            in the preview.
195         */
196         _player->set_always_burn_subtitles (true);
197         _player->set_play_referenced ();
198
199         _film->Changed.connect (boost::bind (&FilmViewer::film_changed, this, _1));
200         _player->Changed.connect (boost::bind (&FilmViewer::player_changed, this, _1));
201
202         /* Keep about 1 second's worth of history samples */
203         _latency_history_count = _film->audio_frame_rate() / _audio_block_size;
204
205         recreate_butler ();
206
207         calculate_sizes ();
208         refresh ();
209
210         setup_sensitivity ();
211 }
212
213 void
214 FilmViewer::recreate_butler ()
215 {
216         bool const was_running = stop ();
217         _butler.reset ();
218
219         if (!_film) {
220                 return;
221         }
222
223         AudioMapping map = AudioMapping (_film->audio_channels(), _audio_channels);
224
225         if (_audio_channels != 2 || _film->audio_channels() < 3) {
226                 for (int i = 0; i < min (_film->audio_channels(), _audio_channels); ++i) {
227                         map.set (i, i, 1);
228                 }
229         } else {
230                 /* Special case: stereo output, at least 3 channel input, map L+R to L/R and
231                    C to both, all 3dB down.
232                 */
233                 map.set (0, 0, 1 / sqrt(2)); // L -> L
234                 map.set (1, 1, 1 / sqrt(2)); // R -> R
235                 map.set (2, 0, 1 / sqrt(2)); // C -> L
236                 map.set (2, 1, 1 / sqrt(2)); // C -> R
237         }
238
239         _butler.reset (new Butler (_film, _player, map, _audio_channels));
240         if (!Config::instance()->preview_sound()) {
241                 _butler->disable_audio ();
242         }
243
244         if (was_running) {
245                 start ();
246         }
247 }
248
249 void
250 FilmViewer::refresh_panel ()
251 {
252         _panel->Refresh ();
253         _panel->Update ();
254 }
255
256 void
257 FilmViewer::get ()
258 {
259         DCPOMATIC_ASSERT (_butler);
260
261         pair<shared_ptr<PlayerVideo>, DCPTime> video;
262         do {
263                 video = _butler->get_video ();
264         } while (
265                 _film->three_d() &&
266                 ((_left_eye->GetValue() && video.first->eyes() == EYES_RIGHT) || (_right_eye->GetValue() && video.first->eyes() == EYES_LEFT))
267                 );
268
269         _butler->rethrow ();
270
271         if (!video.first) {
272                 _frame.reset ();
273                 refresh_panel ();
274                 return;
275         }
276
277         /* In an ideal world, what we would do here is:
278          *
279          * 1. convert to XYZ exactly as we do in the DCP creation path.
280          * 2. convert back to RGB for the preview display, compensating
281          *    for the monitor etc. etc.
282          *
283          * but this is inefficient if the source is RGB.  Since we don't
284          * (currently) care too much about the precise accuracy of the preview's
285          * colour mapping (and we care more about its speed) we try to short-
286          * circuit this "ideal" situation in some cases.
287          *
288          * The content's specified colour conversion indicates the colourspace
289          * which the content is in (according to the user).
290          *
291          * PlayerVideo::image (bound to PlayerVideo::always_rgb) will take the source
292          * image and convert it (from whatever the user has said it is) to RGB.
293          */
294
295         _frame = video.first->image (
296                 bind (&Log::dcp_log, _film->log().get(), _1, _2),
297                 bind (&PlayerVideo::always_rgb, _1),
298                 false, true
299                 );
300
301         ImageChanged (video.first);
302
303         _video_position = video.second;
304         _inter_position = video.first->inter_position ();
305         _inter_size = video.first->inter_size ();
306
307         refresh_panel ();
308 }
309
310 void
311 FilmViewer::timer ()
312 {
313         if (!_film || !_playing) {
314                 return;
315         }
316
317         get ();
318         update_position_label ();
319         update_position_slider ();
320         DCPTime const next = _video_position + DCPTime::from_frames (1, _film->video_frame_rate ());
321
322         if (next >= _film->length()) {
323                 stop ();
324         }
325
326         _timer.Start (max ((next.seconds() - time().seconds()) * 1000, 1.0), wxTIMER_ONE_SHOT);
327
328         if (_butler) {
329                 _butler->rethrow ();
330         }
331 }
332
333 void
334 FilmViewer::paint_panel ()
335 {
336         wxPaintDC dc (_panel);
337
338         if (!_frame || !_film || !_out_size.width || !_out_size.height) {
339                 dc.Clear ();
340                 return;
341         }
342
343         wxImage frame (_out_size.width, _out_size.height, _frame->data()[0], true);
344         wxBitmap frame_bitmap (frame);
345         dc.DrawBitmap (frame_bitmap, 0, 0);
346
347         if (_out_size.width < _panel_size.width) {
348                 wxPen p (GetBackgroundColour ());
349                 wxBrush b (GetBackgroundColour ());
350                 dc.SetPen (p);
351                 dc.SetBrush (b);
352                 dc.DrawRectangle (_out_size.width, 0, _panel_size.width - _out_size.width, _panel_size.height);
353         }
354
355         if (_out_size.height < _panel_size.height) {
356                 wxPen p (GetBackgroundColour ());
357                 wxBrush b (GetBackgroundColour ());
358                 dc.SetPen (p);
359                 dc.SetBrush (b);
360                 dc.DrawRectangle (0, _out_size.height, _panel_size.width, _panel_size.height - _out_size.height);
361         }
362
363         if (_outline_content->GetValue ()) {
364                 wxPen p (wxColour (255, 0, 0), 2);
365                 dc.SetPen (p);
366                 dc.SetBrush (*wxTRANSPARENT_BRUSH);
367                 dc.DrawRectangle (_inter_position.x, _inter_position.y, _inter_size.width, _inter_size.height);
368         }
369 }
370
371 void
372 FilmViewer::slider_moved ()
373 {
374         if (!_film) {
375                 return;
376         }
377
378         DCPTime t (_slider->GetValue() * _film->length().get() / 4096);
379         /* Ensure that we hit the end of the film at the end of the slider */
380         if (t >= _film->length ()) {
381                 t = _film->length() - DCPTime::from_frames (1, _film->video_frame_rate ());
382         }
383         seek (t, false);
384         update_position_label ();
385         update_position_slider ();
386 }
387
388 void
389 FilmViewer::panel_sized (wxSizeEvent& ev)
390 {
391         _panel_size.width = ev.GetSize().GetWidth();
392         _panel_size.height = ev.GetSize().GetHeight();
393
394         calculate_sizes ();
395         refresh ();
396         update_position_label ();
397         update_position_slider ();
398 }
399
400 void
401 FilmViewer::calculate_sizes ()
402 {
403         if (!_film) {
404                 return;
405         }
406
407         Ratio const * container = _film->container ();
408
409         float const panel_ratio = _panel_size.ratio ();
410         float const film_ratio = container ? container->ratio () : 1.78;
411
412         if (panel_ratio < film_ratio) {
413                 /* panel is less widscreen than the film; clamp width */
414                 _out_size.width = _panel_size.width;
415                 _out_size.height = lrintf (_out_size.width / film_ratio);
416         } else {
417                 /* panel is more widescreen than the film; clamp height */
418                 _out_size.height = _panel_size.height;
419                 _out_size.width = lrintf (_out_size.height * film_ratio);
420         }
421
422         /* Catch silly values */
423         _out_size.width = max (64, _out_size.width);
424         _out_size.height = max (64, _out_size.height);
425
426         _player->set_video_container_size (_out_size);
427 }
428
429 void
430 FilmViewer::play_clicked ()
431 {
432         check_play_state ();
433 }
434
435 void
436 FilmViewer::check_play_state ()
437 {
438         if (!_film || _film->video_frame_rate() == 0) {
439                 return;
440         }
441
442         if (_play_button->GetValue()) {
443                 start ();
444         } else {
445                 stop ();
446         }
447 }
448
449 void
450 FilmViewer::start ()
451 {
452         if (_audio.isStreamOpen()) {
453                 _audio.setStreamTime (_video_position.seconds());
454                 _audio.startStream ();
455         }
456
457         _playing = true;
458         timer ();
459 }
460
461 bool
462 FilmViewer::stop ()
463 {
464         if (_audio.isStreamRunning()) {
465                 /* stop stream and discard any remainig queued samples */
466                 _audio.abortStream ();
467         }
468
469         if (!_playing) {
470                 return false;
471         }
472
473         _playing = false;
474         _play_button->SetValue (false);
475         return true;
476 }
477
478 void
479 FilmViewer::update_position_slider ()
480 {
481         if (!_film) {
482                 _slider->SetValue (0);
483                 return;
484         }
485
486         DCPTime const len = _film->length ();
487
488         if (len.get ()) {
489                 int const new_slider_position = 4096 * _video_position.get() / len.get();
490                 if (new_slider_position != _slider->GetValue()) {
491                         _slider->SetValue (new_slider_position);
492                 }
493         }
494 }
495
496 void
497 FilmViewer::update_position_label ()
498 {
499         if (!_film) {
500                 _frame_number->SetLabel ("0");
501                 _timecode->SetLabel ("0:0:0.0");
502                 return;
503         }
504
505         double const fps = _film->video_frame_rate ();
506         /* Count frame number from 1 ... not sure if this is the best idea */
507         _frame_number->SetLabel (wxString::Format (wxT("%ld"), lrint (_video_position.seconds() * fps) + 1));
508         _timecode->SetLabel (time_to_timecode (_video_position, fps));
509 }
510
511 void
512 FilmViewer::active_jobs_changed (optional<string> j)
513 {
514         /* examine content is the only job which stops the viewer working */
515         bool const a = !j || *j != "examine_content";
516         _slider->Enable (a);
517         _play_button->Enable (a);
518 }
519
520 DCPTime
521 FilmViewer::nudge_amount (wxMouseEvent& ev)
522 {
523         DCPTime amount = DCPTime::from_frames (1, _film->video_frame_rate ());
524
525         if (ev.ShiftDown() && !ev.ControlDown()) {
526                 amount = DCPTime::from_seconds (1);
527         } else if (!ev.ShiftDown() && ev.ControlDown()) {
528                 amount = DCPTime::from_seconds (10);
529         } else if (ev.ShiftDown() && ev.ControlDown()) {
530                 amount = DCPTime::from_seconds (60);
531         }
532
533         return amount;
534 }
535
536 void
537 FilmViewer::go_to (DCPTime t)
538 {
539         if (t < DCPTime ()) {
540                 t = DCPTime ();
541         }
542
543         if (t >= _film->length ()) {
544                 t = _film->length ();
545         }
546
547         seek (t, true);
548         update_position_label ();
549         update_position_slider ();
550 }
551
552 void
553 FilmViewer::back_clicked (wxMouseEvent& ev)
554 {
555         go_to (_video_position - nudge_amount (ev));
556         ev.Skip ();
557 }
558
559 void
560 FilmViewer::forward_clicked (wxMouseEvent& ev)
561 {
562         go_to (_video_position + nudge_amount (ev));
563         ev.Skip ();
564 }
565
566 void
567 FilmViewer::player_changed (bool frequent)
568 {
569         if (frequent) {
570                 return;
571         }
572
573         if (_coalesce_player_changes) {
574                 _pending_player_change = true;
575                 return;
576         }
577
578         calculate_sizes ();
579         refresh ();
580         update_position_label ();
581         update_position_slider ();
582 }
583
584 void
585 FilmViewer::setup_sensitivity ()
586 {
587         bool const c = _film && !_film->content().empty ();
588
589         _slider->Enable (c);
590         _back_button->Enable (c);
591         _forward_button->Enable (c);
592         _play_button->Enable (c);
593         _outline_content->Enable (c);
594         _frame_number->Enable (c);
595         _timecode->Enable (c);
596         _jump_to_selected->Enable (c);
597
598         _left_eye->Enable (c && _film->three_d ());
599         _right_eye->Enable (c && _film->three_d ());
600 }
601
602 void
603 FilmViewer::film_changed (Film::Property p)
604 {
605         if (p == Film::CONTENT || p == Film::THREE_D) {
606                 setup_sensitivity ();
607         }
608 }
609
610 /** Re-get the current frame */
611 void
612 FilmViewer::refresh ()
613 {
614         seek (_video_position, _last_seek_accurate);
615 }
616
617 void
618 FilmViewer::set_position (DCPTime p)
619 {
620         _video_position = p;
621         seek (p, true);
622         update_position_label ();
623         update_position_slider ();
624 }
625
626 void
627 FilmViewer::set_coalesce_player_changes (bool c)
628 {
629         _coalesce_player_changes = c;
630
631         if (c) {
632                 _pending_player_change = false;
633         } else {
634                 if (_pending_player_change) {
635                         player_changed (false);
636                 }
637         }
638 }
639
640 void
641 FilmViewer::timecode_clicked ()
642 {
643         PlayheadToTimecodeDialog* dialog = new PlayheadToTimecodeDialog (this, _film->video_frame_rate ());
644         if (dialog->ShowModal() == wxID_OK) {
645                 go_to (dialog->get ());
646         }
647         dialog->Destroy ();
648 }
649
650 void
651 FilmViewer::frame_number_clicked ()
652 {
653         PlayheadToFrameDialog* dialog = new PlayheadToFrameDialog (this, _film->video_frame_rate ());
654         if (dialog->ShowModal() == wxID_OK) {
655                 go_to (dialog->get ());
656         }
657         dialog->Destroy ();
658 }
659
660 void
661 FilmViewer::jump_to_selected_clicked ()
662 {
663         Config::instance()->set_jump_to_selected (_jump_to_selected->GetValue ());
664 }
665
666 void
667 FilmViewer::seek (DCPTime t, bool accurate)
668 {
669         if (!_butler) {
670                 return;
671         }
672
673         bool const was_running = stop ();
674
675         _butler->seek (t, accurate);
676         _last_seek_accurate = accurate;
677         get ();
678
679         if (was_running) {
680                 start ();
681         }
682 }
683
684 void
685 FilmViewer::config_changed (Config::Property p)
686 {
687         if (p != Config::PREVIEW_SOUND && p != Config::PREVIEW_SOUND_OUTPUT) {
688                 return;
689         }
690
691         if (_audio.isStreamOpen ()) {
692                 _audio.closeStream ();
693         }
694
695         if (Config::instance()->preview_sound()) {
696                 unsigned int st = 0;
697                 if (Config::instance()->preview_sound_output()) {
698                         while (st < _audio.getDeviceCount()) {
699                                 if (_audio.getDeviceInfo(st).name == Config::instance()->preview_sound_output().get()) {
700                                         break;
701                                 }
702                                 ++st;
703                         }
704                         if (st == _audio.getDeviceCount()) {
705                                 st = _audio.getDefaultOutputDevice();
706                         }
707                 } else {
708                         st = _audio.getDefaultOutputDevice();
709                 }
710
711                 _audio_channels = _audio.getDeviceInfo(st).outputChannels;
712
713                 recreate_butler ();
714
715                 RtAudio::StreamParameters sp;
716                 sp.deviceId = st;
717                 sp.nChannels = _audio_channels;
718                 sp.firstChannel = 0;
719                 try {
720                         _audio.openStream (&sp, 0, RTAUDIO_FLOAT32, 48000, &_audio_block_size, &rtaudio_callback, this);
721 #ifdef DCPOMATIC_USE_RTERROR
722                 } catch (RtError& e) {
723 #else
724                 } catch (RtAudioError& e) {
725 #endif
726                         error_dialog (
727                                 this,
728                                 wxString::Format (_("Could not set up audio output (%s).  There will be no audio during the preview."), e.what())
729                                 );
730                 }
731
732         } else {
733                 _audio_channels = 0;
734                 recreate_butler ();
735         }
736 }
737
738 DCPTime
739 FilmViewer::time () const
740 {
741         if (_audio.isStreamRunning ()) {
742                 return DCPTime::from_seconds (const_cast<RtAudio*>(&_audio)->getStreamTime ()) -
743                         DCPTime::from_frames (average_latency(), _film->audio_frame_rate());
744         }
745
746         return _video_position;
747 }
748
749 int
750 FilmViewer::audio_callback (void* out_p, unsigned int frames)
751 {
752         _butler->get_audio (reinterpret_cast<float*> (out_p), frames);
753
754         boost::mutex::scoped_lock lm (_latency_history_mutex, boost::try_to_lock);
755         if (lm) {
756                 _latency_history.push_back (_audio.getStreamLatency ());
757                 if (_latency_history.size() > static_cast<size_t> (_latency_history_count)) {
758                         _latency_history.pop_front ();
759                 }
760         }
761
762         return 0;
763 }
764
765 Frame
766 FilmViewer::average_latency () const
767 {
768         boost::mutex::scoped_lock lm (_latency_history_mutex);
769         if (_latency_history.empty()) {
770                 return 0;
771         }
772
773         Frame total = 0;
774         BOOST_FOREACH (Frame i, _latency_history) {
775                 total += i;
776         }
777
778         return total / _latency_history.size();
779 }