Example #1
0
        void MapProgramStreams()
        {
            _streamList.Clear();

            foreach (var program in _programStreamMap.Values)
            {
                ProgramMap newProgramMap;
                if (_newProgramStreams.TryGetValue(program.Pid, out newProgramMap))
                {
                    if (!Equals(newProgramMap.StreamType, program.StreamType))
                        _streamList.Add(program);
                }
                else
                    _streamList.Add(program);
            }

            if (_streamList.Count > 0)
            {
                foreach (var program in _streamList)
                {
                    Debug.WriteLine("*** TsProgramMapTable.MapProgramStreams(): retiring " + program);

                    ClearProgram(program);

                    _retiredProgramStreams[Tuple.Create(program.Pid, program.StreamType)] = program;
                }

                _streamList.Clear();
            }

            var descriptors = _newProgramDescriptors;

            var programLanguage = Iso639_2Normalization.Normalize(descriptors.GetDefaultLanguage());

            var programStreams = new ProgramStreams
            {
                ProgramNumber = _programNumber,
                Language = programLanguage,
                Streams = _newProgramStreams.Values
                    .Select(s => new ProgramStreams.ProgramStream
                    {
                        Pid = s.Pid,
                        StreamType = s.StreamType,
                        Language =  Iso639_2Normalization.Normalize(s.StreamDescriptors.GetDefaultLanguage()) ?? programLanguage
                    })
                    .ToArray()
            };

            if (null != _streamFilter)
                _streamFilter(programStreams);

            foreach (var programStream in from ps in programStreams.Streams
                                          join pm in _newProgramStreams.Values on ps.Pid equals pm.Pid
                                          select new
                                          {
                                              ps.BlockStream,
                                              ProgramStream = pm,
                                              ps.Language
                                          })
            {
                var streamRequested = !programStream.BlockStream;

                var pid = programStream.ProgramStream.Pid;
                var streamType = programStream.ProgramStream.StreamType;
                var language = programStream.Language;

                ProgramMap mappedProgram;
                if (_programStreamMap.TryGetValue(pid, out mappedProgram))
                {
                    if (Equals(mappedProgram.StreamType, streamType) && streamRequested)
                        continue;

                    ClearProgram(mappedProgram);
                }

                if (streamRequested)
                {
                    TsPacketizedElementaryStream pes;

                    var key = Tuple.Create(pid, streamType);

                    ProgramMap retiredProgram;
                    if (_retiredProgramStreams.TryGetValue(key, out retiredProgram))
                    {
                        Debug.WriteLine("*** TsProgramMapTable.MapProgramStreams(): remapping retired program " + retiredProgram);

                        var removed = _retiredProgramStreams.Remove(key);

                        Debug.Assert(removed, "Unable to remove program from retired");

                        pes = retiredProgram.Stream;

                        _programStreamMap[pid] = retiredProgram;
                    }
                    else
                    {
                        IMediaStreamMetadata mediaStreamMetadata = null;

                        if (null != language)
                            mediaStreamMetadata = new MediaStreamMetadata { Language = language };

                        pes = _decoder.CreateStream(streamType, pid, mediaStreamMetadata);

                        programStream.ProgramStream.Stream = pes;

                        _programStreamMap[pid] = programStream.ProgramStream;
                    }

                    if (pid == _pcrPid)
                    {
                        _foundPcrPid = true;

                        _decoder.RegisterHandler(pid,
                            p =>
                            {
                                AddPcr(p);
                                pes.Add(p);
                            });
                    }
                    else
                        _decoder.RegisterHandler(pid, pes.Add);
                }
                else
                {
                    if (pid == _pcrPid)
                    {
                        _foundPcrPid = true;

                        _decoder.RegisterHandler(pid, AddPcr);
                    }
                }
            }

            _newProgramStreams.Clear();

            if (!_foundPcrPid)
            {
                _foundPcrPid = true;
                _decoder.RegisterHandler(_pcrPid, AddPcr);
            }
        }
Example #2
0
        private void MapProgramStreams()
        {
            this._streamList.Clear();
            foreach (TsProgramMapTable.ProgramMap programMap1 in this._programStreamMap.Values)
            {
                TsProgramMapTable.ProgramMap programMap2;
                if (this._newProgramStreams.TryGetValue(programMap1.Pid, out programMap2))
                {
                    if (!object.Equals((object)programMap2.StreamType, (object)programMap1.StreamType))
                    {
                        this._streamList.Add(programMap1);
                    }
                }
                else
                {
                    this._streamList.Add(programMap1);
                }
            }
            if (this._streamList.Count > 0)
            {
                foreach (TsProgramMapTable.ProgramMap program in this._streamList)
                {
                    Debug.WriteLine("*** TsProgramMapTable.MapProgramStreams(): retiring " + (object)program);
                    this.ClearProgram(program);
                    this._retiredProgramStreams[Tuple.Create <uint, TsStreamType>(program.Pid, program.StreamType)] = program;
                }
                this._streamList.Clear();
            }
            string         programLanguage = Iso639_2Normalization.Normalize(TsDescriptors.GetDefaultLanguage((IEnumerable <TsDescriptor>) this._newProgramDescriptors));
            ProgramStreams programStreams  = new ProgramStreams()
            {
                ProgramNumber = this._programNumber,
                Language      = programLanguage,
                Streams       = (ICollection <IProgramStream>)Enumerable.ToArray <ProgramStreams.ProgramStream>(Enumerable.Select <TsProgramMapTable.ProgramMap, ProgramStreams.ProgramStream>((IEnumerable <TsProgramMapTable.ProgramMap>) this._newProgramStreams.Values, (Func <TsProgramMapTable.ProgramMap, ProgramStreams.ProgramStream>)(s => new ProgramStreams.ProgramStream()
                {
                    Pid        = s.Pid,
                    StreamType = s.StreamType,
                    Language   = Iso639_2Normalization.Normalize(TsDescriptors.GetDefaultLanguage((IEnumerable <TsDescriptor>)s.StreamDescriptors)) ?? programLanguage
                })))
            };

            if (null != this._streamFilter)
            {
                this._streamFilter((IProgramStreams)programStreams);
            }
            foreach (var fAnonymousType1 in Enumerable.Join((IEnumerable <IProgramStream>)programStreams.Streams, (IEnumerable <TsProgramMapTable.ProgramMap>) this._newProgramStreams.Values, (Func <IProgramStream, uint>)(ps => ps.Pid), (Func <TsProgramMapTable.ProgramMap, uint>)(pm => pm.Pid), (ps, pm) =>
            {
                var fAnonymousType1 = new
                {
                    BlockStream = ps.BlockStream,
                    ProgramStream = pm,
                    Language = ps.Language
                };
                return(fAnonymousType1);
            }))
            {
                bool         flag       = !fAnonymousType1.BlockStream;
                uint         index      = fAnonymousType1.ProgramStream.Pid;
                TsStreamType streamType = fAnonymousType1.ProgramStream.StreamType;
                string       language   = fAnonymousType1.Language;
                TsProgramMapTable.ProgramMap program;
                if (this._programStreamMap.TryGetValue(index, out program))
                {
                    if (!object.Equals((object)program.StreamType, (object)streamType) || !flag)
                    {
                        this.ClearProgram(program);
                    }
                    else
                    {
                        continue;
                    }
                }
                if (flag)
                {
                    Tuple <uint, TsStreamType>   key = Tuple.Create <uint, TsStreamType>(index, streamType);
                    TsProgramMapTable.ProgramMap programMap;
                    TsPacketizedElementaryStream pes;
                    if (this._retiredProgramStreams.TryGetValue(key, out programMap))
                    {
                        Debug.WriteLine("*** TsProgramMapTable.MapProgramStreams(): remapping retired program " + (object)programMap);
                        Debug.Assert(this._retiredProgramStreams.Remove(key), "Unable to remove program from retired");
                        pes = programMap.Stream;
                        this._programStreamMap[index] = programMap;
                    }
                    else
                    {
                        IMediaStreamMetadata mediaStreamMetadata = (IMediaStreamMetadata)null;
                        if (null != language)
                        {
                            mediaStreamMetadata = (IMediaStreamMetadata) new MediaStreamMetadata()
                            {
                                Language = language
                            }
                        }
                        ;
                        pes = this._decoder.CreateStream(streamType, index, mediaStreamMetadata);
                        fAnonymousType1.ProgramStream.Stream = pes;
                        this._programStreamMap[index]        = fAnonymousType1.ProgramStream;
                    }
                    if ((int)index == (int)this._pcrPid)
                    {
                        this._foundPcrPid = true;
                        this._decoder.RegisterHandler(index, (Action <TsPacket>)(p =>
                        {
                            this.AddPcr(p);
                            pes.Add(p);
                        }));
                    }
                    else
                    {
                        this._decoder.RegisterHandler(index, new Action <TsPacket>(pes.Add));
                    }
                }
                else if ((int)index == (int)this._pcrPid)
                {
                    this._foundPcrPid = true;
                    this._decoder.RegisterHandler(index, new Action <TsPacket>(this.AddPcr));
                }
            }
            this._newProgramStreams.Clear();
            if (this._foundPcrPid)
            {
                return;
            }
            this._foundPcrPid = true;
            this._decoder.RegisterHandler(this._pcrPid, new Action <TsPacket>(this.AddPcr));
        }