Implementing record_audio as a host command instead of a shell command

running on the guest.

Change-Id: I45cd97fd709822ce2b7329f082027f1535fff7a2
Bug: 72216799
Test: manually
diff --git a/guest/hals/audio/vsoc_audio_message.h b/common/vsoc/lib/vsoc_audio_message.h
similarity index 79%
rename from guest/hals/audio/vsoc_audio_message.h
rename to common/vsoc/lib/vsoc_audio_message.h
index 5b36215..230c4de 100644
--- a/guest/hals/audio/vsoc_audio_message.h
+++ b/common/vsoc/lib/vsoc_audio_message.h
@@ -19,21 +19,24 @@
 #include <stdint.h>
 #include <time.h>
 
-#include "guest/libs/platform_support/api_level_fixes.h"
-
-#pragma GCC diagnostic push
-#pragma  GCC diagnostic ignored "-Wparentheses"
-#if VSOC_PLATFORM_SDK_AFTER(K)
-#pragma  GCC diagnostic ignored "-Wgnu-designator"
-#endif
 #include <system/audio.h>
-#pragma GCC diagnostic pop
 
-//TODO(b/71777986) Use a shared memory window instead
-#define AUDIO_HAL_SOCKET_NAME "/var/run/media/audio_hal_socket"
+typedef uint32_t size32_t;
+
+struct timespec32 {
+  uint32_t tv_sec;
+  uint32_t tv_nsec;
+
+  timespec32() = default;
+
+  timespec32(const timespec &from)
+      : tv_sec(from.tv_sec),
+        tv_nsec(from.tv_nsec) {
+  }
+};
 
 struct gce_audio_message {
-  static const size_t kMaxAudioFrameLen = 65536;
+  static const size32_t kMaxAudioFrameLen = 65536;
   enum message_t {
     UNKNOWN = 0,
     DATA_SAMPLES = 1,
@@ -44,16 +47,16 @@
     CONTROL_PAUSE = 100
   };
   // Size of the header + data. Used to frame when we're on TCP.
-  size_t total_size;
+  size32_t total_size;
   // Size of the audio header
-  size_t header_size;
+  size32_t header_size;
   message_t message_type;
   // Identifier for the stream.
   uint32_t stream_number;
   // HAL assigned frame number, starts from 0.
   int64_t frame_num;
   // MONOTONIC_TIME when these frames were presented to the HAL.
-  timespec time_presented;
+  timespec32 time_presented;
   // Sample rate from the audio configuration.
   uint32_t frame_rate;
   // Channel mask from the audio configuration.
@@ -61,19 +64,19 @@
   // Format from the audio configuration.
   audio_format_t format;
   // Size of each frame in bytes.
-  size_t frame_size;
+  size32_t frame_size;
   // Number of frames that were presented to the HAL.
-  size_t num_frames_presented;
+  size32_t num_frames_presented;
   // Number of frames that the HAL accepted.
   //   For blocking audio this will be the same as num_frames.
   //   For non-blocking audio this may be less.
-  size_t num_frames_accepted;
+  size32_t num_frames_accepted;
   // Count of the number of packets that were dropped because they would
   // have blocked the HAL or exceeded the maximum message size.
-  size_t num_packets_dropped;
+  size32_t num_packets_dropped;
   // Count of the number of packets that were shortened to fit within
   // kMaxAudioFrameLen.
-  size_t num_packets_shortened;
+  size32_t num_packets_shortened;
   // num_frames_presented (not num_frames_accepted) will follow here.
 
   gce_audio_message() :
diff --git a/guest/hals/audio/Android.mk b/guest/hals/audio/Android.mk
index 81029cd..3af6e0f 100644
--- a/guest/hals/audio/Android.mk
+++ b/guest/hals/audio/Android.mk
@@ -63,32 +63,3 @@
 LOCAL_VENDOR_MODULE := true
 
 include $(BUILD_SHARED_LIBRARY)
-
-################################################################################
-
-include $(CLEAR_VARS)
-
-LOCAL_MODULE := record_audio
-LOCAL_MODULE_TAGS := optional
-
-LOCAL_SRC_FILES := \
-    record_audio.cpp
-
-LOCAL_CFLAGS := \
-    -Wall -Werror \
-    $(VSOC_VERSION_CFLAGS)
-
-LOCAL_C_INCLUDES := \
-    device/google/cuttlefish_common \
-    device/google/cuttlefish_kernel \
-    frameworks/av/cmds/stagefright  \
-
-LOCAL_HEADER_LIBRARIES := \
-    libhardware_headers
-
-LOCAL_SHARED_LIBRARIES := \
-    libbase               \
-    vsoc_lib
-
-LOCAL_VENDOR_MODULE := true
-include $(BUILD_EXECUTABLE)
diff --git a/guest/hals/audio/vsoc_audio.h b/guest/hals/audio/vsoc_audio.h
index 80a73cf..6ec9eee 100644
--- a/guest/hals/audio/vsoc_audio.h
+++ b/guest/hals/audio/vsoc_audio.h
@@ -22,9 +22,9 @@
 #include "common/libs/fs/shared_fd.h"
 #include "common/libs/threads/cuttlefish_thread.h"
 #include "common/vsoc/lib/audio_data_region_view.h"
+#include "common/vsoc/lib/vsoc_audio_message.h"
 #include "guest/hals/audio/audio_hal.h"
 #include "guest/hals/audio/vsoc_audio_input_stream.h"
-#include "guest/hals/audio/vsoc_audio_message.h"
 #include "guest/libs/platform_support/api_level_fixes.h"
 
 namespace cvd {
diff --git a/guest/hals/audio/vsoc_audio_input_stream.h b/guest/hals/audio/vsoc_audio_input_stream.h
index f7d275c..40d6b19 100644
--- a/guest/hals/audio/vsoc_audio_input_stream.h
+++ b/guest/hals/audio/vsoc_audio_input_stream.h
@@ -17,9 +17,9 @@
 
 #include <memory>
 
+#include "common/vsoc/lib/vsoc_audio_message.h"
 #include "guest/hals/audio/audio_hal.h"
 #include "guest/hals/audio/simulated_buffer.h"
-#include "guest/hals/audio/vsoc_audio_message.h"
 
 namespace cvd {
 
diff --git a/guest/hals/audio/vsoc_audio_output_stream.cpp b/guest/hals/audio/vsoc_audio_output_stream.cpp
index 8490a08..b78351e 100644
--- a/guest/hals/audio/vsoc_audio_output_stream.cpp
+++ b/guest/hals/audio/vsoc_audio_output_stream.cpp
@@ -13,8 +13,6 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-#include "guest/hals/audio/vsoc_audio_message.h"
-
 #include <pthread.h>
 #include <stdio.h>
 #include <stdlib.h>
diff --git a/guest/hals/audio/vsoc_audio_output_stream.h b/guest/hals/audio/vsoc_audio_output_stream.h
index 69b976a..14ea2f4 100644
--- a/guest/hals/audio/vsoc_audio_output_stream.h
+++ b/guest/hals/audio/vsoc_audio_output_stream.h
@@ -17,9 +17,9 @@
 
 #include <memory>
 
+#include "common/vsoc/lib/vsoc_audio_message.h"
 #include "guest/hals/audio/audio_hal.h"
 #include "guest/hals/audio/simulated_buffer.h"
-#include "guest/hals/audio/vsoc_audio_message.h"
 
 namespace cvd {
 
diff --git a/host/commands/Android.bp b/host/commands/Android.bp
index 618dd00..2611603 100644
--- a/host/commands/Android.bp
+++ b/host/commands/Android.bp
@@ -15,4 +15,5 @@
 
 subdirs = [
     "launch",
+    "record_audio",
 ]
diff --git a/host/commands/record_audio/Android.bp b/host/commands/record_audio/Android.bp
new file mode 100644
index 0000000..b4f3cbd
--- /dev/null
+++ b/host/commands/record_audio/Android.bp
@@ -0,0 +1,18 @@
+cc_binary_host {
+    name: "record_audio",
+    srcs: [
+        "main.cc",
+    ],
+    include_dirs: [
+        "frameworks/av/cmds/stagefright",
+    ],
+    shared_libs: [
+        "libbase",
+        "vsoc_lib",
+    ],
+    static_libs: [
+        "libcuttlefish_host_config",
+        "libgflags",
+    ],
+    defaults: ["cuttlefish_host_only"],
+}
diff --git a/guest/hals/audio/record_audio.cpp b/host/commands/record_audio/main.cc
similarity index 72%
rename from guest/hals/audio/record_audio.cpp
rename to host/commands/record_audio/main.cc
index 43330f4..2655838 100644
--- a/guest/hals/audio/record_audio.cpp
+++ b/host/commands/record_audio/main.cc
@@ -14,25 +14,23 @@
  * limitations under the License.
  */
 
-#include "vsoc_audio_message.h"
-
 #include "common/vsoc/lib/audio_data_region_view.h"
 #include "common/vsoc/lib/circqueue_impl.h"
+#include "common/vsoc/lib/vsoc_audio_message.h"
+#include "host/libs/config/host_config.h"
 
 #include "WaveWriter.h"
 
-#include <cstdlib>
-#include <cstring>
+#include <android-base/logging.h>
+#include <gflags/gflags.h>
 #include <iostream>
-#include <unistd.h>
+#include <signal.h>
 
 using AudioDataRegionView = vsoc::audio_data::AudioDataRegionView;
 using WaveWriter = android::WaveWriter;
 
-static void usage(const char *me) {
-  std::cerr << "usage: " << me << " -o filename [-v(erbose)]" << std::endl;
-  std::exit(1);
-}
+DEFINE_string(output_file, "", "Location of the output audio file.");
+DEFINE_bool(verbose, false, "Enable verbose logging.");
 
 volatile bool gDone = false;
 static void SigIntHandler(int /* sig */) {
@@ -40,47 +38,15 @@
 }
 
 int main(int argc, char **argv) {
-  const char *me = argv[0];
+  ::android::base::InitLogging(argv, android::base::StderrLogger);
+  google::ParseCommandLineFlags(&argc, &argv, true);
 
-  std::string outputPath;
-  bool verbose = false;
+  LOG_IF(FATAL, FLAGS_output_file.empty())
+      << "--output_file must be specified.";
 
-  int res;
-  while ((res = getopt(argc, argv, "ho:v")) >= 0) {
-    switch (res) {
-      case 'o':
-      {
-        outputPath = optarg;
-        break;
-      }
+  AudioDataRegionView *audio_data_rv =
+      AudioDataRegionView::GetInstance(vsoc::GetDomain().c_str());
 
-      case 'v' :
-      {
-        verbose = true;
-        break;
-      }
-
-      case '?':
-      case 'h':
-      default:
-      {
-        usage(me);
-        break;
-      }
-    }
-  }
-
-  argc -= optind;
-  argv += optind;
-
-  if (outputPath.empty()) {
-    usage(me);
-  }
-
-  auto audio_data_rv = AudioDataRegionView::GetInstance();
-  CHECK(audio_data_rv != nullptr);
-
-  /* std::unique_ptr<vsoc::RegionWorker> audio_worker = */
   auto worker = audio_data_rv->StartWorker();
 
   std::unique_ptr<WaveWriter> writer;
@@ -108,7 +74,7 @@
             sizeof(buffer));
 
     if (res < 0) {
-        std::cerr << "CircularPacketQueue::Read returned " << res << std::endl;
+      std::cerr << "CircularPacketQueue::Read returned " << res << std::endl;
         continue;
     }
 
@@ -123,8 +89,8 @@
 
     const size_t payloadSize = res - sizeof(gce_audio_message);
 
-    if (verbose) {
-      std::cout
+    if (FLAGS_verbose) {
+      std::cerr
           << "stream "
           << hdr.stream_number
           << ", frame "
@@ -144,7 +110,7 @@
       const size_t numChannels = hdr.frame_size / sizeof(int16_t);
 
       writer.reset(
-          new WaveWriter(outputPath.c_str(), numChannels, hdr.frame_rate));
+          new WaveWriter(FLAGS_output_file.c_str(), numChannels, hdr.frame_rate));
 
       frameCount = hdr.frame_num;
       writer_hdr = hdr;
@@ -165,7 +131,8 @@
     writer->Append(&buffer[sizeof(gce_audio_message)], payloadSize);
   }
 
-  std::cout << "Done." << std::endl;
+  std::cout << "DONE" << std::endl;
 
   return 0;
 }
+