OSDN Git Service

audio: different frame size calculation for input and output
authorEric Laurent <elaurent@google.com>
Wed, 2 Jul 2014 20:45:32 +0000 (13:45 -0700)
committerEric Laurent <elaurent@google.com>
Thu, 3 Jul 2014 23:51:09 +0000 (16:51 -0700)
Bug: 15000850.
Change-Id: I7813e99a0b7ce613cc3b7d7c95be0525cb2d6c81

include/hardware/audio.h
modules/audio/audio_hw.c
modules/audio_remote_submix/audio_hw.cpp
modules/usbaudio/audio_hw.c

index 6558dda..7ff5b78 100644 (file)
@@ -428,7 +428,10 @@ typedef struct audio_stream_in audio_stream_in_t;
 
 /**
  * return the frame size (number of bytes per sample).
+ *
+ * Deprecated: use audio_stream_out_frame_size() or audio_stream_in_frame_size() instead.
  */
+__attribute__((__deprecated__))
 static inline size_t audio_stream_frame_size(const struct audio_stream *s)
 {
     size_t chan_samp_sz;
@@ -442,6 +445,37 @@ static inline size_t audio_stream_frame_size(const struct audio_stream *s)
     return sizeof(int8_t);
 }
 
+/**
+ * return the frame size (number of bytes per sample) of an output stream.
+ */
+static inline size_t audio_stream_out_frame_size(const struct audio_stream_out *s)
+{
+    size_t chan_samp_sz;
+    audio_format_t format = s->common.get_format(&s->common);
+
+    if (audio_is_linear_pcm(format)) {
+        chan_samp_sz = audio_bytes_per_sample(format);
+        return audio_channel_count_from_out_mask(s->common.get_channels(&s->common)) * chan_samp_sz;
+    }
+
+    return sizeof(int8_t);
+}
+
+/**
+ * return the frame size (number of bytes per sample) of an input stream.
+ */
+static inline size_t audio_stream_in_frame_size(const struct audio_stream_in *s)
+{
+    size_t chan_samp_sz;
+    audio_format_t format = s->common.get_format(&s->common);
+
+    if (audio_is_linear_pcm(format)) {
+        chan_samp_sz = audio_bytes_per_sample(format);
+        return audio_channel_count_from_in_mask(s->common.get_channels(&s->common)) * chan_samp_sz;
+    }
+
+    return sizeof(int8_t);
+}
 
 /**********************************************************************/
 
index 3051519..2f44d95 100644 (file)
@@ -105,7 +105,7 @@ static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
                          size_t bytes)
 {
     /* XXX: fake timing for audio output */
-    usleep(bytes * 1000000 / audio_stream_frame_size(&stream->common) /
+    usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
            out_get_sample_rate(&stream->common));
     return bytes;
 }
@@ -193,7 +193,7 @@ static ssize_t in_read(struct audio_stream_in *stream, void* buffer,
                        size_t bytes)
 {
     /* XXX: fake timing for audio input */
-    usleep(bytes * 1000000 / audio_stream_frame_size(&stream->common) /
+    usleep(bytes * 1000000 / audio_stream_in_frame_size(stream) /
            in_get_sample_rate(&stream->common));
     return bytes;
 }
index 2a97347..c7e4305 100644 (file)
@@ -416,8 +416,8 @@ static void submix_audio_device_create_pipe(struct submix_audio_device * const r
         device_config->buffer_size_frames = sink->maxFrames();
         device_config->buffer_period_size_frames = device_config->buffer_size_frames /
                 buffer_period_count;
-        if (in) device_config->pipe_frame_size = audio_stream_frame_size(&in->stream.common);
-        if (out) device_config->pipe_frame_size = audio_stream_frame_size(&out->stream.common);
+        if (in) device_config->pipe_frame_size = audio_stream_in_frame_size(&in->stream);
+        if (out) device_config->pipe_frame_size = audio_stream_out_frame_size(&out->stream);
 #if ENABLE_CHANNEL_CONVERSION
         // Calculate the pipe frame size based upon the number of channels.
         device_config->pipe_frame_size = (device_config->pipe_frame_size * pipe_channel_count) /
@@ -527,9 +527,9 @@ static bool submix_open_validate(const struct submix_audio_device * const rsxade
 // Calculate the maximum size of the pipe buffer in frames for the specified stream.
 static size_t calculate_stream_pipe_size_in_frames(const struct audio_stream *stream,
                                                    const struct submix_config *config,
-                                                   const size_t pipe_frames)
+                                                   const size_t pipe_frames,
+                                                   const size_t stream_frame_size)
 {
-    const size_t stream_frame_size = audio_stream_frame_size(stream);
     const size_t pipe_frame_size = config->pipe_frame_size;
     const size_t max_frame_size = max(stream_frame_size, pipe_frame_size);
     return (pipe_frames * config->pipe_frame_size) / max_frame_size;
@@ -557,7 +557,7 @@ static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
     // The sample rate of the stream can't be changed once it's set since this would change the
     // output buffer size and hence break playback to the shared pipe.
     if (rate != out->dev->config.output_sample_rate) {
-        ALOGE("out_set_sample_rate(rate=%u) resampling enabled can't change sample rate from "
+        ALOGE("out_set_sample_rate() resampling enabled can't change sample rate from "
               "%u to %u", out->dev->config.output_sample_rate, rate);
         return -ENOSYS;
     }
@@ -576,9 +576,11 @@ static size_t out_get_buffer_size(const struct audio_stream *stream)
     const struct submix_stream_out * const out = audio_stream_get_submix_stream_out(
             const_cast<struct audio_stream *>(stream));
     const struct submix_config * const config = &out->dev->config;
+    const size_t stream_frame_size =
+                            audio_stream_out_frame_size((const struct audio_stream_out *)stream);
     const size_t buffer_size_frames = calculate_stream_pipe_size_in_frames(
-        stream, config, config->buffer_period_size_frames);
-    const size_t buffer_size_bytes = buffer_size_frames * audio_stream_frame_size(stream);
+        stream, config, config->buffer_period_size_frames, stream_frame_size);
+    const size_t buffer_size_bytes = buffer_size_frames * stream_frame_size;
     SUBMIX_ALOGV("out_get_buffer_size() returns %zu bytes, %zu frames",
                  buffer_size_bytes, buffer_size_frames);
     return buffer_size_bytes;
@@ -673,8 +675,10 @@ static uint32_t out_get_latency(const struct audio_stream_out *stream)
     const struct submix_stream_out * const out = audio_stream_out_get_submix_stream_out(
             const_cast<struct audio_stream_out *>(stream));
     const struct submix_config * const config = &out->dev->config;
+    const size_t stream_frame_size =
+                            audio_stream_out_frame_size(stream);
     const size_t buffer_size_frames = calculate_stream_pipe_size_in_frames(
-            &stream->common, config, config->buffer_size_frames);
+            &stream->common, config, config->buffer_size_frames, stream_frame_size);
     const uint32_t sample_rate = out_get_sample_rate(&stream->common);
     const uint32_t latency_ms = (buffer_size_frames * 1000) / sample_rate;
     SUBMIX_ALOGV("out_get_latency() returns %u ms, size in frames %zu, sample rate %u",
@@ -696,7 +700,7 @@ static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
 {
     SUBMIX_ALOGV("out_write(bytes=%zd)", bytes);
     ssize_t written_frames = 0;
-    const size_t frame_size = audio_stream_frame_size(&stream->common);
+    const size_t frame_size = audio_stream_out_frame_size(stream);
     struct submix_stream_out * const out = audio_stream_out_get_submix_stream_out(stream);
     struct submix_audio_device * const rsxadev = out->dev;
     const size_t frames = bytes / frame_size;
@@ -831,7 +835,7 @@ static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
     // The sample rate of the stream can't be changed once it's set since this would change the
     // input buffer size and hence break recording from the shared pipe.
     if (rate != in->dev->config.input_sample_rate) {
-        ALOGE("in_set_sample_rate(rate=%u) resampling enabled can't change sample rate from "
+        ALOGE("in_set_sample_rate() resampling enabled can't change sample rate from "
               "%u to %u", in->dev->config.input_sample_rate, rate);
         return -ENOSYS;
     }
@@ -850,8 +854,10 @@ static size_t in_get_buffer_size(const struct audio_stream *stream)
     const struct submix_stream_in * const in = audio_stream_get_submix_stream_in(
             const_cast<struct audio_stream*>(stream));
     const struct submix_config * const config = &in->dev->config;
+    const size_t stream_frame_size =
+                            audio_stream_in_frame_size((const struct audio_stream_in *)stream);
     size_t buffer_size_frames = calculate_stream_pipe_size_in_frames(
-        stream, config, config->buffer_period_size_frames);
+        stream, config, config->buffer_period_size_frames, stream_frame_size);
 #if ENABLE_RESAMPLING
     // Scale the size of the buffer based upon the maximum number of frames that could be returned
     // given the ratio of output to input sample rate.
@@ -859,7 +865,7 @@ static size_t in_get_buffer_size(const struct audio_stream *stream)
                                    (float)config->input_sample_rate) /
                                   (float)config->output_sample_rate);
 #endif // ENABLE_RESAMPLING
-    const size_t buffer_size_bytes = buffer_size_frames * audio_stream_frame_size(stream);
+    const size_t buffer_size_bytes = buffer_size_frames * stream_frame_size;
     SUBMIX_ALOGV("in_get_buffer_size() returns %zu bytes, %zu frames", buffer_size_bytes,
                  buffer_size_frames);
     return buffer_size_bytes;
@@ -943,7 +949,7 @@ static ssize_t in_read(struct audio_stream_in *stream, void* buffer,
     struct submix_stream_in * const in = audio_stream_in_get_submix_stream_in(stream);
     struct submix_audio_device * const rsxadev = in->dev;
     struct audio_config *format;
-    const size_t frame_size = audio_stream_frame_size(&stream->common);
+    const size_t frame_size = audio_stream_in_frame_size(stream);
     const size_t frames_to_read = bytes / frame_size;
 
     SUBMIX_ALOGV("in_read bytes=%zu", bytes);
index 7b65014..4b5c257 100644 (file)
@@ -679,7 +679,8 @@ static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
 
 static size_t out_get_buffer_size(const struct audio_stream *stream)
 {
-    return cached_output_hardware_config.period_size * audio_stream_frame_size(stream);
+    return cached_output_hardware_config.period_size *
+                audio_stream_out_frame_size((const struct audio_stream_out *)stream);
 }
 
 static uint32_t out_get_channels(const struct audio_stream *stream)
@@ -933,7 +934,7 @@ err:
     pthread_mutex_unlock(&out->lock);
     pthread_mutex_unlock(&out->dev->lock);
     if (ret != 0) {
-        usleep(bytes * 1000000 / audio_stream_frame_size(&stream->common) /
+        usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) /
                out_get_sample_rate(&stream->common));
     }
 
@@ -1109,9 +1110,10 @@ static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
 
 static size_t in_get_buffer_size(const struct audio_stream *stream)
 {
-    ALOGV("usb: in_get_buffer_size() = %zu",
-          cached_input_hardware_config.period_size * audio_stream_frame_size(stream));
-    return cached_input_hardware_config.period_size * audio_stream_frame_size(stream);
+    size_t buffer_size = cached_input_hardware_config.period_size *
+                            audio_stream_in_frame_size((const struct audio_stream_in *)stream);
+    ALOGV("usb: in_get_buffer_size() = %zu", buffer_size);
+    return buffer_size;
 }
 
 static uint32_t in_get_channels(const struct audio_stream *stream)