Apollo 10.0
自动驾驶开放平台
apollo::cyber::record::Player类 参考

#include <player.h>

apollo::cyber::record::Player 的协作图:

Public 类型

using ConsumerPtr = std::unique_ptr< PlayTaskConsumer >
 
using ProducerPtr = std::unique_ptr< PlayTaskProducer >
 
using TaskBufferPtr = std::shared_ptr< PlayTaskBuffer >
 
using NodePtr = std::shared_ptr< apollo::cyber::Node >
 

Public 成员函数

 Player (const PlayParam &play_param, const NodePtr &node=nullptr, const bool preload_fill_buffer_mode=false)
 
virtual ~Player ()
 
bool Init ()
 
bool Start ()
 
bool Stop ()
 
bool Reset ()
 Reset player for dv will repeatedly use
 
bool PreloadPlayRecord (const double &progress_s=0, bool paused_status=false)
 Preload the player,fill play_task_buffer ahead ofr time to ensure fast playback and avoid consumer waiting.
 
void NohupPlayRecord ()
 set nohup process to play record.
 
void HandleNohupThreadStatus ()
 Pause or continue to play record by change the nohup process status.
 

详细描述

在文件 player.h34 行定义.

成员类型定义说明

◆ ConsumerPtr

在文件 player.h36 行定义.

◆ NodePtr

在文件 player.h39 行定义.

◆ ProducerPtr

在文件 player.h37 行定义.

◆ TaskBufferPtr

在文件 player.h38 行定义.

构造及析构函数说明

◆ Player()

apollo::cyber::record::Player::Player ( const PlayParam play_param,
const NodePtr node = nullptr,
const bool  preload_fill_buffer_mode = false 
)

在文件 player.cc29 行定义.

31 : is_initialized_(false),
32 is_stopped_(true),
33 consumer_(nullptr),
34 producer_(nullptr),
35 task_buffer_(nullptr) {
36 task_buffer_ = std::make_shared<PlayTaskBuffer>();
37 consumer_.reset(new PlayTaskConsumer(task_buffer_, play_param.play_rate));
38 producer_.reset(new PlayTaskProducer(task_buffer_, play_param, node,
39 preload_fill_buffer_mode));
40}

◆ ~Player()

apollo::cyber::record::Player::~Player ( )
virtual

在文件 player.cc42 行定义.

成员函数说明

◆ HandleNohupThreadStatus()

void apollo::cyber::record::Player::HandleNohupThreadStatus ( )

Pause or continue to play record by change the nohup process status.

在文件 player.cc130 行定义.

130{ is_paused_ = !is_paused_; }

◆ Init()

bool apollo::cyber::record::Player::Init ( )

在文件 player.cc44 行定义.

44 {
45 if (is_initialized_.exchange(true)) {
46 AERROR << "player has been initialized.";
47 return false;
48 }
49
50 if (producer_->Init()) {
51 return true;
52 }
53
54 is_initialized_.store(false);
55 return false;
56}
#define AERROR
Definition log.h:44

◆ NohupPlayRecord()

void apollo::cyber::record::Player::NohupPlayRecord ( )

set nohup process to play record.

在文件 player.cc148 行定义.

148 {
149 nohup_play_th_.reset(new std::thread(&Player::ThreadFunc_Play_Nohup, this));
150}

◆ PreloadPlayRecord()

bool apollo::cyber::record::Player::PreloadPlayRecord ( const double &  progress_s = 0,
bool  paused_status = false 
)

Preload the player,fill play_task_buffer ahead ofr time to ensure fast playback and avoid consumer waiting.

参数
progress_sThe start time we begin to read record and fill task buffer.
返回
If the action executed successfully.

在文件 player.cc152 行定义.

152 {
153 if (!producer_->is_initialized()) {
154 return false;
155 }
156 if (is_preloaded_.load()) {
157 producer_->Reset(progress_s);
158 is_preloaded_.store(false);
159 }
160 if (progress_s == 0) {
161 // When the progress is 0, it is completely reloaded. At this
162 // time, the is_paused_ of the player should change to the initial
163 // state to avoid being disturbed by the last state.On the contrary,
164 // reset the progress needs to preserve the past state to ensure the
165 // unity of the state before and after.
166 is_paused_.exchange(false);
167 } else {
168 is_paused_.exchange(paused_status);
169 }
170 producer_->FillPlayTaskBuffer();
171 is_preloaded_.store(true);
172 return true;
173}

◆ Reset()

bool apollo::cyber::record::Player::Reset ( )

Reset player for dv will repeatedly use

返回
If the action executed successfully.

在文件 player.cc273 行定义.

273 {
274 if (is_stopped_.exchange(true)) {
275 return false;
276 }
277 // produer may not be stopped under reset progress
278 // reset is_stopped_ to true to ensure logical unity
279 producer_->set_stopped();
280 producer_->Stop();
281 consumer_->Stop();
282 // clear task buffer for refill it
283 task_buffer_->Clear();
284 if (nohup_play_th_ != nullptr && nohup_play_th_->joinable()) {
285 nohup_play_th_->join();
286 nohup_play_th_ = nullptr;
287 }
288 return true;
289}

◆ Start()

bool apollo::cyber::record::Player::Start ( )

在文件 player.cc175 行定义.

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;
194 while (preload_sec > 0 && !is_stopped_.load() && apollo::cyber::OK()) {
195 std::this_thread::sleep_for(std::chrono::seconds(1));
196 --preload_sec;
197 }
198
199 auto delay_sec = play_param.delay_time_s;
200 while (delay_sec > 0 && !is_stopped_.load() && apollo::cyber::OK()) {
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));
215 while (!is_stopped_.load() && apollo::cyber::OK()) {
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}
bool OK()
Definition state.h:44

◆ Stop()

bool apollo::cyber::record::Player::Stop ( )

在文件 player.cc260 行定义.

260 {
261 if (is_stopped_.exchange(true)) {
262 return false;
263 }
264 producer_->Stop();
265 consumer_->Stop();
266 if (term_thread_ != nullptr && term_thread_->joinable()) {
267 term_thread_->join();
268 term_thread_ = nullptr;
269 }
270 return true;
271}

该类的文档由以下文件生成: