175 {
176 if (!is_initialized_.load()) {
177 AERROR <<
"please call Init firstly.";
178 return false;
179 }
180
181 if (!is_stopped_.exchange(false)) {
182 AERROR <<
"player has been stopped.";
183 return false;
184 }
185
186 auto& play_param = producer_->play_param();
187 std::cout << "\nPlease wait " << play_param.preload_time_s
188 << " second(s) for loading...\n"
189 << "Hit Ctrl+C to stop, Space to pause, or 's' to step.\n"
190 << std::endl;
191 producer_->Start();
192
193 auto preload_sec = play_param.preload_time_s;
195 std::this_thread::sleep_for(std::chrono::seconds(1));
196 --preload_sec;
197 }
198
199 auto delay_sec = play_param.delay_time_s;
201 std::this_thread::sleep_for(std::chrono::seconds(1));
202 --delay_sec;
203 }
204
205 consumer_->Start(play_param.begin_time_ns);
206
207 std::ios::fmtflags before(std::cout.flags());
208 std::cout << std::fixed;
209 const double total_progress_time_s =
210 static_cast<double>(play_param.end_time_ns - play_param.begin_time_ns) /
211 1e9 +
212 static_cast<double>(play_param.start_time_s);
213
214 term_thread_.reset(new std::thread(&Player::ThreadFunc_Term, this));
216 if (is_playonce_) {
217 consumer_->PlayOnce();
218 is_playonce_ = false;
219 }
220 if (is_paused_) {
221 consumer_->Pause();
222 std::cout << "\r[PAUSED ] Record Time: ";
223 } else {
224 consumer_->Continue();
225 std::cout << "\r[RUNNING] Record Time: ";
226 }
227
228 double last_played_msg_real_time_s =
229 static_cast<double>(consumer_->last_played_msg_real_time_ns()) / 1e9;
230
231 double progress_time_s =
232 static_cast<double>(producer_->play_param().start_time_s);
233 if (consumer_->last_played_msg_real_time_ns() > 0) {
234 progress_time_s +=
235 static_cast<double>(consumer_->last_played_msg_real_time_ns() -
236 consumer_->base_msg_play_time_ns() +
237 consumer_->base_msg_real_time_ns() -
238 producer_->play_param().begin_time_ns) /
239 1e9;
240 }
241
242 std::cout << std::setprecision(3) << last_played_msg_real_time_s
243 << " Progress: " << progress_time_s << " / "
244 << total_progress_time_s;
245 std::cout.flush();
246
247 if (producer_->is_stopped() && task_buffer_->Empty()) {
248 consumer_->Stop();
249 break;
250 }
251 std::this_thread::sleep_for(
252 std::chrono::milliseconds(kSleepIntervalMiliSec));
253 }
254
255 std::cout << "\nplay finished." << std::endl;
256 std::cout.flags(before);
257 return true;
258}