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