-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsample.cpp
214 lines (199 loc) · 4.48 KB
/
sample.cpp
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
/*
$ g++ -O2 -Wall -rdynamic -o sample sample.cpp -ldl
*/
#define _FILE_OFFSET_BITS 64
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <time.h>
#include <dlfcn.h>
#include "typedef.h"
#include "IBonDriver2.h"
static volatile BOOL bStop = FALSE;
static void handler(int sig)
{
bStop = TRUE;
}
void usage(char *p)
{
fprintf(stderr, "usage: %s [-b bondriver] [-s space_no] [-c channel_no] ( [-t sec] [-o filename] )\n", p);
exit(0);
}
int main(int argc, char *argv[])
{
int opt, bfind, sfind, cfind, ofind, wfd;
char *bon, *output;
DWORD sec, dwSpace, dwChannel;
// パラメータ処理
sec = 0xffffffff;
bon = output = NULL;
bfind = sfind = cfind = ofind = 0;
dwSpace = dwChannel = 0;
while ((opt = getopt(argc, argv, "b:s:c:t:o:")) != -1)
{
switch (opt)
{
case 'b': // BonDriver指定
bon = optarg;
bfind = 1;
break;
case 's': // 使用スペース指定
dwSpace = strtoul(optarg, NULL, 10);
sfind = 1;
break;
case 'c': // 使用チャンネル指定
dwChannel = strtoul(optarg, NULL, 10);
cfind = 1;
break;
case 't': // チューナ使用時間指定(単位:秒)
sec = strtoul(optarg, NULL, 10);
break;
case 'o': // 出力先指定
output = optarg;
ofind = 1;
break;
default:
usage(argv[0]);
}
}
if (!bfind || !sfind || !cfind)
usage(argv[0]);
// モジュールロード
void *hModule = dlopen(bon, RTLD_LAZY);
if (!hModule)
{
fprintf(stderr, "dlopen error: %s\n", dlerror());
return -1;
}
// 出力先指定無しなら標準出力へ
if (!ofind)
wfd = 1;
else
{
wfd = open(output, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR);
if (wfd < 0)
{
perror("open");
dlclose(hModule);
return -2;
}
}
// インスタンス作成
IBonDriver *pIBon;
IBonDriver2 *pIBon2;
pIBon = pIBon2 = NULL;
char *err;
IBonDriver *(*f)() = (IBonDriver *(*)())dlsym(hModule, "CreateBonDriver");
if ((err = dlerror()) == NULL)
{
pIBon = f();
if (pIBon)
pIBon2 = dynamic_cast<IBonDriver2 *>(pIBon);
}
else
{
fprintf(stderr, "dlsym error: %s\n", err);
dlclose(hModule);
close(wfd);
return -2;
}
if (!pIBon || !pIBon2)
{
fprintf(stderr, "CreateBonDriver error: pIBon[%p] pIBon2[%p]\n", pIBon, pIBon2);
dlclose(hModule);
close(wfd);
return -3;
}
// ここから実質のチューナオープン & TS取得処理
BOOL b = pIBon->OpenTuner();
if (!b)
{
fprintf(stderr, "OpenTuner error\n");
pIBon->Release();
dlclose(hModule);
close(wfd);
return -3;
}
// 指定チャンネルにセット
pIBon2->SetChannel(dwSpace, dwChannel);
// 終了タイマー & 停止シグナル用ハンドラ登録
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = handler;
sa.sa_flags = SA_RESTART;
sigemptyset(&sa.sa_mask);
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGALRM, &sa, NULL);
// 終了タイマーセット
alarm(sec);
// TS取得開始
BYTE *pBuf;
DWORD dwSize, dwRemain;
timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = 10 * 1000 * 1000; // 10ms
while (!bStop)
{
// TSストリーム取得
dwSize = dwRemain = 0;
if (pIBon->GetTsStream(&pBuf, &dwSize, &dwRemain) && (dwSize != 0))
{
// この時点でpBufに長さdwSize分のTSストリームを取得した状態なので、それを書き出し
// (もしバッファリングやデスクランブルやTS分離処理を入れるならこの辺で)
int len, left = (int)dwSize;
do
{
len = write(wfd, pBuf, left);
if (len < 0)
{
perror("write");
goto err;
}
left -= len;
pBuf += len;
} while (left > 0);
}
// 取得待ちTSデータが無ければ適当な時間待つ
if (dwRemain == 0)
nanosleep(&ts, NULL);
}
// チューナクローズ
pIBon->CloseTuner();
// 現在のBonDriver_LinuxPT/DVB/Proxyの実装では無意味なのに気付いたので削除(;´Д`)失礼しました
#if 0
// BonDriver内のバッファに残っている分があれば一応回収
while (1)
{
dwSize = dwRemain = 0;
if (pIBon->GetTsStream(&pBuf, &dwSize, &dwRemain) && (dwSize != 0))
{
int len, left = (int)dwSize;
do
{
len = write(wfd, pBuf, left);
if (len < 0)
{
perror("write");
goto err;
}
left -= len;
pBuf += len;
} while (left > 0);
}
if (dwRemain == 0)
break;
}
#endif
err:
close(wfd);
// インスタンス解放 & モジュールリリース
pIBon->Release();
dlclose(hModule);
return 0;
}