3 #include "devicedvbinput.h"
4 #include "devicedvbinput.inc"
6 #include "edlsession.h"
9 #include "recordconfig.h"
10 #include "renderfarm.h"
16 #include <netinet/in.h>
18 #include <sys/socket.h>
24 DeviceDVBInput::DeviceDVBInput(MWindow *mwindow, VideoInConfig *config)
25 : GarbageObject("DeviceDVBInput")
28 this->config = config;
29 this->mwindow = mwindow;
30 signal_lock = new Condition(0, "DeviceDVBInput::signal_lock", 0);
31 channel = new Channel;
34 DeviceDVBInput::~DeviceDVBInput()
38 // Because MWindow::dvb_input_lock must be locked before
39 // Garbage::delete_object,
40 // MWindow::dvb_input_lock is always going to be locked when this is called.
41 mwindow->dvb_input = 0;
46 void DeviceDVBInput::reset()
56 DeviceDVBInput* DeviceDVBInput::get_input_thread(MWindow *mwindow)
58 DeviceDVBInput *input_thread = 0;
59 mwindow->dvb_input_lock->lock("DeviceDVBInput::get_input_thread");
60 // reference existing input if it exists
61 if(mwindow->dvb_input)
63 input_thread = mwindow->dvb_input;
64 input_thread->add_user();
67 // create shared input if it doesn't exist
69 input_thread = mwindow->dvb_input =
70 new DeviceDVBInput(mwindow, mwindow->edl->session->vconfig_in);
71 input_thread->GarbageObject::add_user();
75 mwindow->dvb_input_lock->unlock();
79 void DeviceDVBInput::put_input_thread(MWindow *mwindow)
81 mwindow->dvb_input_lock->lock("DeviceDVBInput::put_input_thread");
82 mwindow->dvb_input->GarbageObject::remove_user();
83 Garbage::delete_object(mwindow->dvb_input);
84 mwindow->dvb_input_lock->unlock();
87 int DeviceDVBInput::close()
98 int DeviceDVBInput::try_tuner()
101 printf("DeviceDVBInput::try_tuner 1\n");
102 tuner_fd = RenderFarmServerThread::open_client(config->dvb_in_host,
103 config->dvb_in_port);
105 printf("DeviceDVBInput::try_tuner 10\n");
106 if(tuner_fd < 0) return 1;
109 // Send command for tuner mode
110 error = write_int64(RENDERFARM_TUNER);
118 printf("DeviceDVBInput::try_tuner 20\n");
119 // Read status from tuner
121 int available = !read_int64(&error);
123 if(!available || error)
125 printf("DeviceDVBInput::try_tuner available=%d error=%d\n", available, error);
136 int DeviceDVBInput::reopen_tuner()
149 // Try tuner until it works.
150 for(int attempts = 0; attempts < 3; attempts++)
152 if(!try_tuner()) break;
162 // No tuner available
165 printf("DeviceDVBInput::reopen_tuner: tuner '%s' not available\n",
166 config->dvb_in_host);
172 // Extra parameters should come before the NETTUNE_CHANNEL command
173 // in the form of NETTUNE_SET commands.
174 if(write_int64(NETTUNE_SET_CHANNEL)) return 1;
175 if(write_int64(channel->entry)) return 1;
177 if(write_int64(NETTUNE_SET_TABLE)) return 1;
178 if(write_int64(channel->freqtable)) return 1;
180 if(write_int64(NETTUNE_SET_AUDIO_PID)) return 1;
181 if(write_int64(channel->audio_pid)) return 1;
183 if(write_int64(NETTUNE_SET_VIDEO_PID)) return 1;
184 if(write_int64(channel->video_pid)) return 1;
188 if(write_int64(NETTUNE_OPEN)) return 1;
192 printf("DeviceDVBInput::reopen_tuner: open failed\n");
201 int DeviceDVBInput::get_signal_strength(int channel, int tuner_id)
206 signal_lock->lock("NetTune::get_signal_strength");
211 // Zero power if no lock
224 return signal_result;
230 int64_t DeviceDVBInput::read_int64(int *error)
233 if(!error) error = &temp;
235 unsigned char data[sizeof(int64_t)];
236 *error = (read_tuner(data, sizeof(int64_t)) != sizeof(int64_t));
238 // Make it return 1 if error so it can be used to read a result code from the
243 result = (((int64_t)data[0]) << 56) |
244 (((uint64_t)data[1]) << 48) |
245 (((uint64_t)data[2]) << 40) |
246 (((uint64_t)data[3]) << 32) |
247 (((uint64_t)data[4]) << 24) |
248 (((uint64_t)data[5]) << 16) |
249 (((uint64_t)data[6]) << 8) |
255 int DeviceDVBInput::write_int64(int64_t value)
257 unsigned char data[sizeof(int64_t)];
258 data[0] = (value >> 56) & 0xff;
259 data[1] = (value >> 48) & 0xff;
260 data[2] = (value >> 40) & 0xff;
261 data[3] = (value >> 32) & 0xff;
262 data[4] = (value >> 24) & 0xff;
263 data[5] = (value >> 16) & 0xff;
264 data[6] = (value >> 8) & 0xff;
265 data[7] = value & 0xff;
266 // printf("DeviceDVBInput::write_int64 %02x %02x %02x %02x %02x %02x %02x %02x\n",
275 return (write_tuner(data, sizeof(int64_t)) != sizeof(int64_t));
278 int DeviceDVBInput::write_tuner(unsigned char *data, int size)
280 return write(tuner_fd, data, size);
283 int DeviceDVBInput::read_stream(unsigned char *data, int size)
286 if(tuner_fd < 0 || channel_changed)
288 int result2 = reopen_tuner();
305 int DeviceDVBInput::read_tuner(unsigned char *data, int size)
311 bytes_read = ::read(tuner_fd, data + offset, size);
314 offset += bytes_read;
320 printf("NetTune::read_tuner connection closed\n");