Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit dc719127 authored by Eric Laurent's avatar Eric Laurent Committed by Android (Google) Code Review
Browse files

Merge "Revert "audio HAL: fix thread starvation"" into mnc-dev

parents a0a75dad 6010f700
Loading
Loading
Loading
Loading
+18 −37
Original line number Original line Diff line number Diff line
@@ -807,20 +807,6 @@ error_config:
    return ret;
    return ret;
}
}


void lock_input_stream(struct stream_in *in)
{
    pthread_mutex_lock(&in->pre_lock);
    pthread_mutex_lock(&in->lock);
    pthread_mutex_unlock(&in->pre_lock);
}

void lock_output_stream(struct stream_out *out)
{
    pthread_mutex_lock(&out->pre_lock);
    pthread_mutex_lock(&out->lock);
    pthread_mutex_unlock(&out->pre_lock);
}

/* must be called with out->lock locked */
/* must be called with out->lock locked */
static int send_offload_cmd_l(struct stream_out* out, int command)
static int send_offload_cmd_l(struct stream_out* out, int command)
{
{
@@ -861,7 +847,7 @@ static void *offload_thread_loop(void *context)
    prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
    prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);


    ALOGV("%s", __func__);
    ALOGV("%s", __func__);
    lock_output_stream(out);
    pthread_mutex_lock(&out->lock);
    for (;;) {
    for (;;) {
        struct offload_cmd *cmd = NULL;
        struct offload_cmd *cmd = NULL;
        stream_callback_event_t event;
        stream_callback_event_t event;
@@ -920,7 +906,7 @@ static void *offload_thread_loop(void *context)
            ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
            ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
            break;
            break;
        }
        }
        lock_output_stream(out);
        pthread_mutex_lock(&out->lock);
        out->offload_thread_blocked = false;
        out->offload_thread_blocked = false;
        pthread_cond_signal(&out->cond);
        pthread_cond_signal(&out->cond);
        if (send_callback) {
        if (send_callback) {
@@ -952,7 +938,7 @@ static int create_offload_callback_thread(struct stream_out *out)


static int destroy_offload_callback_thread(struct stream_out *out)
static int destroy_offload_callback_thread(struct stream_out *out)
{
{
    lock_output_stream(out);
    pthread_mutex_lock(&out->lock);
    stop_compressed_output_l(out);
    stop_compressed_output_l(out);
    send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
    send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);


@@ -1275,7 +1261,7 @@ static int out_standby(struct audio_stream *stream)
    ALOGV("%s: enter: usecase(%d: %s)", __func__,
    ALOGV("%s: enter: usecase(%d: %s)", __func__,
          out->usecase, use_case_table[out->usecase]);
          out->usecase, use_case_table[out->usecase]);


    lock_output_stream(out);
    pthread_mutex_lock(&out->lock);
    if (!out->standby) {
    if (!out->standby) {
        pthread_mutex_lock(&adev->lock);
        pthread_mutex_lock(&adev->lock);
        out->standby = true;
        out->standby = true;
@@ -1361,7 +1347,7 @@ static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
    ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
    ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
    if (ret >= 0) {
    if (ret >= 0) {
        val = atoi(value);
        val = atoi(value);
        lock_output_stream(out);
        pthread_mutex_lock(&out->lock);
        pthread_mutex_lock(&adev->lock);
        pthread_mutex_lock(&adev->lock);


        /*
        /*
@@ -1525,7 +1511,7 @@ static ssize_t out_write_for_no_output(struct audio_stream_out *stream,
    /* No Output device supported other than BT for playback.
    /* No Output device supported other than BT for playback.
     * Sleep for the amount of buffer duration
     * Sleep for the amount of buffer duration
     */
     */
    lock_output_stream(out);
    pthread_mutex_lock(&out->lock);
    usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
    usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) /
            out_get_sample_rate(&out->stream.common));
            out_get_sample_rate(&out->stream.common));
    pthread_mutex_unlock(&out->lock);
    pthread_mutex_unlock(&out->lock);
@@ -1540,7 +1526,7 @@ static ssize_t out_write(struct audio_stream_out *stream, const void *buffer,
    struct audio_device *adev = out->dev;
    struct audio_device *adev = out->dev;
    ssize_t ret = 0;
    ssize_t ret = 0;


    lock_output_stream(out);
    pthread_mutex_lock(&out->lock);
    if (out->standby) {
    if (out->standby) {
        out->standby = false;
        out->standby = false;
        pthread_mutex_lock(&adev->lock);
        pthread_mutex_lock(&adev->lock);
@@ -1607,7 +1593,7 @@ static int out_get_render_position(const struct audio_stream_out *stream,
    struct stream_out *out = (struct stream_out *)stream;
    struct stream_out *out = (struct stream_out *)stream;
    *dsp_frames = 0;
    *dsp_frames = 0;
    if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
    if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) {
        lock_output_stream(out);
        pthread_mutex_lock(&out->lock);
        if (out->compr != NULL) {
        if (out->compr != NULL) {
            compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
            compress_get_tstamp(out->compr, (unsigned long *)dsp_frames,
                    &out->sample_rate);
                    &out->sample_rate);
@@ -1645,7 +1631,7 @@ static int out_get_presentation_position(const struct audio_stream_out *stream,
    int ret = -1;
    int ret = -1;
    unsigned long dsp_frames;
    unsigned long dsp_frames;


    lock_output_stream(out);
    pthread_mutex_lock(&out->lock);


    if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
    if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
        if (out->compr != NULL) {
        if (out->compr != NULL) {
@@ -1689,7 +1675,7 @@ static int out_set_callback(struct audio_stream_out *stream,
    struct stream_out *out = (struct stream_out *)stream;
    struct stream_out *out = (struct stream_out *)stream;


    ALOGV("%s", __func__);
    ALOGV("%s", __func__);
    lock_output_stream(out);
    pthread_mutex_lock(&out->lock);
    out->offload_callback = callback;
    out->offload_callback = callback;
    out->offload_cookie = cookie;
    out->offload_cookie = cookie;
    pthread_mutex_unlock(&out->lock);
    pthread_mutex_unlock(&out->lock);
@@ -1702,7 +1688,7 @@ static int out_pause(struct audio_stream_out* stream)
    int status = -ENOSYS;
    int status = -ENOSYS;
    ALOGV("%s", __func__);
    ALOGV("%s", __func__);
    if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
    if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
        lock_output_stream(out);
        pthread_mutex_lock(&out->lock);
        if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
        if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) {
            status = compress_pause(out->compr);
            status = compress_pause(out->compr);
            out->offload_state = OFFLOAD_STATE_PAUSED;
            out->offload_state = OFFLOAD_STATE_PAUSED;
@@ -1719,7 +1705,7 @@ static int out_resume(struct audio_stream_out* stream)
    ALOGV("%s", __func__);
    ALOGV("%s", __func__);
    if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
    if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
        status = 0;
        status = 0;
        lock_output_stream(out);
        pthread_mutex_lock(&out->lock);
        if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
        if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) {
            status = compress_resume(out->compr);
            status = compress_resume(out->compr);
            out->offload_state = OFFLOAD_STATE_PLAYING;
            out->offload_state = OFFLOAD_STATE_PLAYING;
@@ -1735,7 +1721,7 @@ static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type )
    int status = -ENOSYS;
    int status = -ENOSYS;
    ALOGV("%s", __func__);
    ALOGV("%s", __func__);
    if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
    if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
        lock_output_stream(out);
        pthread_mutex_lock(&out->lock);
        if (type == AUDIO_DRAIN_EARLY_NOTIFY)
        if (type == AUDIO_DRAIN_EARLY_NOTIFY)
            status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
            status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN);
        else
        else
@@ -1750,7 +1736,7 @@ static int out_flush(struct audio_stream_out* stream)
    struct stream_out *out = (struct stream_out *)stream;
    struct stream_out *out = (struct stream_out *)stream;
    ALOGV("%s", __func__);
    ALOGV("%s", __func__);
    if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
    if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
        lock_output_stream(out);
        pthread_mutex_lock(&out->lock);
        stop_compressed_output_l(out);
        stop_compressed_output_l(out);
        pthread_mutex_unlock(&out->lock);
        pthread_mutex_unlock(&out->lock);
        return 0;
        return 0;
@@ -1802,8 +1788,7 @@ static int in_standby(struct audio_stream *stream)
    struct audio_device *adev = in->dev;
    struct audio_device *adev = in->dev;
    int status = 0;
    int status = 0;
    ALOGV("%s: enter", __func__);
    ALOGV("%s: enter", __func__);

    pthread_mutex_lock(&in->lock);
    lock_input_stream(in);


    if (!in->standby && in->is_st_session) {
    if (!in->standby && in->is_st_session) {
        ALOGD("%s: sound trigger pcm stop lab", __func__);
        ALOGD("%s: sound trigger pcm stop lab", __func__);
@@ -1848,8 +1833,7 @@ static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)


    ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));
    ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value));


    lock_input_stream(in);
    pthread_mutex_lock(&in->lock);

    pthread_mutex_lock(&adev->lock);
    pthread_mutex_lock(&adev->lock);
    if (ret >= 0) {
    if (ret >= 0) {
        val = atoi(value);
        val = atoi(value);
@@ -1897,8 +1881,7 @@ static ssize_t in_read(struct audio_stream_in *stream, void *buffer,
    struct audio_device *adev = in->dev;
    struct audio_device *adev = in->dev;
    int i, ret = -1;
    int i, ret = -1;


    lock_input_stream(in);
    pthread_mutex_lock(&in->lock);

    if (in->is_st_session) {
    if (in->is_st_session) {
        ALOGVV(" %s: reading on st session bytes=%d", __func__, bytes);
        ALOGVV(" %s: reading on st session bytes=%d", __func__, bytes);
        /* Read from sound trigger HAL */
        /* Read from sound trigger HAL */
@@ -1962,7 +1945,7 @@ static int add_remove_audio_effect(const struct audio_stream *stream,
    if (status != 0)
    if (status != 0)
        return status;
        return status;


    lock_input_stream(in);
    pthread_mutex_lock(&in->lock);
    pthread_mutex_lock(&in->dev->lock);
    pthread_mutex_lock(&in->dev->lock);
    if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
    if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) &&
            in->enable_aec != enable &&
            in->enable_aec != enable &&
@@ -2210,7 +2193,6 @@ static int adev_open_output_stream(struct audio_hw_device *dev,
    /* out->written = 0; by calloc() */
    /* out->written = 0; by calloc() */


    pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
    pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
    pthread_mutex_init(&out->pre_lock, (const pthread_mutexattr_t *) NULL);
    pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);
    pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL);


    config->format = out->stream.common.get_format(&out->stream.common);
    config->format = out->stream.common.get_format(&out->stream.common);
@@ -2460,7 +2442,6 @@ static int adev_open_input_stream(struct audio_hw_device *dev,
    in = (struct stream_in *)calloc(1, sizeof(struct stream_in));
    in = (struct stream_in *)calloc(1, sizeof(struct stream_in));


    pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
    pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
    pthread_mutex_init(&in->pre_lock, (const pthread_mutexattr_t *) NULL);


    in->stream.common.get_sample_rate = in_get_sample_rate;
    in->stream.common.get_sample_rate = in_get_sample_rate;
    in->stream.common.set_sample_rate = in_set_sample_rate;
    in->stream.common.set_sample_rate = in_set_sample_rate;
+0 −2
Original line number Original line Diff line number Diff line
@@ -123,7 +123,6 @@ struct offload_cmd {
struct stream_out {
struct stream_out {
    struct audio_stream_out stream;
    struct audio_stream_out stream;
    pthread_mutex_t lock; /* see note below on mutex acquisition order */
    pthread_mutex_t lock; /* see note below on mutex acquisition order */
    pthread_mutex_t pre_lock; /* acquire before lock to avoid DOS by playback thread */
    pthread_cond_t  cond;
    pthread_cond_t  cond;
    struct pcm_config config;
    struct pcm_config config;
    struct compr_config compr_config;
    struct compr_config compr_config;
@@ -162,7 +161,6 @@ struct stream_out {
struct stream_in {
struct stream_in {
    struct audio_stream_in stream;
    struct audio_stream_in stream;
    pthread_mutex_t lock; /* see note below on mutex acquisition order */
    pthread_mutex_t lock; /* see note below on mutex acquisition order */
    pthread_mutex_t pre_lock; /* acquire before lock to avoid DOS by capture thread */
    struct pcm_config config;
    struct pcm_config config;
    struct pcm *pcm;
    struct pcm *pcm;
    int standby;
    int standby;