private void SetInternalStream(SelectableMediaStream stream)
        {
            _logger.Debug("SetInternalStream {0} {1} {2}", stream.Index, stream.Type, stream.Name);

            // if we are already playing an external subtitle, we have to clear it first or
            // we wont be able to swicth subtitles
            var activeExternalSub = GetActiveExternalSubtitleStream();
            if (activeExternalSub != null)
            {
                ClearExternalSubtitles();
            }

            IEnumFilters enumFilters;
            var hr = m_graph.EnumFilters(out enumFilters);

            DsError.ThrowExceptionForHR(hr);

            var filters = new DirectShowLib.IBaseFilter[1];

            while (enumFilters.Next(filters.Length, filters, IntPtr.Zero) == 0)
            {
                FilterInfo filterInfo;

                hr = filters[0].QueryFilterInfo(out filterInfo);
                DsError.ThrowExceptionForHR(hr);

                Guid cl;
                filters[0].GetClassID(out cl);

                if (stream.Type == MediaStreamType.Audio)
                {
                    if (cl != _audioSelector)
                    {
                        continue;
                    }
                }
                else if (stream.Type == MediaStreamType.Subtitle)
                {
                    if (cl != _grp2Selector && cl != _vobsubSelector)
                    {
                        continue;
                    }
                }

                if (filterInfo.pGraph != null)
                {
                    Marshal.ReleaseComObject(filterInfo.pGraph);
                }

                var iss = filters[0] as IAMStreamSelect;

                if (iss != null)
                    iss.Enable(stream.Index, AMStreamSelectEnableFlags.Enable);

                Marshal.ReleaseComObject(filters[0]);
            }

            Marshal.ReleaseComObject(enumFilters);

            UpdateStreamActiveSetting(stream.Name, stream.Type);
        }
        private async void LoadExternalSubtitleFromStream(SelectableMediaStream stream)
        {
            _logger.Debug("LoadExternalSubtitleFromStream: {0}", stream);
            var path = stream.Path;

            if (path.StartsWith("http", StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    // as xvfilter throw's an error for a stream, we copy the stream to
                    // a local temp file and change it ext to 'srt'
                    var tempFile = await _httpClient.GetTempFile(new HttpRequestOptions
                    {
                        Url = path,
                        Progress = new Progress<double>()
                    });

                    var pathWithExtension = Path.ChangeExtension(tempFile, Path.GetExtension(path));
                    File.Move(tempFile, pathWithExtension);
                    path = pathWithExtension;
                }
                catch
                {
                    return;
                }
            }

            _logger.Debug("loadExternalSubtitleFromStream {0} {1} {2}", stream.Index, stream.Type, path);
            LoadExternalSubtitle(path);
        }
        private async void SetExternalSubtitleStream(SelectableMediaStream stream)
        {
            _logger.Debug("SetExternalSubtitleStream: {0}", stream);

            if (stream.Name.ToLower().Contains("no subtitles"))
            {
                ToggleHideSubtitles(true);
                UpdateStreamsAsInactive(stream.Type); // display all streams as inactive
                stream.IsActive = true; // set no subtitles stream active
            }
            else
            {
                //make sure the splitter isn't providing a subtitle
                int subIndex = (int)CurrentSubtitleStreamIndex;
                if (subIndex > 0)
                {
                    _logger.Debug("Clear internal subtitle");
                    var iss = _sourceFilter as IAMStreamSelect;
                    if (iss != null)
                    {
                        int hr = iss.Enable(subIndex, AMStreamSelectEnableFlags.DisableAll);
                    }
                }
                // if not, we need to copy the stream to the local system and play form there (xyfilter issue)
                LoadExternalSubtitleFromStream(stream);

                UpdateStreamActiveSetting(stream.Name, stream.Type); // display this  streams as active
            }
        }
        public async void SetSubtitleStream(SelectableMediaStream stream)
        {
            _logger.Debug("SetSubtitleStream {0} {1} {2} {3}", stream.Index, stream.Type, stream.Name, stream.Identifier);
            if (stream.Identifier == "external" || stream.Name.ToLower().Contains("no subtitles"))
            {
                SetExternalSubtitleStream(stream);
            }

            if (stream.Identifier != "external")
            {
                SetInternalStream(stream);
            }

            ToggleHideSubtitles(stream.Name.ToLower().Contains("no subtitles"));
        }
 public void SetAudioTrack(SelectableMediaStream stream)
 {
     SetInternalStream(stream);
 }
        private List<SelectableMediaStream> GetInternalStreams()
        {
            _logger.Debug("GetInternalStreams");

            var streams = new List<SelectableMediaStream>();

            IEnumFilters enumFilters = null;
            try
            {
                var hr = m_graph.EnumFilters(out enumFilters);

                DsError.ThrowExceptionForHR(hr);

                var filters = new DirectShowLib.IBaseFilter[1];

                while (enumFilters.Next(filters.Length, filters, IntPtr.Zero) == 0)
                {
                    FilterInfo filterInfo;

                    hr = filters[0].QueryFilterInfo(out filterInfo);
                    DsError.ThrowExceptionForHR(hr);

                    Guid cl;
                    filters[0].GetClassID(out cl);

                    if (filterInfo.pGraph != null)
                    {
                        Marshal.ReleaseComObject(filterInfo.pGraph);
                    }

                    var iss = filters[0] as IAMStreamSelect;

                    if (iss != null)
                    {
                        int count;

                        hr = iss.Count(out count);
                        DsError.ThrowExceptionForHR(hr);

                        for (int i = 0; i < count; i++)
                        {
                            DirectShowLib.AMMediaType type;
                            AMStreamSelectInfoFlags flags;
                            int plcid, pwdGrp; // language
                            String pzname;

                            object ppobject, ppunk;

                            hr = iss.Info(i, out type, out flags, out plcid, out pwdGrp, out pzname, out ppobject, out ppunk);
                            DsError.ThrowExceptionForHR(hr);

                            if (ppobject != null)
                            {
                                Marshal.ReleaseComObject(ppobject);
                            }

                            if (type != null)
                            {
                                DsUtils.FreeAMMediaType(type);
                            }

                            if (ppunk != null)
                            {
                                Marshal.ReleaseComObject(ppunk);
                            }

                            if (pwdGrp == 2)
                            {
                                if (_grp2Selector == Guid.Empty)
                                {
                                    filters[0].GetClassID(out _grp2Selector);
                                }

                                var stream = new SelectableMediaStream
                                {
                                    Index = i,
                                    Name = pzname,
                                    Type = MediaStreamType.Subtitle
                                };

                                if ((AMStreamSelectInfoFlags.Enabled & flags) == AMStreamSelectInfoFlags.Enabled)
                                {
                                    stream.IsActive = true;
                                }
                                streams.Add(stream);
                            }

                            if (pwdGrp == 1)
                            {
                                if (_audioSelector == Guid.Empty)
                                {
                                    filters[0].GetClassID(out _audioSelector);
                                }
                                var stream = new SelectableMediaStream
                                {
                                    Index = i,
                                    Name = pzname,
                                    Type = MediaStreamType.Audio
                                };
                                if ((AMStreamSelectInfoFlags.Enabled & flags) == AMStreamSelectInfoFlags.Enabled)
                                {
                                    stream.IsActive = true;
                                }
                                streams.Add(stream);
                            }

                            if (pwdGrp == 6590033)
                            {
                                if (_vobsubSelector == Guid.Empty)
                                {
                                    filters[0].GetClassID(out _vobsubSelector);
                                }

                                var stream = new SelectableMediaStream
                                {
                                    Index = i,
                                    Name = pzname,
                                    Type = MediaStreamType.Subtitle,
                                    Identifier = "Vobsub"
                                };

                                if ((AMStreamSelectInfoFlags.Enabled & flags) == AMStreamSelectInfoFlags.Enabled)
                                {
                                    stream.IsActive = true;
                                }
                                streams.Add(stream);
                            }
                        }
                    }

                    Marshal.ReleaseComObject(filters[0]);
                }
            }
            finally
            {
                if (enumFilters != null)
                    Marshal.ReleaseComObject(enumFilters);
            }

            _logger.Debug("Return InternalStreamCount: {0}", streams.Count);
            return streams;
        }
 public void ChangeSubtitleStream(SelectableMediaStream track)
 {
     InvokeOnPlayerThread(() => _mediaPlayer.SetSubtitleStream(track));
 }
 public void ChangeAudioStream(SelectableMediaStream track)
 {
     InvokeOnPlayerThread(() => _mediaPlayer.SetAudioTrack(track));
 }