problem with c++, mplayer interface.

This is a discussion on problem with c++, mplayer interface. within the C++ Programming forums, part of the General Programming Boards category; anyone with c++ and mplayer knowledge know whats wrong with this? Code: #include <string.h> #include <iostream> #include <fstream> class mplayer_interface ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    29

    problem with c++, mplayer interface.

    anyone with c++ and mplayer knowledge know whats wrong with this?

    Code:
    #include <string.h>
    #include <iostream>
    #include <fstream>
    
    class mplayer_interface
    {
        private:
    
        FILE *mp_pipe;
    
        public:
        void setup()
        {
            mp_pipe = popen("mplayer -slave -quiet -idle &> /dev/null", "w");
        }
        void close()
        {
            pclose(mp_pipe);
        }
        void play( )
        {
            fputs("loadlist ./data/.currentplaylist\n", mp_pipe);
        }
        void stop()
        {
            fputs("stop\n", mp_pipe);
        }
    };
    
    void cd_to_exe_dir( char *argv[] )
    {
        std::string path = argv[0];
        int ii = path.length();
        while ( !( path[ii] == '/' || path[ii] == '\\' ) )
        {
            ii--;
        }
        path.erase( ii, 100 );
        chdir( path.c_str() );
    }
    
    int main( int argc, char *argv[] )
    {
        cd_to_exe_dir( argv );
        mplayer_interface mplayer;
        mplayer.setup();
        mplayer.play();
        std::string userI;
        while ( strcmp( userI.c_str(), "exit") != 0 )
        {
            std::cout<<'>';
            std::cin>>userI;
        }
        mplayer.stop();
        mplayer.close();
        return(0);
    }
    nothing plays until i type exit, then mplayer plays the first half second of the first file in ./data/.currentplaylist. i want it to play until exit is typed.
    Last edited by IM back!; 02-20-2010 at 07:24 AM.

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    It may need to be in its own thread, and the while loop is basically choking it out. I don't know if mplayer.play() creates its own thread or not, but worth a shot.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    So i would have to create a thread for that, darn im not so good at that.
    I'd use boost::thread for that but thread() only accepts void functions so i would have to create a new function, somthing like:
    Code:
    void mplayer_thread( int &mpipe ) 
    {
           mpipe = popen("mplayer -slave -quiet -idle" , "w");
    }
    and that would have to be outside of the class. is thair any better whay to do it?

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    why dose this now segfault?

    Code:
    #include <string.h>
    #include <iostream>
    #include <fstream>
    #include <boost/thread.hpp>
    
    void mplayer_thread( FILE *&mpipe )
    {
           mpipe = popen("mplayer -slave -quiet -idle" , "w");
    }
    
    class mplayer_interface
    {
        private:
    
        FILE *mp_pipe;
        boost::thread thread_mp;
    
        public:
        void setup()
        {
            thread_mp =  boost::thread(mplayer_thread , mp_pipe  );
            //mp_pipe = popen("mplayer -slave -quiet -idle" , "w");
        }
        void close()
        {
            pclose(mp_pipe);
            thread_mp.join();
        }
        void play( )
        {
            fputs("loadlist ./data/.currentplaylist\n", mp_pipe); //segfaults here
        }
        void stop()
        {
            fputs("stop\n", mp_pipe);
        }
    };
    
    void cd_to_exe_dir( char *argv[] )
    {
        std::string path = argv[0];
        int ii = path.length();
        while ( !( path[ii] == '/' || path[ii] == '\\' ) )
        {
            ii--;
        }
        path.erase( ii, 100 );
        chdir( path.c_str() );
    }
    
    int main( int argc, char *argv[] )
    {
        cd_to_exe_dir( argv );
        mplayer_interface mplayer;
        mplayer.setup();
        mplayer.play();
        mplayer.stop();
        std::string userI;
        while ( strcmp( userI.c_str(), "exit") != 0 )
        {
            std::cout<<'>';
            std::cin>>userI;
        }
        mplayer.stop();
        mplayer.close();
        return(0);
    }
    Last edited by IM back!; 02-21-2010 at 03:16 AM.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Do you really mean those periods to be in that order? Why not just remove them since . means current directory. The only thing worth worry about is .. since it means parent directory.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    you mean the "." in ./data/.currentplaylist? you need it. this is on linux, so /data/.currentplaylist means that data is a folder in root (wrong). and the "." in .currentplaylist is allso neaded, in unix "." before a file/folder means thats its hidden .currentplaylist is the name of the file. this also has nothing to do with the segfault.

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Code:
    void play( )
    {
        fputs("loadlist ./data/.currentplaylist\n", mp_pipe); //segfaults here
    }
    You are not testing if mp_pipe is a valid pointer before trying to write to it. It's possible your main thread calls play() before mplayer_thread() has been executed.

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,145
    Are you sure the working directory is what you think it is?

    I thought popen will start a new shell and run your command in there.

    Have you tried using absolute paths for everything?

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    29
    Code:
    void mplayer_thread( FILE *&mpipe )
    {
           mpipe = popen("mplayer -slave -quiet -idle &> /dev/null" , "w");
    }
    
    class mplayer_interface
    {
        private:
        FILE *mp_pipe;
        boost::mutex io_mutex;
        boost::thread thread_mp;
    
        public:
        void setup()
        {
            mp_pipe = NULL;
            thread_mp =  boost::thread(mplayer_thread , mp_pipe  );
            while ( mp_pipe == NULL ) std::cout<<"whaiting for pipe\n";
            //mp_pipe = popen("mplayer -slave -quiet -idle" , "w");
        }
        void close()
        {
            pclose(mp_pipe);
            thread_mp.join();
        }
        void play( )
        {
            fputs("loadlist ./data/.currentplaylist\n", mp_pipe);
        }
        void stop()
        {
            fputs("stop\n", mp_pipe);
        }
    };
    
    //other functions unchainged
    now its at an endless loop at
    Code:
    while ( mp_pipe == NULL ) std::cout<<"whaiting for pipe\n";
    Quote Originally Posted by cyberfish View Post
    Are you sure the working directory is what you think it is?
    I thought popen will start a new shell and run your command in there.
    Have you tried using absolute paths for everything?
    I donsent work with absolute paths either. and the code in the fist post worked somewhat:
    nothing plays until i type exit, then mplayer plays the first half second of the first file in ./data/.currentplaylist. i want it to play until exit is typed.
    it plays for a short time while the program exits, that means that the path must be correct, and that mplayer recived the loadlist command.

    the commands are correct, because this works in bash:
    [user@localhost ~]$ mplayer -slave -quiet -idle
    MPlayer SVN-r30040-4.4.1 (C) 2000-2009 MPlayer Team

    loadlist /home/user/C++/scratchpad/bin/Debug/data/.currentplaylist
    I'm really at a loss here.
    Last edited by IM back!; 02-21-2010 at 01:24 PM.

  10. #10
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by IM back! View Post
    it plays for a short time while the program exits, that means that the path must be correct, and that mplayer recived the loadlist command.
    Ah, I missed that part of your first post Sounds like the loadlist command to mplayer is being delayed because of output buffering. Try adding
    fflush(mp_pipe);
    after the fputs in play() and stop()

  11. #11
    Registered User
    Join Date
    Sep 2007
    Posts
    29

    Thumbs up

    Quote Originally Posted by _Mike View Post
    Ah, I missed that part of your first post Sounds like the loadlist command to mplayer is being delayed because of output buffering. Try adding
    fflush(mp_pipe);
    after the fputs in play() and stop()
    that worked! Thank you.

    heres the final code just in case someone has the same problem:

    Code:
    #include <string.h>
    #include <iostream>
    #include <fstream>
    
    class mplayer_interface
    {
        private:
    
        FILE *mp_pipe;
    
        public:
        void setup()
        {
            mp_pipe = popen("mplayer -slave -quiet -idle &> /dev/null", "w");
        }
        void close()
        {
            pclose(mp_pipe);
        }
        void play( )
        {
            fputs("loadlist ./data/.currentplaylist\n", mp_pipe);
            fflush(mp_pipe);
        }
        void stop()
        {
            fputs("stop\n", mp_pipe);
            fflush(mp_pipe);
        }
    };
    
    void cd_to_exe_dir( char *argv[] )
    {
        std::string path = argv[0];
        int ii = path.length();
        while ( !( path[ii] == '/' || path[ii] == '\\' ) )
        {
            ii--;
        }
        path.erase( ii, 100 );
        chdir( path.c_str() );
    }
    
    int main( int argc, char *argv[] )
    {
        cd_to_exe_dir( argv );
        mplayer_interface mplayer;
        mplayer.setup();
        mplayer.play();
        std::string userI;
        while ( strcmp( userI.c_str(), "exit") != 0 )
        {
            std::cout<<'>';
            std::cin>>userI;
        }
        mplayer.stop();
        mplayer.close();
        return(0);
    }
    Last edited by IM back!; 02-21-2010 at 01:25 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  2. Replies: 1
    Last Post: 03-03-2006, 11:22 AM
  3. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21