【问题标题】:C program that controls mplayer控制mplayer的C程序
【发布时间】:2016-10-24 19:46:06
【问题描述】:

我必须在 linux 上用 C 语言编写程序,可以使用 mknod() 函数控制 mplayer。

当我通过这个命令运行 mplayer 时

mplayer -input file=/tmp/film spiderman.ts

我想用我的 C 程序来控制它,就像使用 echo 函数一样

echo "pause" >> /tmp/film

这是我的代码:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <signal.h>
#include <unistd.h>


int main(int argc, char* argv[]){
    int fdes,res;
    char x;

    /*commands*/
    char msg1[] = "pause\n";
    char msg2[] = "quit\n";

     /*creating fifo file*/
    unlink("/tmp/film");
    res=mknod("/tmp/film", S_IFIFO|0666, 0);
    if (res<0) perror("error_creating_fifo");

    fdes = open("/tmp/film", O_WRONLY);
    if (fdes<0) perror("error_open_fifo");


    while(1)
    {
        printf("Enter command\n");
        x = getchar();
        getchar();//dont take enter character
        switch(x)
        {
            case 'p': 
                printf("PAUSE\n");
                write(fdes, msg1, sizeof(msg1));
                break;
            case 'q': 
                printf("QUIT\n");
                write(fdes, msg2, sizeof(msg2));
                break;
            default:
                printf("Unknown command");
                break;
        }
    }
    close(fdes);
    return 0;
}

问题是,它只能工作一次。例如,我不能暂停然后取消暂停电影。

【问题讨论】:

  • mplayer不是已经有合适的控制界面了吗?甚至是 dbus?
  • fdes 从未初始化。也许您打算在其他地方使用 res ?
  • 是的,我在准备发布正确问题的代码时忘记粘贴 fdes 初始化。已修复:)
  • 您解决了这个问题吗?您是否可能必须为每个命令关闭和打开管道?如果你运行echo "pause" &gt;&gt; /tmp/film,管道将在回显完成后关闭,mplayer 会看到它关闭。您可以通过运行cat /tmp/film 而不是 mplayer 来测试这一点,然后运行 ​​echo 命令,cat 将打印“暂停”然后退出。可能 mplayer 正在寻找命令之间管道的这个 close

标签: c linux mknod


【解决方案1】:

为每个命令关闭和重新打开管道对我来说很有效:

#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>

char MPLAYER_CTRL[] = "/tmp/mplayer-control";

int startMPlayerInBackground()
{
    pid_t processId = fork();

    if (processId == 0)
    {
        printf("running mplayer\n");
        char cmd[256];
        snprintf(cmd, 256, "mplayer -quiet -fs -slave -idle -input file=%s", MPLAYER_CTRL);
        int status = system(cmd);
        printf("mplayer ended with status %d\n", status);
        exit(status);
    }
    else 
    {
        return processId;
    }
}

void send(char* cmd)
{
    int fdes = open(MPLAYER_CTRL, O_WRONLY);
    write(fdes, cmd, strlen(cmd));
    close(fdes);    
}

int main(int argc, char *args[])
{
    unlink(MPLAYER_CTRL);
    int res = mknod(MPLAYER_CTRL, S_IFIFO|0777, 0);

    pid_t processId = startMPlayerInBackground();

    if (processId < 0) 
    {
        printf("failed to start child process\n");
    }
    else
    {
        send("loadfile /home/duo/ninja.mp3\n");
        sleep(2);
        send("pause\n");
        sleep(1);
        send("pause\n");
        sleep(2);
        send("quit\n");
    }
    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-15
    • 2016-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多