Skip to content

Commit de939a0

Browse files
committed
refactor
1 parent 6d3a5b2 commit de939a0

File tree

4 files changed

+234
-216
lines changed

4 files changed

+234
-216
lines changed

app/src/client_audio.c

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -47,73 +47,73 @@ sc_microphone_list_audio_sources(void) {
4747
AVInputFormat *input_format = av_find_input_format(format_name);
4848

4949
if (!input_format) {
50-
fprintf(stderr, "Could not find audio input format '%s'\n", format_name);
50+
LOGE("Could not find audio input format '%s'", format_name);
5151
return;
5252
}
5353

54-
printf("Audio input format: %s (%s)\n",
54+
LOGI("Audio input format: %s (%s)",
5555
input_format->name,
5656
input_format->long_name ? input_format->long_name : "no description");
57-
printf("\nAvailable audio sources:\n");
57+
LOGI("Available audio sources:");
5858

5959
AVDeviceInfoList *device_list = NULL;
6060
int ret = avdevice_list_input_sources(input_format, NULL, NULL, &device_list);
6161

6262
if (ret < 0) {
63-
fprintf(stderr, "Could not list audio devices (error code: %d)\n", ret);
64-
fprintf(stderr, "Note: You can still use device names directly with --client-audio-source\n");
65-
fprintf(stderr, "Common device names:\n");
63+
LOGE("Could not list audio devices (error code: %d)", ret);
64+
LOGI("Note: You can still use device names directly with --client-audio-source");
65+
LOGI("Common device names:");
6666
#ifdef _WIN32
67-
fprintf(stderr, " - \"audio=DEVICE_NAME\" (for dshow)\n");
68-
fprintf(stderr, " - Try running 'ffmpeg -list_devices true -f dshow -i dummy' to see available devices\n");
67+
LOGI(" - \"audio=DEVICE_NAME\" (for dshow)");
68+
LOGI(" - Try running 'ffmpeg -list_devices true -f dshow -i dummy' to see available devices");
6969
#elif defined(__APPLE__)
70-
fprintf(stderr, " - \":0\" (default microphone)\n");
71-
fprintf(stderr, " - Try running 'ffmpeg -f avfoundation -list_devices true -i \"\"' to see available devices\n");
70+
LOGI(" - \":0\" (default microphone)");
71+
LOGI(" - Try running 'ffmpeg -f avfoundation -list_devices true -i \"\"' to see available devices");
7272
#else
73-
fprintf(stderr, " - \"default\" (default ALSA device)\n");
74-
fprintf(stderr, " - \"hw:0,0\" (hardware device)\n");
75-
fprintf(stderr, " - Try running 'arecord -L' to list available devices\n");
73+
LOGI(" - \"default\" (default ALSA device)");
74+
LOGI(" - \"hw:0,0\" (hardware device)");
75+
LOGI(" - Try running 'arecord -L' to list available devices");
7676
#endif
7777
return;
7878
}
7979

8080
if (device_list->nb_devices == 0) {
81-
printf(" No audio input devices found.\n");
81+
LOGI(" No audio input devices found.");
8282
} else {
8383
for (int i = 0; i < device_list->nb_devices; i++) {
8484
AVDeviceInfo *device = device_list->devices[i];
85-
printf(" %s", device->device_name);
8685
if (device->device_description) {
87-
printf(" (%s)", device->device_description);
86+
LOGI(" %s (%s)", device->device_name, device->device_description);
87+
} else {
88+
LOGI(" %s", device->device_name);
8889
}
89-
printf("\n");
9090
}
9191
}
9292

9393
avdevice_free_list_devices(&device_list);
9494

95-
printf("\nHow to use:\n");
96-
printf(" Use the device name exactly as shown above with --client-audio-source\n");
97-
printf("\nCommon microphone devices:\n");
95+
LOGI("How to use:");
96+
LOGI(" Use the device name exactly as shown above with --client-audio-source");
97+
LOGI("Common microphone devices:");
9898
#ifdef _WIN32
99-
printf(" - \"audio=DEVICE_NAME\" (use the exact name from the list)\n");
99+
LOGI(" - \"audio=DEVICE_NAME\" (use the exact name from the list)");
100100
#elif defined(__APPLE__)
101-
printf(" - \":0\" or \":1\" (device indices for macOS)\n");
102-
printf(" - \"default\" (default microphone)\n");
101+
LOGI(" - \":0\" or \":1\" (device indices for macOS)");
102+
LOGI(" - \"default\" (default microphone)");
103103
#else
104-
printf(" - \"default\" (usually your default microphone)\n");
105-
printf(" - \"hw:CARD,DEV\" devices are hardware devices\n");
106-
printf(" - \"pulse\" uses PulseAudio (if available)\n");
107-
printf(" Tip: Devices with \"capture\", \"input\", or \"microphone\" are likely input devices\n");
104+
LOGI(" - \"default\" (usually your default microphone)");
105+
LOGI(" - \"hw:CARD,DEV\" devices are hardware devices");
106+
LOGI(" - \"pulse\" uses PulseAudio (if available)");
107+
LOGI(" Tip: Devices with \"capture\", \"input\", or \"microphone\" are likely input devices");
108108
#endif
109-
printf("\nExamples:\n");
110-
printf(" scrcpy --client-audio-source default\n");
109+
LOGI("Examples:");
110+
LOGI(" scrcpy --client-audio-source default");
111111
#ifdef _WIN32
112-
printf(" scrcpy --client-audio-source \"audio=Microphone (Realtek Audio)\"\n");
112+
LOGI(" scrcpy --client-audio-source \"audio=Microphone (Realtek Audio)\"");
113113
#else
114-
printf(" scrcpy --client-audio-source \"hw:0,0\"\n");
114+
LOGI(" scrcpy --client-audio-source \"hw:0,0\"");
115115
#endif
116-
printf(" scrcpy --client-audio-source file:///path/to/audio.mp3\n");
116+
LOGI(" scrcpy --client-audio-source file:///path/to/audio.mp3");
117117
}
118118

119119
int
@@ -141,7 +141,7 @@ sc_microphone_run(void *data) {
141141
// Open audio file (MP3, OGG, WAV, etc.)
142142
LOGD("Opening audio file: %s", input_path);
143143
if (avformat_open_input(&fmt_ctx, input_path, NULL, NULL) < 0) {
144-
fprintf(stderr, "Could not open audio file '%s'\n", input_path);
144+
LOGE("Could not open audio file '%s'", input_path);
145145
goto cleanup;
146146
}
147147
} else {
@@ -151,18 +151,18 @@ sc_microphone_run(void *data) {
151151

152152
input_format = av_find_input_format(format_name);
153153
if (!input_format) {
154-
fprintf(stderr, "Could not find audio input format '%s'\n", format_name);
154+
LOGE("Could not find audio input format '%s'", format_name);
155155
goto cleanup;
156156
}
157157

158158
if (avformat_open_input(&fmt_ctx, input_path, input_format, NULL) < 0) {
159-
fprintf(stderr, "Could not open audio device '%s'\n", input_path);
159+
LOGE("Could not open audio device '%s'", input_path);
160160
goto cleanup;
161161
}
162162
}
163163

164164
if (avformat_find_stream_info(fmt_ctx, NULL) < 0) {
165-
fprintf(stderr, "Could not read stream info\n");
165+
LOGE("Could not read stream info");
166166
goto cleanup;
167167
}
168168

@@ -171,36 +171,36 @@ sc_microphone_run(void *data) {
171171
fmt_ctx->streams[audio_stream_index]->codecpar;
172172
AVCodec *in_codec = avcodec_find_decoder(in_codecpar->codec_id);
173173
if (!in_codec) {
174-
fprintf(stderr, "Input codec not found\n");
174+
LOGE("Input codec not found");
175175
goto cleanup;
176176
}
177177

178178
in_codec_ctx = avcodec_alloc_context3(in_codec);
179179
if (!in_codec_ctx) {
180-
fprintf(stderr, "Could not allocate input codec context\n");
180+
LOGE("Could not allocate input codec context");
181181
goto cleanup;
182182
}
183183

184184
avcodec_parameters_to_context(in_codec_ctx, in_codecpar);
185185
if (avcodec_open2(in_codec_ctx, in_codec, NULL) < 0) {
186-
fprintf(stderr, "Could not open input codec\n");
186+
LOGE("Could not open input codec");
187187
goto cleanup;
188188
}
189189

190-
LOGD("Input: sample_fmt=%d, sample_rate=%d, channels=%d\n",
190+
LOGD("Input: sample_fmt=%d, sample_rate=%d, channels=%d",
191191
in_codec_ctx->sample_fmt, in_codec_ctx->sample_rate,
192192
in_codec_ctx->ch_layout.nb_channels);
193193

194194
// Setup Opus encoder
195195
AVCodec *opus_codec = avcodec_find_encoder(AV_CODEC_ID_OPUS);
196196
if (!opus_codec) {
197-
fprintf(stderr, "Opus encoder not found\n");
197+
LOGE("Opus encoder not found");
198198
goto cleanup;
199199
}
200200

201201
opus_ctx = avcodec_alloc_context3(opus_codec);
202202
if (!opus_ctx) {
203-
fprintf(stderr, "Could not allocate Opus codec context\n");
203+
LOGE("Could not allocate Opus codec context");
204204
goto cleanup;
205205
}
206206

@@ -210,19 +210,19 @@ sc_microphone_run(void *data) {
210210
opus_ctx->bit_rate = 128000; // 128 kbps
211211

212212
if (avcodec_open2(opus_ctx, opus_codec, NULL) < 0) {
213-
fprintf(stderr, "Could not open Opus encoder\n");
213+
LOGE("Could not open Opus encoder");
214214
goto cleanup;
215215
}
216-
LOGD("opus_ctx->frame_size = %d\n", opus_ctx->frame_size);
216+
LOGD("opus_ctx->frame_size = %d", opus_ctx->frame_size);
217217

218-
LOGD("Opus encoder: sample_rate=%d, channels=%d, bit_rate=%" PRId64 " fmt=%d\n",
218+
LOGD("Opus encoder: sample_rate=%d, channels=%d, bit_rate=%" PRId64 " fmt=%d",
219219
opus_ctx->sample_rate, opus_ctx->ch_layout.nb_channels,
220220
opus_ctx->bit_rate, opus_ctx->sample_fmt);
221221

222222
// Setup resampler (ALSA format -> Opus format)
223223
swr_ctx = swr_alloc();
224224
if (!swr_ctx) {
225-
fprintf(stderr, "Could not allocate resampler\n");
225+
LOGE("Could not allocate resampler");
226226
goto cleanup;
227227
}
228228

@@ -231,7 +231,7 @@ sc_microphone_run(void *data) {
231231
in_codec_ctx->sample_fmt, in_codec_ctx->sample_rate, 0,
232232
NULL);
233233
if (swr_init(swr_ctx) < 0) {
234-
fprintf(stderr, "Could not initialize resampler\n");
234+
LOGE("Could not initialize resampler");
235235
goto cleanup;
236236
}
237237

@@ -240,30 +240,30 @@ sc_microphone_run(void *data) {
240240
in_frame = av_frame_alloc();
241241
out_frame = av_frame_alloc();
242242
if (!in_pkt || !out_pkt || !in_frame || !out_frame) {
243-
fprintf(stderr, "Could not allocate packets/frames\n");
243+
LOGE("Could not allocate packets/frames");
244244
goto cleanup;
245245
}
246246

247247
out_frame->format = opus_ctx->sample_fmt;
248248
out_frame->ch_layout = opus_ctx->ch_layout;
249249
out_frame->sample_rate = opus_ctx->sample_rate;
250250
out_frame->nb_samples = opus_ctx->frame_size;
251-
LOGD("setting nb_samples to %d\n", opus_ctx->frame_size);
251+
LOGD("setting nb_samples to %d", opus_ctx->frame_size);
252252
if (av_frame_get_buffer(out_frame, 0) < 0) {
253-
fprintf(stderr, "Could not allocate frame buffer\n");
253+
LOGE("Could not allocate frame buffer");
254254
goto cleanup;
255255
}
256256

257257
fifo = av_audio_fifo_alloc(opus_ctx->sample_fmt, opus_ctx->ch_layout.nb_channels,
258258
opus_ctx->frame_size * 2);
259259
if (!fifo) {
260-
fprintf(stderr, "Could not allocate audio FIFO\n");
260+
LOGE("Could not allocate audio FIFO");
261261
goto cleanup;
262262
}
263263

264-
printf("Recording audio with Opus encoding...\n");
264+
LOGI("Recording audio with Opus encoding...");
265265
if (is_file) {
266-
printf("File will loop continuously. Press Ctrl+C to stop.\n");
266+
LOGI("File will loop continuously. Press Ctrl+C to stop.");
267267
}
268268

269269
// For file playback: track timing to simulate real-time playback

app/src/client_audio.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#ifndef SC_CLIENT_AUDIO_H
2+
#define SC_CLIENT_AUDIO_H
3+
14
#include "util/net.h"
25

36
struct sc_microphone_params {
@@ -10,3 +13,5 @@ sc_microphone_list_audio_sources(void);
1013

1114
int
1215
sc_microphone_run(void *data);
16+
17+
#endif

0 commit comments

Comments
 (0)