void SoundTriggerHalLegacy::onFirstRef() { const hw_module_t *mod; int rc; if (mModuleName == NULL) { mModuleName = "primary"; } rc = hw_get_module_by_class(SOUND_TRIGGER_HARDWARE_MODULE_ID, mModuleName, &mod); if (rc != 0) { ALOGE("couldn't load sound trigger module %s.%s (%s)", SOUND_TRIGGER_HARDWARE_MODULE_ID, mModuleName, strerror(-rc)); return; } rc = sound_trigger_hw_device_open(mod, &mHwDevice); if (rc != 0) { ALOGE("couldn't open sound trigger hw device in %s.%s (%s)", SOUND_TRIGGER_HARDWARE_MODULE_ID, mModuleName, strerror(-rc)); mHwDevice = NULL; return; } if (mHwDevice->common.version < SOUND_TRIGGER_DEVICE_API_VERSION_1_0 || mHwDevice->common.version > SOUND_TRIGGER_DEVICE_API_VERSION_CURRENT) { ALOGE("wrong sound trigger hw device version %04x", mHwDevice->common.version); return; } }
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 int check_vendor_module() { int rv = 0; if(gVendorModule) return 0; rv = hw_get_module_by_class("camera", "vendor", (const hw_module_t **)&gVendorModule); if (rv) ALOGE("failed to open vendor camera module"); return rv; }
static int get_keymaster_version() { int rc = -1; const hw_module_t* mod; rc = hw_get_module_by_class(KEYSTORE_HARDWARE_MODULE_ID, NULL, &mod); if (rc) { SLOGE("could not find any keystore module"); return rc; } return mod->module_api_version; }
LocalClock::LocalClock() { int res; const hw_module_t* mod; AutoMutex lock(&dev_lock_); if (dev_ != NULL) return; res = hw_get_module_by_class(LOCAL_TIME_HARDWARE_MODULE_ID, NULL, &mod); if (res) { ALOGE("Failed to open local time HAL module (res = %d)", res); } else { res = local_time_hw_device_open(mod, &dev_); if (res) { ALOGE("Failed to open local time HAL device (res = %d)", res); dev_ = NULL; } } }
GateKeeperProxy() { int ret = hw_get_module_by_class(GATEKEEPER_HARDWARE_MODULE_ID, NULL, &module); device = NULL; if (ret < 0) { ALOGW("falling back to software GateKeeper"); soft_device.reset(new SoftGateKeeperDevice()); } else { ret = gatekeeper_open(module, &device); if (ret < 0) LOG_ALWAYS_FATAL_IF(ret < 0, "Unable to open GateKeeper HAL"); } if (mark_cold_boot()) { ALOGI("cold boot: clearing state"); if (device != NULL && device->delete_all_users != NULL) { device->delete_all_users(device); } } }
static void init_p(int argc, const char **argv) { int err; const hw_module_t *module; audio_hw_device_t *device; err = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, AUDIO_HARDWARE_MODULE_ID_A2DP, &module); if (err) { haltest_error("hw_get_module_by_class returned %d\n", err); return; } err = audio_hw_device_open(module, &device); if (err) { haltest_error("audio_hw_device_open returned %d\n", err); return; } if_audio = device; }
static int open_a2dp_output() { hw_module_t *mod; int format = AUDIO_FORMAT_PCM_16_BIT; int rc=0; uint32_t channels = AUDIO_CHANNEL_OUT_STEREO; uint32_t sampleRate = DEFAULT_OUTPUT_SAMPLING_RATE; struct audio_config config; ALOGV("open_a2dp_output"); config.sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE; config.channel_mask = AUDIO_CHANNEL_OUT_STEREO; config.format = AUDIO_FORMAT_PCM_16_BIT; if (a2dp.a2dp_device == NULL){ rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, (const char*)"a2dp", (const hw_module_t**)&mod); if (rc != 0) { ALOGE("Could not get a2dp hardware module"); return rc; } ALOGV("Opening A2DP device HAL for the first time"); rc = audio_hw_device_open(mod, &a2dp.a2dp_device); if (rc != 0) { ALOGE("couldn't open a2dp audio hw device"); return rc; } } rc = a2dp.a2dp_device->open_output_stream(a2dp.a2dp_device, 0,AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, (audio_output_flags_t)AUDIO_OUTPUT_FLAG_NONE, &config, &a2dp.a2dp_stream, NULL); if( rc != 0 ) { ALOGE("Failed to open output stream for a2dp: status %d", rc); } a2dp.a2dp_suspended = false; return rc; }
static int keymaster_init(keymaster0_device_t **keymaster0_dev, keymaster1_device_t **keymaster1_dev) { int rc; const hw_module_t* mod; rc = hw_get_module_by_class(KEYSTORE_HARDWARE_MODULE_ID, NULL, &mod); if (rc) { ALOGE("could not find any keystore module"); goto err; } SLOGI("keymaster module name is %s", mod->name); SLOGI("keymaster version is %d", mod->module_api_version); *keymaster0_dev = NULL; *keymaster1_dev = NULL; if (mod->module_api_version == KEYMASTER_MODULE_API_VERSION_1_0) { SLOGI("Found keymaster1 module, using keymaster1 API."); rc = keymaster1_open(mod, keymaster1_dev); } else { SLOGI("Found keymaster0 module, using keymaster0 API."); rc = keymaster0_open(mod, keymaster0_dev); } if (rc) { ALOGE("could not open keymaster device in %s (%s)", KEYSTORE_HARDWARE_MODULE_ID, strerror(-rc)); goto err; } return 0; err: *keymaster0_dev = NULL; *keymaster1_dev = NULL; return rc; }
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; }
// 一个空壳函数,主要实现在hw_get_module_by_class()中 int hw_get_module(const char *id, const struct hw_module_t **module) { return hw_get_module_by_class(id, NULL, module); }
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; }