-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfplay.cpp
More file actions
636 lines (500 loc) · 22.1 KB
/
fplay.cpp
File metadata and controls
636 lines (500 loc) · 22.1 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
/**
fplay.cpp is the ffplay wrapper for making it easier to consume media files.
fplay can parse ssh config for resolving remote hosts.
Supports local file playback and RTSP streams with player controls.
open source under the MIT license.
author: github.com/yocoholo
*/
#include "fplay.h"
using namespace fplay;
// ── Global state & helpers ──────────────────────────────────────────────────
std::atomic<bool> fplay::g_running{true};
void fplay::handle_sigint(int) {
g_running = false;
}
void fplay::sdl_panic(const std::string& msg) {
std::cerr << "SDL error: " << msg << " | " << SDL_GetError() << std::endl;
std::exit(1);
}
void fplay::ff_panic(const std::string& msg, int err) {
char errbuf[256];
av_strerror(err, errbuf, sizeof(errbuf));
std::cerr << "FFmpeg error: " << msg << " | " << errbuf << std::endl;
std::exit(1);
}
void fplay::sdl_audio_callback(void* userdata, Uint8* stream, int len) {
AudioState* a = static_cast<AudioState*>(userdata);
SDL_memset(stream, 0, len);
std::lock_guard<std::mutex> lock(a->mtx);
if (a->buffer.empty()) return;
int n = std::min<int>(len, static_cast<int>(a->buffer.size()));
SDL_MixAudioFormat(stream, a->buffer.data(), AUDIO_S16, n, SDL_MIX_MAXVOLUME);
a->buffer.erase(a->buffer.begin(), a->buffer.begin() + n);
}
// ── FPlayArgs ───────────────────────────────────────────────────────────────
std::string FPlayArgs::to_url() const {
if (is_file()) return file;
return "rtsp://" + ip + ":" + port + "/" + stream;
}
bool fplay::parse_args(int argc, char** argv, FPlayArgs& args) {
CLI::App app{"fplay - FFmpeg + SDL2 media player"};
// Positional argument: bare file path (e.g. fplay video.mkv)
app.add_option("file", args.file, "Local file path to play");
app.add_option("-i,--ip", args.ip, "IP address of RTSP server");
app.add_option("-p,--port", args.port, "RTSP port")->default_val("554");
app.add_option("-s,--stream", args.stream, "RTSP stream path");
app.set_version_flag("--version,-v",
"fplay version " + std::to_string(MAJOR_VER) + "." +
std::to_string(MINOR_VER) + "." + std::to_string(PATCH_VER));
try {
app.parse(argc, argv);
} catch (const CLI::ParseError& e) {
app.exit(e);
return false;
}
if (args.file.empty() && (args.ip.empty() || args.stream.empty())) {
std::cerr << "Usage: fplay <file> or fplay -i <ip> -s <stream>\n";
return false;
}
return true;
}
// ── Player construction / destruction ───────────────────────────────────────
Player::Player() {
last_mouse_move_ = std::chrono::steady_clock::now();
}
Player::~Player() {
cleanup();
}
// ── Player::open ────────────────────────────────────────────────────────────
bool Player::open(const std::string& source) {
if (!init_format(source)) return false;
if (!find_streams()) return false;
bool has_video = init_video_decoder();
bool has_audio = init_audio_decoder();
if (!has_video && !has_audio) {
std::cerr << "No audio or video streams found.\n";
return false;
}
if (!init_sdl()) return false;
if (has_video) {
if (!init_sdl_window(width_, height_, SDL_WINDOW_RESIZABLE)) return false;
} else {
// Audio-only: small control window
if (!init_sdl_window(AUDIO_ONLY_WIDTH, AUDIO_ONLY_HEIGHT, 0)) return false;
}
if (has_audio && !init_sdl_audio()) return false;
// Determine seekability
seekable_ = !(fmt_ctx_->ctx_flags & AVFMTCTX_NOHEADER) &&
fmt_ctx_->pb && (fmt_ctx_->pb->seekable & AVIO_SEEKABLE_NORMAL);
if (fmt_ctx_->duration != AV_NOPTS_VALUE)
duration_ = static_cast<double>(fmt_ctx_->duration) / AV_TIME_BASE;
play_state_ = PlayState::Playing;
return true;
}
// ── Initialisation helpers ──────────────────────────────────────────────────
bool Player::init_format(const std::string& source) {
av_log_set_level(AV_LOG_WARNING);
avformat_network_init();
AVDictionary* opts = nullptr;
if (source.rfind("rtsp://", 0) == 0) {
av_dict_set(&opts, "rtsp_transport", "tcp", 0);
av_dict_set(&opts, "stimeout", "5000000", 0);
av_dict_set(&opts, "fflags", "nobuffer", 0);
av_dict_set(&opts, "buffer_size", "102400", 0);
}
int ret = avformat_open_input(&fmt_ctx_, source.c_str(), nullptr, &opts);
av_dict_free(&opts);
if (ret < 0) ff_panic("avformat_open_input", ret);
ret = avformat_find_stream_info(fmt_ctx_, nullptr);
if (ret < 0) ff_panic("avformat_find_stream_info", ret);
return true;
}
bool Player::find_streams() {
video_idx_ = -1;
audio_idx_ = -1;
for (unsigned i = 0; i < fmt_ctx_->nb_streams; ++i) {
auto type = fmt_ctx_->streams[i]->codecpar->codec_type;
if (type == AVMEDIA_TYPE_VIDEO && video_idx_ < 0) video_idx_ = i;
else if (type == AVMEDIA_TYPE_AUDIO && audio_idx_ < 0) audio_idx_ = i;
}
return video_idx_ >= 0 || audio_idx_ >= 0;
}
bool Player::init_video_decoder() {
if (video_idx_ < 0) return false;
AVStream* vs = fmt_ctx_->streams[video_idx_];
const AVCodec* codec = avcodec_find_decoder(vs->codecpar->codec_id);
if (!codec) { std::cerr << "Video codec not found.\n"; return false; }
video_dec_ = avcodec_alloc_context3(codec);
avcodec_parameters_to_context(video_dec_, vs->codecpar);
video_dec_->thread_count = 2;
int ret = avcodec_open2(video_dec_, codec, nullptr);
if (ret < 0) ff_panic("avcodec_open2(video)", ret);
width_ = video_dec_->width;
height_ = video_dec_->height;
sws_ctx_ = sws_getContext(width_, height_, video_dec_->pix_fmt,
width_, height_, AV_PIX_FMT_YUV420P,
SWS_BILINEAR, nullptr, nullptr, nullptr);
if (!sws_ctx_) { std::cerr << "Failed to create sws context.\n"; return false; }
yuv_frame_ = av_frame_alloc();
yuv_frame_->format = AV_PIX_FMT_YUV420P;
yuv_frame_->width = width_;
yuv_frame_->height = height_;
ret = av_frame_get_buffer(yuv_frame_, 32);
if (ret < 0) ff_panic("av_frame_get_buffer(yuv)", ret);
return true;
}
bool Player::init_audio_decoder() {
if (audio_idx_ < 0) return false;
AVStream* as = fmt_ctx_->streams[audio_idx_];
const AVCodec* codec = avcodec_find_decoder(as->codecpar->codec_id);
if (!codec) { std::cerr << "Audio codec not found.\n"; return false; }
audio_dec_ = avcodec_alloc_context3(codec);
avcodec_parameters_to_context(audio_dec_, as->codecpar);
audio_dec_->request_sample_fmt = AV_SAMPLE_FMT_S16;
int ret = avcodec_open2(audio_dec_, codec, nullptr);
if (ret < 0) ff_panic("avcodec_open2(audio)", ret);
AVChannelLayout out_ch = AV_CHANNEL_LAYOUT_STEREO;
AVChannelLayout in_ch = audio_dec_->ch_layout;
if (in_ch.nb_channels == 0) in_ch = AV_CHANNEL_LAYOUT_STEREO;
swr_ctx_ = swr_alloc();
if (!swr_ctx_) ff_panic("swr_alloc", AVERROR(ENOMEM));
av_opt_set_chlayout(swr_ctx_, "in_chlayout", &in_ch, 0);
av_opt_set_chlayout(swr_ctx_, "out_chlayout", &out_ch, 0);
av_opt_set_int(swr_ctx_, "in_sample_rate", audio_dec_->sample_rate, 0);
av_opt_set_int(swr_ctx_, "out_sample_rate", audio_state_.dst_rate, 0);
av_opt_set_sample_fmt(swr_ctx_, "in_sample_fmt", audio_dec_->sample_fmt, 0);
av_opt_set_sample_fmt(swr_ctx_, "out_sample_fmt", audio_state_.dst_fmt, 0);
ret = swr_init(swr_ctx_);
if (ret < 0) ff_panic("swr_init", ret);
audio_state_.swr = swr_ctx_;
return true;
}
bool Player::init_sdl() {
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_EVENTS))
sdl_panic("SDL_Init");
return true;
}
bool Player::init_sdl_window(int w, int h, Uint32 flags) {
window_ = SDL_CreateWindow("fplay",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, flags);
if (!window_) { sdl_panic("CreateWindow"); return false; }
renderer_ = SDL_CreateRenderer(window_, -1,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (!renderer_) { sdl_panic("CreateRenderer"); return false; }
SDL_SetRenderDrawBlendMode(renderer_, SDL_BLENDMODE_BLEND);
if (video_dec_) {
texture_ = SDL_CreateTexture(renderer_, SDL_PIXELFORMAT_IYUV,
SDL_TEXTUREACCESS_STREAMING, width_, height_);
if (!texture_) { sdl_panic("CreateTexture"); return false; }
}
return true;
}
bool Player::init_sdl_audio() {
SDL_AudioSpec want{};
want.freq = audio_state_.dst_rate;
want.format = AUDIO_S16;
want.channels = static_cast<Uint8>(audio_state_.dst_channels);
want.samples = 1024;
want.callback = sdl_audio_callback;
want.userdata = &audio_state_;
SDL_AudioSpec have{};
if (SDL_OpenAudio(&want, &have) < 0) { sdl_panic("OpenAudio"); return false; }
SDL_PauseAudio(0);
return true;
}
// ── Playback controls ──────────────────────────────────────────────────────
void Player::toggle_pause() {
if (play_state_ == PlayState::Playing) {
play_state_ = PlayState::Paused;
SDL_PauseAudio(1);
} else if (play_state_ == PlayState::Paused) {
play_state_ = PlayState::Playing;
SDL_PauseAudio(0);
}
}
void Player::seek(double target_sec) {
if (!seekable_ || !fmt_ctx_) return;
target_sec = std::max(0.0, std::min(target_sec, duration_));
int64_t ts = static_cast<int64_t>(target_sec * AV_TIME_BASE);
int ret = avformat_seek_file(fmt_ctx_, -1, INT64_MIN, ts, INT64_MAX, 0);
if (ret < 0) return;
flush_decoders();
position_ = target_sec;
{ // clear audio buffer so we don't hear stale data
std::lock_guard<std::mutex> lock(audio_state_.mtx);
audio_state_.buffer.clear();
}
// If we were stopped (EOF), resume into paused so user can hit play
if (play_state_ == PlayState::Stopped)
play_state_ = PlayState::Paused;
}
void Player::seek_relative(double offset_sec) {
seek(position_ + offset_sec);
}
void Player::flush_decoders() {
if (video_dec_) avcodec_flush_buffers(video_dec_);
if (audio_dec_) avcodec_flush_buffers(audio_dec_);
}
// ── Time helpers ────────────────────────────────────────────────────────────
double Player::pts_to_seconds(int64_t pts, AVStream* stream) const {
if (pts == AV_NOPTS_VALUE) return position_;
return static_cast<double>(pts) * av_q2d(stream->time_base);
}
void Player::update_position(AVFrame* frame, int stream_idx) {
if (stream_idx < 0 || stream_idx >= static_cast<int>(fmt_ctx_->nb_streams))
return;
double t = pts_to_seconds(frame->best_effort_timestamp,
fmt_ctx_->streams[stream_idx]);
if (t >= 0) position_ = t;
}
// ── Packet processing ──────────────────────────────────────────────────────
void Player::process_video_packet(AVPacket* pkt) {
if (avcodec_send_packet(video_dec_, pkt) != 0) return;
AVFrame* vframe = av_frame_alloc();
int ret;
while ((ret = avcodec_receive_frame(video_dec_, vframe)) == 0) {
update_position(vframe, video_idx_);
render_video_frame(vframe);
}
av_frame_free(&vframe);
}
void Player::process_audio_packet(AVPacket* pkt) {
if (avcodec_send_packet(audio_dec_, pkt) != 0) return;
AVFrame* aframe = av_frame_alloc();
int ret;
while ((ret = avcodec_receive_frame(audio_dec_, aframe)) == 0) {
update_position(aframe, audio_idx_);
int max_out = swr_get_out_samples(swr_ctx_, aframe->nb_samples);
std::vector<uint8_t> outbuf(
max_out * audio_state_.dst_channels *
av_get_bytes_per_sample(audio_state_.dst_fmt));
uint8_t* outptrs[1] = { outbuf.data() };
int nb = swr_convert(swr_ctx_, outptrs, max_out,
(const uint8_t**)aframe->data, aframe->nb_samples);
int out_bytes = nb * audio_state_.dst_channels *
av_get_bytes_per_sample(audio_state_.dst_fmt);
std::lock_guard<std::mutex> lock(audio_state_.mtx);
audio_state_.buffer.insert(audio_state_.buffer.end(),
outbuf.begin(), outbuf.begin() + out_bytes);
}
av_frame_free(&aframe);
}
// ── Rendering ───────────────────────────────────────────────────────────────
void Player::render_video_frame(AVFrame* frame) {
sws_scale(sws_ctx_, frame->data, frame->linesize,
0, video_dec_->height, yuv_frame_->data, yuv_frame_->linesize);
SDL_UpdateYUVTexture(texture_, nullptr,
yuv_frame_->data[0], yuv_frame_->linesize[0],
yuv_frame_->data[1], yuv_frame_->linesize[1],
yuv_frame_->data[2], yuv_frame_->linesize[2]);
present_current_frame();
}
void Player::present_current_frame() {
if (!renderer_) return;
int w, h;
SDL_GetWindowSize(window_, &w, &h);
SDL_RenderClear(renderer_);
if (texture_) {
SDL_Rect dst{0, 0, w, h};
SDL_RenderCopy(renderer_, texture_, nullptr, &dst);
} else {
// Audio-only: dark background
SDL_SetRenderDrawColor(renderer_, 30, 30, 30, 255);
SDL_Rect bg{0, 0, w, h};
SDL_RenderFillRect(renderer_, &bg);
}
render_controls();
SDL_RenderPresent(renderer_);
}
void Player::render_controls() {
if (!renderer_) return;
// Auto-hide controls after timeout (only while playing video)
auto now = std::chrono::steady_clock::now();
if (!scrubber_dragging_ && play_state_ == PlayState::Playing && video_dec_) {
controls_visible_ = (now - last_mouse_move_) < CONTROLS_TIMEOUT;
} else {
controls_visible_ = true;
}
if (!controls_visible_) return;
int w, h;
SDL_GetWindowSize(window_, &w, &h);
// Semi-transparent background bar
SDL_SetRenderDrawColor(renderer_, 0, 0, 0, 180);
SDL_Rect bar_bg{0, h - CONTROL_BAR_HEIGHT, w, CONTROL_BAR_HEIGHT};
SDL_RenderFillRect(renderer_, &bar_bg);
int cx = CONTROL_PADDING;
int cy = h - CONTROL_BAR_HEIGHT / 2;
// Play / Pause button
render_play_pause_button(cx + BUTTON_SIZE / 2, cy, BUTTON_SIZE);
cx += BUTTON_SIZE + CONTROL_PADDING * 2;
// Scrubber (only for seekable sources with known duration)
if (seekable_ && duration_ > 0) {
int scrubber_w = w - cx - CONTROL_PADDING;
int scrubber_y = cy - SCRUBBER_HEIGHT / 2;
render_scrubber(cx, scrubber_y, scrubber_w, SCRUBBER_HEIGHT);
}
}
void Player::render_play_pause_button(int cx, int cy, int size) {
int half = size / 2;
SDL_SetRenderDrawColor(renderer_, 255, 255, 255, 255);
if (play_state_ == PlayState::Playing) {
// Pause icon: two vertical bars
int bar_w = std::max(size / 4, 2);
int gap = std::max(size / 5, 2);
SDL_Rect left {cx - gap / 2 - bar_w, cy - half, bar_w, size};
SDL_Rect right{cx + gap / 2, cy - half, bar_w, size};
SDL_RenderFillRect(renderer_, &left);
SDL_RenderFillRect(renderer_, &right);
} else {
// Play icon: right-pointing triangle
for (int dy = -half; dy <= half; ++dy) {
float ratio = 1.0f - std::abs(static_cast<float>(dy) / half);
int bar_w = std::max(static_cast<int>(size * ratio), 1);
SDL_Rect line{cx - half + 2, cy + dy, bar_w, 1};
SDL_RenderFillRect(renderer_, &line);
}
}
}
void Player::render_scrubber(int x, int y, int width, int height) {
// Background track
SDL_SetRenderDrawColor(renderer_, 100, 100, 100, 200);
SDL_Rect track{x, y, width, height};
SDL_RenderFillRect(renderer_, &track);
// Progress fill
double progress = (duration_ > 0) ? position_ / duration_ : 0.0;
progress = std::max(0.0, std::min(1.0, progress));
int fill_w = static_cast<int>(width * progress);
SDL_SetRenderDrawColor(renderer_, 230, 70, 70, 255);
SDL_Rect fill{x, y, fill_w, height};
SDL_RenderFillRect(renderer_, &fill);
// Handle (small square at current position)
int hx = x + fill_w - HANDLE_RADIUS;
int hy = y + height / 2 - HANDLE_RADIUS;
SDL_SetRenderDrawColor(renderer_, 255, 255, 255, 255);
SDL_Rect handle{hx, hy, HANDLE_RADIUS * 2, HANDLE_RADIUS * 2};
SDL_RenderFillRect(renderer_, &handle);
}
// ── Event handling ──────────────────────────────────────────────────────────
void Player::handle_events() {
SDL_Event e;
while (SDL_PollEvent(&e)) {
switch (e.type) {
case SDL_QUIT:
g_running = false;
break;
case SDL_KEYDOWN:
switch (e.key.keysym.sym) {
case SDLK_q: g_running = false; break;
case SDLK_SPACE: toggle_pause(); break;
case SDLK_RIGHT: seek_relative(10.0); break;
case SDLK_LEFT: seek_relative(-10.0); break;
case SDLK_UP: seek_relative(60.0); break;
case SDLK_DOWN: seek_relative(-60.0); break;
default: break;
}
break;
case SDL_MOUSEMOTION:
last_mouse_move_ = std::chrono::steady_clock::now();
handle_mouse_motion(e.motion.x, e.motion.y);
break;
case SDL_MOUSEBUTTONDOWN:
if (e.button.button == SDL_BUTTON_LEFT)
handle_mouse_click(e.button.x, e.button.y);
break;
case SDL_MOUSEBUTTONUP:
scrubber_dragging_ = false;
break;
default: break;
}
}
}
void Player::handle_mouse_click(int x, int y) {
if (!window_) return;
int w, h;
SDL_GetWindowSize(window_, &w, &h);
// Only interact with the control-bar region
if (y < h - CONTROL_BAR_HEIGHT) return;
int btn_right = CONTROL_PADDING + BUTTON_SIZE;
// Play / pause button area
if (x < btn_right + CONTROL_PADDING) {
toggle_pause();
return;
}
// Scrubber area
if (seekable_ && duration_ > 0) {
int scrubber_x = btn_right + CONTROL_PADDING * 2;
int scrubber_w = w - scrubber_x - CONTROL_PADDING;
if (x >= scrubber_x && x <= scrubber_x + scrubber_w) {
scrubber_dragging_ = true;
double ratio = static_cast<double>(x - scrubber_x) / scrubber_w;
seek(ratio * duration_);
}
}
}
void Player::handle_mouse_motion(int x, int /*y*/) {
if (!scrubber_dragging_ || !seekable_ || duration_ <= 0) return;
int w, h;
SDL_GetWindowSize(window_, &w, &h);
int btn_right = CONTROL_PADDING + BUTTON_SIZE;
int scrubber_x = btn_right + CONTROL_PADDING * 2;
int scrubber_w = w - scrubber_x - CONTROL_PADDING;
double ratio = static_cast<double>(x - scrubber_x) / scrubber_w;
ratio = std::max(0.0, std::min(1.0, ratio));
seek(ratio * duration_);
}
// ── Main playback loop ─────────────────────────────────────────────────────
int Player::run() {
AVPacket* pkt = av_packet_alloc();
while (g_running) {
handle_events();
if (play_state_ == PlayState::Paused || play_state_ == PlayState::Stopped) {
present_current_frame();
SDL_Delay(16); // ~60 fps event polling while idle
continue;
}
int ret = av_read_frame(fmt_ctx_, pkt);
if (ret == AVERROR_EOF) {
if (seekable_) {
play_state_ = PlayState::Paused; // pause at end, allow seeking
continue;
}
break;
}
if (ret < 0) { SDL_Delay(5); continue; }
if (pkt->stream_index == video_idx_ && video_dec_)
process_video_packet(pkt);
else if (pkt->stream_index == audio_idx_ && audio_dec_)
process_audio_packet(pkt);
// For audio-only sources, render periodically so controls stay visible
if (!video_dec_ && renderer_)
present_current_frame();
av_packet_unref(pkt);
}
av_packet_free(&pkt);
return 0;
}
// ── Cleanup ─────────────────────────────────────────────────────────────────
void Player::cleanup() {
if (texture_) { SDL_DestroyTexture(texture_); texture_ = nullptr; }
if (renderer_) { SDL_DestroyRenderer(renderer_); renderer_ = nullptr; }
if (window_) { SDL_DestroyWindow(window_); window_ = nullptr; }
SDL_CloseAudio();
if (yuv_frame_) { av_frame_free(&yuv_frame_); }
if (sws_ctx_) { sws_freeContext(sws_ctx_); sws_ctx_ = nullptr; }
if (video_dec_) { avcodec_free_context(&video_dec_); }
if (swr_ctx_) { swr_free(&swr_ctx_); audio_state_.swr = nullptr; }
if (audio_dec_) { avcodec_free_context(&audio_dec_); }
if (fmt_ctx_) { avformat_close_input(&fmt_ctx_); }
SDL_Quit();
avformat_network_deinit();
}
// ── main ────────────────────────────────────────────────────────────────────
int main(int argc, char** argv) {
using namespace fplay;
FPlayArgs args;
if (!parse_args(argc, argv, args)) return 1;
std::signal(SIGINT, handle_sigint);
Player player;
if (!player.open(args.to_url())) return 1;
return player.run();
}