summaryrefslogtreecommitdiffstats
path: root/channels/rdpsnd/client/ios/rdpsnd_ios.c
blob: b8f004f936e3d7a5fbbdc614d95b4cae0135234e (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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**
 * FreeRDP: A Remote Desktop Protocol Implementation
 * Audio Output Virtual Channel
 *
 * Copyright 2013 Dell Software <Mike.McDonald@software.dell.com>
 * Copyright 2015 Thincast Technologies GmbH
 * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.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 <winpr/wtypes.h>

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

#import <AudioToolbox/AudioToolbox.h>

#include "rdpsnd_main.h"
#include "TPCircularBuffer.h"

#define INPUT_BUFFER_SIZE 32768
#define CIRCULAR_BUFFER_SIZE (INPUT_BUFFER_SIZE * 4)

typedef struct
{
	rdpsndDevicePlugin device;
	AudioComponentInstance audio_unit;
	TPCircularBuffer buffer;
	BOOL is_opened;
	BOOL is_playing;
} rdpsndIOSPlugin;

#define THIS(__ptr) ((rdpsndIOSPlugin*)__ptr)

static OSStatus rdpsnd_ios_render_cb(void* inRefCon,
                                     AudioUnitRenderActionFlags __unused* ioActionFlags,
                                     const AudioTimeStamp __unused* inTimeStamp, UInt32 inBusNumber,
                                     UInt32 __unused inNumberFrames, AudioBufferList* ioData)
{
	if (inBusNumber != 0)
	{
		return noErr;
	}

	rdpsndIOSPlugin* p = THIS(inRefCon);

	for (unsigned int i = 0; i < ioData->mNumberBuffers; i++)
	{
		AudioBuffer* target_buffer = &ioData->mBuffers[i];
		int32_t available_bytes = 0;
		const void* buffer = TPCircularBufferTail(&p->buffer, &available_bytes);

		if (buffer != NULL && available_bytes > 0)
		{
			const int bytes_to_copy = MIN((int32_t)target_buffer->mDataByteSize, available_bytes);
			memcpy(target_buffer->mData, buffer, bytes_to_copy);
			target_buffer->mDataByteSize = bytes_to_copy;
			TPCircularBufferConsume(&p->buffer, bytes_to_copy);
		}
		else
		{
			target_buffer->mDataByteSize = 0;
			AudioOutputUnitStop(p->audio_unit);
			p->is_playing = 0;
		}
	}

	return noErr;
}

static BOOL rdpsnd_ios_format_supported(rdpsndDevicePlugin* __unused device,
                                        const AUDIO_FORMAT* format)
{
	if (format->wFormatTag == WAVE_FORMAT_PCM)
	{
		return 1;
	}

	return 0;
}

static BOOL rdpsnd_ios_set_volume(rdpsndDevicePlugin* __unused device, UINT32 __unused value)
{
	return TRUE;
}

static void rdpsnd_ios_start(rdpsndDevicePlugin* device)
{
	rdpsndIOSPlugin* p = THIS(device);

	/* If this device is not playing... */
	if (!p->is_playing)
	{
		/* Start the device. */
		int32_t available_bytes = 0;
		TPCircularBufferTail(&p->buffer, &available_bytes);

		if (available_bytes > 0)
		{
			p->is_playing = 1;
			AudioOutputUnitStart(p->audio_unit);
		}
	}
}

static void rdpsnd_ios_stop(rdpsndDevicePlugin* __unused device)
{
	rdpsndIOSPlugin* p = THIS(device);

	/* If the device is playing... */
	if (p->is_playing)
	{
		/* Stop the device. */
		AudioOutputUnitStop(p->audio_unit);
		p->is_playing = 0;
		/* Free all buffers. */
		TPCircularBufferClear(&p->buffer);
	}
}

static UINT rdpsnd_ios_play(rdpsndDevicePlugin* device, const BYTE* data, size_t size)
{
	rdpsndIOSPlugin* p = THIS(device);
	const BOOL ok = TPCircularBufferProduceBytes(&p->buffer, data, size);

	if (!ok)
		return 0;

	rdpsnd_ios_start(device);
	return 10; /* TODO: Get real latencry in [ms] */
}

static BOOL rdpsnd_ios_open(rdpsndDevicePlugin* device, const AUDIO_FORMAT* format,
                            int __unused latency)
{
	rdpsndIOSPlugin* p = THIS(device);

	if (p->is_opened)
		return TRUE;

	/* Find the output audio unit. */
	AudioComponentDescription desc;
	desc.componentManufacturer = kAudioUnitManufacturer_Apple;
	desc.componentType = kAudioUnitType_Output;
	desc.componentSubType = kAudioUnitSubType_RemoteIO;
	desc.componentFlags = 0;
	desc.componentFlagsMask = 0;
	AudioComponent audioComponent = AudioComponentFindNext(NULL, &desc);

	if (audioComponent == NULL)
		return FALSE;

	/* Open the audio unit. */
	OSStatus status = AudioComponentInstanceNew(audioComponent, &p->audio_unit);

	if (status != 0)
		return FALSE;

	/* Set the format for the AudioUnit. */
	AudioStreamBasicDescription audioFormat = { 0 };
	audioFormat.mSampleRate = format->nSamplesPerSec;
	audioFormat.mFormatID = kAudioFormatLinearPCM;
	audioFormat.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
	audioFormat.mFramesPerPacket = 1; /* imminent property of the Linear PCM */
	audioFormat.mChannelsPerFrame = format->nChannels;
	audioFormat.mBitsPerChannel = format->wBitsPerSample;
	audioFormat.mBytesPerFrame = (format->wBitsPerSample * format->nChannels) / 8;
	audioFormat.mBytesPerPacket = audioFormat.mBytesPerFrame * audioFormat.mFramesPerPacket;
	status = AudioUnitSetProperty(p->audio_unit, kAudioUnitProperty_StreamFormat,
	                              kAudioUnitScope_Input, 0, &audioFormat, sizeof(audioFormat));

	if (status != 0)
	{
		AudioComponentInstanceDispose(p->audio_unit);
		p->audio_unit = NULL;
		return FALSE;
	}

	/* Set up the AudioUnit callback. */
	AURenderCallbackStruct callbackStruct = { 0 };
	callbackStruct.inputProc = rdpsnd_ios_render_cb;
	callbackStruct.inputProcRefCon = p;
	status =
	    AudioUnitSetProperty(p->audio_unit, kAudioUnitProperty_SetRenderCallback,
	                         kAudioUnitScope_Input, 0, &callbackStruct, sizeof(callbackStruct));

	if (status != 0)
	{
		AudioComponentInstanceDispose(p->audio_unit);
		p->audio_unit = NULL;
		return FALSE;
	}

	/* Initialize the AudioUnit. */
	status = AudioUnitInitialize(p->audio_unit);

	if (status != 0)
	{
		AudioComponentInstanceDispose(p->audio_unit);
		p->audio_unit = NULL;
		return FALSE;
	}

	/* Allocate the circular buffer. */
	const BOOL ok = TPCircularBufferInit(&p->buffer, CIRCULAR_BUFFER_SIZE);

	if (!ok)
	{
		AudioUnitUninitialize(p->audio_unit);
		AudioComponentInstanceDispose(p->audio_unit);
		p->audio_unit = NULL;
		return FALSE;
	}

	p->is_opened = 1;
	return TRUE;
}

static void rdpsnd_ios_close(rdpsndDevicePlugin* device)
{
	rdpsndIOSPlugin* p = THIS(device);
	/* Make sure the device is stopped. */
	rdpsnd_ios_stop(device);

	/* If the device is open... */
	if (p->is_opened)
	{
		/* Close the device. */
		AudioUnitUninitialize(p->audio_unit);
		AudioComponentInstanceDispose(p->audio_unit);
		p->audio_unit = NULL;
		p->is_opened = 0;
		/* Destroy the circular buffer. */
		TPCircularBufferCleanup(&p->buffer);
	}
}

static void rdpsnd_ios_free(rdpsndDevicePlugin* device)
{
	rdpsndIOSPlugin* p = THIS(device);
	/* Ensure the device is closed. */
	rdpsnd_ios_close(device);
	/* Free memory associated with the device. */
	free(p);
}

/**
 * Function description
 *
 * @return 0 on success, otherwise a Win32 error code
 */
FREERDP_ENTRY_POINT(UINT ios_freerdp_rdpsnd_client_subsystem_entry(
    PFREERDP_RDPSND_DEVICE_ENTRY_POINTS pEntryPoints))
{
	rdpsndIOSPlugin* p = (rdpsndIOSPlugin*)calloc(1, sizeof(rdpsndIOSPlugin));

	if (!p)
		return CHANNEL_RC_NO_MEMORY;

	p->device.Open = rdpsnd_ios_open;
	p->device.FormatSupported = rdpsnd_ios_format_supported;
	p->device.SetVolume = rdpsnd_ios_set_volume;
	p->device.Play = rdpsnd_ios_play;
	p->device.Start = rdpsnd_ios_start;
	p->device.Close = rdpsnd_ios_close;
	p->device.Free = rdpsnd_ios_free;
	pEntryPoints->pRegisterRdpsndDevice(pEntryPoints->rdpsnd, (rdpsndDevicePlugin*)p);
	return CHANNEL_RC_OK;
}