TimeSpan ITimeShifting.GetPosition()
        {
            IStreamBufferMediaSeeking mediaSeeking = this.streamBufferSource as IStreamBufferMediaSeeking;

            if (mediaSeeking != null)
            {
                long currentposition = 0;                 // Reference time (100-nanosecond units). 100 * 10e-9 = 10e-7 = 10000000
                mediaSeeking.GetCurrentPosition(out currentposition);

                return(TimeSpan.FromMilliseconds(currentposition / 10000));
            }
            return(TimeSpan.Zero);
        }
Example #2
0
        public int Percentage()
        {
            if (mediaSeeking == null)
            {
                return(100);
            }
            long lCurrent;

            mediaSeeking.GetCurrentPosition(out lCurrent);
            float percent = ((float)lCurrent) / ((float)m_dDuration);

            percent *= 50.0f;
            if (percent > 100)
            {
                percent = 100;
            }
            return((int)percent);
        }
Example #3
0
        // Set the position of the graph to a specified TimeSpan
        protected void SeekGraphToTime(TimeSpan seekTime)
        {
            SendDebugMessage("Seeking graph to time...");
            int           hr;
            IMediaControl mc = (IMediaControl)currentFilterGraph;
            // Stop graph if not stopped
            FilterState fs;

            mc.GetState(50, out fs);
            if (fs != FilterState.Stopped)
            {
                if (fs != FilterState.Stopped)
                {
                    mc.Stop();
                }
            }

            long   timeInSeconds          = (long)seekTime.TotalSeconds;
            DsLong dsTimeIn100NanoSeconds = DsLong.FromInt64(timeInSeconds * 10000000);

            SendDebugMessage("Setting position to " + dsTimeIn100NanoSeconds.ToInt64().ToString());
            long pos;

            if (UsingSBEFilter)
            {
                // IStreamBufferMediaSeeking is used directly on the source filter   http://msdn.microsoft.com/en-us/library/dd694950(v=vs.85).aspx
                IStreamBufferMediaSeeking mSeek = (IStreamBufferMediaSeeking)currentSBEfilter;
                hr = mSeek.SetPositions(dsTimeIn100NanoSeconds, AMSeekingSeekingFlags.AbsolutePositioning, 0, AMSeekingSeekingFlags.NoPositioning);
                DsError.ThrowExceptionForHR(hr);
                mSeek.GetCurrentPosition(out pos);
            }
            else
            {
                // IMediaSeeking is used on the filter graph which distributes the calls
                IMediaSeeking mSeek = (IMediaSeeking)currentFilterGraph;
                hr = mSeek.SetPositions(dsTimeIn100NanoSeconds, AMSeekingSeekingFlags.AbsolutePositioning, 0, AMSeekingSeekingFlags.NoPositioning);
                DsError.ThrowExceptionForHR(hr);
                mSeek.GetCurrentPosition(out pos);
            }

            SendDebugMessage("New pos is " + pos.ToString());
        }
Example #4
0
 public bool Transcode(TranscodeInfo info, VideoFormat format, Quality quality, Standard standard)
 {
     try
     {
         if (!Supports(format))
         {
             return(false);
         }
         string ext = System.IO.Path.GetExtension(info.file);
         if (ext.ToLower() != ".dvr-ms" && ext.ToLower() != ".sbe")
         {
             Log.Info("DVRMS2WMV: wrong file format");
             return(false);
         }
         Log.Info("DVRMS2WMV: create graph");
         graphBuilder = (IGraphBuilder) new FilterGraph();
         _rotEntry    = new DsROTEntry((IFilterGraph)graphBuilder);
         Log.Info("DVRMS2WMV: add streambuffersource");
         bufferSource = (IStreamBufferSource) new StreamBufferSource();
         IBaseFilter filter = (IBaseFilter)bufferSource;
         graphBuilder.AddFilter(filter, "SBE SOURCE");
         Log.Info("DVRMS2WMV: load file:{0}", info.file);
         IFileSourceFilter fileSource = (IFileSourceFilter)bufferSource;
         int hr = fileSource.Load(info.file, null);
         //add mpeg2 audio/video codecs
         string strVideoCodec = "";
         string strAudioCodec = "";
         using (MediaPortal.Profile.Settings xmlreader = new MediaPortal.Profile.MPSettings())
         {
             strVideoCodec = xmlreader.GetValueAsString("mytv", "videocodec", "MPC - MPEG-2 Video Decoder (Gabest)");
             strAudioCodec = xmlreader.GetValueAsString("mytv", "audiocodec", "MPC - MPA Decoder Filter");
         }
         Log.Info("DVRMS2WMV: add mpeg2 video codec:{0}", strVideoCodec);
         Mpeg2VideoCodec = DirectShowUtil.AddFilterToGraph(graphBuilder, strVideoCodec);
         if (hr != 0)
         {
             Log.Error("DVRMS2WMV:FAILED:Add mpeg2 video  to filtergraph :0x{0:X}", hr);
             Cleanup();
             return(false);
         }
         Log.Info("DVRMS2WMV: add mpeg2 audio codec:{0}", strAudioCodec);
         Mpeg2AudioCodec = DirectShowUtil.AddFilterToGraph(graphBuilder, strAudioCodec);
         if (Mpeg2AudioCodec == null)
         {
             Log.Error("DVRMS2WMV:FAILED:unable to add mpeg2 audio codec");
             Cleanup();
             return(false);
         }
         Log.Info("DVRMS2WMV: connect streambufer source->mpeg audio/video decoders");
         //connect output #0 of streambuffer source->mpeg2 audio codec pin 1
         //connect output #1 of streambuffer source->mpeg2 video codec pin 1
         IPin pinOut0, pinOut1;
         IPin pinIn0, pinIn1;
         pinOut0 = DsFindPin.ByDirection((IBaseFilter)bufferSource, PinDirection.Output, 0); //audio
         pinOut1 = DsFindPin.ByDirection((IBaseFilter)bufferSource, PinDirection.Output, 1); //video
         if (pinOut0 == null || pinOut1 == null)
         {
             Log.Error("DVRMS2WMV:FAILED:unable to get pins of source");
             Cleanup();
             return(false);
         }
         pinIn0 = DsFindPin.ByDirection(Mpeg2VideoCodec, PinDirection.Input, 0); //video
         pinIn1 = DsFindPin.ByDirection(Mpeg2AudioCodec, PinDirection.Input, 0); //audio
         if (pinIn0 == null || pinIn1 == null)
         {
             Log.Error("DVRMS2WMV:FAILED:unable to get pins of mpeg2 video/audio codec");
             Cleanup();
             return(false);
         }
         hr = graphBuilder.Connect(pinOut0, pinIn1);
         if (hr != 0)
         {
             Log.Error("DVRMS2WMV:FAILED:unable to connect audio pins :0x{0:X}", hr);
             Cleanup();
             return(false);
         }
         hr = graphBuilder.Connect(pinOut1, pinIn0);
         if (hr != 0)
         {
             Log.Error("DVRMS2WMV:FAILED:unable to connect video pins :0x{0:X}", hr);
             Cleanup();
             return(false);
         }
         string outputFilename = System.IO.Path.ChangeExtension(info.file, ".wmv");
         if (!AddWmAsfWriter(outputFilename, quality, standard))
         {
             return(false);
         }
         Log.Info("DVRMS2WMV: start pre-run");
         mediaControl = graphBuilder as IMediaControl;
         mediaSeeking = bufferSource as IStreamBufferMediaSeeking;
         mediaEvt     = graphBuilder as IMediaEventEx;
         mediaPos     = graphBuilder as IMediaPosition;
         //get file duration
         long lTime = 5 * 60 * 60;
         lTime *= 10000000;
         long pStop = 0;
         hr = mediaSeeking.SetPositions(new DsLong(lTime), AMSeekingSeekingFlags.AbsolutePositioning, new DsLong(pStop),
                                        AMSeekingSeekingFlags.NoPositioning);
         if (hr == 0)
         {
             long lStreamPos;
             mediaSeeking.GetCurrentPosition(out lStreamPos); // stream position
             m_dDuration = lStreamPos;
             lTime       = 0;
             mediaSeeking.SetPositions(new DsLong(lTime), AMSeekingSeekingFlags.AbsolutePositioning, new DsLong(pStop),
                                       AMSeekingSeekingFlags.NoPositioning);
         }
         double duration = m_dDuration / 10000000d;
         Log.Info("DVRMS2WMV: movie duration:{0}", Util.Utils.SecondsToHMSString((int)duration));
         hr = mediaControl.Run();
         if (hr != 0)
         {
             Log.Error("DVRMS2WMV:FAILED:unable to start graph :0x{0:X}", hr);
             Cleanup();
             return(false);
         }
         int maxCount = 20;
         while (true)
         {
             long lCurrent;
             mediaSeeking.GetCurrentPosition(out lCurrent);
             double dpos = (double)lCurrent;
             dpos /= 10000000d;
             System.Threading.Thread.Sleep(100);
             if (dpos >= 2.0d)
             {
                 break;
             }
             maxCount--;
             if (maxCount <= 0)
             {
                 break;
             }
         }
         Log.Info("DVRMS2WMV: pre-run done");
         Log.Info("DVRMS2WMV: Get duration of movie");
         mediaControl.Stop();
         FilterState state;
         mediaControl.GetState(500, out state);
         GC.Collect();
         GC.Collect();
         GC.Collect();
         GC.WaitForPendingFinalizers();
         Log.Info("DVRMS2WMV: reconnect mpeg2 video codec->ASF WM Writer");
         graphBuilder.RemoveFilter(fileWriterbase);
         if (!AddWmAsfWriter(outputFilename, quality, standard))
         {
             return(false);
         }
         Log.Info("DVRMS2WMV: Start transcoding");
         hr = mediaControl.Run();
         if (hr != 0)
         {
             Log.Error("DVRMS2WMV:FAILED:unable to start graph :0x{0:X}", hr);
             Cleanup();
             return(false);
         }
     }
     catch (Exception e)
     {
         // TODO: Handle exceptions.
         Log.Error("unable to transcode file:{0} message:{1}", info.file, e.Message);
         return(false);
     }
     return(true);
 }
Example #5
0
        public bool Transcode(TranscodeInfo info, MediaPortal.Core.Transcoding.VideoFormat format,
                              MediaPortal.Core.Transcoding.Quality quality, Standard standard)
        {
            if (!Supports(format))
            {
                return(false);
            }
            string ext = System.IO.Path.GetExtension(info.file);

            if (ext.ToLower() != ".dvr-ms" && ext.ToLower() != ".sbe")
            {
                Log.Info("DVRMS2DIVX: wrong file format");
                return(false);
            }

            //disable xvid status window while encoding

            /*  try
             *                        {
             *                                using (RegistryKey subkey = Registry.CurrentUser.OpenSubKey(@"Software\GNU\XviD", true))
             *                                {
             *                                        if (subkey != null)
             *                                        {
             *                                                Int32 uivalue = 0;
             *                                                subkey.SetValue("display_status", (Int32)uivalue);
             *                                                subkey.SetValue("debug", (Int32)uivalue);
             *                                                subkey.SetValue("bitrate", (Int32)bitrate);
             *
             *                                                uivalue = 1;
             *                                                subkey.SetValue("interlacing", (Int32)uivalue);
             *                                        }
             *                                }
             *                        }
             *                        catch (Exception)
             *                        {
             *                        }*/
            //Type comtype = null;
            //object comobj = null;
            try
            {
                graphBuilder = (IGraphBuilder) new FilterGraph();

                _rotEntry = new DsROTEntry((IFilterGraph)graphBuilder);

                Log.Info("DVRMS2DIVX: add filesource");
                bufferSource = (IStreamBufferSource) new StreamBufferSource();

                IBaseFilter filter = (IBaseFilter)bufferSource;
                graphBuilder.AddFilter(filter, "SBE SOURCE");
                IFileSourceFilter fileSource = (IFileSourceFilter)bufferSource;
                Log.Info("DVRMS2DIVX: load file:{0}", info.file);
                int hr = fileSource.Load(info.file, null);


                /*string strDemuxerMoniker = @"@device:sw:{083863F1-70DE-11D0-BD40-00A0C911CE86}\{AFB6C280-2C41-11D3-8A60-0000F81E0E4A}";
                 *
                 *                      mpegDemuxer = Marshal.BindToMoniker(strDemuxerMoniker) as IBaseFilter;
                 *                      if (mpegDemuxer == null)
                 *                      {
                 *                                      Log.Error("DVRMS2DIVX:FAILED:unable to add mpeg2 demuxer");
                 *                                      Cleanup();
                 *                                      return false;
                 *                      }
                 *                      hr = graphBuilder.AddFilter(mpegDemuxer, "MPEG-2 Demultiplexer");
                 *                      if (hr != 0)
                 *                      {
                 *                                      Log.Error("DVRMS2DIVX:FAILED:Add mpeg2 demuxer to filtergraph :0x{0:X}", hr);
                 *                                      Cleanup();
                 *                                      return false;
                 *                      }*/

                //add mpeg2 audio/video codecs
                string strVideoCodecMoniker =
                    @"@device:sw:{083863F1-70DE-11D0-BD40-00A0C911CE86}\{39F498AF-1A09-4275-B193-673B0BA3D478}";
                string strAudioCodec = "MPC - MPA Decoder Filter";
                Log.Info("DVRMS2DIVX: add MPV mpeg2 video decoder");
                Mpeg2VideoCodec = Marshal.BindToMoniker(strVideoCodecMoniker) as IBaseFilter;
                if (Mpeg2VideoCodec == null)
                {
                    Log.Error("DVRMS2DIVX:FAILED:unable to add MPV mpeg2 video decoder");
                    Cleanup();
                    return(false);
                }
                hr = graphBuilder.AddFilter(Mpeg2VideoCodec, "MPC - MPEG-2 Video Decoder (Gabest)");
                if (hr != 0)
                {
                    Log.Error("DVRMS2DIVX:FAILED:Add MPV mpeg2 video  to filtergraph :0x{0:X}", hr);
                    Cleanup();
                    return(false);
                }

                Log.Info("DVRMS2DIVX: add MPA mpeg2 audio codec:{0}", strAudioCodec);
                Mpeg2AudioCodec = DirectShowUtil.AddFilterToGraph(graphBuilder, strAudioCodec);
                if (Mpeg2AudioCodec == null)
                {
                    Log.Error("DVRMS2DIVX:FAILED:unable to add MPV mpeg2 audio codec");
                    Cleanup();
                    return(false);
                }

                //connect output #0 of streambuffer source->mpeg2 audio codec pin 1
                //connect output #1 of streambuffer source->mpeg2 video codec pin 1
                Log.Info("DVRMS2DIVX: connect streambufer source->mpeg audio/video decoders");
                IPin pinOut0, pinOut1;
                IPin pinIn0, pinIn1;
                pinOut0 = DsFindPin.ByDirection((IBaseFilter)bufferSource, PinDirection.Output, 0); //audio
                pinOut1 = DsFindPin.ByDirection((IBaseFilter)bufferSource, PinDirection.Output, 1); //video
                if (pinOut0 == null || pinOut1 == null)
                {
                    Log.Error("DVRMS2DIVX:FAILED:unable to get pins of source");
                    Cleanup();
                    return(false);
                }

                pinIn0 = DsFindPin.ByDirection(Mpeg2VideoCodec, PinDirection.Input, 0); //video
                pinIn1 = DsFindPin.ByDirection(Mpeg2AudioCodec, PinDirection.Input, 0); //audio
                if (pinIn0 == null || pinIn1 == null)
                {
                    Log.Error("DVRMS2DIVX:FAILED:unable to get pins of mpeg2 video/audio codec");
                    Cleanup();
                    return(false);
                }

                hr = graphBuilder.Connect(pinOut0, pinIn1);
                if (hr != 0)
                {
                    Log.Error("DVRMS2DIVX:FAILED:unable to connect audio pins :0x{0:X}", hr);
                    Cleanup();
                    return(false);
                }


                hr = graphBuilder.Connect(pinOut1, pinIn0);
                if (hr != 0)
                {
                    Log.Error("DVRMS2DIVX:FAILED:unable to connect video pins :0x{0:X}", hr);
                    Cleanup();
                    return(false);
                }
                if (!AddCodecs(graphBuilder, info))
                {
                    return(false);
                }

                //				hr=(graphBuilder as IMediaFilter).SetSyncSource(null);
                //				if (hr!=0)
                //					Log.Error("DVRMS2DIVX:FAILED:to SetSyncSource :0x{0:X}",hr);
                mediaControl = graphBuilder as IMediaControl;
                mediaSeeking = bufferSource as IStreamBufferMediaSeeking;
                mediaEvt     = graphBuilder as IMediaEventEx;
                mediaPos     = graphBuilder as IMediaPosition;

                //get file duration
                Log.Info("DVRMS2DIVX: Get duration of movie");
                long lTime = 5 * 60 * 60;
                lTime *= 10000000;
                long pStop = 0;
                hr = mediaSeeking.SetPositions(new DsLong(lTime), AMSeekingSeekingFlags.AbsolutePositioning, new DsLong(pStop),
                                               AMSeekingSeekingFlags.NoPositioning);
                if (hr == 0)
                {
                    long lStreamPos;
                    mediaSeeking.GetCurrentPosition(out lStreamPos); // stream position
                    m_dDuration = lStreamPos;
                    lTime       = 0;
                    mediaSeeking.SetPositions(new DsLong(lTime), AMSeekingSeekingFlags.AbsolutePositioning, new DsLong(pStop),
                                              AMSeekingSeekingFlags.NoPositioning);
                }
                double duration = m_dDuration / 10000000d;
                Log.Info("DVRMS2DIVX: movie duration:{0}", MediaPortal.Util.Utils.SecondsToHMSString((int)duration));

                //				hr=(graphBuilder as IMediaFilter).SetSyncSource(null);
                //				if (hr!=0)
                //					Log.Error("DVRMS2DIVX:FAILED:to SetSyncSource :0x{0:X}",hr);
                hr = mediaControl.Run();
                if (hr != 0)
                {
                    Log.Error("DVRMS2DIVX:FAILED:unable to start graph :0x{0:X}", hr);
                    Cleanup();
                    return(false);
                }
                int maxCount = 20;
                while (true)
                {
                    long lCurrent;
                    mediaSeeking.GetCurrentPosition(out lCurrent);
                    double dpos = (double)lCurrent;
                    dpos /= 10000000d;
                    System.Threading.Thread.Sleep(100);
                    if (dpos >= 2.0d)
                    {
                        break;
                    }
                    maxCount--;
                    if (maxCount <= 0)
                    {
                        break;
                    }
                }

                mediaControl.Stop();
                FilterState state;
                mediaControl.GetState(500, out state);
                GC.Collect();
                GC.Collect();
                GC.Collect();
                GC.WaitForPendingFinalizers();
                graphBuilder.RemoveFilter(aviMuxer);
                graphBuilder.RemoveFilter(divxCodec);
                graphBuilder.RemoveFilter(mp3Codec);
                graphBuilder.RemoveFilter((IBaseFilter)fileWriterFilter);
                if (!AddCodecs(graphBuilder, info))
                {
                    return(false);
                }

                //				hr=(graphBuilder as IMediaFilter).SetSyncSource(null);
                //			if (hr!=0)
                //					Log.Error("DVRMS2DIVX:FAILED:to SetSyncSource :0x{0:X}",hr);

                Log.Info("DVRMS2DIVX: start transcoding");
                hr = mediaControl.Run();
                if (hr != 0)
                {
                    Log.Error("DVRMS2DIVX:FAILED:unable to start graph :0x{0:X}", hr);
                    Cleanup();
                    return(false);
                }
            }
            catch (Exception ex)
            {
                Log.Error("DVRMS2DIVX:Unable create graph: {0}", ex.Message);
                Cleanup();
                return(false);
            }
            return(true);
        }
Example #6
0
 public bool Transcode(TranscodeInfo info, VideoFormat format, Quality quality, Standard standard)
 {
   try
   {
     if (!Supports(format)) return false;
     string ext = System.IO.Path.GetExtension(info.file);
     if (ext.ToLowerInvariant() != ".dvr-ms" && ext.ToLowerInvariant() != ".sbe")
     {
       Log.Info("DVRMS2WMV: wrong file format");
       return false;
     }
     Log.Info("DVRMS2WMV: create graph");
     graphBuilder = (IGraphBuilder)new FilterGraph();
     _rotEntry = new DsROTEntry((IFilterGraph)graphBuilder);
     Log.Info("DVRMS2WMV: add streambuffersource");
     bufferSource = (IStreamBufferSource)new StreamBufferSource();
     IBaseFilter filter = (IBaseFilter)bufferSource;
     graphBuilder.AddFilter(filter, "SBE SOURCE");
     Log.Info("DVRMS2WMV: load file:{0}", info.file);
     IFileSourceFilter fileSource = (IFileSourceFilter)bufferSource;
     int hr = fileSource.Load(info.file, null);
     //add mpeg2 audio/video codecs
     string strVideoCodec = "";
     string strAudioCodec = "";
     using (MediaPortal.Profile.Settings xmlreader = new MediaPortal.Profile.MPSettings())
     {
       strVideoCodec = xmlreader.GetValueAsString("mytv", "videocodec", "MPC - MPEG-2 Video Decoder (Gabest)");
       strAudioCodec = xmlreader.GetValueAsString("mytv", "audiocodec", "MPC - MPA Decoder Filter");
     }
     Log.Info("DVRMS2WMV: add mpeg2 video codec:{0}", strVideoCodec);
     Mpeg2VideoCodec = DirectShowUtil.AddFilterToGraph(graphBuilder, strVideoCodec);
     if (hr != 0)
     {
       Log.Error("DVRMS2WMV:FAILED:Add mpeg2 video  to filtergraph :0x{0:X}", hr);
       Cleanup();
       return false;
     }
     Log.Info("DVRMS2WMV: add mpeg2 audio codec:{0}", strAudioCodec);
     Mpeg2AudioCodec = DirectShowUtil.AddFilterToGraph(graphBuilder, strAudioCodec);
     if (Mpeg2AudioCodec == null)
     {
       Log.Error("DVRMS2WMV:FAILED:unable to add mpeg2 audio codec");
       Cleanup();
       return false;
     }
     Log.Info("DVRMS2WMV: connect streambufer source->mpeg audio/video decoders");
     //connect output #0 of streambuffer source->mpeg2 audio codec pin 1
     //connect output #1 of streambuffer source->mpeg2 video codec pin 1
     IPin pinOut0, pinOut1;
     IPin pinIn0, pinIn1;
     pinOut0 = DsFindPin.ByDirection((IBaseFilter)bufferSource, PinDirection.Output, 0); //audio
     pinOut1 = DsFindPin.ByDirection((IBaseFilter)bufferSource, PinDirection.Output, 1); //video
     if (pinOut0 == null || pinOut1 == null)
     {
       Log.Error("DVRMS2WMV:FAILED:unable to get pins of source");
       Cleanup();
       return false;
     }
     pinIn0 = DsFindPin.ByDirection(Mpeg2VideoCodec, PinDirection.Input, 0); //video
     pinIn1 = DsFindPin.ByDirection(Mpeg2AudioCodec, PinDirection.Input, 0); //audio
     if (pinIn0 == null || pinIn1 == null)
     {
       Log.Error("DVRMS2WMV:FAILED:unable to get pins of mpeg2 video/audio codec");
       Cleanup();
       return false;
     }
     hr = graphBuilder.Connect(pinOut0, pinIn1);
     if (hr != 0)
     {
       Log.Error("DVRMS2WMV:FAILED:unable to connect audio pins :0x{0:X}", hr);
       Cleanup();
       return false;
     }
     hr = graphBuilder.Connect(pinOut1, pinIn0);
     if (hr != 0)
     {
       Log.Error("DVRMS2WMV:FAILED:unable to connect video pins :0x{0:X}", hr);
       Cleanup();
       return false;
     }
     string outputFilename = System.IO.Path.ChangeExtension(info.file, ".wmv");
     if (!AddWmAsfWriter(outputFilename, quality, standard)) return false;
     Log.Info("DVRMS2WMV: start pre-run");
     mediaControl = graphBuilder as IMediaControl;
     mediaSeeking = bufferSource as IStreamBufferMediaSeeking;
     mediaEvt = graphBuilder as IMediaEventEx;
     mediaPos = graphBuilder as IMediaPosition;
     //get file duration
     long lTime = 5 * 60 * 60;
     lTime *= 10000000;
     long pStop = 0;
     hr = mediaSeeking.SetPositions(new DsLong(lTime), AMSeekingSeekingFlags.AbsolutePositioning, new DsLong(pStop),
                                    AMSeekingSeekingFlags.NoPositioning);
     if (hr == 0)
     {
       long lStreamPos;
       mediaSeeking.GetCurrentPosition(out lStreamPos); // stream position
       m_dDuration = lStreamPos;
       lTime = 0;
       mediaSeeking.SetPositions(new DsLong(lTime), AMSeekingSeekingFlags.AbsolutePositioning, new DsLong(pStop),
                                 AMSeekingSeekingFlags.NoPositioning);
     }
     double duration = m_dDuration / 10000000d;
     Log.Info("DVRMS2WMV: movie duration:{0}", Util.Utils.SecondsToHMSString((int)duration));
     hr = mediaControl.Run();
     if (hr != 0)
     {
       Log.Error("DVRMS2WMV:FAILED:unable to start graph :0x{0:X}", hr);
       Cleanup();
       return false;
     }
     int maxCount = 20;
     while (true)
     {
       long lCurrent;
       mediaSeeking.GetCurrentPosition(out lCurrent);
       double dpos = (double)lCurrent;
       dpos /= 10000000d;
       System.Threading.Thread.Sleep(100);
       if (dpos >= 2.0d) break;
       maxCount--;
       if (maxCount <= 0) break;
     }
     Log.Info("DVRMS2WMV: pre-run done");
     Log.Info("DVRMS2WMV: Get duration of movie");
     mediaControl.Stop();
     FilterState state;
     mediaControl.GetState(500, out state);
     GC.Collect();
     GC.Collect();
     GC.Collect();
     GC.WaitForPendingFinalizers();
     Log.Info("DVRMS2WMV: reconnect mpeg2 video codec->ASF WM Writer");
     graphBuilder.RemoveFilter(fileWriterbase);
     if (!AddWmAsfWriter(outputFilename, quality, standard)) return false;
     Log.Info("DVRMS2WMV: Start transcoding");
     hr = mediaControl.Run();
     if (hr != 0)
     {
       Log.Error("DVRMS2WMV:FAILED:unable to start graph :0x{0:X}", hr);
       Cleanup();
       return false;
     }
   }
   catch (Exception e)
   {
     // TODO: Handle exceptions.
     Log.Error("unable to transcode file:{0} message:{1}", info.file, e.Message);
     return false;
   }
   return true;
 }
    public bool Transcode(TranscodeInfo info, MediaPortal.Core.Transcoding.VideoFormat format,
                          MediaPortal.Core.Transcoding.Quality quality, Standard standard)
    {
      if (!Supports(format)) return false;
      string ext = System.IO.Path.GetExtension(info.file);
      if (ext.ToLower() != ".dvr-ms" && ext.ToLower() != ".sbe")
      {
        Log.Info("DVRMS2DIVX: wrong file format");
        return false;
      }

      //disable xvid status window while encoding
      /*  try
				{
					using (RegistryKey subkey = Registry.CurrentUser.OpenSubKey(@"Software\GNU\XviD", true))
					{
						if (subkey != null)
						{
							Int32 uivalue = 0;
							subkey.SetValue("display_status", (Int32)uivalue);
							subkey.SetValue("debug", (Int32)uivalue);
							subkey.SetValue("bitrate", (Int32)bitrate);

							uivalue = 1;
							subkey.SetValue("interlacing", (Int32)uivalue);
						}
					}
				}
				catch (Exception)
				{
				}*/
      //Type comtype = null;
      //object comobj = null;
      try
      {
        graphBuilder = (IGraphBuilder)new FilterGraph();

        _rotEntry = new DsROTEntry((IFilterGraph)graphBuilder);

        Log.Info("DVRMS2DIVX: add filesource");
        bufferSource = (IStreamBufferSource)new StreamBufferSource();

        IBaseFilter filter = (IBaseFilter)bufferSource;
        graphBuilder.AddFilter(filter, "SBE SOURCE");
        IFileSourceFilter fileSource = (IFileSourceFilter)bufferSource;
        Log.Info("DVRMS2DIVX: load file:{0}", info.file);
        int hr = fileSource.Load(info.file, null);


        /*string strDemuxerMoniker = @"@device:sw:{083863F1-70DE-11D0-BD40-00A0C911CE86}\{AFB6C280-2C41-11D3-8A60-0000F81E0E4A}";

				mpegDemuxer = Marshal.BindToMoniker(strDemuxerMoniker) as IBaseFilter;
				if (mpegDemuxer == null)
				{
						Log.Error("DVRMS2DIVX:FAILED:unable to add mpeg2 demuxer");
						Cleanup();
						return false;
				}
				hr = graphBuilder.AddFilter(mpegDemuxer, "MPEG-2 Demultiplexer");
				if (hr != 0)
				{
						Log.Error("DVRMS2DIVX:FAILED:Add mpeg2 demuxer to filtergraph :0x{0:X}", hr);
						Cleanup();
						return false;
				}*/

        //add mpeg2 audio/video codecs
        string strVideoCodecMoniker =
          @"@device:sw:{083863F1-70DE-11D0-BD40-00A0C911CE86}\{39F498AF-1A09-4275-B193-673B0BA3D478}";
        string strAudioCodec = "MPC - MPA Decoder Filter";
        Log.Info("DVRMS2DIVX: add MPV mpeg2 video decoder");
        Mpeg2VideoCodec = Marshal.BindToMoniker(strVideoCodecMoniker) as IBaseFilter;
        if (Mpeg2VideoCodec == null)
        {
          Log.Error("DVRMS2DIVX:FAILED:unable to add MPV mpeg2 video decoder");
          Cleanup();
          return false;
        }
        hr = graphBuilder.AddFilter(Mpeg2VideoCodec, "MPC - MPEG-2 Video Decoder (Gabest)");
        if (hr != 0)
        {
          Log.Error("DVRMS2DIVX:FAILED:Add MPV mpeg2 video  to filtergraph :0x{0:X}", hr);
          Cleanup();
          return false;
        }

        Log.Info("DVRMS2DIVX: add MPA mpeg2 audio codec:{0}", strAudioCodec);
        Mpeg2AudioCodec = DirectShowUtil.AddFilterToGraph(graphBuilder, strAudioCodec);
        if (Mpeg2AudioCodec == null)
        {
          Log.Error("DVRMS2DIVX:FAILED:unable to add MPV mpeg2 audio codec");
          Cleanup();
          return false;
        }

        //connect output #0 of streambuffer source->mpeg2 audio codec pin 1
        //connect output #1 of streambuffer source->mpeg2 video codec pin 1
        Log.Info("DVRMS2DIVX: connect streambufer source->mpeg audio/video decoders");
        IPin pinOut0, pinOut1;
        IPin pinIn0, pinIn1;
        pinOut0 = DsFindPin.ByDirection((IBaseFilter)bufferSource, PinDirection.Output, 0); //audio
        pinOut1 = DsFindPin.ByDirection((IBaseFilter)bufferSource, PinDirection.Output, 1); //video
        if (pinOut0 == null || pinOut1 == null)
        {
          Log.Error("DVRMS2DIVX:FAILED:unable to get pins of source");
          Cleanup();
          return false;
        }

        pinIn0 = DsFindPin.ByDirection(Mpeg2VideoCodec, PinDirection.Input, 0); //video
        pinIn1 = DsFindPin.ByDirection(Mpeg2AudioCodec, PinDirection.Input, 0); //audio
        if (pinIn0 == null || pinIn1 == null)
        {
          Log.Error("DVRMS2DIVX:FAILED:unable to get pins of mpeg2 video/audio codec");
          Cleanup();
          return false;
        }

        hr = graphBuilder.Connect(pinOut0, pinIn1);
        if (hr != 0)
        {
          Log.Error("DVRMS2DIVX:FAILED:unable to connect audio pins :0x{0:X}", hr);
          Cleanup();
          return false;
        }


        hr = graphBuilder.Connect(pinOut1, pinIn0);
        if (hr != 0)
        {
          Log.Error("DVRMS2DIVX:FAILED:unable to connect video pins :0x{0:X}", hr);
          Cleanup();
          return false;
        }
        if (!AddCodecs(graphBuilder, info)) return false;

        //				hr=(graphBuilder as IMediaFilter).SetSyncSource(null);
        //				if (hr!=0)
        //					Log.Error("DVRMS2DIVX:FAILED:to SetSyncSource :0x{0:X}",hr);
        mediaControl = graphBuilder as IMediaControl;
        mediaSeeking = bufferSource as IStreamBufferMediaSeeking;
        mediaEvt = graphBuilder as IMediaEventEx;
        mediaPos = graphBuilder as IMediaPosition;

        //get file duration
        Log.Info("DVRMS2DIVX: Get duration of movie");
        long lTime = 5 * 60 * 60;
        lTime *= 10000000;
        long pStop = 0;
        hr = mediaSeeking.SetPositions(new DsLong(lTime), AMSeekingSeekingFlags.AbsolutePositioning, new DsLong(pStop),
                                       AMSeekingSeekingFlags.NoPositioning);
        if (hr == 0)
        {
          long lStreamPos;
          mediaSeeking.GetCurrentPosition(out lStreamPos); // stream position
          m_dDuration = lStreamPos;
          lTime = 0;
          mediaSeeking.SetPositions(new DsLong(lTime), AMSeekingSeekingFlags.AbsolutePositioning, new DsLong(pStop),
                                    AMSeekingSeekingFlags.NoPositioning);
        }
        double duration = m_dDuration / 10000000d;
        Log.Info("DVRMS2DIVX: movie duration:{0}", MediaPortal.Util.Utils.SecondsToHMSString((int)duration));

        //				hr=(graphBuilder as IMediaFilter).SetSyncSource(null);
        //				if (hr!=0)
        //					Log.Error("DVRMS2DIVX:FAILED:to SetSyncSource :0x{0:X}",hr);
        hr = mediaControl.Run();
        if (hr != 0)
        {
          Log.Error("DVRMS2DIVX:FAILED:unable to start graph :0x{0:X}", hr);
          Cleanup();
          return false;
        }
        int maxCount = 20;
        while (true)
        {
          long lCurrent;
          mediaSeeking.GetCurrentPosition(out lCurrent);
          double dpos = (double)lCurrent;
          dpos /= 10000000d;
          System.Threading.Thread.Sleep(100);
          if (dpos >= 2.0d) break;
          maxCount--;
          if (maxCount <= 0) break;
        }

        mediaControl.Stop();
        FilterState state;
        mediaControl.GetState(500, out state);
        GC.Collect();
        GC.Collect();
        GC.Collect();
        GC.WaitForPendingFinalizers();
        graphBuilder.RemoveFilter(aviMuxer);
        graphBuilder.RemoveFilter(divxCodec);
        graphBuilder.RemoveFilter(mp3Codec);
        graphBuilder.RemoveFilter((IBaseFilter)fileWriterFilter);
        if (!AddCodecs(graphBuilder, info)) return false;

        //				hr=(graphBuilder as IMediaFilter).SetSyncSource(null);
        //			if (hr!=0)
        //					Log.Error("DVRMS2DIVX:FAILED:to SetSyncSource :0x{0:X}",hr);

        Log.Info("DVRMS2DIVX: start transcoding");
        hr = mediaControl.Run();
        if (hr != 0)
        {
          Log.Error("DVRMS2DIVX:FAILED:unable to start graph :0x{0:X}", hr);
          Cleanup();
          return false;
        }
      }
      catch (Exception ex)
      {
        Log.Error("DVRMS2DIVX:Unable create graph: {0}", ex.Message);
        Cleanup();
        return false;
      }
      return true;
    }