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