-
Notifications
You must be signed in to change notification settings - Fork 0
/
TTMPEGExporter.m
267 lines (234 loc) · 6.59 KB
/
TTMPEGExporter.m
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
//
// TTMPEGExporter.m
// EyeTVEPGParser
//
// Created by Ryan Walklin on 9/03/11.
// Copyright 2011 Test Toast. All rights reserved.
//
#import "TTMPEGExporter.h"
@interface TTMPEGExporter (Private)
-(NSURL *)applicationSupportFolderURL;
@end
@implementation TTMPEGExporter
-(id)init
{
self = [super init];
if (self)
{
_streamHandle = nil;
_updateTimer = [NSTimer scheduledTimerWithTimeInterval:3600
target:self
selector:@selector(updateEPG:)
userInfo:nil
repeats:YES];
_streamWriteQueue = dispatch_queue_create(NULL, NULL);
_wantPackets = NO;
}
return self;
}
-(avPIDArray *)avPIDArray
{
return _pidArray;
}
-(void)updateEPG:(NSTimer *)timer
{
NSLog(@"Running EPG update");
if ([self hasValidTSStream])
{
NSLog(@"Closing previous EPG dump");
[self closeTSStream];
}
_firstPacketTime = CFAbsoluteTimeGetCurrent();
_wantPackets = YES;
_packetCount = 0;
}
-(BOOL)hasValidTSStream
{
return (_streamHandle != nil);
}
-(BOOL)writePackets:(void *)packets count:(UInt32)count
{
if (!packets || count == 0 || !_wantPackets)
{
return NO;
}
NSData *packetData = [[NSData alloc] initWithBytes:packets length:count];
//dispatch_async(_streamWriteQueue, ^{
NSURL *streamURL = [[self applicationSupportFolderURL] URLByAppendingPathComponent:@"epg.ts"];
NSError *err = nil;
@try
{
if (!_streamHandle)
{
// NSFileHandle utils don't like opening new files as streams, so write the first packet directly to disk
if (![packetData writeToURL:streamURL options:0 error:&err])
{
NSLog(@"Packet write failed: %@", [err localizedDescription]);
}
_streamHandle = [[NSFileHandle fileHandleForUpdatingURL:streamURL error:&err] retain];
[_streamHandle seekToEndOfFile];
NSLog(@"Started writing EPG dump to %@", [streamURL path]);
}
else
{
[_streamHandle writeData:packetData];
}
_packetCount += count / sizeof(TransportStreamPacket);
if (_packetCount % 1000 == 0)
{
NSLog(@"%llu packets written", _packetCount);
}
}
@catch (NSException * e)
{
NSLog(@"Exception while creating file handle: %@", [e description]);
[self closeTSStream];
}
@finally
{
[packetData release];
}
if (!_streamHandle)
{
return;
}
CFAbsoluteTime now = CFAbsoluteTimeGetCurrent();
if (_packetCount > 2000 && !_pidArray)
{
// should be ok to parse PIDs
[_streamHandle synchronizeFile];
[_streamHandle seekToFileOffset:0];
_pidArray = [TTTransportStreamParser parseDSMCC:_streamHandle];
[_streamHandle seekToEndOfFile];
if (!_pidArray)
{
NSLog(@"Error retrieving PIDs from transport stream");
[self closeTSStream];
_wantPackets = NO;
_firstPacketTime = now;
return;
}
}
if (now -_firstPacketTime > 65)
{
// parse EPG
if (_pidArray->mheg5PID == 0)
{
NSLog(@"No DSM-CC object found in carousel, cannot parse EPG");
return;
}
// we have enough packets
_wantPackets = NO;
NSLog(@"Stored %llu packets at %@", _packetCount, [streamURL path]);
[_streamHandle synchronizeFile];
[self closeTSStream];
// generate config for EPG Collector
NSURL *iniURL = [[self applicationSupportFolderURL] URLByAppendingPathComponent:@"EyeTVMHEG5.ini"];
NSString *iniString = [NSString stringWithFormat:@"Output=%@/TVGuide.xml\n\n"
"TSFile=%@/epg.ts\nOption=MHEG5PID-%u\n\n"
"ScanningFrequency=12456000,MHEG5\n",
[[self applicationSupportFolderURL] path],
[[self applicationSupportFolderURL] path],
_pidArray->mheg5PID];
NSError *err = nil;
if (![iniString writeToURL:iniURL
atomically:YES
encoding:NSUTF8StringEncoding
error:&err])
{
NSLog(@"Failed to write EPG Collector configuration - %@", [err localizedDescription]);
return;
}
@try
{
NSTask *monoTask = [[NSTask alloc] init];
[monoTask setLaunchPath:@"/usr/bin/mono"];
[monoTask setArguments:[NSArray arrayWithObjects:@"/Library/Application Support/EyeTV/Plugins/EyeTVEPGParser.bundle/Contents/Resources/EPGCollector/EPGCollector.exe",
[NSString stringWithFormat:@"/ini=%@",
[iniURL path]], nil]];
[monoTask launch];
[monoTask waitUntilExit];
if ([monoTask terminationStatus] != 0)
{
NSLog(@"EPG parse from TS failed");
}
}
@catch (NSException * e)
{
NSLog(@"Exception while launching EPG Collector: %@", [e description]);
}
@finally
{
// remove TS
if (![[NSFileManager defaultManager] removeItemAtURL:[[self applicationSupportFolderURL] URLByAppendingPathComponent:@"epg.ts"] error:&err])
{
NSLog(@"Failed to remove epg TS dump - %@", [err localizedDescription]);
}
}
// load XML into EyeTV
@try
{
NSTask *importTask = [[NSTask alloc] init];
[importTask setLaunchPath:@"/usr/bin/open"];
[importTask setArguments:[NSArray arrayWithObjects:@"-ga",
@"/Applications/EyeTV.app",
[[[self applicationSupportFolderURL] URLByAppendingPathComponent:@"TVGuide.xml"] path],
nil]];
[importTask launch];
//[importTask waitUntilExit];
//if ([importTask terminationStatus] != 0)
{
// NSLog(@"Import into EyeTV failed");
}
}
@catch (NSException * e)
{
NSLog(@"Exception while running EyeTV import: %@", [e description]);
}
@finally
{
free(_pidArray);
_pidArray = nil;
}
}
//});
return YES;
}
-(BOOL)closeTSStream
{
NSLog(@"Closing TS Stream");
if (_streamHandle)
{
[_streamHandle closeFile];
}
_streamHandle = nil;
_wantPackets = NO;
_packetCount = 0;
return YES;
}
-(void)dealloc
{
dispatch_release(_streamWriteQueue);
[_updateTimer invalidate];
[_updateTimer release];
[super dealloc];
}
-(NSURL *)applicationSupportFolderURL
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : NSTemporaryDirectory();
NSString *appSupportPath = [basePath stringByAppendingPathComponent:[[NSProcessInfo processInfo] processName]];
NSURL *appSupportURL = [NSURL fileURLWithPath:appSupportPath];
if (![appSupportURL checkResourceIsReachableAndReturnError:nil])
{
// create folder if it doesn't exist
NSError *err = nil;
if (![[NSFileManager defaultManager] createDirectoryAtPath:appSupportPath withIntermediateDirectories:YES attributes:nil error:&err])
{
NSLog(@"Failed to create Application Support directory: %@", [err localizedDescription]);
return nil;
}
}
return appSupportURL;
}
@end