0891189b33d0d47a236b0f61d331cc00d7b2d7d1
[dcpomatic.git] / src / lib / cpu_player_video_preparer.cc
1 #include "cpu_player_video_preparer.h"
2 #include "cross.h"
3 #include "dcpomatic_log.h"
4 #include "player_video.h"
5 #include "timer.h"
6 #include <boost/thread.hpp>
7
8
9 using boost::bind;
10 using boost::shared_ptr;
11 using boost::weak_ptr;
12
13
14 CPUPlayerVideoPreparer::CPUPlayerVideoPreparer (boost::function<AVPixelFormat (AVPixelFormat)> pixel_format, bool aligned, bool fast)
15         : _work (new boost::asio::io_service::work(_service))
16         , _pixel_format (pixel_format)
17         , _aligned (aligned)
18         , _fast (fast)
19 {
20         LOG_TIMING("start-prepare-threads %1", boost::thread::hardware_concurrency() * 2);
21
22         for (size_t i = 0; i < boost::thread::hardware_concurrency() * 2; ++i) {
23                 _pool.create_thread (bind (&boost::asio::io_service::run, &_service));
24         }
25 }
26
27
28 CPUPlayerVideoPreparer::~CPUPlayerVideoPreparer ()
29 {
30         _work.reset ();
31         _pool.join_all ();
32         _service.stop ();
33 }
34
35
36 void
37 CPUPlayerVideoPreparer::prepare (weak_ptr<PlayerVideo> weak_video)
38 try
39 {
40         shared_ptr<PlayerVideo> video = weak_video.lock ();
41         /* If the weak_ptr cannot be locked the video obviously no longer requires any work */
42         if (video) {
43                 LOG_TIMING("start-prepare in %1", thread_id());
44                 video->prepare (_pixel_format, _aligned, _fast);
45                 LOG_TIMING("finish-prepare in %1", thread_id());
46                 timestamped_printf("cpu finishes %d\n", video->time.frames_round(24));
47         }
48 }
49 catch (...)
50 {
51         store_current ();
52 }
53
54
55 void
56 CPUPlayerVideoPreparer::request (shared_ptr<PlayerVideo> pv)
57 {
58         _service.post (bind(&CPUPlayerVideoPreparer::prepare, this, weak_ptr<PlayerVideo>(pv)));
59 }
60