/
HDPVRChannel.cs
258 lines (231 loc) · 7.51 KB
/
HDPVRChannel.cs
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
#region Copyright (C) 2005-2011 Team MediaPortal
// Copyright (C) 2005-2011 Team MediaPortal
// http://www.team-mediaportal.com
//
// MediaPortal is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// MediaPortal is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with MediaPortal. If not, see <http://www.gnu.org/licenses/>.
#endregion
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading;
using DirectShowLib;
using TvLibrary.Interfaces;
using TvLibrary.Implementations.Helper;
using TvLibrary.Interfaces.Analyzer;
using TvLibrary.Implementations.DVB;
using TvLibrary.Implementations.DVB.Structures;
using TvLibrary.Channels;
namespace TvLibrary.Implementations.Analog
{
/// <summary>
/// Implementation of <see cref="T:TvLibrary.Interfaces.ITVCard"/> which handles analog tv cards
/// </summary>
public class HDPVRChannel : TvDvbChannel, ITvSubChannel, IVideoAudioObserver, IPMTCallback
{
#region constants
private readonly TvCardHDPVR _card;
// The Hauppauge HD PVR and Colossus deliver a DVB stream with a single service on a fixed
// service ID with a fixed PMT PID. The HD PVR 2 uses a different PMT PID.
private readonly String _deviceType;
private readonly int SERVICE_ID = 1;
private int PMT_PID = 0x100;
#endregion
/// <summary>
/// Initializes a new instance of the <see cref="HDPVRChannel"/> class.
/// </summary>
public HDPVRChannel(TvCardHDPVR card, String deviceType, Int32 subchannelId, IBaseFilter filterTsWriter, IFilterGraph2 graphBuilder)
{
_eventPMT = new ManualResetEvent(false);
_graphState = GraphState.Created;
_graphBuilder = graphBuilder;
_channelInfo = new ChannelInfo();
_pmtPid = -1;
_subChannelIndex = -1;
// Keep a reference to the card for quality control.
_card = card;
_deviceType = deviceType;
if (_deviceType.Equals("HD-PVR 2"))
{
PMT_PID = 0x200;
}
_tsFilterInterface = (ITsFilter)filterTsWriter;
_tsFilterInterface.AddChannel(ref _subChannelIndex);
_subChannelId = subchannelId;
_timeshiftFileName = "";
_recordingFileName = "";
_pmtData = null;
_pmtLength = 0;
_grabTeletext = false;
_mdplugs = null;
}
/// <summary>
/// Destructor
/// </summary>
~HDPVRChannel()
{
if (_eventPMT != null)
{
_eventPMT.Close();
}
}
#region tuning and graph method overrides
/// <summary>
/// Start timeshifting
/// </summary>
/// <param name="fileName">timeshifting filename</param>
protected override bool OnStartTimeShifting(string fileName)
{
if (_card.SupportsQualityControl && !IsRecording)
{
_card.Quality.StartPlayback();
}
return base.OnStartTimeShifting(fileName);
}
/// <summary>
/// Stop timeshifting
/// </summary>
/// <returns></returns>
protected override void OnStopTimeShifting()
{
base.OnStopTimeShifting();
if (_card.SupportsQualityControl && IsRecording)
{
_card.Quality.StartRecord();
}
}
/// <summary>
/// Start recording
/// </summary>
/// <param name="fileName">recording filename</param>
protected override void OnStartRecording(string fileName)
{
if (_card.SupportsQualityControl)
{
_card.Quality.StartRecord();
}
base.OnStartRecording(fileName);
}
/// <summary>
/// Stop recording
/// </summary>
/// <returns></returns>
protected override void OnStopRecording()
{
base.OnStopRecording();
if (_card.SupportsQualityControl && IsTimeShifting)
{
_card.Quality.StartPlayback();
}
}
/// <summary>
/// Wait for PMT to be found in the transport stream.
/// </summary>
/// <returns><c>true</c> if PMT was found, otherwise <c>false</c></returns>
protected override bool WaitForPMT()
{
int pid = PMT_PID;
bool pmtFound = false;
TimeSpan waitLength = TimeSpan.MinValue;
while (!pmtFound)
{
SetupPmtGrabber(pid, SERVICE_ID);
_pmtRequested = true;
DateTime dtStartWait = DateTime.Now;
pmtFound = _eventPMT.WaitOne(_parameters.TimeOutPMT * 1000, true);
waitLength = DateTime.Now - dtStartWait;
if (!pmtFound)
{
Log.Log.Debug("HDPVR: timed out waiting for PMT after {0} seconds", waitLength.TotalSeconds);
if (pid == 0)
{
Log.Log.Debug("Giving up waiting for PMT. You might need to increase the PMT timeout value.");
return false;
}
else
{
Log.Log.Debug("Setting pid to 0 to search for new PMT.");
pid = 0;
}
}
}
Log.Log.Debug("HDPVR: found PMT after {0} seconds", waitLength.TotalSeconds);
return HandlePmt();
}
/// <summary>
/// Stub override to allow inherritance from TvDvbChannel.
/// </summary>
protected override bool SendPmtToCam(out bool updatePids, out int waitInterval)
{
updatePids = false;
waitInterval = 0;
return true;
}
#endregion
#region IPMTCallback Members
/// <summary>
/// Called when the PMT has been received.
/// </summary>
/// <returns></returns>
public override int OnPMTReceived(int pmtPid)
{
Log.Log.WriteFile("HDPVR: OnPMTReceived() subch:{0} pid 0x{1:X}", _subChannelId, pmtPid);
_pmtPid = pmtPid;
if (_eventPMT != null)
{
_eventPMT.Set();
}
if (!_pmtRequested)
{
HandlePmt();
}
_pmtRequested = false;
return 0;
}
private bool HandlePmt()
{
IntPtr pmtMem = Marshal.AllocCoTaskMem(4096); // max. size for pmt
try
{
_pmtLength = _tsFilterInterface.PmtGetPMTData(_subChannelIndex, pmtMem);
if (_pmtLength < 6)
{
return false;
}
// Check the program number.
_pmtData = new byte[_pmtLength];
Marshal.Copy(pmtMem, _pmtData, 0, _pmtLength);
int version = ((_pmtData[5] >> 1) & 0x1F);
int pmtProgramNumber = (_pmtData[3] << 8) + _pmtData[4];
Log.Log.Info("HDPVR: PMT sid=0x{0:X} pid=0x{1:X} version=0x{2:X}", pmtProgramNumber, _pmtPid, version);
if (pmtProgramNumber != SERVICE_ID)
{
throw new TvException("HDPVRChannel: PMT program number doesn't match expected service ID");
}
// Get the program PIDs.
_pmtVersion = version;
_channelInfo = new ChannelInfo();
_channelInfo.DecodePmt(_pmtData);
_channelInfo.serviceID = pmtProgramNumber;
_channelInfo.network_pmt_PID = _pmtPid;
SetMpegPidMapping(_channelInfo);
return true;
}
finally
{
Marshal.FreeCoTaskMem(pmtMem);
}
}
#endregion
}
}