1 // replay.cxx - a system to record and replay FlightGear flights
3 // Written by Curtis Olson, started July 2003.
4 // Updated by Thorsten Brehm, September 2011 and November 2012.
6 // Copyright (C) 2003 Curtis L. Olson - http://www.flightgear.org/~curt
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
30 #include <simgear/constants.h>
31 #include <simgear/structure/exception.hxx>
32 #include <simgear/props/props_io.hxx>
33 #include <simgear/misc/gzcontainerfile.hxx>
34 #include <simgear/misc/sg_dir.hxx>
35 #include <simgear/misc/stdint.hxx>
36 #include <simgear/misc/strutils.hxx>
38 #include <Main/fg_props.hxx>
41 #include "flightrecorder.hxx"
45 using simgear::gzContainerReader;
46 using simgear::gzContainerWriter;
49 #define MY_SG_DEBUG SG_DEBUG
51 #define MY_SG_DEBUG SG_ALERT
54 /** Magic string to verify valid FG flight recorder tapes. */
55 static const char* const FlightRecorderFileMagic = "FlightGear Flight Recorder Tape";
57 namespace ReplayContainer
62 Header = 0, /**< Used for initial file header (fixed identification string). */
63 MetaData = 1, /**< XML data / properties with arbitrary data, such as description, aircraft type, ... */
64 Properties = 2, /**< XML data describing the recorded flight recorder properties.
65 Format is identical to flight recorder XML configuration. Also contains some
66 extra data to verify flight recorder consistency. */
67 RawData = 3 /**< Actual binary data blobs (the recorder's tape).
68 One "RawData" blob is used for each resolution. */
76 FGReplay::FGReplay() :
82 m_high_res_time(60.0),
83 m_medium_res_time(600.0),
84 m_low_res_time(3600.0),
85 m_medium_sample_rate(0.5), // medium term sample rate (sec)
86 m_long_sample_rate(5.0), // long term sample rate (sec)
87 m_pRecorder(new FGFlightRecorder("replay-config"))
104 * Clear all internal buffers.
109 while ( !short_term.empty() )
111 m_pRecorder->deleteRecord(short_term.front());
112 short_term.pop_front();
114 while ( !medium_term.empty() )
116 m_pRecorder->deleteRecord(medium_term.front());
117 medium_term.pop_front();
119 while ( !long_term.empty() )
121 m_pRecorder->deleteRecord(long_term.front());
122 long_term.pop_front();
124 while ( !recycler.empty() )
126 m_pRecorder->deleteRecord(recycler.front());
127 recycler.pop_front();
130 // clear messages belonging to old replay session
131 fgGetNode("/sim/replay/messages", 0, true)->removeChildren("msg", false);
135 * Initialize the data structures
141 disable_replay = fgGetNode("/sim/replay/disable", true);
142 replay_master = fgGetNode("/sim/replay/replay-state", true);
143 replay_time = fgGetNode("/sim/replay/time", true);
144 replay_time_str = fgGetNode("/sim/replay/time-str", true);
145 replay_looped = fgGetNode("/sim/replay/looped", true);
146 speed_up = fgGetNode("/sim/speed-up", true);
148 // alias to keep backward compatibility
149 fgGetNode("/sim/freeze/replay-state", true)->alias(replay_master);
155 * Reset replay queues.
168 m_pRecorder->reinit();
170 m_high_res_time = fgGetDouble("/sim/replay/buffer/high-res-time", 60.0);
171 m_medium_res_time = fgGetDouble("/sim/replay/buffer/medium-res-time", 600.0); // 10 mins
172 m_low_res_time = fgGetDouble("/sim/replay/buffer/low-res-time", 3600.0); // 1 h
173 // short term sample rate is as every frame
174 m_medium_sample_rate = fgGetDouble("/sim/replay/buffer/medium-res-sample-dt", 0.5); // medium term sample rate (sec)
175 m_long_sample_rate = fgGetDouble("/sim/replay/buffer/low-res-sample-dt", 5.0); // long term sample rate (sec)
180 replay_master->setIntValue(0);
181 disable_replay->setBoolValue(0);
182 replay_time->setDoubleValue(0);
183 replay_time_str->setStringValue("");
187 * Bind to the property tree
196 * Unbind from the property tree
206 FGReplay::fillRecycler()
208 // Create an estimated nr of required ReplayData objects
209 // 120 is an estimated maximum frame rate.
210 int estNrObjects = (int) ((m_high_res_time*120) + (m_medium_res_time*m_medium_sample_rate) +
211 (m_low_res_time*m_long_sample_rate));
212 for (int i = 0; i < estNrObjects; i++)
214 FGReplayData* r = m_pRecorder->createEmptyRecord();
216 recycler.push_back(r);
219 SG_LOG(SG_SYSTEMS, SG_ALERT, "ReplaySystem: Out of memory!");
225 printTimeStr(char* pStrBuffer,double _Time, bool ShowDecimal=true)
229 unsigned int Time = _Time*10;
230 unsigned int h = Time/36000;
231 unsigned int m = (Time % 36000)/600;
232 unsigned int s = (Time % 600)/10;
233 unsigned int d = Time % 10;
237 len = sprintf(pStrBuffer,"%u:%02u:%02u",h,m,s);
239 len = sprintf(pStrBuffer,"%u:%02u",m,s);
248 sprintf(&pStrBuffer[len],".%u",d);
252 FGReplay::guiMessage(const char* message)
254 fgSetString("/sim/messages/copilot", message);
258 FGReplay::loadMessages()
261 replay_messages.clear();
262 simgear::PropertyList msgs = fgGetNode("/sim/replay/messages", true)->getChildren("msg");
264 for (simgear::PropertyList::iterator it = msgs.begin();it != msgs.end();++it)
266 const char* msgText = (*it)->getStringValue("text", "");
267 const double msgTime = (*it)->getDoubleValue("time", -1.0);
268 const char* msgSpeaker = (*it)->getStringValue("speaker", "pilot");
269 if ((msgText[0] != 0)&&(msgTime >= 0))
271 FGReplayMessages data;
272 data.sim_time = msgTime;
273 data.message = msgText;
274 data.speaker = msgSpeaker;
275 replay_messages.push_back(data);
278 current_msg = replay_messages.begin();
282 FGReplay::replayMessage(double time)
284 if (time < last_msg_time)
286 current_msg = replay_messages.begin();
289 // check if messages have to be triggered
290 while ((current_msg != replay_messages.end())&&
291 (time >= current_msg->sim_time))
293 // don't trigger messages when too long ago (fast-forward/skipped replay)
294 if (time - current_msg->sim_time < 3.0)
296 fgGetNode("/sim/messages", true)->getNode(current_msg->speaker, true)->setStringValue(current_msg->message);
300 last_msg_time = time;
303 /** Start replay session
306 FGReplay::start(bool NewTape)
308 // freeze the fdm, resume from sim pause
309 double StartTime = get_start_time();
310 double EndTime = get_end_time();
311 was_finished_already = false;
312 fgSetDouble("/sim/replay/start-time", StartTime);
313 fgSetDouble("/sim/replay/end-time", EndTime);
315 printTimeStr(StrBuffer,StartTime,false);
316 fgSetString("/sim/replay/start-time-str", StrBuffer);
317 printTimeStr(StrBuffer,EndTime,false);
318 fgSetString("/sim/replay/end-time-str", StrBuffer);
320 unsigned long buffer_elements = short_term.size()+medium_term.size()+long_term.size();
321 fgSetDouble("/sim/replay/buffer-size-mbyte",
322 buffer_elements*m_pRecorder->getRecordSize() / (1024*1024.0));
323 if ((fgGetBool("/sim/freeze/master"))||
324 (0 == replay_master->getIntValue()))
325 guiMessage("Replay active. 'Esc' to stop.");
326 fgSetBool ("/sim/freeze/master", 0);
327 fgSetBool ("/sim/freeze/clock", 0);
328 if (0 == replay_master->getIntValue())
330 replay_master->setIntValue(1);
333 // start replay at initial time, when loading a new tape
334 replay_time->setDoubleValue(StartTime);
338 // start replay at "loop interval" when starting instant replay
339 replay_time->setDoubleValue(-1);
341 replay_time_str->setStringValue("");
349 * Update the saved data
353 FGReplay::update( double dt )
355 int current_replay_state = last_replay_state;
359 if ( disable_replay->getBoolValue() )
361 if (fgGetBool("/sim/freeze/master",false)||
362 fgGetBool("/sim/freeze/clock",false))
364 // unpause - disable the replay system in next loop
365 fgSetBool("/sim/freeze/master",false);
366 fgSetBool("/sim/freeze/clock",false);
367 last_replay_state = 1;
370 if ((replay_master->getIntValue() != 3)||
371 (last_replay_state == 3))
373 // disable the replay system
374 current_replay_state = replay_master->getIntValue();
375 replay_master->setIntValue(0);
376 replay_time->setDoubleValue(0);
377 replay_time_str->setStringValue("");
378 disable_replay->setBoolValue(0);
379 speed_up->setDoubleValue(1.0);
380 speed_up->setDoubleValue(1.0);
381 if (fgGetBool("/sim/replay/mute",false))
383 fgSetBool("/sim/sound/enabled",true);
384 fgSetBool("/sim/replay/mute",false);
386 guiMessage("Replay stopped. Your controls!");
390 int replay_state = replay_master->getIntValue();
391 if ((replay_state == 0)&&
392 (last_replay_state > 0))
394 if (current_replay_state == 3)
396 // take control at current replay position ("My controls!").
397 // May need to uncrash the aircraft here :)
398 fgSetBool("/sim/crashed", false);
402 // normal replay exit, restore most recent frame
406 // replay is finished
407 last_replay_state = replay_state;
411 // remember recent state
412 last_replay_state = replay_state;
417 // replay inactive, keep recording
419 case 1: // normal replay
420 case 3: // prepare to resume normal flight at current replay position
423 double current_time = replay_time->getDoubleValue();
424 bool ResetTime = (current_time<=0.0);
427 // initialize start time
428 double startTime = get_start_time();
429 double endTime = get_end_time();
430 fgSetDouble( "/sim/replay/start-time", startTime );
431 fgSetDouble( "/sim/replay/end-time", endTime );
433 if (replay_looped->getBoolValue())
434 fgGetDouble("/sim/replay/duration");
435 if( duration && (duration < (endTime - startTime)) ) {
436 current_time = endTime - duration;
438 current_time = startTime;
441 bool IsFinished = replay( replay_time->getDoubleValue() );
444 if (!was_finished_already)
446 guiMessage("End of tape. 'Esc' to return.");
447 was_finished_already = true;
449 current_time = (replay_looped->getBoolValue()) ? -1 : get_end_time()+0.01;
453 current_time += dt * speed_up->getDoubleValue();
454 was_finished_already = false;
456 replay_time->setDoubleValue(current_time);
458 printTimeStr(StrBuffer,current_time);
459 replay_time_str->setStringValue((const char*)StrBuffer);
461 // when time skipped (looped replay), trigger listeners to reset views etc
463 replay_master->setIntValue(replay_state);
465 return; // don't record the replay session
467 case 2: // normal replay operation
468 return; // don't record the replay session
470 throw sg_range_exception("unknown FGReplay state");
475 sim_time += dt * speed_up->getDoubleValue();
477 // sanity check, don't collect data if FDM data isn't good
478 if (!fgGetBool("/sim/fdm-initialized", false)) {
482 FGReplayData* r = record(sim_time);
485 SG_LOG(SG_SYSTEMS, SG_ALERT, "ReplaySystem: Out of memory!");
489 // update the short term list
490 short_term.push_back( r );
491 FGReplayData *st_front = short_term.front();
495 SG_LOG(SG_SYSTEMS, SG_ALERT, "ReplaySystem: Inconsistent data!");
498 if ( sim_time - st_front->sim_time > m_high_res_time )
500 while ( sim_time - st_front->sim_time > m_high_res_time )
502 st_front = short_term.front();
503 recycler.push_back(st_front);
504 short_term.pop_front();
507 // update the medium term list
508 if ( sim_time - last_mt_time > m_medium_sample_rate )
510 last_mt_time = sim_time;
511 st_front = short_term.front();
512 medium_term.push_back( st_front );
513 short_term.pop_front();
515 FGReplayData *mt_front = medium_term.front();
516 if ( sim_time - mt_front->sim_time > m_medium_res_time )
518 while ( sim_time - mt_front->sim_time > m_medium_res_time )
520 mt_front = medium_term.front();
521 recycler.push_back(mt_front);
522 medium_term.pop_front();
524 // update the long term list
525 if ( sim_time - last_lt_time > m_long_sample_rate )
527 last_lt_time = sim_time;
528 mt_front = medium_term.front();
529 long_term.push_back( mt_front );
530 medium_term.pop_front();
532 FGReplayData *lt_front = long_term.front();
533 if ( sim_time - lt_front->sim_time > m_low_res_time )
535 while ( sim_time - lt_front->sim_time > m_low_res_time )
537 lt_front = long_term.front();
538 recycler.push_back(lt_front);
539 long_term.pop_front();
548 cout << "short term size = " << short_term.size()
549 << " time = " << sim_time - short_term.front().sim_time
551 cout << "medium term size = " << medium_term.size()
552 << " time = " << sim_time - medium_term.front().sim_time
554 cout << "long term size = " << long_term.size()
555 << " time = " << sim_time - long_term.front().sim_time
558 //stamp("point_finished");
562 FGReplay::record(double time)
564 FGReplayData* r = NULL;
568 r = recycler.front();
569 recycler.pop_front();
572 return m_pRecorder->capture(time, r);
576 * interpolate a specific time from a specific list
579 FGReplay::interpolate( double time, const replay_list_type &list)
582 if ( list.size() == 0 )
586 } else if ( list.size() == 1 )
588 // handle list size == 1
589 replay(time, list[0]);
593 unsigned int last = list.size() - 1;
594 unsigned int first = 0;
595 unsigned int mid = ( last + first ) / 2;
600 // cout << " " << first << " <=> " << last << endl;
601 if ( last == first ) {
603 } else if ( list[mid]->sim_time < time && list[mid+1]->sim_time < time ) {
606 mid = ( last + first ) / 2;
607 } else if ( list[mid]->sim_time > time && list[mid+1]->sim_time > time ) {
610 mid = ( last + first ) / 2;
616 replay(time, list[mid+1], list[mid]);
620 * Replay a saved frame based on time, interpolate from the two
621 * nearest saved frames.
622 * Returns true when replay sequence has finished, false otherwise.
626 FGReplay::replay( double time ) {
627 // cout << "replay: " << time << " ";
628 // find the two frames to interpolate between
633 if ( short_term.size() > 0 ) {
634 t1 = short_term.back()->sim_time;
635 t2 = short_term.front()->sim_time;
637 // replay the most recent frame
638 replay( time, short_term.back() );
639 // replay is finished now
641 } else if ( time <= t1 && time >= t2 ) {
642 interpolate( time, short_term );
643 } else if ( medium_term.size() > 0 ) {
644 t1 = short_term.front()->sim_time;
645 t2 = medium_term.back()->sim_time;
646 if ( time <= t1 && time >= t2 )
648 replay(time, medium_term.back(), short_term.front());
650 t1 = medium_term.back()->sim_time;
651 t2 = medium_term.front()->sim_time;
652 if ( time <= t1 && time >= t2 ) {
653 interpolate( time, medium_term );
654 } else if ( long_term.size() > 0 ) {
655 t1 = medium_term.front()->sim_time;
656 t2 = long_term.back()->sim_time;
657 if ( time <= t1 && time >= t2 )
659 replay(time, long_term.back(), medium_term.front());
661 t1 = long_term.back()->sim_time;
662 t2 = long_term.front()->sim_time;
663 if ( time <= t1 && time >= t2 ) {
664 interpolate( time, long_term );
666 // replay the oldest long term frame
667 replay(time, long_term.front());
671 // replay the oldest medium term frame
672 replay(time, medium_term.front());
676 // replay the oldest short term frame
677 replay(time, short_term.front());
687 * given two FGReplayData elements and a time, interpolate between them
690 FGReplay::replay(double time, FGReplayData* pCurrentFrame, FGReplayData* pOldFrame)
692 m_pRecorder->replay(time,pCurrentFrame,pOldFrame);
696 FGReplay::get_start_time()
698 if ( long_term.size() > 0 )
700 return long_term.front()->sim_time;
701 } else if ( medium_term.size() > 0 )
703 return medium_term.front()->sim_time;
704 } else if ( short_term.size() )
706 return short_term.front()->sim_time;
714 FGReplay::get_end_time()
716 if ( short_term.size() )
718 return short_term.back()->sim_time;
725 /** Save raw replay data in a separate container */
727 saveRawReplayData(gzContainerWriter& output, const replay_list_type& ReplayData, size_t RecordSize)
729 // get number of records in this stream
730 size_t Count = ReplayData.size();
732 // write container header for raw data
733 if (!output.writeContainerHeader(ReplayContainer::RawData, Count * RecordSize))
735 SG_LOG(SG_SYSTEMS, SG_ALERT, "Failed to save replay data. Cannot write data container. Disk full?");
739 // write the raw data (all records in the given list)
740 replay_list_type::const_iterator it = ReplayData.begin();
741 size_t CheckCount = 0;
742 while ((it != ReplayData.end())&&
745 const FGReplayData* pRecord = *it++;
746 output.write((char*)pRecord, RecordSize);
750 // Did we really write as much as we intended?
751 if (CheckCount != Count)
753 SG_LOG(SG_SYSTEMS, SG_ALERT, "Failed to save replay data. Expected to write " << Count << " records, but wrote " << CheckCount);
757 SG_LOG(SG_SYSTEMS, MY_SG_DEBUG, "Saved " << CheckCount << " records of size " << RecordSize);
758 return !output.fail();
761 /** Load raw replay data from a separate container */
763 loadRawReplayData(gzContainerReader& input, FGFlightRecorder* pRecorder, replay_list_type& ReplayData, size_t RecordSize)
766 simgear::ContainerType Type = ReplayContainer::Invalid;
768 // write container header for raw data
769 if (!input.readContainerHeader(&Type, &Size))
771 SG_LOG(SG_SYSTEMS, SG_ALERT, "Failed to load replay data. Missing data container.");
775 if (Type != ReplayContainer::RawData)
777 SG_LOG(SG_SYSTEMS, SG_ALERT, "Failed to load replay data. Expected data container, got " << Type);
782 size_t Count = Size / RecordSize;
783 SG_LOG(SG_SYSTEMS, MY_SG_DEBUG, "Loading replay data. Container size is " << Size << ", record size " << RecordSize <<
784 ", expected record count " << Count << ".");
786 size_t CheckCount = 0;
787 for (CheckCount=0; (CheckCount<Count)&&(!input.eof()); ++CheckCount)
789 FGReplayData* pBuffer = pRecorder->createEmptyRecord();
790 input.read((char*) pBuffer, RecordSize);
791 ReplayData.push_back(pBuffer);
794 // did we get all we have hoped for?
795 if (CheckCount != Count)
799 SG_LOG(SG_SYSTEMS, SG_ALERT, "Unexpected end of file.");
801 SG_LOG(SG_SYSTEMS, SG_ALERT, "Failed to load replay data. Expected " << Count << " records, but got " << CheckCount);
805 SG_LOG(SG_SYSTEMS, MY_SG_DEBUG, "Loaded " << CheckCount << " records of size " << RecordSize);
809 /** Write flight recorder tape with given filename and meta properties to disk */
811 FGReplay::saveTape(const char* Filename, SGPropertyNode* MetaDataProps)
815 /* open output stream *******************************************/
816 gzContainerWriter output(Filename, FlightRecorderFileMagic);
819 SG_LOG(SG_SYSTEMS, SG_ALERT, "Cannot open file" << Filename);
823 /* write meta data **********************************************/
824 ok &= output.writeContainer(ReplayContainer::MetaData, MetaDataProps);
826 /* write flight recorder configuration **************************/
827 SGPropertyNode_ptr Config;
830 Config = new SGPropertyNode();
831 m_pRecorder->getConfig(Config.get());
832 ok &= output.writeContainer(ReplayContainer::Properties, Config.get());
835 /* write raw data ***********************************************/
838 size_t RecordSize = Config->getIntValue("recorder/record-size", 0);
839 SG_LOG(SG_SYSTEMS, MY_SG_DEBUG, "Total signal count: " << Config->getIntValue("recorder/signal-count", 0)
840 << ", record size: " << RecordSize);
842 ok &= saveRawReplayData(output, short_term, RecordSize);
844 ok &= saveRawReplayData(output, medium_term, RecordSize);
846 ok &= saveRawReplayData(output, long_term, RecordSize);
850 /* done *********************************************************/
856 /** Write flight recorder tape to disk. User/script command. */
858 FGReplay::saveTape(const SGPropertyNode* ConfigData)
860 const char* tapeDirectory = fgGetString("/sim/replay/tape-directory", "");
861 const char* aircraftType = fgGetString("/sim/aircraft", "unknown");
863 SGPropertyNode_ptr myMetaData = new SGPropertyNode();
864 SGPropertyNode* meta = myMetaData->getNode("meta", 0, true);
866 // add some data to the file - so we know for which aircraft/version it was recorded
867 meta->setStringValue("aircraft-type", aircraftType);
868 meta->setStringValue("aircraft-description", fgGetString("/sim/description", ""));
869 meta->setStringValue("aircraft-fdm", fgGetString("/sim/flight-model", ""));
870 meta->setStringValue("closest-airport-id", fgGetString("/sim/airport/closest-airport-id", ""));
871 const char* aircraft_version = fgGetString("/sim/aircraft-version", "");
872 if (aircraft_version[0]==0)
873 aircraft_version = "(unknown aircraft version)";
874 meta->setStringValue("aircraft-version", aircraft_version);
876 // add information on the tape's recording duration
877 double Duration = get_end_time()-get_start_time();
878 meta->setDoubleValue("tape-duration", Duration);
880 printTimeStr(StrBuffer, Duration, false);
881 meta->setStringValue("tape-duration-str", StrBuffer);
883 // add simulator version
884 copyProperties(fgGetNode("/sim/version", 0, true), meta->getNode("version", 0, true));
885 if (ConfigData->getNode("user-data"))
887 copyProperties(ConfigData->getNode("user-data"), meta->getNode("user-data", 0, true));
890 // store replay messages
891 copyProperties(fgGetNode("/sim/replay/messages", 0, true), myMetaData->getNode("messages", 0, true));
893 // generate file name (directory + aircraft type + date + time + suffix)
894 SGPath p(tapeDirectory);
895 p.append(aircraftType);
897 time_t calendar_time = time(NULL);
899 local_tm = localtime( &calendar_time );
901 strftime( time_str, 256, "%Y%02m%02d-%02H%02M%02S", local_tm);
906 // make sure we're not overwriting something
910 SG_LOG(SG_SYSTEMS, SG_ALERT, "Error, flight recorder tape file with same name already exists.");
915 ok &= saveTape(p.c_str(), myMetaData.get());
918 guiMessage("Flight recorder tape saved successfully!");
920 guiMessage("Failed to save tape! See log output.");
925 /** Read a flight recorder tape with given filename from disk and return meta properties.
926 * Actual data and signal configuration is not read when in "Preview" mode.
929 FGReplay::loadTape(const char* Filename, bool Preview, SGPropertyNode* UserData)
933 /* open input stream ********************************************/
934 gzContainerReader input(Filename, FlightRecorderFileMagic);
935 if (input.eof() || !input.good())
937 SG_LOG(SG_SYSTEMS, SG_ALERT, "Cannot open file " << Filename);
941 SGPropertyNode_ptr MetaDataProps = new SGPropertyNode();
943 /* read meta data ***********************************************/
946 char* MetaData = NULL;
948 simgear::ContainerType Type = ReplayContainer::Invalid;
949 if (!input.readContainer(&Type, &MetaData, &Size) || Size<1)
951 SG_LOG(SG_SYSTEMS, SG_ALERT, "File not recognized. This is not a valid FlightGear flight recorder tape: " << Filename
952 << ". Invalid meta data.");
956 if (Type != ReplayContainer::MetaData)
958 SG_LOG(SG_SYSTEMS, MY_SG_DEBUG, "Invalid header. Container type " << Type);
959 SG_LOG(SG_SYSTEMS, SG_ALERT, "File not recognized. This is not a valid FlightGear flight recorder tape: " << Filename);
966 readProperties(MetaData, Size-1, MetaDataProps);
967 copyProperties(MetaDataProps->getNode("meta", 0, true), UserData);
968 } catch (const sg_exception &e)
970 SG_LOG(SG_SYSTEMS, SG_ALERT, "Error reading flight recorder tape: " << Filename
971 << ", XML parser message:" << e.getFormattedMessage());
978 //printf("%s\n", MetaData);
984 /* read flight recorder configuration **************************/
985 if ((ok)&&(!Preview))
987 SG_LOG(SG_SYSTEMS, MY_SG_DEBUG, "Loading flight recorder data...");
988 char* ConfigXML = NULL;
990 simgear::ContainerType Type = ReplayContainer::Invalid;
991 if (!input.readContainer(&Type, &ConfigXML, &Size) || Size<1)
993 SG_LOG(SG_SYSTEMS, SG_ALERT, "File not recognized. This is not a valid FlightGear flight recorder tape: " << Filename
994 << ". Invalid configuration container.");
998 if ((!ConfigXML)||(Type != ReplayContainer::Properties))
1000 SG_LOG(SG_SYSTEMS, SG_ALERT, "File not recognized. This is not a valid FlightGear flight recorder tape: " << Filename
1001 << ". Unexpected container type, expected \"properties\".");
1005 SGPropertyNode_ptr Config = new SGPropertyNode();
1010 readProperties(ConfigXML, Size-1, Config);
1011 } catch (const sg_exception &e)
1013 SG_LOG(SG_SYSTEMS, SG_ALERT, "Error reading flight recorder tape: " << Filename
1014 << ", XML parser message:" << e.getFormattedMessage());
1019 // reconfigure the recorder - and wipe old data (no longer matches the current recorder)
1020 m_pRecorder->reinit(Config);
1032 /* read raw data ***********************************************/
1035 size_t RecordSize = m_pRecorder->getRecordSize();
1036 size_t OriginalSize = Config->getIntValue("recorder/record-size", 0);
1037 // check consistency - ugly things happen when data vs signals mismatch
1038 if ((OriginalSize != RecordSize)&&
1039 (OriginalSize != 0))
1042 SG_LOG(SG_SYSTEMS, SG_ALERT, "Error: Data inconsistency. Flight recorder tape has record size " << RecordSize
1043 << ", expected size was " << OriginalSize << ".");
1047 ok &= loadRawReplayData(input, m_pRecorder, short_term, RecordSize);
1049 ok &= loadRawReplayData(input, m_pRecorder, medium_term, RecordSize);
1051 ok &= loadRawReplayData(input, m_pRecorder, long_term, RecordSize);
1053 // restore replay messages
1056 copyProperties(MetaDataProps->getNode("messages", 0, true),
1057 fgGetNode("/sim/replay/messages", 0, true));
1059 sim_time = get_end_time();
1060 // TODO we could (re)store these too
1061 last_mt_time = last_lt_time = sim_time;
1063 /* done *********************************************************/
1072 guiMessage("Flight recorder tape loaded successfully!");
1076 guiMessage("Failed to load tape. See log output.");
1082 /** List available tapes in current directory.
1083 * Limits to tapes matching current aircraft when SameAircraftFilter is enabled.
1086 FGReplay::listTapes(bool SameAircraftFilter, const SGPath& tapeDirectory)
1088 const std::string& aircraftType = simgear::strutils::uppercase(fgGetString("/sim/aircraft", "unknown"));
1090 // process directory listing of ".fgtape" files
1091 simgear::Dir dir(tapeDirectory);
1092 simgear::PathList list = dir.children(simgear::Dir::TYPE_FILE, ".fgtape");
1094 SGPropertyNode* TapeList = fgGetNode("/sim/replay/tape-list", true);
1095 TapeList->removeChildren("tape", false);
1097 size_t l = aircraftType.size();
1098 for (simgear::PathList::iterator it = list.begin(); it!=list.end(); ++it)
1100 SGPath file(it->file());
1101 std::string name(file.base());
1102 if ((!SameAircraftFilter)||
1103 (0==simgear::strutils::uppercase(name).compare(0,l, aircraftType)))
1105 TapeList->getNode("tape", Index++, true)->setStringValue(name);
1112 /** Load a flight recorder tape from disk. User/script command. */
1114 FGReplay::loadTape(const SGPropertyNode* ConfigData)
1116 SGPath tapeDirectory(fgGetString("/sim/replay/tape-directory", ""));
1118 // see if shall really load the file - or just obtain the meta data for preview
1119 bool Preview = ConfigData->getBoolValue("preview", 0);
1121 // file/tape to be loaded
1122 std::string tape = ConfigData->getStringValue("tape", "");
1127 return listTapes(ConfigData->getBoolValue("same-aircraft", 0), tapeDirectory);
1132 SGPropertyNode* UserData = fgGetNode("/sim/gui/dialogs/flightrecorder/preview", true);
1133 tapeDirectory.append(tape);
1134 tapeDirectory.concat(".fgtape");
1135 SG_LOG(SG_SYSTEMS, MY_SG_DEBUG, "Checking flight recorder file " << tapeDirectory << ", preview: " << Preview);
1136 return loadTape(tapeDirectory.c_str(), Preview, UserData);