linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Szymon Janc <szymon.janc@tieto.com>
To: Andrzej Kaczmarek <andrzej.kaczmarek@tieto.com>
Cc: linux-bluetooth@vger.kernel.org
Subject: Re: [PATCH 1/5] android/hal-audio: Add open/close_endpoint helpers
Date: Sat, 01 Mar 2014 12:51:02 +0100	[thread overview]
Message-ID: <1913683.ZfTpeeEFIY@leonov> (raw)
In-Reply-To: <1393610480-5775-2-git-send-email-andrzej.kaczmarek@tieto.com>

Hi Andrzej,

On Friday 28 of February 2014 19:01:16 Andrzej Kaczmarek wrote:
> ---
>  android/hal-audio.c | 112
> +++++++++++++++++++++++++++++----------------------- 1 file changed, 63
> insertions(+), 49 deletions(-)
> 
> diff --git a/android/hal-audio.c b/android/hal-audio.c
> index e1f3f0d..36e8d2e 100644
> --- a/android/hal-audio.c
> +++ b/android/hal-audio.c
> @@ -914,6 +914,67 @@ static void unregister_endpoints(void)
>  	}
>  }
> 
> +static int set_blocking(int fd)
> +{
> +	int flags;
> +
> +	flags = fcntl(fd, F_GETFL, 0);
> +	if (flags < 0) {
> +		error("fcntl(F_GETFL): %s (%d)", strerror(errno), errno);
> +		return -errno;

I'm aware that this function is just moved up but just to not forget it :)

error() can modify errno so it should be saved before call.
(this should be fixed in multiple places)

Also strerror() is not thread safe, we should probably start using 
strerror_r() (eg. have wrapping macros that we could use in multithreaded 
code). BT HAL has same problem so I guess this can be done outside of this 
patchset.

> +	}
> +
> +	if (fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) < 0) {
> +		error("fcntl(F_SETFL): %s (%d)", strerror(errno), errno);
> +		return -errno;
> +	}
> +
> +	return 0;
> +}
> +
> +static bool open_endpoint(struct audio_endpoint *ep,
> +						struct audio_input_config *cfg)
> +{
> +	struct audio_preset *preset;
> +	const struct audio_codec *codec;
> +	uint16_t mtu;
> +	int fd;
> +
> +	if (ipc_open_stream_cmd(ep->id, &mtu, &fd, &preset) !=
> +							AUDIO_STATUS_SUCCESS)
> +		return false;
> +
> +	if (!preset || fd < 0)

If this can happen despite ipc_open_stream_cmd() returned success then freeing 
preset or closing fd should be handled here.

> +		return false;
> +
> +	if (set_blocking(fd) < 0) {
> +		free(preset);

Shouldn't fd be closed here?

> +		return false;
> +	}
> +
> +	ep->fd = fd;
> +
> +	codec = ep->codec;
> +	codec->init(preset, mtu, &ep->codec_data);
> +	codec->get_config(ep->codec_data, cfg);
> +
> +	free(preset);
> +
> +	return true;
> +}
> +
> +static void close_endpoint(struct audio_endpoint *ep)
> +{
> +	ipc_close_stream_cmd(ep->id);
> +	if (ep->fd >= 0) {
> +		close(ep->fd);
> +		ep->fd = -1;
> +	}
> +
> +	ep->codec->cleanup(ep->codec_data);
> +	ep->codec_data = NULL;
> +}
> +
>  static void downmix_to_mono(struct a2dp_stream_out *out, const uint8_t
> *buffer, size_t bytes)
>  {
> @@ -1260,24 +1321,6 @@ static int in_remove_audio_effect(const struct
> audio_stream *stream, return -ENOSYS;
>  }
> 
> -static int set_blocking(int fd)
> -{
> -	int flags;
> -
> -	flags = fcntl(fd, F_GETFL, 0);
> -	if (flags < 0) {
> -		error("fcntl(F_GETFL): %s (%d)", strerror(errno), errno);
> -		return -errno;
> -	}
> -
> -	if (fcntl(fd, F_SETFL, flags & ~O_NONBLOCK) < 0) {
> -		error("fcntl(F_SETFL): %s (%d)", strerror(errno), errno);
> -		return -errno;
> -	}
> -
> -	return 0;
> -}
> -
>  static int audio_open_output_stream(struct audio_hw_device *dev,
>  					audio_io_handle_t handle,
>  					audio_devices_t devices,
> @@ -1288,10 +1331,6 @@ static int audio_open_output_stream(struct
> audio_hw_device *dev, {
>  	struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
>  	struct a2dp_stream_out *out;
> -	struct audio_preset *preset;
> -	const struct audio_codec *codec;
> -	uint16_t mtu;
> -	int fd;
> 
>  	out = calloc(1, sizeof(struct a2dp_stream_out));
>  	if (!out)
> @@ -1319,29 +1358,12 @@ static int audio_open_output_stream(struct
> audio_hw_device *dev, /* TODO: for now we always use endpoint 0 */
>  	out->ep = &audio_endpoints[0];
> 
> -	if (ipc_open_stream_cmd(out->ep->id, &mtu, &fd, &preset) !=
> -							AUDIO_STATUS_SUCCESS)
> -		goto fail;
> -
> -	if (!preset || fd < 0)
> -		goto fail;
> -
> -	if (set_blocking(fd) < 0) {
> -		free(preset);
> +	if (!open_endpoint(out->ep, &out->cfg))
>  		goto fail;
> -	}
> -
> -	out->ep->fd = fd;
> -	codec = out->ep->codec;
> -
> -	codec->init(preset, mtu, &out->ep->codec_data);
> -	codec->get_config(out->ep->codec_data, &out->cfg);
> 
>  	DBG("rate=%d channels=%d format=%d", out->cfg.rate,
>  					out->cfg.channels, out->cfg.format);
> 
> -	free(preset);
> -
>  	if (out->cfg.channels == AUDIO_CHANNEL_OUT_MONO) {
>  		out->downmix_buf = malloc(FIXED_BUFFER_SIZE / 2);
>  		if (!out->downmix_buf)
> @@ -1367,18 +1389,10 @@ static void audio_close_output_stream(struct
> audio_hw_device *dev, {
>  	struct a2dp_audio_dev *a2dp_dev = (struct a2dp_audio_dev *) dev;
>  	struct a2dp_stream_out *out = (struct a2dp_stream_out *) stream;
> -	struct audio_endpoint *ep = a2dp_dev->out->ep;
> 
>  	DBG("");
> 
> -	ipc_close_stream_cmd(ep->id);
> -	if (ep->fd >= 0) {
> -		close(ep->fd);
> -		ep->fd = -1;
> -	}
> -
> -	ep->codec->cleanup(ep->codec_data);
> -	ep->codec_data = NULL;
> +	close_endpoint(a2dp_dev->out->ep);
> 
>  	free(out->downmix_buf);

-- 
BR
Szymon Janc

  reply	other threads:[~2014-03-01 11:51 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-28 18:01 [PATCH 0/5] Refactor codec handling Andrzej Kaczmarek
2014-02-28 18:01 ` [PATCH 1/5] android/hal-audio: Add open/close_endpoint helpers Andrzej Kaczmarek
2014-03-01 11:51   ` Szymon Janc [this message]
2014-03-03  8:32     ` Andrzej Kaczmarek
2014-02-28 18:01 ` [PATCH 2/5] android/hal-audio: Add encode_mediapacket function Andrzej Kaczmarek
2014-02-28 18:01 ` [PATCH 3/5] android/hal-audio: Write and sync in common code Andrzej Kaczmarek
2014-03-01 12:00   ` Szymon Janc
2014-02-28 18:01 ` [PATCH 4/5] android/hal-audio: Use payload length for codec init Andrzej Kaczmarek
2014-02-28 18:01 ` [PATCH 5/5] android/hal-audio: Provide better audio synchronization Andrzej Kaczmarek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1913683.ZfTpeeEFIY@leonov \
    --to=szymon.janc@tieto.com \
    --cc=andrzej.kaczmarek@tieto.com \
    --cc=linux-bluetooth@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).