fix audio_bluetooth_hw frame_count overflow am: 9455efcb14

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/modules/Bluetooth/+/20244781

Change-Id: Ie6cd40258660bd6dd3e1064e4df319e379de07ab
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
diff --git a/system/audio_bluetooth_hw/stream_apis.cc b/system/audio_bluetooth_hw/stream_apis.cc
index 1f449fc..5facd12 100644
--- a/system/audio_bluetooth_hw/stream_apis.cc
+++ b/system/audio_bluetooth_hw/stream_apis.cc
@@ -34,6 +34,7 @@
 #include "utils.h"
 
 using ::android::base::StringPrintf;
+using ::android::bluetooth::audio::utils::FrameCount;
 using ::android::bluetooth::audio::utils::GetAudioParamString;
 using ::android::bluetooth::audio::utils::ParseAudioParams;
 
@@ -691,10 +692,6 @@
   out->bluetooth_output_->UpdateSourceMetadata(source_metadata);
 }
 
-static size_t frame_count(size_t microseconds, uint32_t sample_rate) {
-  return (microseconds * sample_rate) / 1000000;
-}
-
 int adev_open_output_stream(struct audio_hw_device* dev,
                             audio_io_handle_t handle, audio_devices_t devices,
                             audio_output_flags_t flags,
@@ -782,7 +779,7 @@
   }
 
   out->frames_count_ =
-      frame_count(out->preferred_data_interval_us, out->sample_rate_);
+      FrameCount(out->preferred_data_interval_us, out->sample_rate_);
 
   out->frames_rendered_ = 0;
   out->frames_presented_ = 0;
@@ -1277,7 +1274,7 @@
   }
 
   in->frames_count_ =
-      frame_count(in->preferred_data_interval_us, in->sample_rate_);
+      FrameCount(in->preferred_data_interval_us, in->sample_rate_);
   in->frames_presented_ = 0;
 
   BluetoothStreamIn* in_ptr = in.release();
diff --git a/system/audio_bluetooth_hw/utils.cc b/system/audio_bluetooth_hw/utils.cc
index b3ac7a5..f864fd5 100644
--- a/system/audio_bluetooth_hw/utils.cc
+++ b/system/audio_bluetooth_hw/utils.cc
@@ -57,6 +57,10 @@
   return sout.str();
 }
 
+size_t FrameCount(uint64_t microseconds, uint32_t sample_rate) {
+  return (microseconds * sample_rate) / 1000000;
+}
+
 }  // namespace utils
 }  // namespace audio
 }  // namespace bluetooth
diff --git a/system/audio_bluetooth_hw/utils.h b/system/audio_bluetooth_hw/utils.h
index 817a432..bdd8a9b 100644
--- a/system/audio_bluetooth_hw/utils.h
+++ b/system/audio_bluetooth_hw/utils.h
@@ -42,6 +42,7 @@
 std::string GetAudioParamString(
     std::unordered_map<std::string, std::string>& params_map);
 
+size_t FrameCount(uint64_t microseconds, uint32_t sample_rate);
 }  // namespace utils
 }  // namespace audio
 }  // namespace bluetooth
diff --git a/system/audio_bluetooth_hw/utils_unittest.cc b/system/audio_bluetooth_hw/utils_unittest.cc
index 665dea6..1bcd5dd 100644
--- a/system/audio_bluetooth_hw/utils_unittest.cc
+++ b/system/audio_bluetooth_hw/utils_unittest.cc
@@ -21,6 +21,7 @@
 
 namespace {
 
+using ::android::bluetooth::audio::utils::FrameCount;
 using ::android::bluetooth::audio::utils::ParseAudioParams;
 
 class UtilsTest : public testing::Test {
@@ -133,4 +134,9 @@
   EXPECT_EQ(map_["key1"], "value1");
 }
 
+TEST_F(UtilsTest, FrameCountTest) {
+  EXPECT_EQ(FrameCount(120000, 44100), 5292);
+  EXPECT_EQ(FrameCount(7500, 32000), 240);
+}
+
 }  // namespace