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