]> git.sesse.net Git - nageru/blob - main.cpp
Add some very basic playback.
[nageru] / main.cpp
1 #include <assert.h>
2 #include <stdio.h>
3 #include <stdint.h>
4
5 #include <chrono>
6 #include <condition_variable>
7 #include <memory>
8 #include <mutex>
9 #include <string>
10 #include <thread>
11 #include <vector>
12
13 extern "C" {
14 #include <libavformat/avformat.h>
15 }
16
17 #include <QApplication>
18
19 #include "clip_list.h"
20 #include "defs.h"
21 #include "mainwindow.h"
22 #include "ffmpeg_raii.h"
23 #include "player.h"
24 #include "post_to_main_thread.h"
25 #include "ui_mainwindow.h"
26
27 using namespace std;
28 using namespace std::chrono;
29
30 // TODO: Replace by some sort of GUI control, I guess.
31 int64_t current_pts = 0;
32
33 string filename_for_frame(unsigned stream_idx, int64_t pts)
34 {
35         char filename[256];
36         snprintf(filename, sizeof(filename), "frames/cam%d-pts%09ld.jpeg", stream_idx, pts);
37         return filename;
38 }
39
40 mutex frame_mu;
41 vector<int64_t> frames[MAX_STREAMS];
42
43 int record_thread_func();
44
45 int main(int argc, char **argv)
46 {
47         av_register_all();
48         avformat_network_init();
49
50         QApplication app(argc, argv);
51         MainWindow mainWindow;
52         mainWindow.show();
53
54         thread(record_thread_func).detach();
55         start_player_thread();
56
57         return app.exec();
58 }
59
60 int record_thread_func()
61 {
62         auto format_ctx = avformat_open_input_unique("multiangle.mp4", nullptr, nullptr);
63         if (format_ctx == nullptr) {
64                 fprintf(stderr, "%s: Error opening file\n", "example.mp4");
65                 return 1;
66         }
67
68         int64_t last_pts = -1;
69
70         for ( ;; ) {
71                 AVPacket pkt;
72                 unique_ptr<AVPacket, decltype(av_packet_unref)*> pkt_cleanup(
73                         &pkt, av_packet_unref);
74                 av_init_packet(&pkt);
75                 pkt.data = nullptr;
76                 pkt.size = 0;
77                 if (av_read_frame(format_ctx.get(), &pkt) != 0) {
78                         break;
79                 }
80                 fprintf(stderr, "Got a frame from camera %d, pts = %ld, size = %d\n",
81                         pkt.stream_index, pkt.pts, pkt.size);
82                 string filename = filename_for_frame(pkt.stream_index, pkt.pts);
83                 FILE *fp = fopen(filename.c_str(), "wb");
84                 if (fp == nullptr) {
85                         perror(filename.c_str());
86                         exit(1);
87                 }
88                 fwrite(pkt.data, pkt.size, 1, fp);
89                 fclose(fp);
90
91                 post_to_main_thread([pkt] {
92                         if (pkt.stream_index == 0) {
93                                 global_mainwindow->ui->input1_display->setFrame(pkt.stream_index, pkt.pts);
94                         } else if (pkt.stream_index == 1) {
95                                 global_mainwindow->ui->input2_display->setFrame(pkt.stream_index, pkt.pts);
96                         } else if (pkt.stream_index == 2) {
97                                 global_mainwindow->ui->input3_display->setFrame(pkt.stream_index, pkt.pts);
98                         }
99                 });
100
101                 assert(pkt.stream_index < MAX_STREAMS);
102                 frames[pkt.stream_index].push_back(pkt.pts);
103
104                 // Hack. Assumes a given timebase.
105                 if (last_pts != -1) {
106                         this_thread::sleep_for(microseconds((pkt.pts - last_pts) * 1000000 / 12800));
107                 }
108                 last_pts = pkt.pts;
109                 current_pts = pkt.pts;
110         }
111
112         return 0;
113 }