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