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