static status_t load_audio_interface(const char *if_name, audio_hw_device_t **dev) { const hw_module_t *mod; int rc; rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, &mod); if (rc) { ALOGE("%s couldn't load audio hw module %s.%s (%s)", __func__, AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc)); goto out; } rc = audio_hw_device_open(mod, dev); if (rc) { ALOGE("%s couldn't open audio hw device in %s.%s (%s)", __func__, AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc)); goto out; } if ((*dev)->common.version < AUDIO_DEVICE_API_VERSION_MIN) { ALOGE("%s wrong audio hw device version %04x", __func__, (*dev)->common.version); rc = BAD_VALUE; audio_hw_device_close(*dev); goto out; } return OK; out: *dev = NULL; return rc; }
static void cleanup_p(int argc, const char **argv) { int err; RETURN_IF_NULL(if_audio); pthread_mutex_lock(&state_mutex); if (current_state != STATE_STOPPED) { pthread_mutex_unlock(&state_mutex); close_output_stream_p(0, NULL); } else { pthread_mutex_unlock(&state_mutex); } err = audio_hw_device_close(if_audio); if (err < 0) { haltest_error("audio_hw_device_close returned %d\n", err); return; } if_audio = NULL; }
int main(int argc, char **argv) { struct hw_module_t *hwmod = 0; struct audio_hw_device *audiohw; hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, #if defined(AUDIO_HARDWARE_MODULE_ID_PRIMARY) AUDIO_HARDWARE_MODULE_ID_PRIMARY, #else "primary", #endif (const hw_module_t**) &hwmod); assert(hwmod != NULL); assert(audio_hw_device_open(hwmod, &audiohw) == 0); assert(audiohw->init_check(audiohw) == 0); printf("Audio Hardware Interface initialized.\n"); #if (ANDROID_VERSION_MAJOR == 4 && ANDROID_VERSION_MINOR >= 1) || (ANDROID_VERSION_MAJOR >= 5) if (audiohw->get_master_volume) { float volume; audiohw->get_master_volume(audiohw, &volume); printf("Master Volume: %f\n", volume); } #endif #if (ANDROID_VERSION_MAJOR == 4 && ANDROID_VERSION_MINOR >= 2) || (ANDROID_VERSION_MAJOR >= 5) if (audiohw->get_master_mute) { bool mute; audiohw->get_master_mute(audiohw, &mute); printf("Master Mute: %d\n", mute); } #endif audio_hw_device_close(audiohw); return 0; }
int main(int argc, char* argv[]) { if (argc < 3) { Usage(); return -1; } // Process command line arguments. const int kAudioDeviceBase = 16; uint32_t desired_output_device = strtol( argv[1], nullptr /* look at full string*/, kAudioDeviceBase); const int kSampleRateBase = 10; uint32_t desired_sample_rate = strtol( argv[2], nullptr /* look at full string*/, kSampleRateBase); char* filename = nullptr; if (desired_sample_rate == 0) { filename = argv[2]; } LOG(INFO) << "Starting audio hal tests."; int rc = 0; const hw_module_t* module = nullptr; // Load audio HAL. rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, "primary", &module); if (rc) { LOG(WARNING) << "Could not get primary hw module, trying usb. (" << strerror(rc) << ")"; rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, "usb", &module); if (rc) { LOG(ERROR) << "Could not get hw module. (" << strerror(rc) << ")"; return -1; } } // Open audio device. CHECK(module != nullptr); audio_hw_device_t* audio_device = nullptr; rc = audio_hw_device_open(module, &audio_device); if (rc) { LOG(ERROR) << "Could not open hw device. (" << strerror(rc) << ")"; return -1; } // Set to a high number so it doesn't interfere with existing stream handles // from AudioFlinger. audio_io_handle_t handle = 0x999; audio_devices_t output_device = static_cast<audio_devices_t>(desired_output_device); audio_output_flags_t flags = AUDIO_OUTPUT_FLAG_NONE; audio_config_t config; SF_INFO file_info; SNDFILE* in_file = nullptr; if (filename) { in_file = sf_open(filename, SFM_READ, &file_info); CHECK(in_file != nullptr); config.channel_mask = audio_channel_out_mask_from_count(file_info.channels); if (!(file_info.format & SF_FORMAT_PCM_16)) { LOG(ERROR) << "File must be of format 16-bit PCM."; return -1; } config.format = kAudioPlaybackFormat; config.sample_rate = file_info.samplerate; } else { config.channel_mask = AUDIO_CHANNEL_OUT_STEREO; config.format = kAudioPlaybackFormat; config.sample_rate = desired_sample_rate; } LOG(INFO) << "Now playing to " << output_device << " at sample rate " << config.sample_rate; const char* stream_name = "output_stream"; // Open audio output stream. audio_stream_out_t* out_stream = nullptr; CHECK(audio_device != nullptr); rc = audio_device->open_output_stream(audio_device, handle, output_device, flags, &config, &out_stream, stream_name); if (rc) { LOG(ERROR) << "Could not open output stream. (" << strerror(rc) << ")"; return -1; } // Set volume. const float kLeftVolume = 0.75; const float kRightVolume = 0.75; CHECK(out_stream != nullptr); rc = out_stream->set_volume(out_stream, kLeftVolume, kRightVolume); if (rc) { LOG(ERROR) << "Could not set volume correctly. (" << strerror(rc) << ")"; } if (filename) { PlayFile(out_stream, in_file, &config); } else { PlaySineWave(out_stream, &config); } // Close output stream and device. audio_device->close_output_stream(audio_device, out_stream); audio_hw_device_close(audio_device); LOG(INFO) << "Done with hal tests"; return 0; }
Device::~Device() { int status = audio_hw_device_close(mDevice); ALOGW_IF(status, "Error closing audio hw device %p: %s", mDevice, strerror(-status)); mDevice = nullptr; }
int main(int argc, char **argv) { struct hw_module_t *hwmod = 0; struct audio_hw_device *audiohw; /* Initializing HAL */ hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, #if defined(AUDIO_HARDWARE_MODULE_ID_PRIMARY) AUDIO_HARDWARE_MODULE_ID_PRIMARY, #else "primary", #endif (const hw_module_t**) &hwmod); if (!hwmod) { fprintf(stderr, "Failed to get hw module id: %s name: %s, trying alternative.", AUDIO_HARDWARE_MODULE_ID, AUDIO_HARDWARE_MODULE_ID_PRIMARY); hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID2, AUDIO_HARDWARE_MODULE_ID_PRIMARY, (const hw_module_t**) &hwmod); } assert(hwmod != NULL); assert(audio_hw_device_open(hwmod, &audiohw) == 0); do { #if defined(AUDIO_DEVICE_API_VERSION_MIN) if (audiohw->common.version < AUDIO_DEVICE_API_VERSION_MIN) { fprintf(stderr, "Audio device API version %04x failed to meet minimum requirement %0x4.", audiohw->common.version, AUDIO_DEVICE_API_VERSION_MIN); } else #endif if (audiohw->common.version != AUDIO_DEVICE_API_VERSION_CURRENT) { fprintf(stderr, "Audio device API version %04x doesn't match platform current %0x4.", audiohw->common.version, AUDIO_DEVICE_API_VERSION_CURRENT); } else break; #if defined(AUDIO_DEVICE_API_VERSION_MIN) assert(audiohw->common.version >= AUDIO_DEVICE_API_VERSION_MIN); #endif assert(audiohw->common.version == AUDIO_DEVICE_API_VERSION_CURRENT); } while(0); assert(audiohw->init_check(audiohw) == 0); fprintf(stdout, "Audio Hardware Interface initialized.\n"); #if (ANDROID_VERSION_MAJOR == 4 && ANDROID_VERSION_MINOR >= 1) || (ANDROID_VERSION_MAJOR >= 5) /* Check volume function calls */ if (audiohw->get_master_volume) { float volume; audiohw->get_master_volume(audiohw, &volume); fprintf(stdout, "Master Volume: %f\n", volume); } #endif #if (ANDROID_VERSION_MAJOR == 4 && ANDROID_VERSION_MINOR >= 2) || (ANDROID_VERSION_MAJOR >= 5) if (audiohw->get_master_mute) { bool mute; audiohw->get_master_mute(audiohw, &mute); fprintf(stdout, "Master Mute: %d\n", mute); } #endif /* Check output and input streams */ struct audio_config config_out = { .sample_rate = 44100, .channel_mask = AUDIO_CHANNEL_OUT_STEREO, .format = AUDIO_FORMAT_PCM_16_BIT }; struct audio_stream_out *stream_out = NULL; audiohw->open_output_stream(audiohw, 0, AUDIO_DEVICE_OUT_DEFAULT, AUDIO_OUTPUT_FLAG_PRIMARY, &config_out, &stream_out #if ANDROID_VERSION_MAJOR >= 5 , NULL #endif ); /* Try it again */ if (!stream_out) audiohw->open_output_stream(audiohw, 0, AUDIO_DEVICE_OUT_DEFAULT, AUDIO_OUTPUT_FLAG_PRIMARY, &config_out, &stream_out #if ANDROID_VERSION_MAJOR >= 5 , NULL #endif ); assert(stream_out != NULL); fprintf(stdout, "Successfully created audio output stream: sample rate: %u, channel_mask: %u, format: %u\n", config_out.sample_rate, config_out.channel_mask, config_out.format); struct audio_config config_in = { .sample_rate = 48000, .channel_mask = AUDIO_CHANNEL_IN_STEREO, .format = AUDIO_FORMAT_PCM_16_BIT }; struct audio_stream_in *stream_in = NULL; audiohw->open_input_stream(audiohw, 0, AUDIO_DEVICE_IN_DEFAULT, &config_in, &stream_in #if ANDROID_VERSION_MAJOR >= 5 , AUDIO_INPUT_FLAG_NONE, NULL, AUDIO_SOURCE_DEFAULT #endif ); assert(stream_in != NULL); fprintf(stdout, "Successfully created audio input stream: sample rate: %u, channel_mask: %u, format: %u\n", config_in.sample_rate, config_in.channel_mask, config_in.format); /* Close streams and device */ audiohw->close_output_stream(audiohw, stream_out); audiohw->close_input_stream(audiohw, stream_in); audio_hw_device_close(audiohw); return 0; }