Merge branch 'master' of ssh://houllier/home/carl/git/dvdomatic
[dcpomatic.git] / src / lib / player_manager.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "player_manager.h"
21 #include "player.h"
22 #include "screen.h"
23
24 using namespace std;
25 using namespace boost;
26
27 PlayerManager* PlayerManager::_instance = 0;
28
29 PlayerManager::PlayerManager ()
30 {
31
32 }
33
34 PlayerManager *
35 PlayerManager::instance ()
36 {
37         if (_instance == 0) {
38                 _instance = new PlayerManager ();
39         }
40
41         return _instance;
42 }
43
44 void
45 PlayerManager::setup (shared_ptr<const Film> f, shared_ptr<const Screen> sc)
46 {
47         boost::mutex::scoped_lock lm (_players_mutex);
48         
49         _players.clear ();
50         _players.push_back (shared_ptr<Player> (new Player (f, sc, Player::SPLIT_NONE)));
51 }
52
53 void
54 PlayerManager::setup (shared_ptr<const Film> fs_a, shared_ptr<const Film> fs_b, shared_ptr<const Screen> sc)
55 {
56         boost::mutex::scoped_lock lm (_players_mutex);
57         
58         _players.clear ();
59
60         _players.push_back (shared_ptr<Player> (new Player (fs_a, sc, Player::SPLIT_LEFT)));
61         _players.push_back (shared_ptr<Player> (new Player (fs_b, sc, Player::SPLIT_RIGHT)));
62 }
63
64 void
65 PlayerManager::pause_or_unpause ()
66 {
67         boost::mutex::scoped_lock lm (_players_mutex);
68         
69         for (list<shared_ptr<Player> >::iterator i = _players.begin(); i != _players.end(); ++i) {
70                 (*i)->command ("pause");
71         }
72 }
73
74 void
75 PlayerManager::set_position (float p)
76 {
77         boost::mutex::scoped_lock lm (_players_mutex);
78         
79         stringstream s;
80         s << "pausing_keep_force seek " << p << " 2";
81         for (list<shared_ptr<Player> >::iterator i = _players.begin(); i != _players.end(); ++i) {
82                 (*i)->command (s.str ());
83         }
84 }
85
86 float
87 PlayerManager::position () const
88 {
89         boost::mutex::scoped_lock lm (_players_mutex);
90         
91         if (_players.empty ()) {
92                 return 0;
93         }
94
95         return _players.front()->position ();
96 }
97
98 void
99 PlayerManager::child_exited (pid_t pid)
100 {
101         boost::mutex::scoped_lock lm (_players_mutex);
102         
103         list<shared_ptr<Player> >::iterator i = _players.begin();
104         while (i != _players.end() && (*i)->mplayer_pid() != pid) {
105                 ++i;
106         }
107         
108         if (i == _players.end()) {
109                 return;
110         }
111
112         _players.erase (i);
113 }
114
115 PlayerManager::State
116 PlayerManager::state () const
117 {
118         boost::mutex::scoped_lock lm (_players_mutex);
119
120         if (_players.empty ()) {
121                 return QUIESCENT;
122         }
123
124         if (_players.front()->paused ()) {
125                 return PAUSED;
126         }
127
128         return PLAYING;
129 }
130
131 void
132 PlayerManager::stop ()
133 {
134         boost::mutex::scoped_lock lm (_players_mutex);
135         _players.clear ();
136 }