1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
/*
Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <sstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <fcntl.h>
#include <poll.h>
#include <boost/thread.hpp>
#include <boost/algorithm/string.hpp>
#include "player.h"
#include "filter.h"
#include "screen.h"
#include "exceptions.h"
using namespace std;
using namespace boost;
Player::Player (shared_ptr<const FilmState> fs, shared_ptr<const Screen> screen, Split split)
: _stdout_reader_should_run (true)
, _position (0)
, _paused (false)
{
assert (fs->format);
if (pipe (_mplayer_stdin) < 0) {
throw PlayError ("could not create pipe");
}
if (pipe (_mplayer_stdout) < 0) {
throw PlayError ("could not create pipe");
}
if (pipe (_mplayer_stderr) < 0) {
throw PlayError ("could not create pipe");
}
int const p = fork ();
if (p < 0) {
throw PlayError ("could not fork for mplayer");
} else if (p == 0) {
close (_mplayer_stdin[1]);
dup2 (_mplayer_stdin[0], STDIN_FILENO);
close (_mplayer_stdout[0]);
dup2 (_mplayer_stdout[1], STDOUT_FILENO);
close (_mplayer_stderr[0]);
dup2 (_mplayer_stderr[1], STDERR_FILENO);
char* p[] = { strdup ("TERM=xterm"), strdup ("DISPLAY=:0"), 0 };
environ = p;
stringstream s;
s << "/usr/local/bin/mplayer";
s << " -vo x11 -noaspect -noautosub -nosub -vo x11 -noborder -slave -quiet -input nodefault-bindings:conf=/dev/null";
s << " -sws " << fs->scaler->mplayer_id ();
stringstream vf;
Position position = screen->position (fs->format);
Size screen_size = screen->size (fs->format);
Size const cropped_size = fs->cropped_size (fs->size);
switch (split) {
case SPLIT_NONE:
vf << crop_string (Position (fs->left_crop, fs->top_crop), cropped_size);
s << " -geometry " << position.x << ":" << position.y;
break;
case SPLIT_LEFT:
{
Size split_size = cropped_size;
split_size.width /= 2;
vf << crop_string (Position (fs->left_crop, fs->top_crop), split_size);
screen_size.width /= 2;
s << " -geometry " << position.x << ":" << position.y;
break;
}
case SPLIT_RIGHT:
{
Size split_size = cropped_size;
split_size.width /= 2;
vf << crop_string (Position (fs->left_crop + split_size.width, fs->top_crop), split_size);
screen_size.width /= 2;
s << " -geometry " << (position.x + screen_size.width) << ":" << position.y;
break;
}
}
vf << ",scale=" << screen_size.width << ":" << screen_size.height;
pair<string, string> filters = Filter::ffmpeg_strings (fs->filters);
if (!filters.first.empty()) {
vf << "," << filters.first;
}
if (!filters.second.empty ()) {
vf << ",pp=" << filters.second;
}
s << " -vf " << vf.str();
s << " \"" << fs->content_path() << "\" ";
string cmd (s.str ());
vector<string> b = split_at_spaces_considering_quotes (cmd);
char** cl = new char*[b.size() + 1];
for (vector<string>::size_type i = 0; i < b.size(); ++i) {
cl[i] = strdup (b[i].c_str ());
}
cl[b.size()] = 0;
execv (cl[0], cl);
stringstream e;
e << "exec of mplayer failed " << strerror (errno);
throw PlayError (e.str ());
} else {
_mplayer_pid = p;
command ("pause");
_stdout_reader = new boost::thread (boost::bind (&Player::stdout_reader, this));
}
}
Player::~Player ()
{
_stdout_reader_should_run = false;
_stdout_reader->join ();
delete _stdout_reader;
close (_mplayer_stdin[0]);
close (_mplayer_stdout[1]);
kill (_mplayer_pid, SIGTERM);
}
void
Player::command (string c)
{
char buf[64];
snprintf (buf, sizeof (buf), "%s\n", c.c_str ());
write (_mplayer_stdin[1], buf, strlen (buf));
}
void
Player::stdout_reader ()
{
while (_stdout_reader_should_run) {
char buf[1024];
int r = read (_mplayer_stdout[0], buf, sizeof (buf));
if (r > 0) {
stringstream s (buf);
while (s.good ()) {
string line;
getline (s, line);
vector<string> b;
split (b, line, is_any_of ("="));
if (b.size() < 2) {
continue;
}
if (b[0] == "ANS_time_pos") {
set_position (atof (b[1].c_str ()));
} else if (b[0] == "ANS_pause") {
set_paused (b[1] == "yes");
}
}
}
usleep (5e5);
snprintf (buf, sizeof (buf), "pausing_keep_force get_property time_pos\npausing_keep_force get_property pause\n");
write (_mplayer_stdin[1], buf, strlen (buf));
}
}
void
Player::set_position (float p)
{
/* XXX: could be an atomic */
boost::mutex::scoped_lock lm (_state_mutex);
_position = p;
}
void
Player::set_paused (bool p)
{
/* XXX: could be an atomic */
boost::mutex::scoped_lock lm (_state_mutex);
_paused = p;
}
float
Player::position () const
{
boost::mutex::scoped_lock lm (_state_mutex);
return _position;
}
bool
Player::paused () const
{
boost::mutex::scoped_lock lm (_state_mutex);
return _paused;
}
|