AAudio: add setBufferCapacity()

This is needed so that an app can request a larger buffer.
Also fix a bug related to passing configuration data to the service.

Test: test_aaudio.cpp
Change-Id: Idd3066c84f6bac76a5d545b12081bc311025a6c3
Signed-off-by: Phil Burk <philburk@google.com>
diff --git a/services/oboeservice/AAudioServiceStreamFakeHal.cpp b/services/oboeservice/AAudioServiceStreamFakeHal.cpp
index 627a504..1caeb3f 100644
--- a/services/oboeservice/AAudioServiceStreamFakeHal.cpp
+++ b/services/oboeservice/AAudioServiceStreamFakeHal.cpp
@@ -53,13 +53,14 @@
 }
 
 aaudio_result_t AAudioServiceStreamFakeHal::open(aaudio::AAudioStreamRequest &request,
-                                             aaudio::AAudioStreamConfiguration &configuration) {
+                                       aaudio::AAudioStreamConfiguration &configurationOutput) {
     // Open stream on HAL and pass information about the ring buffer to the client.
     mmap_buffer_info mmapInfo;
     aaudio_result_t error;
 
     // Open HAL
-    error = fake_hal_open(CARD_ID, DEVICE_ID, &mStreamId);
+    int bufferCapacity = request.getConfiguration().getBufferCapacity();
+    error = fake_hal_open(CARD_ID, DEVICE_ID, bufferCapacity, &mStreamId);
     if(error < 0) {
         ALOGE("Could not open card %d, device %d", CARD_ID, DEVICE_ID);
         return error;
@@ -87,9 +88,9 @@
          mmapInfo.buffer_capacity_in_bytes);
 
     // Fill in AAudioStreamConfiguration
-    configuration.setSampleRate(mSampleRate);
-    configuration.setSamplesPerFrame(mmapInfo.channel_count);
-    configuration.setAudioFormat(AAUDIO_FORMAT_PCM_I16);
+    configurationOutput.setSampleRate(mSampleRate);
+    configurationOutput.setSamplesPerFrame(mmapInfo.channel_count);
+    configurationOutput.setAudioFormat(AAUDIO_FORMAT_PCM_I16);
 
     return AAUDIO_OK;
 }
diff --git a/services/oboeservice/AAudioServiceStreamFakeHal.h b/services/oboeservice/AAudioServiceStreamFakeHal.h
index 170d0ee..e9480fb 100644
--- a/services/oboeservice/AAudioServiceStreamFakeHal.h
+++ b/services/oboeservice/AAudioServiceStreamFakeHal.h
@@ -37,7 +37,7 @@
     virtual aaudio_result_t getDescription(AudioEndpointParcelable &parcelable) override;
 
     virtual aaudio_result_t open(aaudio::AAudioStreamRequest &request,
-                               aaudio::AAudioStreamConfiguration &configuration) override;
+                                 aaudio::AAudioStreamConfiguration &configurationOutput) override;
 
     /**
      * Start the flow of data.
diff --git a/services/oboeservice/FakeAudioHal.cpp b/services/oboeservice/FakeAudioHal.cpp
index cf0dc86..34a2476 100644
--- a/services/oboeservice/FakeAudioHal.cpp
+++ b/services/oboeservice/FakeAudioHal.cpp
@@ -94,14 +94,25 @@
 #define FRAMES_PER_BURST_QUALCOMM 192
 #define FRAMES_PER_BURST_NVIDIA   128
 
-int fake_hal_open(int card_id, int device_id, fake_hal_stream_ptr *streamPP) {
+int fake_hal_open(int card_id, int device_id,
+                  int frameCapacity,
+                  fake_hal_stream_ptr *streamPP) {
     int framesPerBurst = FRAMES_PER_BURST_QUALCOMM; // TODO update as needed
+    int periodCountRequested = frameCapacity / framesPerBurst;
     int periodCount = 32;
     unsigned int offset1;
     unsigned int frames1;
     void *area = nullptr;
     int mmapAvail = 0;
 
+    // Try to match requested size with a power of 2.
+    while (periodCount < periodCountRequested && periodCount < 1024) {
+        periodCount *= 2;
+    }
+    std::cout << "fake_hal_open() requested frameCapacity = " << frameCapacity << std::endl;
+    std::cout << "fake_hal_open() periodCountRequested = " << periodCountRequested << std::endl;
+    std::cout << "fake_hal_open() periodCount = " << periodCount << std::endl;
+
     // Configuration for an ALSA stream.
     pcm_config cfg;
     memset(&cfg, 0, sizeof(cfg));
diff --git a/services/oboeservice/FakeAudioHal.h b/services/oboeservice/FakeAudioHal.h
index f47ccd9..d3aa4e8 100644
--- a/services/oboeservice/FakeAudioHal.h
+++ b/services/oboeservice/FakeAudioHal.h
@@ -39,7 +39,9 @@
 //extern "C"
 //{
 
-int fake_hal_open(int card_id, int device_id, fake_hal_stream_ptr *stream_pp);
+int fake_hal_open(int card_id, int device_id,
+                  int frameCapacity,
+                  fake_hal_stream_ptr *stream_pp);
 
 int fake_hal_get_mmap_info(fake_hal_stream_ptr stream, mmap_buffer_info *info);