Add exit command to stress tester.
[dcpomatic.git] / src / wx / player_stress_tester.cc
1 /*
2     Copyright (C) 2017-2020 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 #include "player_stress_tester.h"
22 #include "controls.h"
23 #include <dcp/raw_convert.h>
24 #include <dcp/util.h>
25 #include <wx/wx.h>
26 #include <boost/algorithm/string.hpp>
27 #include <boost/foreach.hpp>
28 #include <boost/bind.hpp>
29 #include <string>
30 #include <vector>
31 #include <iostream>
32
33 using std::string;
34 using std::vector;
35 using std::cout;
36 using dcp::raw_convert;
37 using boost::optional;
38
39 /* Interval to check for things to do with the stress script (in milliseconds) */
40 #define CHECK_INTERVAL 20
41
42 Command::Command (string line)
43         : type (NONE)
44         , int_param (0)
45 {
46         vector<string> bits;
47         boost::split (bits, line, boost::is_any_of(" "));
48         if (bits[0] == "O") {
49                 if (bits.size() != 2) {
50                         return;
51                 }
52                 type = OPEN;
53                 string_param = bits[1];
54         } else if (bits[0] == "P") {
55                 type = PLAY;
56         } else if (bits[0] == "W") {
57                 if (bits.size() != 2) {
58                         return;
59                 }
60                 type = WAIT;
61                 int_param = raw_convert<int>(bits[1]);
62         } else if (bits[0] == "S") {
63                 type = STOP;
64         } else if (bits[0] == "K") {
65                 if (bits.size() != 2) {
66                         return;
67                 }
68                 type = SEEK;
69                 int_param = raw_convert<int>(bits[1]);
70         } else if (bits[0] == "E") {
71                 type = EXIT;
72         }
73 }
74
75 PlayerStressTester::PlayerStressTester ()
76         : _parent (0)
77         , _controls (0)
78         , _suspended (false)
79 {
80
81 }
82
83
84 void
85 PlayerStressTester::setup (wxWindow* parent, Controls* controls)
86 {
87         _parent = parent;
88         _controls = controls;
89 }
90
91
92 void
93 PlayerStressTester::load_script (boost::filesystem::path file)
94 {
95         DCPOMATIC_ASSERT (_parent);
96
97         _timer.Bind (wxEVT_TIMER, boost::bind(&PlayerStressTester::check_commands, this));
98         _timer.Start (CHECK_INTERVAL);
99         vector<string> lines;
100         string const script = dcp::file_to_string(file);
101         boost::split (lines, script, boost::is_any_of("\n"));
102         BOOST_FOREACH (string i, lines) {
103                 _commands.push_back (Command(i));
104         }
105         _current_command = _commands.begin();
106 }
107
108 void
109 PlayerStressTester::check_commands ()
110 {
111         DCPOMATIC_ASSERT (_controls);
112
113         if (_suspended) {
114                 return;
115         }
116
117         if (_current_command == _commands.end()) {
118                 _timer.Stop ();
119                 cout << "ST: finished.\n";
120                 return;
121         }
122
123         switch (_current_command->type) {
124                 case Command::OPEN:
125                         LoadDCP(_current_command->string_param);
126                         ++_current_command;
127                         break;
128                 case Command::PLAY:
129                         cout << "ST: play\n";
130                         _controls->play ();
131                         ++_current_command;
132                         break;
133                 case Command::WAIT:
134                         /* int_param here is the number of milliseconds to wait */
135                         if (_wait_remaining) {
136                                 _wait_remaining = *_wait_remaining - CHECK_INTERVAL;
137                                 if (_wait_remaining < 0) {
138                                         cout << "ST: wait done.\n";
139                                         _wait_remaining = optional<int>();
140                                         ++_current_command;
141                                 }
142                         } else {
143                                 _wait_remaining = _current_command->int_param;
144                                 cout << "ST: waiting for " << *_wait_remaining << ".\n";
145                         }
146                         break;
147                 case Command::STOP:
148                         cout << "ST: stop\n";
149                         _controls->stop ();
150                         ++_current_command;
151                         break;
152                 case Command::NONE:
153                         ++_current_command;
154                         break;
155                 case Command::SEEK:
156                         /* int_param here is a number between 0 and 4095, corresponding to the possible slider positions */
157                         cout << "ST: seek to " << _current_command->int_param << "\n";
158                         _controls->seek (_current_command->int_param);
159                         ++_current_command;
160                         break;
161                 case Command::EXIT:
162                         wxTheApp->GetTopWindow()->Destroy();
163                         break;
164         }
165 }
166