| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#include <freerdp/config.h> |
| 21 |
|
| 22 |
#include <stdio.h> |
| 23 |
#include <stdlib.h> |
| 24 |
#include <string.h> |
| 25 |
#include <unistd.h> |
| 26 |
|
| 27 |
#include <winpr/crt.h> |
| 28 |
|
| 29 |
#include <alsa/asoundlib.h> |
| 30 |
|
| 31 |
#include <freerdp/types.h> |
| 32 |
#include <freerdp/codec/dsp.h> |
| 33 |
|
| 34 |
#include "tsmf_audio.h" |
| 35 |
|
| 36 |
typedef struct |
| 37 |
{ |
| 38 |
ITSMFAudioDevice iface; |
| 39 |
|
| 40 |
char device[32]; |
| 41 |
snd_pcm_t* out_handle; |
| 42 |
UINT32 source_rate; |
| 43 |
UINT32 actual_rate; |
| 44 |
UINT32 source_channels; |
| 45 |
UINT32 actual_channels; |
| 46 |
UINT32 bytes_per_sample; |
| 47 |
} TSMFAlsaAudioDevice; |
| 48 |
|
| 49 |
static BOOL tsmf_alsa_open_device(TSMFAlsaAudioDevice* alsa) |
| 50 |
{ |
| 51 |
int error = 0; |
| 52 |
error = snd_pcm_open(&alsa->out_handle, alsa->device, SND_PCM_STREAM_PLAYBACK, 0); |
| 53 |
|
| 54 |
if (error < 0) |
| 55 |
{ |
| 56 |
WLog_ERR(TAG, "failed to open device %s", alsa->device); |
| 57 |
return FALSE; |
| 58 |
} |
| 59 |
|
| 60 |
DEBUG_TSMF("open device %s", alsa->device); |
| 61 |
return TRUE; |
| 62 |
} |
| 63 |
|
| 64 |
static BOOL tsmf_alsa_open(ITSMFAudioDevice* audio, const char* device) |
| 65 |
{ |
| 66 |
TSMFAlsaAudioDevice* alsa = (TSMFAlsaAudioDevice*)audio; |
| 67 |
|
| 68 |
if (!device) |
| 69 |
{ |
| 70 |
strncpy(alsa->device, "default", sizeof(alsa->device)); |
| 71 |
} |
| 72 |
else |
| 73 |
{ |
| 74 |
strncpy(alsa->device, device, sizeof(alsa->device) - 1); |
| 75 |
} |
| 76 |
|
| 77 |
return tsmf_alsa_open_device(alsa); |
| 78 |
} |
| 79 |
|
| 80 |
static BOOL tsmf_alsa_set_format(ITSMFAudioDevice* audio, UINT32 sample_rate, UINT32 channels, |
| 81 |
UINT32 bits_per_sample) |
| 82 |
{ |
| 83 |
int error = 0; |
| 84 |
snd_pcm_uframes_t frames = 0; |
| 85 |
snd_pcm_hw_params_t* hw_params = nullptr; |
| 86 |
snd_pcm_sw_params_t* sw_params = nullptr; |
| 87 |
TSMFAlsaAudioDevice* alsa = (TSMFAlsaAudioDevice*)audio; |
| 88 |
|
| 89 |
if (!alsa->out_handle) |
| 90 |
return FALSE; |
| 91 |
|
| 92 |
snd_pcm_drop(alsa->out_handle); |
| 93 |
alsa->actual_rate = alsa->source_rate = sample_rate; |
| 94 |
alsa->actual_channels = alsa->source_channels = channels; |
| 95 |
alsa->bytes_per_sample = bits_per_sample / 8; |
| 96 |
error = snd_pcm_hw_params_malloc(&hw_params); |
| 97 |
|
| 98 |
if (error < 0) |
| 99 |
{ |
| 100 |
WLog_ERR(TAG, "snd_pcm_hw_params_malloc failed"); |
| 101 |
return FALSE; |
| 102 |
} |
| 103 |
|
| 104 |
snd_pcm_hw_params_any(alsa->out_handle, hw_params); |
| 105 |
snd_pcm_hw_params_set_access(alsa->out_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED); |
| 106 |
snd_pcm_hw_params_set_format(alsa->out_handle, hw_params, SND_PCM_FORMAT_S16_LE); |
| 107 |
snd_pcm_hw_params_set_rate_near(alsa->out_handle, hw_params, &alsa->actual_rate, nullptr); |
| 108 |
snd_pcm_hw_params_set_channels_near(alsa->out_handle, hw_params, &alsa->actual_channels); |
| 109 |
frames = sample_rate; |
| 110 |
snd_pcm_hw_params_set_buffer_size_near(alsa->out_handle, hw_params, &frames); |
| 111 |
snd_pcm_hw_params(alsa->out_handle, hw_params); |
| 112 |
snd_pcm_hw_params_free(hw_params); |
| 113 |
error = snd_pcm_sw_params_malloc(&sw_params); |
| 114 |
|
| 115 |
if (error < 0) |
| 116 |
{ |
| 117 |
WLog_ERR(TAG, "snd_pcm_sw_params_malloc"); |
| 118 |
return FALSE; |
| 119 |
} |
| 120 |
|
| 121 |
snd_pcm_sw_params_current(alsa->out_handle, sw_params); |
| 122 |
snd_pcm_sw_params_set_start_threshold(alsa->out_handle, sw_params, frames / 2); |
| 123 |
snd_pcm_sw_params(alsa->out_handle, sw_params); |
| 124 |
snd_pcm_sw_params_free(sw_params); |
| 125 |
snd_pcm_prepare(alsa->out_handle); |
| 126 |
DEBUG_TSMF("sample_rate %" PRIu32 " channels %" PRIu32 " bits_per_sample %" PRIu32 "", |
| 127 |
sample_rate, channels, bits_per_sample); |
| 128 |
DEBUG_TSMF("hardware buffer %lu frames", frames); |
| 129 |
|
| 130 |
if ((alsa->actual_rate != alsa->source_rate) || |
| 131 |
(alsa->actual_channels != alsa->source_channels)) |
| 132 |
{ |
| 133 |
DEBUG_TSMF("actual rate %" PRIu32 " / channel %" PRIu32 " is different " |
| 134 |
"from source rate %" PRIu32 " / channel %" PRIu32 ", resampling required.", |
| 135 |
alsa->actual_rate, alsa->actual_channels, alsa->source_rate, |
| 136 |
alsa->source_channels); |
| 137 |
} |
| 138 |
|
| 139 |
return TRUE; |
| 140 |
} |
| 141 |
|
| 142 |
static BOOL tsmf_alsa_play(ITSMFAudioDevice* audio, const BYTE* src, UINT32 data_size) |
| 143 |
{ |
| 144 |
const BYTE* pindex = nullptr; |
| 145 |
TSMFAlsaAudioDevice* alsa = (TSMFAlsaAudioDevice*)audio; |
| 146 |
DEBUG_TSMF("data_size %" PRIu32 "", data_size); |
| 147 |
|
| 148 |
if (alsa->out_handle) |
| 149 |
{ |
| 150 |
const size_t rbytes_per_frame = 1ULL * alsa->actual_channels * alsa->bytes_per_sample; |
| 151 |
pindex = src; |
| 152 |
const BYTE* end = pindex + data_size; |
| 153 |
|
| 154 |
while (pindex < end) |
| 155 |
{ |
| 156 |
const size_t len = (size_t)(end - pindex); |
| 157 |
const size_t frames = len / rbytes_per_frame; |
| 158 |
snd_pcm_sframes_t error = snd_pcm_writei(alsa->out_handle, pindex, frames); |
| 159 |
|
| 160 |
if (error == -EPIPE) |
| 161 |
{ |
| 162 |
snd_pcm_recover(alsa->out_handle, -EPIPE, 0); |
| 163 |
error = 0; |
| 164 |
} |
| 165 |
else if (error < 0) |
| 166 |
{ |
| 167 |
DEBUG_TSMF("error len %ld", error); |
| 168 |
snd_pcm_close(alsa->out_handle); |
| 169 |
alsa->out_handle = 0; |
| 170 |
tsmf_alsa_open_device(alsa); |
| 171 |
break; |
| 172 |
} |
| 173 |
|
| 174 |
DEBUG_TSMF("%d frames played.", error); |
| 175 |
|
| 176 |
if (error == 0) |
| 177 |
break; |
| 178 |
|
| 179 |
pindex += (size_t)error * rbytes_per_frame; |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
return TRUE; |
| 184 |
} |
| 185 |
|
| 186 |
static UINT64 tsmf_alsa_get_latency(ITSMFAudioDevice* audio) |
| 187 |
{ |
| 188 |
UINT64 latency = 0; |
| 189 |
snd_pcm_sframes_t frames = 0; |
| 190 |
TSMFAlsaAudioDevice* alsa = (TSMFAlsaAudioDevice*)audio; |
| 191 |
|
| 192 |
if (alsa->out_handle && alsa->actual_rate > 0 && |
| 193 |
snd_pcm_delay(alsa->out_handle, &frames) == 0 && frames > 0) |
| 194 |
{ |
| 195 |
latency = ((UINT64)frames) * 10000000LL / (UINT64)alsa->actual_rate; |
| 196 |
} |
| 197 |
|
| 198 |
return latency; |
| 199 |
} |
| 200 |
|
| 201 |
static BOOL tsmf_alsa_flush(WINPR_ATTR_UNUSED ITSMFAudioDevice* audio) |
| 202 |
{ |
| 203 |
return TRUE; |
| 204 |
} |
| 205 |
|
| 206 |
static void tsmf_alsa_free(ITSMFAudioDevice* audio) |
| 207 |
{ |
| 208 |
TSMFAlsaAudioDevice* alsa = (TSMFAlsaAudioDevice*)audio; |
| 209 |
DEBUG_TSMF(""); |
| 210 |
|
| 211 |
if (alsa->out_handle) |
| 212 |
{ |
| 213 |
snd_pcm_drain(alsa->out_handle); |
| 214 |
snd_pcm_close(alsa->out_handle); |
| 215 |
} |
| 216 |
|
| 217 |
free(alsa); |
| 218 |
} |
| 219 |
|
| 220 |
FREERDP_ENTRY_POINT(UINT VCAPITYPE alsa_freerdp_tsmf_client_audio_subsystem_entry(void* ptr)) |
| 221 |
{ |
| 222 |
ITSMFAudioDevice** sptr = (ITSMFAudioDevice**)ptr; |
| 223 |
WINPR_ASSERT(sptr); |
| 224 |
*sptr = nullptr; |
| 225 |
|
| 226 |
TSMFAlsaAudioDevice* alsa = calloc(1, sizeof(TSMFAlsaAudioDevice)); |
| 227 |
if (!alsa) |
| 228 |
return ERROR_OUTOFMEMORY; |
| 229 |
|
| 230 |
alsa->iface.Open = tsmf_alsa_open; |
| 231 |
alsa->iface.SetFormat = tsmf_alsa_set_format; |
| 232 |
alsa->iface.Play = tsmf_alsa_play; |
| 233 |
alsa->iface.GetLatency = tsmf_alsa_get_latency; |
| 234 |
alsa->iface.Flush = tsmf_alsa_flush; |
| 235 |
alsa->iface.Free = tsmf_alsa_free; |
| 236 |
*sptr = &alsa->iface; |
| 237 |
return CHANNEL_RC_OK; |
| 238 |
} |