20#include <freerdp/config.h> 
   29#include <alsa/asoundlib.h> 
   31#include <freerdp/types.h> 
   32#include <freerdp/codec/dsp.h> 
   34#include "tsmf_audio.h" 
   38  ITSMFAudioDevice iface;
 
   41  snd_pcm_t* out_handle;
 
   44  UINT32 source_channels;
 
   45  UINT32 actual_channels;
 
   46  UINT32 bytes_per_sample;
 
   49static BOOL tsmf_alsa_open_device(TSMFAlsaAudioDevice* alsa)
 
   52  error = snd_pcm_open(&alsa->out_handle, alsa->device, SND_PCM_STREAM_PLAYBACK, 0);
 
   56    WLog_ERR(TAG, 
"failed to open device %s", alsa->device);
 
   60  DEBUG_TSMF(
"open device %s", alsa->device);
 
   64static BOOL tsmf_alsa_open(ITSMFAudioDevice* audio, 
const char* device)
 
   66  TSMFAlsaAudioDevice* alsa = (TSMFAlsaAudioDevice*)audio;
 
   70    strncpy(alsa->device, 
"default", 
sizeof(alsa->device));
 
   74    strncpy(alsa->device, device, 
sizeof(alsa->device) - 1);
 
   77  return tsmf_alsa_open_device(alsa);
 
   80static BOOL tsmf_alsa_set_format(ITSMFAudioDevice* audio, UINT32 sample_rate, UINT32 channels,
 
   81                                 UINT32 bits_per_sample)
 
   84  snd_pcm_uframes_t frames = 0;
 
   85  snd_pcm_hw_params_t* hw_params = NULL;
 
   86  snd_pcm_sw_params_t* sw_params = NULL;
 
   87  TSMFAlsaAudioDevice* alsa = (TSMFAlsaAudioDevice*)audio;
 
   89  if (!alsa->out_handle)
 
   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);
 
  100    WLog_ERR(TAG, 
"snd_pcm_hw_params_malloc failed");
 
  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, NULL);
 
  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);
 
  117    WLog_ERR(TAG, 
"snd_pcm_sw_params_malloc");
 
  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);
 
  130  if ((alsa->actual_rate != alsa->source_rate) ||
 
  131      (alsa->actual_channels != alsa->source_channels))
 
  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);
 
  142static BOOL tsmf_alsa_play(ITSMFAudioDevice* audio, 
const BYTE* src, UINT32 data_size)
 
  144  const BYTE* pindex = NULL;
 
  145  TSMFAlsaAudioDevice* alsa = (TSMFAlsaAudioDevice*)audio;
 
  146  DEBUG_TSMF(
"data_size %" PRIu32 
"", data_size);
 
  148  if (alsa->out_handle)
 
  150    const size_t rbytes_per_frame = 1ULL * alsa->actual_channels * alsa->bytes_per_sample;
 
  152    const BYTE* end = pindex + data_size;
 
  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);
 
  162        snd_pcm_recover(alsa->out_handle, -EPIPE, 0);
 
  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);
 
  174      DEBUG_TSMF(
"%d frames played.", error);
 
  179      pindex += (size_t)error * rbytes_per_frame;
 
  186static UINT64 tsmf_alsa_get_latency(ITSMFAudioDevice* audio)
 
  189  snd_pcm_sframes_t frames = 0;
 
  190  TSMFAlsaAudioDevice* alsa = (TSMFAlsaAudioDevice*)audio;
 
  192  if (alsa->out_handle && alsa->actual_rate > 0 &&
 
  193      snd_pcm_delay(alsa->out_handle, &frames) == 0 && frames > 0)
 
  195    latency = ((UINT64)frames) * 10000000LL / (UINT64)alsa->actual_rate;
 
  201static BOOL tsmf_alsa_flush(ITSMFAudioDevice* audio)
 
  206static void tsmf_alsa_free(ITSMFAudioDevice* audio)
 
  208  TSMFAlsaAudioDevice* alsa = (TSMFAlsaAudioDevice*)audio;
 
  211  if (alsa->out_handle)
 
  213    snd_pcm_drain(alsa->out_handle);
 
  214    snd_pcm_close(alsa->out_handle);
 
  220FREERDP_ENTRY_POINT(UINT VCAPITYPE alsa_freerdp_tsmf_client_audio_subsystem_entry(
void* ptr))
 
  222  ITSMFAudioDevice** sptr = (ITSMFAudioDevice**)ptr;
 
  226  TSMFAlsaAudioDevice* alsa = calloc(1, 
sizeof(TSMFAlsaAudioDevice));
 
  228    return ERROR_OUTOFMEMORY;
 
  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;