简体   繁体   English

C:以这种方式从内核模块写入文件正确吗?

[英]C: write file from kernel-module correct this way?

I try to copy/write pcm-bytes (from ALSA-buffer) from the kernel-space into a file within a kernel-module (LKM). 我尝试将pcm-bytes(从ALSA缓冲区)从内核空间复制/写入到内核模块(LKM)中的文件中。

The file got's written and the size looks ok for me, but because it's PCM-data I can't see if it's correct (raw audio data, not readable). 文件写好了,大小对我来说还可以,但是因为它是PCM数据,所以我看不到它是否正确(原始音频数据,不可读)。

My audio-players (MPlayer="Invalid seek to negative position ffffffffffffffff!", VLC, Foobar2000) fail to play my written file so I think I have a mistake in my code. 我的音频播放器(MPlayer =“无效搜索到否定位置ffffffffffffffff!”,VLC,Foobar2000)无法播放我的书面文件,因此我认为我的代码有误。

When I open the file via SCITE I see many many "NUL" and other cryted stuff (bytes ;). 当我通过SCITE打开文件时,我看到很多“ NUL”和其他哭泣的东西(字节;)。

Maybe one of you find a bug? 也许你们中的一个发现了错误?

I have this script: 我有这个脚本:

unsigned char *dma_area; // this is the source, an mmapped-area
int pcm_offset_bytes; // the actual offset in the dma_area
int size_bytes; // the amount of bytes to copy
struct file *target_file; // the target-file


int ret; // used in write-process below
int wrote = 0; // used in write-process below
mm_segment_t fs;
void *data; // dma_area + pcm_offset_bytes

// (..) calculate offset and size

// open the target file
target_file = filp_open("/dev/snd/pcmC0D0p_output", (O_CREAT | O_TRUNC | O_WRONLY), (S_IRWXU | S_IRWXG | S_IRWXO));

data = dma_area + pcm_offset_bytes


fs = get_fs();
set_fs (get_ds ());

if (!target_file || !target_file->f_op || !target_file->f_op->write) {
    LOGI("something's missing\n");
    return -EIO;
}

// loop until every byte is written
while (size_bytes > 0) {

    ret = target_file->f_op->write(target_file, (char *)data, size_bytes, &target_file->f_pos);

    LOGI ("wrote %d bytes to target_file@%p, return %d\n", size_bytes, target_file, ret);
    if (ret <= 0)
        goto done;

    size_bytes -= ret;
    data += ret;
    wrote += ret;
}

ret = wrote;

done:
    set_fs(fs);
    LOGI("result %d\n", ret);

First, you aren't "supposed to" write to files within the kernel. 首先,您不会“应该”写入内核中的文件。

For the more proper question of how to play headerless raw pcm, you can use the import function of audacity. 有关如何播放无标题的原始pcm的更恰当的问题,可以使用audacity的导入功能。

Or use aplay 或使用游戏

aplay some_file -t raw -f S16_LE -c 2 -r 44100

With whatever parameters are appropriate. 使用任何合适的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM