change to new api
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -177,3 +177,5 @@ cython_debug/
|
||||
music
|
||||
logs
|
||||
data
|
||||
|
||||
bin
|
||||
2
Makefile
2
Makefile
@@ -1,5 +1,5 @@
|
||||
CC := gcc
|
||||
TARGET := player
|
||||
TARGET := bin/player
|
||||
SRC_DIR := src
|
||||
BUILD := build
|
||||
|
||||
|
||||
91
src/main.c
91
src/main.c
@@ -1,13 +1,13 @@
|
||||
// player.c
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <libavformat/avformat.h>
|
||||
#include <libavcodec/avcodec.h>
|
||||
#include <libswresample/swresample.h>
|
||||
#include <libavformat/avformat.h>
|
||||
#include <libavutil/opt.h>
|
||||
#include <libswresample/swresample.h>
|
||||
|
||||
#include <alsa/asoundlib.h>
|
||||
|
||||
@@ -56,8 +56,7 @@ int main(int argc, char **argv) {
|
||||
|
||||
/* ---------- Decoder ---------- */
|
||||
|
||||
AVCodec *codec =
|
||||
avcodec_find_decoder(stream->codecpar->codec_id);
|
||||
const AVCodec *codec = avcodec_find_decoder(stream->codecpar->codec_id);
|
||||
if (!codec) {
|
||||
fprintf(stderr, "decoder not found\n");
|
||||
return 1;
|
||||
@@ -73,18 +72,27 @@ int main(int argc, char **argv) {
|
||||
|
||||
/* ---------- Resampler ---------- */
|
||||
|
||||
SwrContext *swr = swr_alloc_set_opts(
|
||||
NULL,
|
||||
OUT_LAYOUT,
|
||||
OUT_FORMAT,
|
||||
OUT_RATE,
|
||||
dec->channel_layout ? dec->channel_layout
|
||||
: av_get_default_channel_layout(dec->channels),
|
||||
dec->sample_fmt,
|
||||
dec->sample_rate,
|
||||
0,
|
||||
NULL
|
||||
);
|
||||
SwrContext *swr = swr_alloc();
|
||||
if (!swr) {
|
||||
fprintf(stderr, "failed to alloc swr\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
AVChannelLayout out_ch_layout;
|
||||
av_channel_layout_default(&out_ch_layout, OUT_CHANNELS);
|
||||
|
||||
av_opt_set_chlayout(swr, "out_chlayout", &out_ch_layout, 0);
|
||||
av_opt_set_int(swr, "out_sample_rate", OUT_RATE, 0);
|
||||
av_opt_set_sample_fmt(swr, "out_sample_fmt", OUT_FORMAT, 0);
|
||||
|
||||
av_opt_set_chlayout(swr, "in_chlayout", &dec->ch_layout, 0);
|
||||
av_opt_set_int(swr, "in_sample_rate", dec->sample_rate, 0);
|
||||
av_opt_set_sample_fmt(swr, "in_sample_fmt", dec->sample_fmt, 0);
|
||||
|
||||
if (swr_init(swr) < 0) {
|
||||
fprintf(stderr, "failed to init swr\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!swr || swr_init(swr) < 0) {
|
||||
fprintf(stderr, "failed to init resampler\n");
|
||||
@@ -94,19 +102,13 @@ int main(int argc, char **argv) {
|
||||
/* ---------- ALSA ---------- */
|
||||
|
||||
snd_pcm_t *pcm;
|
||||
if (snd_pcm_open(&pcm, "default",
|
||||
SND_PCM_STREAM_PLAYBACK, 0) < 0) {
|
||||
if (snd_pcm_open(&pcm, "default", SND_PCM_STREAM_PLAYBACK, 0) < 0) {
|
||||
fprintf(stderr, "failed to open ALSA device\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
snd_pcm_set_params(
|
||||
pcm,
|
||||
SND_PCM_FORMAT_S16_LE,
|
||||
SND_PCM_ACCESS_RW_INTERLEAVED,
|
||||
OUT_CHANNELS,
|
||||
OUT_RATE,
|
||||
1,
|
||||
snd_pcm_set_params(pcm, SND_PCM_FORMAT_S16_LE, SND_PCM_ACCESS_RW_INTERLEAVED,
|
||||
OUT_CHANNELS, OUT_RATE, 1,
|
||||
500000 // 0.5s latency
|
||||
);
|
||||
|
||||
@@ -130,33 +132,19 @@ int main(int argc, char **argv) {
|
||||
avcodec_send_packet(dec, pkt);
|
||||
|
||||
while (avcodec_receive_frame(dec, frm) == 0) {
|
||||
int needed = av_rescale_rnd(
|
||||
swr_get_delay(swr, dec->sample_rate) + frm->nb_samples,
|
||||
OUT_RATE,
|
||||
dec->sample_rate,
|
||||
AV_ROUND_UP
|
||||
);
|
||||
int needed =
|
||||
av_rescale_rnd(swr_get_delay(swr, dec->sample_rate) + frm->nb_samples,
|
||||
OUT_RATE, dec->sample_rate, AV_ROUND_UP);
|
||||
|
||||
if (needed > max_samples) {
|
||||
av_freep(&outbuf);
|
||||
av_samples_alloc(
|
||||
&outbuf,
|
||||
&out_linesize,
|
||||
OUT_CHANNELS,
|
||||
needed,
|
||||
OUT_FORMAT,
|
||||
1
|
||||
);
|
||||
av_samples_alloc(&outbuf, &out_linesize, OUT_CHANNELS, needed,
|
||||
OUT_FORMAT, 1);
|
||||
max_samples = needed;
|
||||
}
|
||||
|
||||
int samples = swr_convert(
|
||||
swr,
|
||||
&outbuf,
|
||||
needed,
|
||||
(const uint8_t **)frm->data,
|
||||
frm->nb_samples
|
||||
);
|
||||
int samples = swr_convert(swr, &outbuf, needed,
|
||||
(const uint8_t **)frm->data, frm->nb_samples);
|
||||
|
||||
int written = snd_pcm_writei(pcm, outbuf, samples);
|
||||
if (written < 0)
|
||||
@@ -170,13 +158,8 @@ int main(int argc, char **argv) {
|
||||
|
||||
avcodec_send_packet(dec, NULL);
|
||||
while (avcodec_receive_frame(dec, frm) == 0) {
|
||||
int samples = swr_convert(
|
||||
swr,
|
||||
&outbuf,
|
||||
max_samples,
|
||||
(const uint8_t **)frm->data,
|
||||
frm->nb_samples
|
||||
);
|
||||
int samples = swr_convert(swr, &outbuf, max_samples,
|
||||
(const uint8_t **)frm->data, frm->nb_samples);
|
||||
snd_pcm_writei(pcm, outbuf, samples);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user