blob: 6a62647d40fea303c24e00302bb4173e411a5a6c [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);
56void enable_output_path_l(struct stream_out *out);
57int 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);
271 enable_output_path_l(out);
272 pthread_mutex_unlock(&adev->lock);
273 }
274
275 if (out->send_new_metadata) {
276 ALOGVV("send new gapless metadata");
277 compress_set_gapless_metadata(out->compr, &out->gapless_mdata);
278 out->send_new_metadata = 0;
279 }
280
281 ret = compress_write(out->compr, buffer, bytes);
282 ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret);
283 if (ret >= 0 && ret < (ssize_t)bytes) {
284 send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER);
285 }
286 if (out->offload_state != OFFLOAD_STATE_PLAYING) {
287 compress_start(out->compr);
288 out->offload_state = OFFLOAD_STATE_PLAYING;
289 }
290 pthread_mutex_unlock(&out->lock);
291#ifdef PREPROCESSING_ENABLED
292 if (in) {
293 /* This mutex was left locked iff in != NULL */
294 pthread_mutex_unlock(&adev->lock_inputs);
295 }
296#endif
297
298 return ret;
299}
300
301int out_get_render_offload_position(struct stream_out *out,
302 uint32_t *dsp_frames)
303{
304 *dsp_frames = 0;
305 if (dsp_frames != NULL) {
306 lock_output_stream(out);
307 if (out->compr != NULL) {
308 compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
309 &out->sample_rate);
310 ALOGVV("%s rendered frames %d sample_rate %d",
311 __func__, *dsp_frames, out->sample_rate);
312 }
313 pthread_mutex_unlock(&out->lock);
314 return 0;
315 } else
316 return -EINVAL;
317}
318
319int out_get_presentation_offload_position(struct stream_out *out, uint64_t *frames,
320 struct timespec *timestamp)
321{
322 int ret = -1;
323 unsigned long dsp_frames;
324
325 if (out->compr != NULL) {
326 compress_get_tstamp(out->compr, &dsp_frames,
327 &out->sample_rate);
328 ALOGVV("%s rendered frames %ld sample_rate %d",
329 __func__, dsp_frames, out->sample_rate);
330 *frames = dsp_frames;
331 ret = 0;
332 /* this is the best we can do */
333 clock_gettime(CLOCK_MONOTONIC, timestamp);
334 }
335
336 return ret;
337}
338
339int out_pause_offload(struct stream_out *out)
340{
341 int status = -ENOSYS;
342
343 lock_output_stream(out);
344 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
345 status = compress_pause(out->compr);
346 out->offload_state = OFFLOAD_STATE_PAUSED;
347 pthread_mutex_lock(&out->dev->lock);
348 status = disable_output_path_l(out);
349 pthread_mutex_unlock(&out->dev->lock);
350 }
351 pthread_mutex_unlock(&out->lock);
352
353 return status;
354}
355
356int out_resume_offload(struct stream_out *out)
357{
358 int status = -ENOSYS;
359
360 status = 0;
361 lock_output_stream(out);
362 if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
363 pthread_mutex_lock(&out->dev->lock);
364 enable_output_path_l(out);
365 pthread_mutex_unlock(&out->dev->lock);
366 status = compress_resume(out->compr);
367 out->offload_state = OFFLOAD_STATE_PLAYING;
368 }
369 pthread_mutex_unlock(&out->lock);
370
371 return status;
372}
373
374int out_drain_offload(struct stream_out *out, audio_drain_type_t type)
375{
376 int status = -ENOSYS;
377
378 lock_output_stream(out);
379 if (type == AUDIO_DRAIN_EARLY_NOTIFY)
380 status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
381 else
382 status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN);
383 pthread_mutex_unlock(&out->lock);
384
385 return status;
386}
387
388int out_flush_offload(struct stream_out *out)
389{
390 lock_output_stream(out);
391 if (out->offload_state == OFFLOAD_STATE_PLAYING) {
392 ALOGE("out_flush() called in wrong state %d", out->offload_state);
393 pthread_mutex_unlock(&out->lock);
394 return -ENOSYS;
395 }
396 if (out->offload_state == OFFLOAD_STATE_PAUSED) {
397 stop_compressed_output_l(out);
398 out->offload_state = OFFLOAD_STATE_PAUSED_FLUSHED;
399 }
400 pthread_mutex_unlock(&out->lock);
401 return 0;
402}
403
404int out_set_offload_volume(float left, float right)
405{
406 int offload_volume[2];//For stereo
407 struct mixer_ctl *ctl;
408 struct mixer *mixer = NULL;
409
410 offload_volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX);
411 offload_volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX);
412
413 mixer = mixer_open(MIXER_CARD);
414 if (!mixer) {
415 ALOGE("%s unable to open the mixer for card %d, aborting.",
416 __func__, MIXER_CARD);
417 return -EINVAL;
418 }
419 ctl = mixer_get_ctl_by_name(mixer, MIXER_CTL_COMPRESS_PLAYBACK_VOLUME);
420 if (!ctl) {
421 ALOGE("%s: Could not get ctl for mixer cmd - %s",
422 __func__, MIXER_CTL_COMPRESS_PLAYBACK_VOLUME);
423 mixer_close(mixer);
424 return -EINVAL;
425 }
426 ALOGV("out_set_volume set offload volume (%f, %f)", left, right);
427 mixer_ctl_set_array(ctl, offload_volume,
428 sizeof(offload_volume)/sizeof(offload_volume[0]));
429 mixer_close(mixer);
430 return 0;
431}