1 #ifndef INCLUDED_AUDIOIO_FORKED_STREAM_H
2 #define INCLUDED_AUDIOIO_FORKED_STREAM_H
3 
4 #include <string>
5 #include <kvu_temporary_file_directory.h>
6 
7 #include "audioio-barrier.h"
8 #include "sample-specs.h"
9 
10 /**
11  * Helper class providing routines for forking new processes
12  * and creating read/write pipes between the child and the
13  * parent process.
14  *
15  * @author Kai Vehmanen
16  */
17 class AUDIO_IO_FORKED_STREAM : public AUDIO_IO_BARRIER {
18 
19  private:
20 
21   int pid_of_parent_rep;
22   int pid_of_child_rep;
23   int fd_rep;
24   bool last_fork_rep;
25   bool sigkill_sent_rep;
26   std::string tmpfile_repp;
27   bool tmp_file_created_rep;
28   bool use_named_pipe_rep;
29   std::string command_rep;
30   std::string object_rep;
31   TEMPORARY_FILE_DIRECTORY tempfile_dir_rep;
32 
33   void init_temp_directory(void);
34   void fork_child_for_fifo_read(void);
35 
36   void init_state_before_fork(void);
37 
38 public:
39 
40   virtual void stop_io(void);
41 
42  protected:
43 
44   /**
45    * Initializes the command string. This must be done before any other set_*
46    * calls.
47    */
set_fork_command(const std::string & cmd)48   void set_fork_command(const std::string& cmd) { command_rep = cmd; }
49 
50   void set_fork_file_name(const std::string& filename);
51   void set_fork_pipe_name(void);
52   void set_fork_channels(int channels);
53   void set_fork_sample_rate(long int sample_rate);
54   void set_fork_bits(int bits);
55 
56   void fork_child_for_read(void);
57   void fork_child_for_write(void);
58   void clean_child(bool force = false);
59 
fork_command(void)60   const std::string& fork_command(void) const { return(command_rep); }
61 
62   bool wait_for_child(void) const;
child_fork_succeeded(void)63   bool child_fork_succeeded(void) const { return(last_fork_rep); }
pid_of_child(void)64   int pid_of_child(void) const { return(pid_of_child_rep); }
file_descriptor(void)65   int file_descriptor(void) const { return(fd_rep); }
66 
67   virtual bool do_supports_seeking(void) const = 0;
68   virtual void do_set_position_in_samples(SAMPLE_SPECS::sample_pos_t pos) = 0;
69 
70 public:
71 
AUDIO_IO_FORKED_STREAM(void)72   AUDIO_IO_FORKED_STREAM(void) :
73     pid_of_parent_rep(-1),
74     pid_of_child_rep(-1),
75     fd_rep(0),
76     last_fork_rep(false),
77     sigkill_sent_rep(false),
78     tmp_file_created_rep(false),
79     use_named_pipe_rep(false) { }
80   virtual ~AUDIO_IO_FORKED_STREAM(void);
81 };
82 
83 #endif
84