简介
主要函数easy_write,前四个参数可随意填写,用于区分不同的dump位置,后面三个最好真实填写,为了好辨认dump出的audio格式。
功能介绍
可以多线程调用可以在一个module中多个地方调用,函数中维护了一张链表,根据path区分需要创建/data/audio_dump目录才会保存,删除后不会再保存/data/audio_dump目录下的文件删除后会重新生成
用法
include "audio_tools.hpp"
将如下代码保存在audio_tools.hpp里面,然后放到相关module下。
//
// audio_tools.hpp
// audio_tools
//
// Created by 薯条 on 2023/3/31.
//
#ifndef audio_tools_hpp
#define audio_tools_hpp
#include
#include
#include
#include
#include
#define FILE_NAME_SIZE 128
typedef struct output_desc {
char path[FILE_NAME_SIZE];
int output;
void *next;
} output_desc_t;
const char* path = "/data/audio_dump";
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static output_desc_t* outputs = NULL;
void easy_write(char* tag, uint32_t session, uint32_t pid, uint32_t device, uint32_t sample_rate,
uint32_t num_channels, uint32_t bits, const char* data, size_t size) {
pthread_mutex_lock(&mutex);
output_desc_t* current_output = NULL;
char str[FILE_NAME_SIZE];
memset(str, 0, FILE_NAME_SIZE);
sprintf(str, "%s/%s.session%d.pid%d.%#x_%d_%d_%dbit.pcm",
path, tag, session, pid, device, sample_rate, num_channels, bits);
current_output = outputs;
while(current_output != NULL){
if(!strcmp(current_output->path, str)) {
break;
}
current_output = (output_desc_t *)current_output->next;
}
if(current_output == NULL || access(str, F_OK) < 0) {
if(current_output == NULL) {
current_output = (output_desc_t *)malloc(sizeof(output_desc_t));
memcpy(current_output->path, str, FILE_NAME_SIZE);
if(outputs == NULL) {
outputs = current_output;
} else {
output_desc_t* last_output = outputs;
while(last_output->next != NULL){
last_output = (output_desc_t *)last_output->next;
}
last_output->next = current_output;
}
current_output->next = NULL;
}
current_output->output = open(str, O_WRONLY|O_CREAT|O_APPEND, S_IWUSR|S_IRGRP|S_IROTH);
}
write(current_output->output, data, size);
pthread_mutex_unlock(&mutex);
}
#endif /* audio_tools_hpp */