-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
191 lines (151 loc) · 5.79 KB
/
main.cpp
File metadata and controls
191 lines (151 loc) · 5.79 KB
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
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <cstring>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <chrono>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
}
#include <opencv2/opencv.hpp>
using namespace cv;
void display_stream() {
AVCodecContext *codec_ctx;
const AVCodec *codec;
AVFrame *frame;
AVPacket *pkt;
SwsContext *sws_ctx;
// Initialize AVCodec decoder
// TODO: Try NV12 again
if ((codec = avcodec_find_decoder_by_name("h264_cuvid"))) {
std::cout << "Using Nvidia CUVID decoder" << std::endl;
} else if ((codec = avcodec_find_decoder_by_name("h264_qsv"))) {
std::cout << "Using Intel QSV decoder" << std::endl;
} else if ((codec = avcodec_find_decoder(AV_CODEC_ID_H264))) {
std::cout << "Using default software encoder" << std::endl;
} else {
std::cerr << "H.264 Codec not found" << std::endl;
return;
}
codec_ctx = avcodec_alloc_context3(codec);
if (!codec_ctx) {
std::cerr << "Could not allocate codec context" << std::endl;
return;
}
codec_ctx->pkt_timebase = {1, 90000};
if (avcodec_open2(codec_ctx, codec, nullptr) < 0) {
std::cerr << "Could not open codec" << std::endl;
return;
}
frame = av_frame_alloc();
pkt = av_packet_alloc();
constexpr int PORT = 12345;
int sockfd;
sockaddr_in servaddr{}, cliaddr{};
// Creating socket file descriptor
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
std::cerr << "Socket creation failed" << std::endl;
exit(EXIT_FAILURE);
}
memset(&servaddr, 0, sizeof(servaddr));
memset(&cliaddr, 0, sizeof(cliaddr));
// Filling server information
servaddr.sin_family = AF_INET; // IPv4
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(PORT);
// Bind the socket with the server address
if (bind(sockfd, reinterpret_cast<const sockaddr *>(&servaddr), sizeof(servaddr)) < 0) {
std::cerr << "Bind failed" << std::endl;
exit(EXIT_FAILURE);
}
const std::string window_name = "Stream";
startWindowThread();
namedWindow(window_name, WINDOW_NORMAL);
socklen_t len = sizeof(cliaddr); //len is value/result
uint64_t im_received = 0;
AVPixelFormat pix_fmt;
int width = 640, height = 480;
Mat img(width, height, CV_8UC3);
uint8_t *dest[4] = {img.data, nullptr, nullptr, nullptr};
int dest_linesize[4] = {static_cast<int>(img.step1()), 0, 0, 0};
std::chrono::time_point<std::chrono::high_resolution_clock> current_time;
auto prev_time = std::chrono::high_resolution_clock::now();
long elapsed;
float fps;
std::stringstream fps_text;
constexpr int MAXLINE = 56321;
char buffer[MAXLINE];
ssize_t buffer_len;
while (true) {
buffer_len = recvfrom(sockfd, buffer, MAXLINE, MSG_WAITALL, reinterpret_cast<sockaddr *>(&cliaddr), &len);
if (buffer_len < 0) {
std::cerr << "recvfrom failed" << std::endl;
continue;
}
++im_received;
pkt->data = reinterpret_cast<uint8_t *>(buffer);
pkt->size = buffer_len;
pkt->pts = AV_NOPTS_VALUE;
pkt->dts = AV_NOPTS_VALUE;
int ret = avcodec_send_packet(codec_ctx, pkt);
if (ret) {
char errstr[200];
av_strerror(ret, errstr, sizeof(errstr));
std::cout << "Failed to send packet to avcodec: " << errstr << std::endl;
}
while (avcodec_receive_frame(codec_ctx, frame) == 0) {
width = frame->width;
height = frame->height;
pix_fmt = static_cast<AVPixelFormat>(frame->format);
if (img.cols != width || img.rows != height) {
img = Mat(height, width, CV_8UC3);
}
if (!sws_ctx || frame->width != width || frame->height != height) {
if (sws_ctx) {
sws_freeContext(sws_ctx);
}
std::cout << "dim = " << width << 'x' << height << " format = " << pix_fmt << std::endl;
sws_ctx = sws_getContext(width, height, pix_fmt, width, height,
AV_PIX_FMT_BGR24, SWS_FAST_BILINEAR, nullptr, nullptr, nullptr);
if (!sws_ctx) {
std::cerr << "Could not initialize sws context" << std::endl;
av_frame_free(&frame);
av_packet_free(&pkt);
close(sockfd);
return;
}
}
dest[0] = img.data;
dest_linesize[0] = static_cast<int>(img.step1());
sws_scale(sws_ctx, frame->data, frame->linesize, 0, height, dest, dest_linesize);
// std::cout << "Showing frame " << width << 'x' << height << std::endl;
current_time = std::chrono::high_resolution_clock::now();
elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(current_time - prev_time).count();
fps = 1.f / (static_cast<float>(elapsed) / 1000.f);
if (im_received % 5 == 0) {
fps_text.str("");
fps_text << "FPS: " << roundf(fps);
}
putText(img, fps_text.str(), {0, img.rows - 5}, FONT_HERSHEY_SIMPLEX, 0.5, {0, 255, 0}, 1);
imshow(window_name, img);
if (waitKey(1) == 'q') {
av_frame_free(&frame);
av_packet_free(&pkt);
sws_freeContext(sws_ctx);
avcodec_free_context(&codec_ctx);
close(sockfd);
return;
}
prev_time = current_time;
}
}
}
int main() {
std::cout << "Hello, World!" << std::endl;
display_stream();
return 0;
}