+void load_existing_frames()
+{
+ DIR *dir = opendir("frames/");
+ if (dir == nullptr) {
+ perror("frames/");
+ start_pts = 0;
+ return;
+ }
+
+ for ( ;; ) {
+ errno = 0;
+ dirent *de = readdir(dir);
+ if (de == nullptr) {
+ if (errno != 0) {
+ perror("readdir");
+ exit(1);
+ }
+ break;
+ }
+
+ int stream_idx;
+ int64_t pts;
+ if (sscanf(de->d_name, "cam%d-pts%ld.jpeg", &stream_idx, &pts) == 2 &&
+ stream_idx >= 0 && stream_idx < MAX_STREAMS) {
+ frames[stream_idx].push_back(pts);
+ start_pts = max(start_pts, pts);
+ }
+ }
+
+ closedir(dir);
+
+ if (start_pts == -1) {
+ start_pts = 0;
+ } else {
+ // Add a gap of one second from the old frames to the new ones.
+ start_pts += TIMEBASE;
+ }
+
+ for (int stream_idx = 0; stream_idx < MAX_STREAMS; ++stream_idx) {
+ sort(frames[stream_idx].begin(), frames[stream_idx].end());
+ }
+}
+