Example #1
0
        private void btnScan_Click(object sender, EventArgs e)
        {
            if (lvResult.Items.Count > 0)
            {
                lvResult.Items.Clear();
            }
            //StartScanThread();
            _decoder.TableChangeDetected += _decoder_TableChangeDetected;
            var programMapTables = new List <ProgramMapTable>();

            _isScanning   = true;
            _stopScanning = false;
            SetControlPropertyThreadSafe(btnScan, "Text", "Stop Search");
            programMapTables.Clear();
            patfound  = false;
            pmtsfound = false;
            nitfound  = false;
            sdtfound  = false;
            SetControlPropertyThreadSafe(lblPat, "BackColor", Color.LightGreen);
            SetControlPropertyThreadSafe(lblPmt, "BackColor", Color.LightGreen);
            SetControlPropertyThreadSafe(lblSdt, "BackColor", Color.LightGreen);
            SetControlPropertyThreadSafe(lblNit, "BackColor", Color.LightGreen);
            if (_stopScanning)
            {
                return;
            }

            RtspStatusCode statuscode;

            if (string.IsNullOrEmpty(_device.RtspSession.SessionID))
            {
                _device.RtspSession.Options();
                _device.RtspSession.Setup(_currenttunestring, TransmissionMode.Unicast);
                _device.RtspSession.Play(_currenttunestring);
                _udpclient      = new UdpClient(_device.RtspSession.RtpPort);
                _remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
                _device.RtspSession.RecieptionInfoChanged += RtspSession_RecieptionInfoChanged;
            }
            else
            {
                _device.RtspSession.Play(_currenttunestring);
            }

            /* Say the Sat>IP server we want Receives the Recieption Details SDP */
            statuscode = _device.RtspSession.Describe();

            if (_locked)
            {
                while (!patfound || !pmtsfound || !sdtfound || !nitfound)
                {
                    var receivedbytes = _udpclient.Receive(ref _remoteEndPoint);
                    var rtp           = RtpPacket.Decode(receivedbytes);
                    if (rtp.HasPayload)
                    {
                        var packets = packetFactory.GetTsPacketsFromData(rtp.Payload);

                        Logger.Write(rtp.Payload.ToHexString());
                        _decoder.AddData(receivedbytes);
                    }

                    //}
                    lock (_decoder)
                    {
                        programMapTables = _decoder?.ProgramMapTables.OrderBy(p => p.ProgramNumber).ToList();
                        foreach (var programMapTable in programMapTables)
                        {
                            short videoPid = -1;
                            short audioPid = -1;
                            short ac3pid   = -1;
                            short ttxpid   = -1;
                            short subpid   = -1;
                            var   desc     = _decoder.GetServiceDescriptorForProgramNumber(programMapTable?.ProgramNumber);
                            if (desc != null)
                            {
                                if (programMapTable?.EsStreams != null)
                                {
                                    foreach (var stream in programMapTable.EsStreams)
                                    {
                                        if (stream != null)
                                        {
                                            if (stream.Descriptors.OfType <Ac3Descriptor>().Any())
                                            {
                                                ac3pid = stream.ElementaryPid;
                                            }
                                            if (stream.Descriptors.OfType <Eac3Descriptor>().Any())
                                            {
                                                ac3pid = stream.ElementaryPid;
                                            }
                                            if (stream.Descriptors.OfType <SubtitlingDescriptor>().Any())
                                            {
                                                subpid = stream.ElementaryPid;
                                            }
                                            if (stream.Descriptors.OfType <TeletextDescriptor>().Any())
                                            {
                                                ttxpid = stream.ElementaryPid;
                                            }
                                            switch (stream.StreamType)
                                            {
                                            case 0x01: // ISO/IEC 11172-2 (MPEG-1 video) in a packetized stream
                                            case 0x02
                                                :      // ITU-T Rec. H.262 and ISO/IEC 13818-2 (MPEG-2 higher rate interlaced video) in a packetized stream
                                            case 0x1B
                                                :      // ITU-T Rec. H.264 and ISO/IEC 14496-10 (lower bit-rate video) in a packetized stream
                                            case 0x24
                                                :      // ITU - T Rec.H.265 and ISO/ IEC 23008 - 2(Ultra HD video) in a packetized stream
                                            {
                                                videoPid = stream.ElementaryPid;
                                                break;
                                            }

                                            case 0x03: // ISO/IEC 11172-3 (MPEG-1 audio) in a packetized stream
                                            case 0x04
                                                :      // ISO/IEC 13818-3 (MPEG-2 halved sample rate audio) in a packetized stream

                                            {
                                                audioPid = stream.ElementaryPid;
                                                break;
                                            }

                                            default:
                                            {
                                                Console.Write(stream.StreamType.ToString());
                                                break;
                                            }
                                            }
                                        }
                                    }
                                }

                                var chan = new Channel
                                {
                                    ServiceType             = desc.ServiceType,
                                    ServiceName             = desc.ServiceName.Value,
                                    ServiceProvider         = desc.ServiceProviderName.Value,
                                    ServiceId               = programMapTable.ProgramNumber,
                                    ProgramClockReferenceId = programMapTable.PcrPid,
                                    AudioPid    = audioPid,
                                    VideoPid    = videoPid,
                                    AC3Pid      = ac3pid,
                                    TTXPid      = ttxpid,
                                    SubTitlePid = subpid
                                };
                                AddResults(chan);
                            }
                        }
                    }
                }

                //Thread.Sleep(55000);
                _device.RtspSession.TearDown();
                SetControlPropertyThreadSafe(pgbSearchResult, "Value", 100);
                _isScanning = false;
                SetControlPropertyThreadSafe(btnScan, "Text", "Start Search");
            }
        }
        private void DoScan()
        {
            _decoder.TableChangeDetected += _decoder_TableChangeDetected;
            List <ProgramMapTable> programMapTables = new List <ProgramMapTable>();

            _isScanning   = true;
            _stopScanning = false;
            SetControlPropertyThreadSafe(btnScan, "Text", "Stop Search");
            foreach (var mapping in _mappings)
            {
                IniReader reader = new IniReader(mapping.Mapping.File);
                var       Count  = reader.ReadInteger("DVB", "0", 0);
                try
                {
                    var    Index  = 1;
                    string source = mapping.Source;
                    string tuning;
                    while (Index <= Count)
                    {
                        programMapTables.Clear();
                        patfound  = false;
                        pmtsfound = false;
                        nitfound  = false;
                        sdtfound  = false;
                        SetControlPropertyThreadSafe(lblPat, "BackColor", Color.LightGreen);
                        SetControlPropertyThreadSafe(lblPmt, "BackColor", Color.LightGreen);
                        SetControlPropertyThreadSafe(lblSdt, "BackColor", Color.LightGreen);
                        SetControlPropertyThreadSafe(lblNit, "BackColor", Color.LightGreen);
                        if (_stopScanning)
                        {
                            return;
                        }
                        float percent = ((float)(Index)) / Count;
                        percent *= 100f;
                        if (percent > 100f)
                        {
                            percent = 100f;
                        }
                        SetControlPropertyThreadSafe(pgbSearchResult, "Value", (int)percent);
                        string[] strArray = reader.ReadString("DVB", Index.ToString()).Split(new char[] { ',' });

                        if (strArray[2] == "T2")
                        {
                            // frequency,bandwith,modulationsystem, modulationtype, guardintervall,fec
                            tuning = string.Format("freq={0}&bw={1}&msys={2}&pids=0,16",
                                                   strArray[0].ToString(), strArray[1].ToLower().ToString(), "dvbt2");
                        }
                        else
                        {
                            // frequency,bandwith,modulationsystem, modulationtype, guardintervall,fec
                            tuning = string.Format("freq={0}&bw={1}&msys={2}&tmode=2k&mtype={3}&gi={4}&fec={5}&pids=0",
                                                   strArray[0].ToString(), strArray[1].ToLower().ToString(), "dvbt", strArray[3].ToLower().ToString(), strArray[4].ToLower().ToString(), strArray[5].ToString());
                        }

                        RtspStatusCode statuscode;
                        if (string.IsNullOrEmpty(_device.RtspSession.SessionID))
                        {
                            _device.RtspSession.Options();
                            _device.RtspSession.Setup(tuning, TransmissionMode.Unicast);
                            _device.RtspSession.Play(string.Empty);
                            _udpclient      = new UdpClient(_device.RtspSession.RtpPort);
                            _remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
                            _device.RtspSession.RecieptionInfoChanged += RtspSession_RecieptionInfoChanged;
                            _device.RtspSession.Play(tuning);
                        }
                        else
                        {
                            _device.RtspSession.Play(tuning);
                        }

                        /* Say the Sat>IP server we want Receives the Recieption Details SDP */
                        statuscode = _device.RtspSession.Describe();
                        if (_locked)
                        {
                            while ((!patfound) || (!pmtsfound) || (!sdtfound))
                            {
                                var       receivedbytes = _udpclient.Receive(ref _remoteEndPoint);
                                RtpPacket rtp           = RtpPacket.Decode(receivedbytes);
                                if (rtp.HasPayload)
                                {
                                    Logger.Write(Utils.ToHexString(rtp.Payload));
                                    _decoder.AddData(receivedbytes);
                                }
                            }
                            lock (_decoder)
                            {
                                programMapTables = _decoder?.ProgramMapTables.OrderBy(p => p.ProgramNumber).ToList();
                                foreach (var programMapTable in programMapTables)
                                {
                                    short videoPid = -1;
                                    short audioPid = -1;
                                    short ac3pid   = -1;
                                    short ttxpid   = -1;
                                    short subpid   = -1;
                                    short aacpid   = -1;
                                    short dtspid   = -1;
                                    short eac3pid  = -1;
                                    var   desc     = _decoder.GetServiceDescriptorForProgramNumber(programMapTable?.ProgramNumber);
                                    if (desc != null)
                                    {
                                        if (programMapTable?.EsStreams != null)
                                        {
                                            foreach (var stream in programMapTable.EsStreams)
                                            {
                                                if (stream != null)
                                                {
                                                    if (stream.Descriptors.OfType <Ac3Descriptor>().Any())
                                                    {
                                                        ac3pid = stream.ElementaryPid;
                                                    }
                                                    if (stream.Descriptors.OfType <Eac3Descriptor>().Any())
                                                    {
                                                        eac3pid = stream.ElementaryPid;
                                                    }
                                                    if (stream.Descriptors.OfType <AACDescriptor>().Any())
                                                    {
                                                        aacpid = stream.ElementaryPid;
                                                    }
                                                    if (stream.Descriptors.OfType <DTSDescriptor>().Any())
                                                    {
                                                        dtspid = stream.ElementaryPid;
                                                    }
                                                    if (stream.Descriptors.OfType <SubtitlingDescriptor>().Any())
                                                    {
                                                        subpid = stream.ElementaryPid;
                                                    }
                                                    if (stream.Descriptors.OfType <TeletextDescriptor>().Any())
                                                    {
                                                        ttxpid = stream.ElementaryPid;
                                                    }
                                                    switch (stream.StreamType)
                                                    {
                                                    case 0x01:     // ISO/IEC 11172-2 (MPEG-1 video) in a packetized stream
                                                    case 0x02:     // ITU-T Rec. H.262 and ISO/IEC 13818-2 (MPEG-2 higher rate interlaced video) in a packetized stream
                                                    case 0x1B:     // ITU-T Rec. H.264 and ISO/IEC 14496-10 (lower bit-rate video) in a packetized stream
                                                    case 0x24:     // ITU - T Rec.H.265 and ISO/ IEC 23008 - 2(Ultra HD video) in a packetized stream
                                                    {
                                                        videoPid = stream.ElementaryPid;
                                                        break;
                                                    }

                                                    case 0x03:     // ISO/IEC 11172-3 (MPEG-1 audio) in a packetized stream
                                                    case 0x04:     // ISO/IEC 13818-3 (MPEG-2 halved sample rate audio) in a packetized stream

                                                    {
                                                        audioPid = stream.ElementaryPid;
                                                        break;
                                                    }

                                                    default:
                                                    { Console.Write(stream.StreamType.ToString()); break; }
                                                    }
                                                }
                                            }
                                        }

                                        var chan = new Channel
                                        {
                                            ServiceType             = desc.ServiceType,
                                            ServiceName             = desc.ServiceName.Value,
                                            ServiceProvider         = desc.ServiceProviderName.Value,
                                            ServiceId               = programMapTable.ProgramNumber,
                                            ProgramClockReferenceId = programMapTable.PcrPid,
                                            AudioPid    = audioPid,
                                            VideoPid    = videoPid,
                                            AC3Pid      = ac3pid,
                                            EAC3Pid     = eac3pid,
                                            AACPid      = aacpid,
                                            DTSPid      = dtspid,
                                            TTXPid      = ttxpid,
                                            SubTitlePid = subpid
                                        };
                                        AddResults(chan);
                                    }
                                }
                            }
                        }
                        Thread.Sleep(5000);
                        Index++;
                    }
                }
                catch
                {
                }
            }
            _device.RtspSession.TearDown();
            SetControlPropertyThreadSafe(pgbSearchResult, "Value", 100);
            _isScanning = false;
            SetControlPropertyThreadSafe(btnScan, "Text", "Start Search");
            StopScanThread();
        }