【问题标题】:C: write file from kernel-module correct this way?C:以这种方式从内核模块写入文件正确吗?
【发布时间】:2012-06-13 14:22:34
【问题描述】:

我尝试将 pcm 字节(来自 ALSA 缓冲区)从内核空间复制/写入到内核模块 (LKM) 内的文件中。

文件已写入,大小对我来说还可以,但因为它是 PCM 数据,我看不出它是否正确(原始音频数据,不可读)。

我的音频播放器(MPlayer="Invalid seek to negative position ffffffffffffffff!", VLC, Foobar2000)无法播放我写的文件,所以我认为我的代码有错误。

当我通过 SCITE 打开文件时,我看到许多“NUL”和其他内容(字节;)。

也许你们中的某个人发现了一个错误?

我有这个脚本:

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);

【问题讨论】:

  • 这看起来是正确的。我猜媒体播放器只是不理解该文件,因为它是原始 PCM。尝试从现有 wav 文件中窃取 RIFF 标头并将其添加到文件中,然后重试。

标签: c file kernel-module fwrite


【解决方案1】:

首先,您不“应该”写入内核中的文件。

关于如何播放headerless raw pcm的更恰当的问题,可以使用audacity的导入功能。

或者使用aplay

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

使用任何合适的参数。

【讨论】:

  • 嘿,是的,我知道在内核空间写文件不好。这只是为了测试当前的功能。我会尝试 aplay-tip,但这并不容易,因为该模块在 android 上(没有 aplay)。但也许在我的虚拟机中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-21
  • 1970-01-01
  • 1970-01-01
  • 2021-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多