Add an audio demuxer

Add a demuxer which will read the stream from the audio socket.
This commit is contained in:
Romain Vimont 2023-02-06 10:33:47 +01:00
parent 6a3d311d47
commit d722b9de4d
2 changed files with 36 additions and 0 deletions

View file

@ -23,6 +23,7 @@ sc_demuxer_to_avcodec_id(uint32_t codec_id) {
#define SC_CODEC_ID_H264 UINT32_C(0x68323634) // "h264" in ASCII
#define SC_CODEC_ID_H265 UINT32_C(0x68323635) // "h265" in ASCII
#define SC_CODEC_ID_AV1 UINT32_C(0x00617631) // "av1" in ASCII
#define SC_CODEC_ID_OPUS UINT32_C(0x6f707573) // "opus" in ASCII
switch (codec_id) {
case SC_CODEC_ID_H264:
return AV_CODEC_ID_H264;
@ -30,6 +31,8 @@ sc_demuxer_to_avcodec_id(uint32_t codec_id) {
return AV_CODEC_ID_HEVC;
case SC_CODEC_ID_AV1:
return AV_CODEC_ID_AV1;
case SC_CODEC_ID_OPUS:
return AV_CODEC_ID_OPUS;
default:
LOGE("Unknown codec id 0x%08" PRIx32, codec_id);
return AV_CODEC_ID_NONE;
@ -236,6 +239,8 @@ end:
void
sc_demuxer_init(struct sc_demuxer *demuxer, sc_socket socket,
const struct sc_demuxer_callbacks *cbs, void *cbs_userdata) {
assert(socket != SC_SOCKET_NONE);
demuxer->socket = socket;
demuxer->sink_count = 0;

View file

@ -41,6 +41,7 @@ struct scrcpy {
struct sc_server server;
struct sc_screen screen;
struct sc_demuxer video_demuxer;
struct sc_demuxer audio_demuxer;
struct sc_decoder decoder;
struct sc_recorder recorder;
#ifdef HAVE_V4L2
@ -262,6 +263,16 @@ sc_video_demuxer_on_ended(struct sc_demuxer *demuxer, bool eos,
}
}
static void
sc_audio_demuxer_on_ended(struct sc_demuxer *demuxer, bool eos,
void *userdata) {
(void) demuxer;
(void) eos;
(void) userdata;
// Contrary to the video demuxer, keep mirroring if only the audio fails
}
static void
sc_server_on_connection_failed(struct sc_server *server, void *userdata) {
(void) server;
@ -319,6 +330,7 @@ scrcpy(struct scrcpy_options *options) {
bool v4l2_sink_initialized = false;
#endif
bool video_demuxer_started = false;
bool audio_demuxer_started = false;
#ifdef HAVE_USB
bool aoa_hid_initialized = false;
bool hid_keyboard_initialized = false;
@ -452,6 +464,14 @@ scrcpy(struct scrcpy_options *options) {
sc_demuxer_init(&s->video_demuxer, s->server.video_socket,
&video_demuxer_cbs, NULL);
if (options->audio) {
static const struct sc_demuxer_callbacks audio_demuxer_cbs = {
.on_ended = sc_audio_demuxer_on_ended,
};
sc_demuxer_init(&s->audio_demuxer, s->server.audio_socket,
&audio_demuxer_cbs, NULL);
}
if (dec) {
sc_demuxer_add_sink(&s->video_demuxer, &dec->packet_sink);
}
@ -671,6 +691,13 @@ aoa_hid_end:
}
video_demuxer_started = true;
if (options->audio) {
if (!sc_demuxer_start(&s->audio_demuxer)) {
goto end;
}
audio_demuxer_started = true;
}
ret = event_loop(s);
LOGD("quit...");
@ -720,6 +747,10 @@ end:
sc_demuxer_join(&s->video_demuxer);
}
if (audio_demuxer_started) {
sc_demuxer_join(&s->audio_demuxer);
}
#ifdef HAVE_V4L2
if (v4l2_sink_initialized) {
sc_v4l2_sink_destroy(&s->v4l2_sink);