FreeRDP
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
tsmf_oss.c
1
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 <err.h>
30#include <errno.h>
31#include <fcntl.h>
32#include <libgen.h>
33#include <limits.h>
34#if defined(__OpenBSD__)
35#include <soundcard.h>
36#else
37#include <sys/soundcard.h>
38#endif
39#include <sys/ioctl.h>
40
41#include <freerdp/types.h>
42#include <freerdp/codec/dsp.h>
43
44#include "tsmf_audio.h"
45
46typedef struct
47{
48 ITSMFAudioDevice iface;
49
50 char dev_name[PATH_MAX];
51 int pcm_handle;
52
53 UINT32 sample_rate;
54 UINT32 channels;
55 UINT32 bits_per_sample;
56
57 UINT32 data_size_last;
58} TSMFOssAudioDevice;
59
60#define OSS_LOG_ERR(_text, _error) \
61 do \
62 { \
63 if ((_error) != 0) \
64 { \
65 char ebuffer[256] = { 0 }; \
66 WLog_ERR(TAG, "%s: %i - %s", (_text), (_error), \
67 winpr_strerror((_error), ebuffer, sizeof(ebuffer))); \
68 } \
69 } while (0)
70
71static BOOL tsmf_oss_open(ITSMFAudioDevice* audio, const char* device)
72{
73 int tmp = 0;
74 TSMFOssAudioDevice* oss = (TSMFOssAudioDevice*)audio;
75
76 if (oss == NULL || oss->pcm_handle != -1)
77 return FALSE;
78
79 if (device == NULL) /* Default device. */
80 {
81 strncpy(oss->dev_name, "/dev/dsp", sizeof(oss->dev_name));
82 }
83 else
84 {
85 strncpy(oss->dev_name, device, sizeof(oss->dev_name) - 1);
86 }
87
88 if ((oss->pcm_handle = open(oss->dev_name, O_WRONLY)) < 0)
89 {
90 OSS_LOG_ERR("sound dev open failed", errno);
91 oss->pcm_handle = -1;
92 return FALSE;
93 }
94
95 const int rc = ioctl(oss->pcm_handle, SNDCTL_DSP_GETFMTS, &tmp);
96 if (rc == -1)
97 {
98 OSS_LOG_ERR("SNDCTL_DSP_GETFMTS failed", errno);
99 close(oss->pcm_handle);
100 oss->pcm_handle = -1;
101 return FALSE;
102 }
103
104 if ((AFMT_S16_LE & tmp) == 0)
105 {
106 OSS_LOG_ERR("SNDCTL_DSP_GETFMTS - AFMT_S16_LE", EOPNOTSUPP);
107 close(oss->pcm_handle);
108 oss->pcm_handle = -1;
109 return FALSE;
110 }
111
112 WLog_INFO(TAG, "open: %s", oss->dev_name);
113 return TRUE;
114}
115
116static BOOL tsmf_oss_set_format(ITSMFAudioDevice* audio, UINT32 sample_rate, UINT32 channels,
117 UINT32 bits_per_sample)
118{
119 int tmp = 0;
120 TSMFOssAudioDevice* oss = (TSMFOssAudioDevice*)audio;
121
122 if (oss == NULL || oss->pcm_handle == -1)
123 return FALSE;
124
125 oss->sample_rate = sample_rate;
126 oss->channels = channels;
127 oss->bits_per_sample = bits_per_sample;
128 tmp = AFMT_S16_LE;
129
130 if (ioctl(oss->pcm_handle, SNDCTL_DSP_SETFMT, &tmp) == -1)
131 OSS_LOG_ERR("SNDCTL_DSP_SETFMT failed", errno);
132
133 tmp = WINPR_ASSERTING_INT_CAST(int, channels);
134
135 if (ioctl(oss->pcm_handle, SNDCTL_DSP_CHANNELS, &tmp) == -1)
136 OSS_LOG_ERR("SNDCTL_DSP_CHANNELS failed", errno);
137
138 tmp = WINPR_ASSERTING_INT_CAST(int, sample_rate);
139
140 if (ioctl(oss->pcm_handle, SNDCTL_DSP_SPEED, &tmp) == -1)
141 OSS_LOG_ERR("SNDCTL_DSP_SPEED failed", errno);
142
143 tmp = WINPR_ASSERTING_INT_CAST(int, ((bits_per_sample / 8) * channels * sample_rate));
144
145 if (ioctl(oss->pcm_handle, SNDCTL_DSP_SETFRAGMENT, &tmp) == -1)
146 OSS_LOG_ERR("SNDCTL_DSP_SETFRAGMENT failed", errno);
147
148 DEBUG_TSMF("sample_rate %" PRIu32 " channels %" PRIu32 " bits_per_sample %" PRIu32 "",
149 sample_rate, channels, bits_per_sample);
150 return TRUE;
151}
152
153static BOOL tsmf_oss_play(ITSMFAudioDevice* audio, const BYTE* data, UINT32 data_size)
154{
155 UINT32 offset = 0;
156 TSMFOssAudioDevice* oss = (TSMFOssAudioDevice*)audio;
157 DEBUG_TSMF("tsmf_oss_play: data_size %" PRIu32 "", data_size);
158
159 if (oss == NULL || oss->pcm_handle == -1)
160 return FALSE;
161
162 if (data == NULL || data_size == 0)
163 return TRUE;
164
165 offset = 0;
166 oss->data_size_last = data_size;
167
168 while (offset < data_size)
169 {
170 const ssize_t status = write(oss->pcm_handle, &data[offset], (data_size - offset));
171
172 if (status < 0)
173 {
174 OSS_LOG_ERR("write fail", errno);
175 return FALSE;
176 }
177
178 offset += status;
179 }
180
181 return TRUE;
182}
183
184static UINT64 tsmf_oss_get_latency(ITSMFAudioDevice* audio)
185{
186 UINT64 latency = 0;
187 TSMFOssAudioDevice* oss = (TSMFOssAudioDevice*)audio;
188
189 if (oss == NULL)
190 return 0;
191
192 // latency = ((oss->data_size_last / (oss->bits_per_sample / 8)) * oss->sample_rate);
193 // WLog_INFO(TAG, "latency: %zu", latency);
194 return latency;
195}
196
197static BOOL tsmf_oss_flush(ITSMFAudioDevice* audio)
198{
199 return TRUE;
200}
201
202static void tsmf_oss_free(ITSMFAudioDevice* audio)
203{
204 TSMFOssAudioDevice* oss = (TSMFOssAudioDevice*)audio;
205
206 if (oss == NULL)
207 return;
208
209 if (oss->pcm_handle != -1)
210 {
211 WLog_INFO(TAG, "close: %s", oss->dev_name);
212 close(oss->pcm_handle);
213 }
214
215 free(oss);
216}
217
218FREERDP_ENTRY_POINT(UINT VCAPITYPE oss_freerdp_tsmf_client_audio_subsystem_entry(void* ptr))
219{
220 ITSMFAudioDevice** sptr = (ITSMFAudioDevice**)ptr;
221 WINPR_ASSERT(sptr);
222 *sptr = NULL;
223
224 TSMFOssAudioDevice* oss = calloc(1, sizeof(TSMFOssAudioDevice));
225 if (!oss)
226 return ERROR_OUTOFMEMORY;
227
228 oss->iface.Open = tsmf_oss_open;
229 oss->iface.SetFormat = tsmf_oss_set_format;
230 oss->iface.Play = tsmf_oss_play;
231 oss->iface.GetLatency = tsmf_oss_get_latency;
232 oss->iface.Flush = tsmf_oss_flush;
233 oss->iface.Free = tsmf_oss_free;
234 oss->pcm_handle = -1;
235 *sptr = &oss->iface;
236 return CHANNEL_RC_OK;
237}