-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathstreamTranscoderv3Service.cpp
More file actions
276 lines (229 loc) · 6.78 KB
/
streamTranscoderv3Service.cpp
File metadata and controls
276 lines (229 loc) · 6.78 KB
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
#include <windows.h>
#include <stdio.h>
#include <errno.h>
#include <direct.h>
#include "socket_client.h"
#include "sourcethread.h"
#include "decodethread.h"
#include "reconnectthread.h"
#include "liboddcast/liboddcast.h"
#include <stdio.h>
#include <stdlib.h>
#ifndef WIN32
#include <errno.h>
#include <unistd.h>
#endif
#include <pthread.h>
#include <string.h>
SERVICE_STATUS ServiceStatus;
SERVICE_STATUS_HANDLE hStatus;
pthread_t sourceThread = 0;
pthread_t decodeThread = 0;
pthread_t reconnectThread = 0;
int showConsole = 1;
#define MAX_ENCODERS 50
oddcastGlobals *g[MAX_ENCODERS];
oddcastGlobals gMain;
void ServiceMain(int argc, char** argv);
void ControlHandler(DWORD request);
int InitService();
extern "C" int mainService(int argc, char **argv);
int InitService()
{
int result = 0;
return(result);
}
void installService(char *path)
{
if (path) {
char fullPath[8096*2] = "";
sprintf(fullPath, "\"%s\\streamTranscoderv3Service.exe\" \"%s\"", path, path);
SC_HANDLE handle = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
SC_HANDLE service = CreateService(
handle,
"StreamTranscoderV3",
"Stream Transcoder V3",
GENERIC_READ | GENERIC_EXECUTE,
SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START,
SERVICE_ERROR_IGNORE,
fullPath,
NULL,
NULL,
NULL,
NULL,
NULL
);
CloseServiceHandle(service);
CloseServiceHandle(handle);
printf("Service Installed\n");
}
}
void removeService()
{
SC_HANDLE handle = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
SC_HANDLE service = OpenService(handle, "StreamTranscoderV3", DELETE);
if (service) {
DeleteService(service);
}
CloseServiceHandle(service);
CloseServiceHandle(handle);
// printf("Service Removed\n");
}
void ControlHandler(DWORD request)
{
switch(request) {
case SERVICE_CONTROL_STOP:
/* Stop here */
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus (hStatus, &ServiceStatus);
return;
case SERVICE_CONTROL_SHUTDOWN:
/* Stop Here */
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus (hStatus, &ServiceStatus);
return;
default:
break;
}
// Report current status
SetServiceStatus (hStatus, &ServiceStatus);
return;
}
void ServiceMain(int argc, char** argv)
{
int error;
ServiceStatus.dwServiceType = SERVICE_WIN32;
ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwServiceSpecificExitCode = 0;
ServiceStatus.dwCheckPoint = 0;
ServiceStatus.dwWaitHint = 0;
hStatus = RegisterServiceCtrlHandler("StreamTranscoderV3", (LPHANDLER_FUNCTION)ControlHandler);
if (hStatus == (SERVICE_STATUS_HANDLE)0) {
// Registering Control Handler failed
return;
}
// Initialize Service
error = InitService();
if (error) {
// Initialization failed
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
ServiceStatus.dwWin32ExitCode = -1;
SetServiceStatus(hStatus, &ServiceStatus);
return;
}
// We report the running status to SCM.
ServiceStatus.dwCurrentState = SERVICE_RUNNING;
SetServiceStatus (hStatus, &ServiceStatus);
/* Here we do the work */
int ret = mainService(argc, (char **)argv);
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
ServiceStatus.dwWin32ExitCode = -1;
SetServiceStatus(hStatus, &ServiceStatus);
return;
}
void main(int argc, char **argv)
{
bool matched = false;
if (argv[0]) {
if (argv[1]) {
if (!strcmp(argv[1], "install")) {
removeService();
installService(argv[2]);
matched = true;
}
if (!strcmp(argv[1], "remove")) {
removeService();
matched = true;
}
}
}
if (matched) {
return;
}
_chdir(argv[1]);
SERVICE_TABLE_ENTRY ServiceTable[2];
ServiceTable[0].lpServiceName = "Stream Transcoder V3";
ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain;
ServiceTable[1].lpServiceName = NULL;
ServiceTable[1].lpServiceProc = NULL;
// Start the control dispatcher thread for our service
StartServiceCtrlDispatcher(ServiceTable);
}
void inputMetadataCallback(void *gbl, void *pValue) {
;
}
void outputStatusCallback(void *gbl, void *pValue) {
;
}
void writeBytesCallback(void *gbl, void *pValue) {
;
}
void outputServerNameCallback(void *gbl, void *pValue) {
;
}
void outputBitrateCallback(void *gbl, void *pValue) {
;
}
void outputStreamURLCallback(void *gbl, void *pValue) {
;
}
int oddcastv3_init(oddcastGlobals *g)
{
int printConfig = 0;
setServerStatusCallback(g, outputStatusCallback);
setGeneralStatusCallback(g, NULL);
setWriteBytesCallback(g, writeBytesCallback);
setBitrateCallback(g, outputBitrateCallback);
setServerNameCallback(g, outputServerNameCallback);
setDestURLCallback(g, outputStreamURLCallback);
readConfigFile(g);
return 1;
}
void usage() {
printf("usage: streamTranscoderv3Service install Dir");
}
int mainService(int argc, char *argv[])
{
char sourceURL[1024] = "";
char configFile[1024] = "";
int c = 0;
int numEncoders = 1;
char currentlogFile[1024] = "";
char logPrefix[255] = "streamTranscoder";
sprintf(configFile, "%s", logPrefix);
sprintf(currentlogFile, "%s", logPrefix);
setgLogFile(&gMain, currentlogFile);
setConfigFileName(&gMain, configFile);
addConfigVariable(&gMain, "SourceURL");
addConfigVariable(&gMain, "NumEncoders");
addConfigVariable(&gMain, "AutomaticReconnectSecs");
addConfigVariable(&gMain, "AutoConnect");
addConfigVariable(&gMain, "LogLevel");
addConfigVariable(&gMain, "LogFile");
readConfigFile(&gMain, 0);
strcpy(sourceURL, gMain.gSourceURL);
for(int i = 0; i < gMain.gNumEncoders; i++) {
if(!g[i]) {
g[i] = (oddcastGlobals *) malloc(sizeof(oddcastGlobals));
memset(g[i], '\000', sizeof(oddcastGlobals));
}
g[i]->encoderNumber = i + 1;
setgLogFile(g[i], gMain.gLogFile);
setConfigFileName(g[i], gMain.gConfigFileName);
addBasicEncoderSettings(g[i]);
initializeGlobals(g[i]);
oddcastv3_init(g[i]);
connectToServer(g[i]);
}
pthread_create(&sourceThread, NULL, &startSourceThread, sourceURL);
pthread_create(&reconnectThread, NULL, &startReconnectThread, NULL);
pthread_create(&decodeThread, NULL, &startDecodeThread, NULL);
pthread_join(sourceThread, NULL);
pthread_join(decodeThread, NULL);
return 1;
}