summaryrefslogtreecommitdiffstats
path: root/channels/tsmf/client/oss/tsmf_oss.c
blob: 666b4193581c8baf20a0e74c30dc71e5dec614f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/**
 * FreeRDP: A Remote Desktop Protocol Implementation
 * Video Redirection Virtual Channel - OSS Audio Device
 *
 * Copyright (c) 2015 Rozhuk Ivan <rozhuk.im@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <freerdp/config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <winpr/crt.h>

#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <limits.h>
#include <unistd.h>
#if defined(__OpenBSD__)
#include <soundcard.h>
#else
#include <sys/soundcard.h>
#endif
#include <sys/ioctl.h>

#include <freerdp/types.h>
#include <freerdp/codec/dsp.h>

#include "tsmf_audio.h"

typedef struct
{
	ITSMFAudioDevice iface;

	char dev_name[PATH_MAX];
	int pcm_handle;

	UINT32 sample_rate;
	UINT32 channels;
	UINT32 bits_per_sample;

	UINT32 data_size_last;
} TSMFOssAudioDevice;

#define OSS_LOG_ERR(_text, _error)                                        \
	do                                                                    \
	{                                                                     \
		if ((_error) != 0)                                                \
		{                                                                 \
			char ebuffer[256] = { 0 };                                    \
			WLog_ERR(TAG, "%s: %i - %s", (_text), (_error),               \
			         winpr_strerror((_error), ebuffer, sizeof(ebuffer))); \
		}                                                                 \
	} while (0)

static BOOL tsmf_oss_open(ITSMFAudioDevice* audio, const char* device)
{
	int tmp = 0;
	TSMFOssAudioDevice* oss = (TSMFOssAudioDevice*)audio;

	if (oss == NULL || oss->pcm_handle != -1)
		return FALSE;

	if (device == NULL) /* Default device. */
	{
		strncpy(oss->dev_name, "/dev/dsp", sizeof(oss->dev_name));
	}
	else
	{
		strncpy(oss->dev_name, device, sizeof(oss->dev_name) - 1);
	}

	if ((oss->pcm_handle = open(oss->dev_name, O_WRONLY)) < 0)
	{
		OSS_LOG_ERR("sound dev open failed", errno);
		oss->pcm_handle = -1;
		return FALSE;
	}

#if 0 /* FreeBSD OSS implementation at this moment (2015.03) does not set PCM_CAP_OUTPUT flag. */
	if (ioctl(oss->pcm_handle, SNDCTL_DSP_GETCAPS, &mask) == -1)
	{
		OSS_LOG_ERR("SNDCTL_DSP_GETCAPS failed, try ignory", errno);
	}
	else if ((mask & PCM_CAP_OUTPUT) == 0)
	{
		OSS_LOG_ERR("Device does not supports playback", EOPNOTSUPP);
		close(oss->pcm_handle);
		oss->pcm_handle = -1;
		return FALSE;
	}

#endif
	const int rc = ioctl(oss->pcm_handle, SNDCTL_DSP_GETFMTS, &tmp);
	if (rc == -1)
	{
		OSS_LOG_ERR("SNDCTL_DSP_GETFMTS failed", errno);
		close(oss->pcm_handle);
		oss->pcm_handle = -1;
		return FALSE;
	}

	if ((AFMT_S16_LE & tmp) == 0)
	{
		OSS_LOG_ERR("SNDCTL_DSP_GETFMTS - AFMT_S16_LE", EOPNOTSUPP);
		close(oss->pcm_handle);
		oss->pcm_handle = -1;
		return FALSE;
	}

	WLog_INFO(TAG, "open: %s", oss->dev_name);
	return TRUE;
}

static BOOL tsmf_oss_set_format(ITSMFAudioDevice* audio, UINT32 sample_rate, UINT32 channels,
                                UINT32 bits_per_sample)
{
	int tmp = 0;
	TSMFOssAudioDevice* oss = (TSMFOssAudioDevice*)audio;

	if (oss == NULL || oss->pcm_handle == -1)
		return FALSE;

	oss->sample_rate = sample_rate;
	oss->channels = channels;
	oss->bits_per_sample = bits_per_sample;
	tmp = AFMT_S16_LE;

	if (ioctl(oss->pcm_handle, SNDCTL_DSP_SETFMT, &tmp) == -1)
		OSS_LOG_ERR("SNDCTL_DSP_SETFMT failed", errno);

	tmp = channels;

	if (ioctl(oss->pcm_handle, SNDCTL_DSP_CHANNELS, &tmp) == -1)
		OSS_LOG_ERR("SNDCTL_DSP_CHANNELS failed", errno);

	tmp = sample_rate;

	if (ioctl(oss->pcm_handle, SNDCTL_DSP_SPEED, &tmp) == -1)
		OSS_LOG_ERR("SNDCTL_DSP_SPEED failed", errno);

	tmp = ((bits_per_sample / 8) * channels * sample_rate);

	if (ioctl(oss->pcm_handle, SNDCTL_DSP_SETFRAGMENT, &tmp) == -1)
		OSS_LOG_ERR("SNDCTL_DSP_SETFRAGMENT failed", errno);

	DEBUG_TSMF("sample_rate %" PRIu32 " channels %" PRIu32 " bits_per_sample %" PRIu32 "",
	           sample_rate, channels, bits_per_sample);
	return TRUE;
}

static BOOL tsmf_oss_play(ITSMFAudioDevice* audio, const BYTE* data, UINT32 data_size)
{
	int status = 0;
	UINT32 offset = 0;
	TSMFOssAudioDevice* oss = (TSMFOssAudioDevice*)audio;
	DEBUG_TSMF("tsmf_oss_play: data_size %" PRIu32 "", data_size);

	if (oss == NULL || oss->pcm_handle == -1)
		return FALSE;

	if (data == NULL || data_size == 0)
		return TRUE;

	offset = 0;
	oss->data_size_last = data_size;

	while (offset < data_size)
	{
		status = write(oss->pcm_handle, &data[offset], (data_size - offset));

		if (status < 0)
		{
			OSS_LOG_ERR("write fail", errno);
			return FALSE;
		}

		offset += status;
	}

	return TRUE;
}

static UINT64 tsmf_oss_get_latency(ITSMFAudioDevice* audio)
{
	UINT64 latency = 0;
	TSMFOssAudioDevice* oss = (TSMFOssAudioDevice*)audio;

	if (oss == NULL)
		return 0;

	// latency = ((oss->data_size_last / (oss->bits_per_sample / 8)) * oss->sample_rate);
	// WLog_INFO(TAG, "latency: %zu", latency);
	return latency;
}

static BOOL tsmf_oss_flush(ITSMFAudioDevice* audio)
{
	return TRUE;
}

static void tsmf_oss_free(ITSMFAudioDevice* audio)
{
	TSMFOssAudioDevice* oss = (TSMFOssAudioDevice*)audio;

	if (oss == NULL)
		return;

	if (oss->pcm_handle != -1)
	{
		WLog_INFO(TAG, "close: %s", oss->dev_name);
		close(oss->pcm_handle);
	}

	free(oss);
}

FREERDP_ENTRY_POINT(ITSMFAudioDevice* oss_freerdp_tsmf_client_audio_subsystem_entry(void))
{
	TSMFOssAudioDevice* oss = calloc(1, sizeof(TSMFOssAudioDevice));
	if (!oss)
		return NULL;

	oss->iface.Open = tsmf_oss_open;
	oss->iface.SetFormat = tsmf_oss_set_format;
	oss->iface.Play = tsmf_oss_play;
	oss->iface.GetLatency = tsmf_oss_get_latency;
	oss->iface.Flush = tsmf_oss_flush;
	oss->iface.Free = tsmf_oss_free;
	oss->pcm_handle = -1;
	return &oss->iface;
}