summaryrefslogtreecommitdiff
path: root/ffnetdev.c
blob: e2f098005cd79d926f33afdb056e5d8d4add9e3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/* 
 * ffnetdev.c: Full Featured Network Device VDR-Plugin for Streaming 
 *
 * See the README file for copyright information and how to reach the author.
 *
 */
 
#include <getopt.h>
#include <stdlib.h>

#include <vdr/tools.h>

#include "i18n.h"
#include "tsworker.h"
#include "netosd.h"
#include "ffnetdev.h"
#include "streamdevice.h"
#include "remote.h"
#include "osdworker.h"
#include "clientcontrol.h"
#include "config.h"
#include "ffnetdevsetup.h"


const char *cPluginFFNetDev::VERSION = "0.1.0";
const char *cPluginFFNetDev::DESCRIPTION 		= "Full Featured Network Device for Streaming";
//const char *cOSDWorker::MAINMENUENTRY 		= "FFNetDev";
 
// --- cNetOSDProvider -----------------------------------------------

cNetOSDProvider::cNetOSDProvider(void)
{
	
}

cOsd * cNetOSDProvider::CreateOsd(int Left, int Top)
{
    
    osd = new cNetOSD(Left, Top);
    return osd;
}


// --- cPluginFFNetDev ----------------------------------------------------------

const char *cPluginFFNetDev::Version(void) {
		return tr(VERSION);
}

const char *cPluginFFNetDev::Description(void) {
		return tr(DESCRIPTION);
}

cPluginFFNetDev::cPluginFFNetDev(void)
{
  // Initialize any member variables here.
  // DON'T DO ANYTHING ELSE THAT MAY HAVE SIDE EFFECTS, REQUIRE GLOBAL
  // VDR OBJECTS TO EXIST OR PRODUCE ANY OUTPUT!
  TSPort = STREAMPORT;
  OSDPort = OSDPORT;
  ControlPort = CONTROLPORT;
  EnableRemote = false;
  m_origPrimaryDevice = -1;
  m_Remote = NULL;
  
  config.iAutoSetPrimaryDVB = 0;
}

cPluginFFNetDev::~cPluginFFNetDev()
{
  dsyslog("[ffnetdev] Destructor cPluginFFNetDev\n");

  cOSDWorker::Exit();
  cTSWorker::Exit();
  cClientControl::Exit();
  
  delete m_Remote;
}

const char *cPluginFFNetDev::CommandLineHelp(void)
{
  // Return a string that describes all known command line options.
  return 
  "  -t PORT, --tsport PORT      port number for sending TS to.\n"
  "  -o PORT, --osdport PORT     listen on this port for OSD connect.\n"
  "  -c PORT, --controlport PORT listen on this port for ClientControl connect.\n"
  "  -e 			 enable remote control over OSD connection.\n"
  "\n";
}

bool cPluginFFNetDev::ProcessArgs(int argc, char *argv[])
{
  // Implement command line argument processing here if applicable.
  dsyslog("[ffnetdev] processing args.\n");
  static struct option long_options[] = {
      { "tsport",    		required_argument	, NULL, 't' },
      { "osdport",   		required_argument	, NULL, 'o' },
      { "controlport",   	required_argument	, NULL, 'c' },
      { "enable-remote",   	no_argument		, NULL, 'e' },
      { NULL, 			0			, NULL, 0 }
  };

  int c;
  while ((c = getopt_long(argc, argv, "t:o:c:e", long_options, NULL)) != -1) {
        switch (c) {
          case 'e': EnableRemote = true;
		    dsyslog("[ffnetdev] Remote enabled\n");
          	    break;
          case 't': if (isnumber(optarg)) {
                       int n = atoi(optarg);
                       if (0 < n && n < 65536) {
                          TSPort = n;
                          dsyslog("[ffnetdev] TS Port: %d\n", n);
                          break;
                       }
                    }
                    esyslog("[ffnetdev] invalid port number: %s\n", optarg);
                    return 2;
                    break;
          case 'o': if (isnumber(optarg)) {
                       int n = atoi(optarg);
                       if (0 < n && n < 65536) {
                          OSDPort = n;
                          dsyslog("[ffnetdev] OSD Port: %d\n", n);
                          break;
                       }
                    }
                    esyslog("[ffnetdev] invalid port number: %s\n", optarg);
                    return 2;
                    break;
          case 'c': if (isnumber(optarg)) {
                       int n = atoi(optarg);
                       if (0 < n && n < 65536) {
                          ControlPort = n;
                          dsyslog("[ffnetdev] ClientControl Port: %d\n", n);
                          break;
                       }
                    }
                    esyslog("[ffnetdev] invalid port number: %s\n", optarg);
                    return 2;
                    break;
	  default : return 2;
	}
  }
  dsyslog("[ffnetdev] finished processing args.\n");
  return true;
}

#if VDRVERSNUM >= 10347
cString cPluginFFNetDev::Active(void) {

    if(cOSDWorker::Active() || cTSWorker::Active() || cClientControl::Active())
           return tr("ffnetdev is running");
 
    return NULL;
}
#else
bool cPluginFFNetDev::Active(void) {
    return (cOSDWorker::Active() || cTSWorker::Active() || cClientControl::Active());
}
#endif

bool cPluginFFNetDev::Start(void)
{
  // Start any background activities the plugin shall perform.
  RegisterI18n(Phrases);
  
  	  
  cOSDWorker::Init(OSDPort, this);
  cTSWorker::Init(m_StreamDevice, TSPort, this);
  cClientControl::Init(ControlPort, this);
 
  return true;
}

bool cPluginFFNetDev::Initialize(void)
{
  // Start any background activities the plugin shall perform.
  dsyslog("[ffnetdev] initializing plugin.\n");
  m_StreamDevice = new cStreamDevice();
  return true;
}

void cPluginFFNetDev::Housekeeping(void)
{
  // Perform any cleanup or other regular tasks.
}

cOsdObject *cPluginFFNetDev::MainMenuAction(void)
{
  // Perform the action when selected from the main VDR menu.
  dsyslog("[ffnetdev] MainMenuAction called.\n");
//  return new cMenuSetupSoftdevice;
  return NULL;
}

cMenuSetupPage *cPluginFFNetDev::SetupMenu(void)
{
  // Return our setup menu
//  return new cMenuSetupFFNetDev;
  return new cFFNetDevSetup;
}

bool cPluginFFNetDev::SetupParse(const char *Name, const char *Value)
{
  // Parse your own setup parameters and store their values.
  if (!strcasecmp(Name, "AutoSetPrimaryDVB"))     config.iAutoSetPrimaryDVB = atoi(Value);
  else
    return false;
    
  return true;
}

void cPluginFFNetDev::SetPrimaryDevice()
{	
   int i = 0;
   while ((cOsd::IsOpen() > 0) && (i-- > 0))
      cRemote::Put(kBack);
      
   i = 0;
   while ((strlen(m_ClientName) == 0) && (i++ < 2))
      sleep(1); 
   
   if ((config.iAutoSetPrimaryDVB == 1) && (m_origPrimaryDevice == -1))
   {
      cDevice *PrimaryDevice;
      if ((PrimaryDevice = cDevice::PrimaryDevice()) != NULL)
         m_origPrimaryDevice = PrimaryDevice->DeviceNumber() + 1;
      else
         m_origPrimaryDevice = -1;
      
      if (m_StreamDevice->DeviceNumber() + 1 != m_origPrimaryDevice)
      {
         cDevice::SetPrimaryDevice(m_StreamDevice->DeviceNumber() + 1);
         isyslog("[ffnetdev] set Primary Device to %d", m_StreamDevice->DeviceNumber() + 1);
      }
      else
      {
         m_origPrimaryDevice = -1;
      }
   }
   
   if(EnableRemote) 
   {
      if (m_Remote == NULL)
      {            
         char str[30];
         if (strlen(m_ClientName) > 0)
            sprintf(str, "ffnetdev-%s", m_ClientName);
         else
            strcpy(str, "ffnetdev");
         
         m_Remote = new cMyRemote(str);
      }
   
      if (!cRemote::HasKeys())
         new cLearningThread();
      dsyslog("[ffnetdev] remote control enabled.\n");
   }
   else 
   {
      dsyslog("[ffnetdev] remote control disabled.\n");
   }
}


void cPluginFFNetDev::RestorePrimaryDevice()
{
   int i = 10;
   while ((cOsd::IsOpen() > 0) && (i-- > 0))
      cRemote::Put(kBack);
   
   dsyslog("[ffnetdev] remote control disabled.\n");
   
   if (m_origPrimaryDevice != -1)
   {
      cDevice::SetPrimaryDevice(m_origPrimaryDevice);
      isyslog("[ffnetdev] restore Primary Device to %d", m_origPrimaryDevice);
      m_origPrimaryDevice = -1;
      sleep(5);
   }					
   
   m_ClientName[0] = '\0';
   if (m_Remote)
   {
      Remotes.Del(m_Remote);
      m_Remote = NULL;
   }
}

void cPluginFFNetDev::SetClientName(char* ClientName)
{
   strcpy(m_ClientName, ClientName);
   dsyslog("[ffnetdev] SetClientname %s", m_ClientName);
}


sFFNetDevConfig config;

VDRPLUGINCREATOR(cPluginFFNetDev); // Don't touch this!