blob: 5ce865eb106c804df105a5592a2c7b36ebd4f512 [file] [log] [blame]
Christopher N. Hesse757ac412017-01-28 14:42:48 +01001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 * Copyright (C) 2017 Christopher N. Hesse <raymanfx@gmail.com>
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#define LOG_TAG "audio_hw_primary"
19/*#define LOG_NDEBUG 0*/
20/*#define VERY_VERY_VERBOSE_LOGGING*/
21#ifdef VERY_VERY_VERBOSE_LOGGING
22#define ALOGVV ALOGV
23#else
24#define ALOGVV(a...) do { } while(0)
25#endif
26
27#define _GNU_SOURCE
28#include <errno.h>
29#include <pthread.h>
30#include <stdint.h>
31#include <stdlib.h>
32#include <sys/resource.h>
33#include <sys/prctl.h>
34
35#include <cutils/log.h>
36#include <cutils/str_parms.h>
37#include <cutils/sched_policy.h>
38
39#include <system/thread_defs.h>
Christopher N. Hesse757ac412017-01-28 14:42:48 +010040
Christopher N. Hessed23c6b52017-01-28 14:18:10 +010041#include <samsung_audio.h>
42
43#include "audio_hw.h"
Christopher N. Hesse757ac412017-01-28 14:42:48 +010044#include "sound/compress_params.h"
45
46#define MIXER_CTL_COMPRESS_PLAYBACK_VOLUME "Compress Playback Volume"
47
48
49/* Prototypes */
50void lock_input_stream(struct stream_in *in);
51void lock_output_stream(struct stream_out *out);
52int disable_snd_device(struct audio_device *adev,
53 struct audio_usecase *uc_info,
54 snd_device_t snd_device,
55 bool update_mixer);
Andreas Schneider56204f62017-01-31 08:17:32 +010056int enable_output_path_l(struct stream_out *out);
Christopher N. Hesse757ac412017-01-28 14:42:48 +010057int disable_output_path_l(struct stream_out *out);
58
59/* must be called with out->lock locked */
60static int send_offload_cmd_l(struct stream_out* out, int command)
61{
62 struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
63
64 ALOGVV("%s %d", __func__, command);
65
66 cmd->cmd = command;
67 list_add_tail(&out->offload_cmd_list, &cmd->node);
68 pthread_cond_signal(&out->offload_cond);
69 return 0;
70}
71
72/* must be called iwth out->lock locked */
73void stop_compressed_output_l(struct stream_out *out)
74{
75 out->send_new_metadata = 1;
76 if (out->compr != NULL) {
77 compress_stop(out->compr);
78 while (out->offload_thread_blocked) {
79 pthread_cond_wait(&out->cond, &out->lock);
80 }
81 }
82}
83
84static void *offload_thread_loop(void *context)
85{
86 struct stream_out *out = (struct stream_out *) context;
87 struct listnode *item;
88
89 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
90 set_sched_policy(0, SP_FOREGROUND);
91 prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
92
93 ALOGV("%s", __func__);
94 lock_output_stream(out);
95 for (;;) {
96 struct offload_cmd *cmd = NULL;
97 stream_callback_event_t event;
98 bool send_callback = false;
99
100 ALOGVV("%s offload_cmd_list %d out->offload_state %d",
101 __func__, list_empty(&out->offload_cmd_list),
102 out->offload_state);
103 if (list_empty(&out->offload_cmd_list)) {
104 ALOGV("%s SLEEPING", __func__);
105 pthread_cond_wait(&out->offload_cond, &out->lock);
106 ALOGV("%s RUNNING", __func__);
107 continue;
108 }
109
110 item = list_head(&out->offload_cmd_list);
111 cmd = node_to_item(item, struct offload_cmd, node);
112 list_remove(item);
113
114 ALOGVV("%s STATE %d CMD %d out->compr %p",
115 __func__, out->offload_state, cmd->cmd, out->compr);
116
117 if (cmd->cmd == OFFLOAD_CMD_EXIT) {
118 free(cmd);
119 break;
120 }
121
122 if (out->compr == NULL) {
123 ALOGE("%s: Compress handle is NULL", __func__);
124 pthread_cond_signal(&out->cond);
125 continue;
126 }
127 out->offload_thread_blocked = true;
128 pthread_mutex_unlock(&out->lock);
129 send_callback = false;
130 switch(cmd->cmd) {
131 case OFFLOAD_CMD_WAIT_FOR_BUFFER:
132 compress_wait(out->compr, -1);
133 send_callback = true;
134 event = STREAM_CBK_EVENT_WRITE_READY;
135 break;
136 case OFFLOAD_CMD_PARTIAL_DRAIN:
137 compress_next_track(out->compr);
138 compress_partial_drain(out->compr);
139 send_callback = true;
140 event = STREAM_CBK_EVENT_DRAIN_READY;
141 break;
142 case OFFLOAD_CMD_DRAIN:
143 compress_drain(out->compr);
144 send_callback = true;
145 event = STREAM_CBK_EVENT_DRAIN_READY;
146 break;
147 default:
148 ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
149 break;
150 }
151 lock_output_stream(out);
152 out->offload_thread_blocked = false;
153 pthread_cond_signal(&out->cond);
154 if (send_callback) {
155 out->offload_callback(event, NULL, out->offload_cookie);
156 }
157 free(cmd);
158 }
159
160 pthread_cond_signal(&out->cond);
161 while (!list_empty(&out->offload_cmd_list)) {
162 item = list_head(&out->offload_cmd_list);
163 list_remove(item);
164 free(node_to_item(item, struct offload_cmd, node));
165 }
166 pthread_mutex_unlock(&out->lock);
167
168 return NULL;
169}
170
171int create_offload_callback_thread(struct stream_out *out)
172{
173 pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
174 list_init(&out->offload_cmd_list);
175 pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
176 offload_thread_loop, out);
177 return 0;
178}
179
180int destroy_offload_callback_thread(struct stream_out *out)
181{
182 lock_output_stream(out);
183 send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
184
185 pthread_mutex_unlock(&out->lock);
186 pthread_join(out->offload_thread, (void **) NULL);
187 pthread_cond_destroy(&out->offload_cond);
188
189 return 0;
190}
191
192int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
193{
194 int ret = 0;
195 char value[32];
196 struct compr_gapless_mdata tmp_mdata;
197
198 if (!out || !parms) {
199 return -EINVAL;
200 }
201
202 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
203 if (ret >= 0) {
204 tmp_mdata.encoder_delay = atoi(value); /* what is a good limit check? */
205 } else {
206 return -EINVAL;
207 }
208
209 ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
210 if (ret >= 0) {
211 tmp_mdata.encoder_padding = atoi(value);
212 } else {
213 return -EINVAL;
214 }
215
216 out->gapless_mdata = tmp_mdata;
217 out->send_new_metadata = 1;
218 ALOGV("%s new encoder delay %u and padding %u", __func__,
219 out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
220
221 return 0;
222}
223
224int stop_output_offload_stream(struct stream_out *out, bool *disable)
225{
226 int ret = 0;
227 struct audio_device *adev = out->dev;
228
229 if (adev->offload_fx_stop_output != NULL) {
230 adev->offload_fx_stop_output(out->handle);
231
232 if (out->offload_state == OFFLOAD_STATE_PAUSED ||
233 out->offload_state == OFFLOAD_STATE_PAUSED_FLUSHED)
234 *disable = false;
235 out->offload_state = OFFLOAD_STATE_IDLE;
236 }
237
238 return ret;
239}
240
241int out_set_offload_parameters(struct audio_device *adev, struct audio_usecase *uc_info)
242{
243 int ret = 0;
244
245 if (uc_info == NULL) {
246 ALOGE("%s: Could not find the usecase (%d) in the list",
247 __func__, USECASE_AUDIO_PLAYBACK);
248 ret = -1;
249 }
250 if (uc_info != NULL && uc_info->out_snd_device == SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES) {
251 ALOGV("Out_set_param: spk+headset enabled\n");
252 uc_info->out_snd_device = SND_DEVICE_OUT_HEADPHONES;
253 disable_snd_device(adev, uc_info, SND_DEVICE_OUT_SPEAKER, true);
254 }
255
256 return ret;
257}
258
259ssize_t out_write_offload(struct audio_stream_out *stream, const void *buffer,
260 size_t bytes)
261{
262 struct stream_out *out = (struct stream_out *)stream;
263 struct audio_device *adev = out->dev;
264 ssize_t ret = 0;
265
266 ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes);
267
268 if (out->offload_state == OFFLOAD_STATE_PAUSED_FLUSHED) {
269 ALOGV("start offload write from pause state");
270 pthread_mutex_lock(&adev->lock);
Andreas Schneider56204f62017-01-31 08:17:32 +0100271 ret = enable_output_path_l(out);
Christopher N. Hesse757ac412017-01-28 14:42:48 +0100272 pthread_mutex_unlock(&adev->lock);
Andreas Schneider56204f62017-01-31 08:17:32 +0100273 if (ret != 0) {
274 return ret;
275 }
Christopher N. Hesse757ac412017-01-28 14:42:48 +0100276 }
277
278 if (out->send_new_metadata) {
279 ALOGVV("send new gapless metadata");
280 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
281 out->send_new_metadata = 0;
282 }
283
284 ret = compress_write(out->compr, buffer, bytes);
285 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
286 if (ret >= 0 && ret < (ssize_t)bytes) {
287 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
288 }
289 if (out->offload_state != OFFLOAD_STATE_PLAYING) {
290 compress_start(out->compr);
291 out->offload_state = OFFLOAD_STATE_PLAYING;
292 }
293 pthread_mutex_unlock(&out->lock);
294#ifdef PREPROCESSING_ENABLED
295 if (in) {
296 /* This mutex was left locked iff in != NULL */
297 pthread_mutex_unlock(&adev->lock_inputs);
298 }
299#endif
300
301 return ret;
302}
303
304int out_get_render_offload_position(struct stream_out *out,
305 uint32_t *dsp_frames)
306{
307 *dsp_frames = 0;
308 if (dsp_frames != NULL) {
309 lock_output_stream(out);
310 if (out->compr != NULL) {
311 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
312 &out->sample_rate);
313 ALOGVV("%s rendered frames %d sample_rate %d",
314 __func__, *dsp_frames, out->sample_rate);
315 }
316 pthread_mutex_unlock(&out->lock);
317 return 0;
318 } else
319 return -EINVAL;
320}
321
322int out_get_presentation_offload_position(struct stream_out *out, uint64_t *frames,
323 struct timespec *timestamp)
324{
325 int ret = -1;
326 unsigned long dsp_frames;
327
328 if (out->compr != NULL) {
329 compress_get_tstamp(out->compr, &dsp_frames,
330 &out->sample_rate);
331 ALOGVV("%s rendered frames %ld sample_rate %d",
332 __func__, dsp_frames, out->sample_rate);
333 *frames = dsp_frames;
334 ret = 0;
335 /* this is the best we can do */
336 clock_gettime(CLOCK_MONOTONIC, timestamp);
337 }
338
339 return ret;
340}
341
342int out_pause_offload(struct stream_out *out)
343{
344 int status = -ENOSYS;
345
346 lock_output_stream(out);
347 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
348 status = compress_pause(out->compr);
349 out->offload_state = OFFLOAD_STATE_PAUSED;
350 pthread_mutex_lock(&out->dev->lock);
351 status = disable_output_path_l(out);
352 pthread_mutex_unlock(&out->dev->lock);
353 }
354 pthread_mutex_unlock(&out->lock);
355
356 return status;
357}
358
359int out_resume_offload(struct stream_out *out)
360{
361 int status = -ENOSYS;
362
363 status = 0;
364 lock_output_stream(out);
365 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
366 pthread_mutex_lock(&out->dev->lock);
367 enable_output_path_l(out);
368 pthread_mutex_unlock(&out->dev->lock);
369 status = compress_resume(out->compr);
370 out->offload_state = OFFLOAD_STATE_PLAYING;
371 }
372 pthread_mutex_unlock(&out->lock);
373
374 return status;
375}
376
377int out_drain_offload(struct stream_out *out, audio_drain_type_t type)
378{
379 int status = -ENOSYS;
380
381 lock_output_stream(out);
382 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
383 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
384 else
385 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
386 pthread_mutex_unlock(&out->lock);
387
388 return status;
389}
390
391int out_flush_offload(struct stream_out *out)
392{
393 lock_output_stream(out);
394 if (out->offload_state == OFFLOAD_STATE_PLAYING) {
395 ALOGE("out_flush() called in wrong state %d", out->offload_state);
396 pthread_mutex_unlock(&out->lock);
397 return -ENOSYS;
398 }
399 if (out->offload_state == OFFLOAD_STATE_PAUSED) {
400 stop_compressed_output_l(out);
401 out->offload_state = OFFLOAD_STATE_PAUSED_FLUSHED;
402 }
403 pthread_mutex_unlock(&out->lock);
404 return 0;
405}
406
407int out_set_offload_volume(float left, float right)
408{
409 int offload_volume[2];//For stereo
410 struct mixer_ctl *ctl;
411 struct mixer *mixer = NULL;
412
413 offload_volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
414 offload_volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
415
416 mixer = mixer_open(MIXER_CARD);
417 if (!mixer) {
418 ALOGE("%s unable to open the mixer for card %d, aborting.",
419 __func__, MIXER_CARD);
420 return -EINVAL;
421 }
422 ctl = mixer_get_ctl_by_name(mixer, MIXER_CTL_COMPRESS_PLAYBACK_VOLUME);
423 if (!ctl) {
424 ALOGE("%s: Could not get ctl for mixer cmd - %s",
425 __func__, MIXER_CTL_COMPRESS_PLAYBACK_VOLUME);
426 mixer_close(mixer);
427 return -EINVAL;
428 }
429 ALOGV("out_set_volume set offload volume (%f, %f)", left, right);
430 mixer_ctl_set_array(ctl, offload_volume,
431 sizeof(offload_volume)/sizeof(offload_volume[0]));
432 mixer_close(mixer);
433 return 0;
434}