-
Notifications
You must be signed in to change notification settings - Fork 304
/
Copy pathLogPlayback.cc
642 lines (555 loc) · 19.3 KB
/
LogPlayback.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/*
* Copyright (C) 2019 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include "LogPlayback.hh"
#include <gz/msgs/log_playback_stats.pb.h>
#include <set>
#include <string>
#include <unordered_map>
#include <gz/common/Filesystem.hh>
#include <gz/common/Profiler.hh>
#include <gz/fuel_tools/Zip.hh>
#include <gz/math/Pose3.hh>
#include <gz/msgs/Utility.hh>
#include <gz/plugin/RegisterMore.hh>
#include <gz/transport/log/QueryOptions.hh>
#include <gz/transport/log/Log.hh>
#include <gz/transport/log/Message.hh>
#include <sdf/Geometry.hh>
#include <sdf/Mesh.hh>
#include <sdf/Root.hh>
#include "gz/sim/Conversions.hh"
#include "gz/sim/Events.hh"
#include "gz/sim/SdfEntityCreator.hh"
#include "gz/sim/components/Geometry.hh"
#include "gz/sim/components/LogPlaybackStatistics.hh"
#include "gz/sim/components/Material.hh"
#include "gz/sim/components/ParticleEmitter.hh"
#include "gz/sim/components/Pose.hh"
#include "gz/sim/components/World.hh"
using namespace gz;
using namespace sim;
using namespace systems;
/// \brief Private LogPlayback data class.
class gz::sim::systems::LogPlaybackPrivate
{
/// \brief Extract model resource files and state file from compression.
/// \return True if extraction was successful.
public: bool ExtractStateAndResources();
/// \brief Start log playback.
/// \param[in] _logPath Path of recorded state to playback.
/// \param[in] _ecm The EntityComponentManager of the given simulation
/// instance.
/// \return True if any playback has been started successfully.
public: bool Start(EntityComponentManager &_ecm);
/// \brief Replace URIs of resources in components with recorded path.
public: void ReplaceResourceURIs(EntityComponentManager &_ecm);
/// \brief Prepend log path to mesh file path in the SDF element.
/// \param[in] _uri URI of mesh in geometry
/// \return String of prepended path.
public: std::string PrependLogPath(const std::string &_uri);
/// \brief Updates the ECM according to the given message.
/// \param[in] _ecm Mutable ECM.
/// \param[in] _msg Message containing state updates.
public: void Parse(EntityComponentManager &_ecm,
const msgs::SerializedState &_msg);
/// \brief Updates the ECM according to the given message.
/// \param[in] _ecm Mutable ECM.
/// \param[in] _msg Message containing state updates.
public: void Parse(EntityComponentManager &_ecm,
const msgs::SerializedStateMap &_msg);
/// \brief A batch of data from log file, of all pose messages
public: transport::log::Batch batch;
/// \brief Pointer to ign-transport Log
public: std::unique_ptr<transport::log::Log> log;
/// \brief Indicator of whether any playback instance has ever been started
public: static bool started;
/// \brief Directory in which to place log file
public: std::string logPath{""};
/// \brief Directory to which compressed file is extracted to
public: std::string extDest{""};
/// \brief Indicator of whether this instance has been started
public: bool instStarted{false};
/// \brief Flag to print finish message once
public: bool printedEnd{false};
/// \brief Pointer to the event manager
public: EventManager *eventManager{nullptr};
/// \brief Flag for backward compatibility with log files recorded in older
/// plugin versions that did not record resources. False for older log files.
public: bool doReplaceResourceURIs{true};
// \brief Saves which particle emitter emitting components have changed
public: std::unordered_map<Entity, bool> prevParticleEmitterCmds;
};
bool LogPlaybackPrivate::started{false};
//////////////////////////////////////////////////
LogPlayback::LogPlayback()
: System(), dataPtr(std::make_unique<LogPlaybackPrivate>())
{
}
//////////////////////////////////////////////////
LogPlayback::~LogPlayback()
{
if (!this->dataPtr->extDest.empty())
{
common::removeAll(this->dataPtr->extDest);
}
if (this->dataPtr->instStarted)
LogPlaybackPrivate::started = false;
}
//////////////////////////////////////////////////
void LogPlaybackPrivate::Parse(EntityComponentManager &_ecm,
const msgs::SerializedStateMap &_msg)
{
_ecm.SetState(_msg);
}
//////////////////////////////////////////////////
void LogPlaybackPrivate::Parse(EntityComponentManager &_ecm,
const msgs::SerializedState &_msg)
{
_ecm.SetState(_msg);
}
//////////////////////////////////////////////////
void LogPlayback::Configure(const Entity &,
const std::shared_ptr<const sdf::Element> &_sdf,
EntityComponentManager &_ecm, EventManager &_eventMgr)
{
// Get directory paths from SDF
this->dataPtr->logPath = _sdf->Get<std::string>("playback_path");
this->dataPtr->eventManager = &_eventMgr;
// Prepend working directory if path is relative
this->dataPtr->logPath = common::absPath(this->dataPtr->logPath);
// Set the entity offset.
// \todo This number should be included in the log file.
_ecm.SetEntityCreateOffset(math::MAX_I64 / 2);
// If path is a file, assume it is a compressed file
// (Otherwise assume it is a directory containing recorded files.)
if (common::isFile(this->dataPtr->logPath))
{
std::string extension = common::lowercase(this->dataPtr->logPath.substr(
this->dataPtr->logPath.find_last_of(".") + 1));
if (extension != "zip")
{
gzerr << "Please specify a zip file.\n";
return;
}
if (!this->dataPtr->ExtractStateAndResources())
{
gzerr << "Cannot play back files.\n";
return;
}
}
// Enforce only one playback instance
if (!LogPlaybackPrivate::started)
{
this->dataPtr->Start(_ecm);
}
else
{
gzwarn << "A LogPlayback instance has already been started. "
<< "Will not start another.\n";
}
}
//////////////////////////////////////////////////
bool LogPlaybackPrivate::Start(EntityComponentManager &_ecm)
{
if (LogPlaybackPrivate::started)
{
gzwarn << "A LogPlayback instance has already been started. "
<< "Will not start another.\n";
return true;
}
if (this->logPath.empty())
{
gzerr << "Unspecified log path to playback. Nothing to play.\n";
return false;
}
// Append file name
std::string dbPath = common::joinPaths(this->logPath, "state.tlog");
gzmsg << "Loading log file [" + dbPath + "]\n";
if (!common::exists(dbPath))
{
gzerr << "Log path invalid. File [" << dbPath << "] "
<< "does not exist. Nothing to play.\n";
return false;
}
// Call Log.hh directly to load a .tlog file
this->log = std::make_unique<transport::log::Log>();
if (!this->log->Open(dbPath))
{
gzerr << "Failed to open log file [" << dbPath << "]" << std::endl;
}
// Access all messages in .tlog file
this->batch = this->log->QueryMessages();
auto iter = this->batch.begin();
if (iter == this->batch.end())
{
gzerr << "No messages found in log file [" << dbPath << "]" << std::endl;
}
// Look for the first SerializedState message and use it to set the initial
// state of the world. Messages received before this are ignored.
for (; iter != this->batch.end(); ++iter)
{
auto msgType = iter->Type();
if (msgType == "gz.msgs.SerializedState")
{
msgs::SerializedState msg;
msg.ParseFromString(iter->Data());
this->Parse(_ecm, msg);
break;
}
else if (msgType == "gz.msgs.SerializedStateMap")
{
msgs::SerializedStateMap msg;
msg.ParseFromString(iter->Data());
this->Parse(_ecm, msg);
break;
}
}
msgs::LogPlaybackStatistics logStats;
auto startTime = convert<msgs::Time>(this->log->StartTime());
auto endTime = convert<msgs::Time>(this->log->EndTime());
logStats.mutable_start_time()->set_sec(startTime.sec());
logStats.mutable_start_time()->set_nsec(startTime.nsec());
logStats.mutable_end_time()->set_sec(endTime.sec());
logStats.mutable_end_time()->set_nsec(endTime.nsec());
components::LogPlaybackStatistics newLogStatComp(logStats);
auto worldEntity = _ecm.EntityByComponents(components::World());
if (kNullEntity == worldEntity)
{
gzerr << "Missing world entity." << std::endl;
return false;
}
auto currLogStatComp =
_ecm.Component<components::LogPlaybackStatistics>(worldEntity);
if (currLogStatComp)
{
*currLogStatComp = newLogStatComp;
}
else
{
_ecm.CreateComponent(worldEntity, newLogStatComp);
}
this->ReplaceResourceURIs(_ecm);
this->instStarted = true;
LogPlaybackPrivate::started = true;
return true;
}
//////////////////////////////////////////////////
void LogPlaybackPrivate::ReplaceResourceURIs(EntityComponentManager &_ecm)
{
// For backward compatibility with log files recorded in older versions of
// plugin, do not prepend resource paths with logPath.
if (!this->doReplaceResourceURIs)
{
return;
}
// Define equality functions for replacing component uri
auto uriEqual = [&](const std::string &_s1, const std::string &_s2) -> bool
{
return (_s1.compare(_s2) == 0);
};
auto geoUriEqual = [&](const sdf::Geometry &_g1,
const sdf::Geometry &_g2) -> bool
{
if (_g1.Type() == sdf::GeometryType::MESH &&
_g2.Type() == sdf::GeometryType::MESH)
{
return uriEqual(_g1.MeshShape()->Uri(), _g2.MeshShape()->Uri());
}
else
return false;
};
auto matUriEqual = [&](const sdf::Material &_m1,
const sdf::Material &_m2) -> bool
{
return uriEqual(_m1.ScriptUri(), _m2.ScriptUri());
};
// Loop through geometries in world. Prepend log path to URI
// TODO(anyone): When merge forward to Citadel, handle actor skin and
// animation files
_ecm.Each<components::Geometry>(
[&](const Entity &/*_entity*/, components::Geometry *_geoComp) -> bool
{
sdf::Geometry geoSdf = _geoComp->Data();
if (geoSdf.Type() == sdf::GeometryType::MESH)
{
std::string meshUri = geoSdf.MeshShape()->Uri();
std::string newMeshUri;
if (!meshUri.empty())
{
// Make a copy of mesh shape, and change the uri in the new copy
sdf::Mesh meshShape = sdf::Mesh(*(geoSdf.MeshShape()));
newMeshUri = this->PrependLogPath(meshUri);
meshShape.SetUri(newMeshUri);
geoSdf.SetMeshShape(meshShape);
_geoComp->SetData(geoSdf, geoUriEqual);
}
}
return true;
});
// Loop through materials in world. Prepend log path to URI
_ecm.Each<components::Material>(
[&](const Entity &/*_entity*/, components::Material *_matComp) -> bool
{
sdf::Material matSdf = _matComp->Data();
std::string matUri = matSdf.ScriptUri();
std::string newMatUri;
if (!matUri.empty())
{
newMatUri = this->PrependLogPath(matUri);
matSdf.SetScriptUri(newMatUri);
_matComp->SetData(matSdf, matUriEqual);
}
return true;
});
}
//////////////////////////////////////////////////
std::string LogPlaybackPrivate::PrependLogPath(const std::string &_uri)
{
// For backward compatibility with log files recorded in older versions of
// plugin, do not prepend resource paths with logPath.
if (!this->doReplaceResourceURIs)
{
return std::string(_uri);
}
const std::string filePrefix = "file://";
// Prepend if path starts with file:// or /, but recorded path has not
// already been prepended.
if (((_uri.compare(0, filePrefix.length(), filePrefix) == 0) &&
(_uri.substr(filePrefix.length()).compare(
0, this->logPath.length(), this->logPath) != 0))
|| _uri[0] == '/')
{
std::string pathNoPrefix;
if (_uri[0] == '/')
{
pathNoPrefix = std::string(_uri);
}
else
{
pathNoPrefix = _uri.substr(filePrefix.length());
}
// Prepend log path to file path
std::string pathPrepended = common::joinPaths(this->logPath,
pathNoPrefix);
// For backward compatibility. If prepended record path does not exist,
// then do not prepend logPath. Assume recording is from an older version.
if (!common::exists(pathPrepended))
{
this->doReplaceResourceURIs = false;
return std::string(_uri);
}
else
{
return filePrefix + pathPrepended;
}
}
else
{
return std::string(_uri);
}
}
//////////////////////////////////////////////////
bool LogPlaybackPrivate::ExtractStateAndResources()
{
// Create a temporary directory to extract compressed file content into
this->extDest = std::string(this->logPath);
size_t extIdx = this->logPath.find_last_of('.');
if (extIdx != std::string::npos)
this->extDest = this->logPath.substr(0, extIdx);
this->extDest += "_extracted";
this->extDest = common::uniqueDirectoryPath(this->extDest);
if (fuel_tools::Zip::Extract(this->logPath, this->extDest))
{
gzmsg << "Extracted recording to [" << this->extDest << "]" << std::endl;
// Replace value in variable with the directory of extracted files
// Assume directory has same name as compressed file, without extension
// Remove extension
this->logPath = common::joinPaths(this->extDest,
common::basename(this->logPath.substr(0, extIdx)));
return true;
}
else
{
gzerr << "Failed to extract recording to [" << this->extDest << "]"
<< std::endl;
return false;
}
}
//////////////////////////////////////////////////
void LogPlayback::Reset(const UpdateInfo &, EntityComponentManager &)
{
// In this case, Reset is a noop
// LogPlayback already has handling for jumps in time as part of the
// Update method.
// Leaving this function implemented but empty prevents the SystemManager
// from trying to destroy and recreate the plugin.
}
//////////////////////////////////////////////////
void LogPlayback::Update(const UpdateInfo &_info, EntityComponentManager &_ecm)
{
GZ_PROFILE("LogPlayback::Update");
if (_info.dt == std::chrono::steady_clock::duration::zero())
return;
if (!this->dataPtr->instStarted)
return;
// Get all messages from this timestep
// TODO(anyone) Jumping forward can be expensive for long jumps. For now,
// just playing every single step so we don't miss insertions and deletions.
auto startTime = _info.simTime - _info.dt;
auto endTime = _info.simTime;
bool seekRewind = false;
std::set<Entity> entitiesToRemove;
if (_info.dt < std::chrono::steady_clock::duration::zero())
{
// Detected jumping back in time. This can be expensive.
// To rewind / seek backward in time, we also need to play every single
// step from the beginning so we don't miss insertions and deletions
// This is because each serialized state is a changed state and not an
// absolute state.
// todo(anyone) Record absolute states during recording, i.e. key frames,
// so that playback can jump to these states without the need to
// incrementally build the states from the beginning
// Create a list of entities to be removed. The list will be updated later
// as the log steps forward below
seekRewind = true;
const auto &entities = _ecm.Entities().Vertices();
for (const auto &entity : entities)
entitiesToRemove.insert(Entity(entity.first));
startTime = std::chrono::steady_clock::duration::zero();
}
this->dataPtr->batch = this->dataPtr->log->QueryMessages(
transport::log::AllTopics({startTime, endTime}));
auto iter = this->dataPtr->batch.begin();
while (iter != this->dataPtr->batch.end())
{
auto msgType = iter->Type();
// Support ignition.msgs for backwards compatibility, don't remove on tock
// so users can use logs across versions
std::string deprecatedPrefix{"ignition.msgs"};
auto pos = msgType.find(deprecatedPrefix);
if (pos != std::string::npos)
{
msgType.replace(pos, deprecatedPrefix.size(), "gz.msgs");
}
if (msgType == "gz.msgs.SerializedState")
{
msgs::SerializedState msg;
msg.ParseFromString(iter->Data());
// For seeking back in time only:
// While stepping, update the list of entities to be removed
// so we do not remove any entities that are to be created
if (seekRewind)
{
for (const auto &entIt : msg.entities())
{
Entity entity{entIt.id()};
if (entIt.remove())
{
entitiesToRemove.insert(entity);
}
else
{
entitiesToRemove.erase(entity);
}
}
}
this->dataPtr->Parse(_ecm, msg);
}
else if (msgType == "gz.msgs.SerializedStateMap")
{
msgs::SerializedStateMap msg;
msg.ParseFromString(iter->Data());
// For seeking back in time only:
// While stepping, update the list of entities to be removed
// so we do not remove any entities that are to be created
if (seekRewind)
{
for (const auto &entIt : msg.entities())
{
const auto &entityMsg = entIt.second;
Entity entity{entityMsg.id()};
if (entityMsg.remove())
{
entitiesToRemove.insert(entity);
}
else
{
entitiesToRemove.erase(entity);
}
}
}
this->dataPtr->Parse(_ecm, msg);
}
else if (msgType == "gz.msgs.StringMsg")
{
// Do nothing, we assume this is the SDF string
}
else
{
gzwarn << "Trying to playback unsupported message type ["
<< msgType << "]" << std::endl;
}
this->dataPtr->ReplaceResourceURIs(_ecm);
++iter;
}
// particle emitters
_ecm.Each<components::ParticleEmitterCmd>(
[&](const Entity &_entity,
const components::ParticleEmitterCmd *_emitter) -> bool
{
if (this->dataPtr->prevParticleEmitterCmds.find(_entity) ==
this->dataPtr->prevParticleEmitterCmds.end())
{
this->dataPtr->prevParticleEmitterCmds[_entity]
= _emitter->Data().emitting().data();
return true;
}
if (this->dataPtr->prevParticleEmitterCmds[_entity] !=
_emitter->Data().emitting().data())
{
this->dataPtr->prevParticleEmitterCmds[_entity]
= _emitter->Data().emitting().data();
_ecm.SetChanged(_entity, components::ParticleEmitterCmd::typeId,
ComponentState::OneTimeChange);
}
return true;
});
// for seek back in time only
// remove entities that should not be present in the current time step
for (auto entity : entitiesToRemove)
{
_ecm.RequestRemoveEntity(entity);
}
// pause playback if end of log is reached
if (_info.simTime >= this->dataPtr->log->EndTime())
{
gzmsg << "End of log file reached. Time: " <<
std::chrono::duration_cast<std::chrono::seconds>(
this->dataPtr->log->EndTime()).count() << " seconds" << std::endl;
this->dataPtr->eventManager->Emit<events::Pause>(true);
}
}
GZ_ADD_PLUGIN(gz::sim::systems::LogPlayback,
gz::sim::System,
LogPlayback::ISystemConfigure,
LogPlayback::ISystemReset,
LogPlayback::ISystemUpdate)
GZ_ADD_PLUGIN_ALIAS(LogPlayback,
"gz::sim::systems::LogPlayback")
// TODO(CH3): Deprecated, remove on version 8
GZ_ADD_PLUGIN_ALIAS(LogPlayback,
"ignition::gazebo::systems::LogPlayback")