Exemple #1
0
        /// <summary>
        /// Close the output and release the variables
        /// </summary>
        public void Close()
        {
            if (m_Init)   //We are currently writing
            {
                if (m_pWMWriter != null)
                {
                    // Close the file
                    try
                    {
                        m_pWMWriter.EndWriting();
                    }
                    catch { }
                }
                m_Init = false;
            }

            if (m_pInputProps != null)
            {
                Marshal.ReleaseComObject(m_pInputProps);
                m_pInputProps = null;
            }
            if (m_pWMWriter != null)
            {
                Marshal.ReleaseComObject(m_pWMWriter);
                m_pWMWriter = null;
            }
        }
Exemple #2
0
        /// <summary>
        /// Wraps the WMCreateWriter function
        /// </summary>
        /// <returns>The writer object</returns>
        public static IWMWriter CreateWriter()
        {
            IWMWriter res = null;

            Marshal.ThrowExceptionForHR(WMCreateWriter(IntPtr.Zero, out res));
            return(res);
        }
        /// <summary>
        /// Create the writer indicating Metadata information
        /// </summary>
        /// <param name="output"><see cref="System.IO.Stream"/> Where resulting WMA string will be written</param>
        /// <param name="format">PCM format of input data received in <see cref="WmaWriter.Write"/> method</param>
        /// <param name="profile">IWMProfile that describe the resulting compressed stream</param>
        /// <param name="MetadataAttributes">Array of <see cref="Yeti.WMFSdk.WM_Attr"/> structures describing the metadata information that will be in the result stream</param>
        public WmaWriter(Stream output, WaveFormat format, IWMProfile profile, WM_Attr[] MetadataAttributes)
            : base(output, format)
        {
            m_Writer = WM.CreateWriter();
            IWMWriterAdvanced wa = (IWMWriterAdvanced)m_Writer;

            wa.AddSink((IWMWriterSink)this);
            m_Writer.SetProfile(profile);
            uint inputs;

            m_Writer.GetInputCount(out inputs);
            if (inputs == 1)
            {
                IWMInputMediaProps InpProps;
                Guid type;
                m_Writer.GetInputProps(0, out InpProps);
                InpProps.GetType(out type);
                if (type == MediaTypes.WMMEDIATYPE_Audio)
                {
                    WM_MEDIA_TYPE mt;
                    mt.majortype            = MediaTypes.WMMEDIATYPE_Audio;
                    mt.subtype              = MediaTypes.WMMEDIASUBTYPE_PCM;
                    mt.bFixedSizeSamples    = true;
                    mt.bTemporalCompression = false;
                    mt.lSampleSize          = (uint)m_InputDataFormat.nBlockAlign;
                    mt.formattype           = MediaTypes.WMFORMAT_WaveFormatEx;
                    mt.pUnk     = IntPtr.Zero;
                    mt.cbFormat = (uint)Marshal.SizeOf(m_InputDataFormat);

                    GCHandle h = GCHandle.Alloc(m_InputDataFormat, GCHandleType.Pinned);
                    try {
                        mt.pbFormat = h.AddrOfPinnedObject();
                        InpProps.SetMediaType(ref mt);
                    }
                    finally {
                        h.Free();
                    }
                    m_Writer.SetInputProps(0, InpProps);
                    if (MetadataAttributes != null)
                    {
                        WMHeaderInfo info = new WMHeaderInfo((IWMHeaderInfo)m_Writer);
                        foreach (WM_Attr attr in MetadataAttributes)
                        {
                            info.SetAttribute(attr);
                        }
                        info = null;
                    }
                    m_Writer.BeginWriting();
                    m_Profile = profile;
                }
                else
                {
                    throw new ArgumentException("Invalid profile", "profile");
                }
            }
            else
            {
                throw new ArgumentException("Invalid profile", "profile");
            }
        }
Exemple #4
0
        public override void Close()

        {
            try

            {
                if (m_Writer != null)

                {
                    m_Writer.EndWriting();

                    IWMWriterAdvanced wa = (IWMWriterAdvanced)m_Writer;

                    wa.RemoveSink((IWMWriterSink)this);

                    m_Writer = null;

                    m_Profile = null;
                }
            }

            finally

            {
                base.Close();
            }
        }
Exemple #5
0
    /// <summary>
    /// Create the writer indicating Metadata information
    /// </summary>
    /// <param name="output"><see cref="System.IO.Stream"/> Where resulting WMA string will be written</param>
    /// <param name="format">PCM format of input data received in <see cref="WmaWriter.Write"/> method</param>
    /// <param name="profile">IWMProfile that describe the resulting compressed stream</param>
    /// <param name="MetadataAttributes">Array of <see cref="Yeti.WMFSdk.WM_Attr"/> structures describing the metadata information that will be in the result stream</param>
    public WmaWriter(Stream output, WaveFormat format, IWMProfile profile, WM_Attr[] MetadataAttributes)
      : base(output, format)
		{
			m_Writer = WM.CreateWriter();
      IWMWriterAdvanced wa = (IWMWriterAdvanced)m_Writer;
      wa.AddSink((IWMWriterSink)this);
      m_Writer.SetProfile(profile);
      uint inputs;
      m_Writer.GetInputCount(out inputs);
      if (inputs == 1)
      {
        IWMInputMediaProps InpProps;
        Guid type;
        m_Writer.GetInputProps(0, out InpProps);
        InpProps.GetType(out type);
        if (type == MediaTypes.WMMEDIATYPE_Audio)
        {
          WM_MEDIA_TYPE mt;
          mt.majortype = MediaTypes.WMMEDIATYPE_Audio;
          mt.subtype = MediaTypes.WMMEDIASUBTYPE_PCM;
          mt.bFixedSizeSamples	= true;
          mt.bTemporalCompression = false;
          mt.lSampleSize	= (uint)m_InputDataFormat.nBlockAlign;
          mt.formattype	= MediaTypes.WMFORMAT_WaveFormatEx;
          mt.pUnk	= IntPtr.Zero;
          mt.cbFormat	= (uint)Marshal.SizeOf(m_InputDataFormat);

          GCHandle h = GCHandle.Alloc(m_InputDataFormat, GCHandleType.Pinned);
          try
          {
            mt.pbFormat	= h.AddrOfPinnedObject();
            InpProps.SetMediaType(ref mt);
          }
          finally
          {
            h.Free();
          }
          m_Writer.SetInputProps(0, InpProps);
          if ( MetadataAttributes != null )
          {
            WMHeaderInfo info = new WMHeaderInfo((IWMHeaderInfo)m_Writer);
            foreach(WM_Attr attr in MetadataAttributes)
            {
              info.SetAttribute(attr);
            }
            info = null;
          }
          m_Writer.BeginWriting();
          m_Profile = profile;
        }
        else
        {
          throw new ArgumentException("Invalid profile", "profile");
        }
      }
      else
      {
        throw new ArgumentException("Invalid profile", "profile");
      }
		}
Exemple #6
0
        public WMAWriter(string path, WMAWriterSettings settings)
        {
            this.m_settings = settings;
            this.outputPath = path;

            try
            {
                m_pWriter = settings.GetWriter();
                int cInputs;
                m_pWriter.GetInputCount(out cInputs);
                if (cInputs < 1)
                {
                    throw new InvalidOperationException();
                }
                IWMInputMediaProps pInput;
                m_pWriter.GetInputProps(0, out pInput);
                try
                {
                    int         cbType     = 0;
                    AMMediaType pMediaType = null;
                    pInput.GetMediaType(pMediaType, ref cbType);
                    pMediaType            = new AMMediaType();
                    pMediaType.formatSize = cbType - Marshal.SizeOf(typeof(AMMediaType));
                    pInput.GetMediaType(pMediaType, ref cbType);
                    try
                    {
                        var wfe = new WaveFormatExtensible(m_settings.PCM);
                        Marshal.FreeCoTaskMem(pMediaType.formatPtr);
                        pMediaType.formatPtr  = IntPtr.Zero;
                        pMediaType.formatSize = 0;
                        pMediaType.formatPtr  = Marshal.AllocCoTaskMem(Marshal.SizeOf(wfe));
                        pMediaType.formatSize = Marshal.SizeOf(wfe);
                        Marshal.StructureToPtr(wfe, pMediaType.formatPtr, false);
                        pInput.SetMediaType(pMediaType);
                        m_pWriter.SetInputProps(0, pInput);
                    }
                    finally
                    {
                        WMUtils.FreeWMMediaType(pMediaType);
                    }
                }
                finally
                {
                    Marshal.ReleaseComObject(pInput);
                }
            }
            catch (Exception ex)
            {
                if (m_pWriter != null)
                {
                    Marshal.ReleaseComObject(m_pWriter);
                    m_pWriter = null;
                }
                throw ex;
            }
        }
Exemple #7
0
 /// <summary>
 /// Dispose of everything that may need disposing.
 /// </summary>
 public void Cleanup()
 {
     scriptBitrate      = 0;
     writerAdvanced     = null;
     writer             = null;
     netSink            = null;
     fileSink           = null;
     audioProps         = null;
     videoProps         = null;
     audioInput         = 0;
     videoInput         = 0;
     scriptStreamNumber = 0;
     profileManager     = null;
 }
Exemple #8
0
        public override void Dispose(bool disposing)
        {
            AppLogger.Message("ASFNetSink -- Dispose called");

            if (_eventTimer != null)
            {
                _eventTimer.Enabled = false;
            }

            if (_netSink != null)
            {
                try
                {
                    IWMRegisterCallback iCallback = (IWMRegisterCallback)_netSink;
                    iCallback.Unadvise(this, IntPtr.Zero);
                    _netSink.Close();
                }
                catch (Exception ex)
                {
                    AppLogger.Dump(ex);
                }
                finally
                {
                    Marshal.ReleaseComObject(_netSink);
                    _netSink = null;
                }
            }
            if (_writerAdvanced != null)
            {
                RemoveAllSinks(_writerAdvanced);
                Marshal.ReleaseComObject(_writerAdvanced);
                _writerAdvanced = null;
            }
            if (_writer != null)
            {
                Marshal.ReleaseComObject(_writer);
                _writer = null;
            }
            if (_asfFilter != null)
            {
                Marshal.ReleaseComObject(_asfFilter);
                _asfFilter = null;
            }

            base.Dispose(disposing);
        }
Exemple #9
0
 public WMWriter(MediaBuffer mb)
 {
     eventLog           = new EventLog("WMG", ".", "WMGCore");
     mb.OnSampleReady  += new MediaBuffer.sampleReadyHandler(ReceiveSample);
     scriptBitrate      = 0;
     writerAdvanced     = null;
     writer             = null;
     netSink            = null;
     fileSink           = null;
     audioProps         = null;
     videoProps         = null;
     audioInput         = 0;
     videoInput         = 0;
     scriptStreamNumber = 0;
     lastWriteTime      = 0;
     profileManager     = null;
     writeFailed        = false;
 }
Exemple #10
0
 public void Dispose()
 {
     try
     {
         if (m_Writer != null)
         {
             m_Writer.EndWriting();
             IWMWriterAdvanced wa = (IWMWriterAdvanced)m_Writer;
             wa.RemoveSink((IWMWriterSink)this);
             m_Writer  = null;
             m_Profile = null;
         }
     }
     finally
     {
         m_outputStream.Close();
     }
 }
Exemple #11
0
        /// <summary>
        /// Configures the WM ASF Writer filter. (populates member variables as well)
        /// </summary>
        protected void ConfigureWriterFilter()
        {
            IWMProfile profile = GetWMProfile(CurrentProfile);

            WindowsMediaLib.IConfigAsfWriter configAsf = (WindowsMediaLib.IConfigAsfWriter)_asfFilter;
            configAsf.ConfigureFilterUsingProfile(profile);

            //Retreive the IWMWriter* objects from the WM ASF Writer filter.
            _writerAdvanced = GetWriterAdvanced();
            _writer         = (IWMWriter)_writerAdvanced;

            //The WM ASF Writer comes with a File-Writer-Sink, which we need to remove.
            RemoveAllSinks(_writerAdvanced);

            //set SourceConfig settings
            _writerAdvanced.SetSyncTolerance(SourceConfig.SyncToleranceMilliseconds);

            _writerAdvanced.SetLiveSource(SourceConfig.LiveSource);
        }
Exemple #12
0
 public override void Close()
 {
     try
     {
         if (m_Writer != null)
         {
             m_Writer.EndWriting();
             var wa = (IWMWriterAdvanced)m_Writer;
             wa.RemoveSink((IWMWriterSink)this);
             Marshal.ReleaseComObject(m_Writer);
             m_Writer  = null;
             m_Profile = null;
         }
     }
     finally
     {
         //Purposefully leaving the stream open since it gets passed in,
         //its the responsibility of the calling class to manage its own resources.
         base.Flush();
     }
 }
Exemple #13
0
        public void Close()
        {
            if (!this.closed)
            {
                try
                {
                    if (this.writingBegan)
                    {
                        m_pWriter.EndWriting();
                        this.writingBegan = false;
                    }
                }
                finally
                {
                    if (m_pWriter != null)
                    {
                        Marshal.ReleaseComObject(m_pWriter);
                        m_pWriter = null;
                    }
                }

                this.closed = true;
            }
        }
Exemple #14
0
 private static extern int WMCreateWriter(IntPtr pUnkReserved, [Out, MarshalAs(UnmanagedType.Interface)] out IWMWriter ppWriter);
Exemple #15
0
        /// <summary>
        ///  Create filename from specified profile using specified framerate
        /// </summary>
        /// <param name="lpszFileName">File name to create</param>
        /// <param name="guidProfileID">WM Profile to use for compression</param>
        /// <param name="iFrameRate">Frames Per Second</param>
        public CwmvFile(string lpszFileName, ref Guid guidProfileID, int iFrameRate)
        {
            Guid guidInputType;
            int  dwInputCount;

            IWMProfileManager pWMProfileManager = null;
            IWMProfile        pWMProfile        = null;

            // Initialize all member variables
            m_iFrameRate           = iFrameRate;
            m_dwVideoInput         = -1;
            m_dwCurrentVideoSample = 0;
            m_msVideoTime          = 0;

            m_pWMWriter   = null;
            m_pInputProps = null;
            m_Init        = false;

            try
            {
                // Open the profile manager
                WMUtils.WMCreateProfileManager(out pWMProfileManager);

                // Convert pWMProfileManager to a IWMProfileManager2
                IWMProfileManager2 pProfileManager2 = (IWMProfileManager2)pWMProfileManager;

                // Specify the version number of the profiles to use
                pProfileManager2.SetSystemProfileVersion(WMVersion.V8_0);

                // Load the profile specified by the caller
                pProfileManager2.LoadProfileByID(guidProfileID, out pWMProfile);

                // Create a writer.  This is the interface we actually write with
                WMUtils.WMCreateWriter(IntPtr.Zero, out m_pWMWriter);

                // Set the profile we got into the writer.  This controls compression, video
                // size, # of video channels, # of audio channels, etc
                m_pWMWriter.SetProfile(pWMProfile);

                // Find out how many inputs are in the current profile
                m_pWMWriter.GetInputCount(out dwInputCount);

                // Assume we won't find any video pins
                m_dwVideoInput = -1;

                // Find the first video input on the writer
                for (int i = 0; i < dwInputCount; i++)
                {
                    // Get the properties of channel #i
                    m_pWMWriter.GetInputProps(i, out m_pInputProps);

                    // Read the type of the channel
                    m_pInputProps.GetType(out guidInputType);

                    // If it is video, we are done
                    if (guidInputType == MediaType.Video)
                    {
                        m_dwVideoInput = i;
                        break;
                    }
                }

                // Didn't find a video channel
                if (m_dwVideoInput == -1)
                {
                    throw new Exception("Profile does not accept video input");
                }

                // Specify the file name for the output
                m_pWMWriter.SetOutputFilename(lpszFileName);
            }
            catch
            {
                Close();
                throw;
            }
            finally
            {
                // Release the locals
                if (pWMProfile != null)
                {
                    Marshal.ReleaseComObject(pWMProfile);
                    pWMProfile = null;
                }
                if (pWMProfileManager != null)
                {
                    Marshal.ReleaseComObject(pWMProfileManager);
                    pWMProfileManager = null;
                }
            }
        }
Exemple #16
0
 public WmvWriter()
 {
     _writer = Helpers.CreateWriter();
 }
Exemple #17
0
        //------------------------------------------------------------------------------
        // Name: CWMVCopy::Copy()
        // Desc: Copies the input file to the output file. The script stream is moved
        //       to the header if fMoveScriptStream is true.
        //------------------------------------------------------------------------------
        public void Copy(
            string pwszInputFile,
            string pwszOutputFile,
            long qwMaxDuration,
            bool fMoveScriptStream)
        {
            //
            // Initialize the pointers
            //
            m_hEvent            = null;
            m_pReader           = null;
            m_pReaderAdvanced   = null;
            m_pReaderHeaderInfo = null;
            m_pReaderProfile    = null;
            m_pWriter           = null;
            m_pWriterAdvanced   = null;
            m_pWriterHeaderInfo = null;

            m_dwStreamCount   = 0;
            m_pguidStreamType = null;
            m_pwStreamNumber  = null;

            if (null == pwszInputFile || null == pwszOutputFile)
            {
                throw new COMException("Missing input or output file", E_InvalidArgument);
            }

            m_fMoveScriptStream = fMoveScriptStream;
            m_qwMaxDuration     = qwMaxDuration;

            //
            // Event for the asynchronous calls
            //
            m_hEvent = new AutoResetEvent(false);

            //
            // Create the Reader
            //
            CreateReader(pwszInputFile);

            //
            // Get profile information
            //
            GetProfileInfo();

            //
            // Create the Writer
            //
            CreateWriter(pwszOutputFile);

            //
            // Copy all attributes
            //
            CopyAttribute();

            //
            // Copy codec info
            //
            CopyCodecInfo();

            //
            // Copy all scripts in the header
            //
            CopyScriptInHeader();

            //
            // Process samples: read from the reader, and write to the writer
            //

            try
            {
                Process();
            }
            catch (Exception e)
            {
                int hr = Marshal.GetHRForException(e);
                //
                // Output some common error messages.
                //
                if (NSResults.E_VIDEO_CODEC_NOT_INSTALLED == hr)
                {
                    Console.WriteLine("Processing samples failed: Video codec not installed");
                }
                if (NSResults.E_AUDIO_CODEC_NOT_INSTALLED == hr)
                {
                    Console.WriteLine("Processing samples failed: Audio codec not installed");
                }
                else if (NSResults.E_INVALID_OUTPUT_FORMAT == hr)
                {
                    Console.WriteLine("Processing samples failed: Invalid output format ");
                }
                else if (NSResults.E_VIDEO_CODEC_ERROR == hr)
                {
                    Console.WriteLine("Processing samples failed: An unexpected error occurred with the video codec ");
                }
                else if (NSResults.E_AUDIO_CODEC_ERROR == hr)
                {
                    Console.WriteLine("Processing samples failed: An unexpected error occurred with the audio codec ");
                }
                else
                {
                    Console.WriteLine(string.Format("Processing samples failed: Error (hr=0x{0:x})", hr));
                }
                throw;
            }

            //
            // Copy all scripts in m_ScriptList
            //
            CopyScriptInList(pwszOutputFile);

            //
            // Copy marker information
            //
            CopyMarker(pwszOutputFile);

            Console.WriteLine("Copy finished.");

            //
            // Note: The output file is indexed automatically.
            // You can use IWMWriterFileSink3::SetAutoIndexing(false) to disable
            // auto indexing.
            //

            Dispose();
        }
Exemple #18
0
        public static void CopyWmv(string inputFilePath, string outputFilePath, ulong startTime, long duration)
        {
            IWMWriterAdvanced writerAdvanced = null;
            IWMProfile        profile        = null;
            IWMStreamConfig   streamConfig   = null;
            uint streamCount = 0;
            uint inputCount  = 0;

            ushort[] streamNumbers = null;
            WMT_STREAM_SELECTION[] streamSelections = null;
            ulong      sampleTime, sampleDuration;
            uint       flags, outputNum;
            ushort     streamNum;
            INSSBuffer sample = null;

            IWMSyncReader reader = Helpers.CreateSyncReader(WMT_RIGHTS.WMT_RIGHT_NO_DRM);
            IWMWriter     writer = Helpers.CreateWriter();

            try
            {
                reader.Open(inputFilePath);

                Logger.WriteLogMessage("Opened file [" + inputFilePath + "] for reading.");

                profile = (IWMProfile)reader;

                profile.GetStreamCount(out streamCount);

                streamNumbers = new ushort[streamCount];

                streamSelections = new WMT_STREAM_SELECTION[streamCount];

                for (uint i = 0; i < streamCount; i++)
                {
                    profile.GetStream(i, out streamConfig);

                    streamConfig.GetStreamNumber(out streamNumbers[i]);

                    streamSelections[i] = WMT_STREAM_SELECTION.WMT_ON;

                    //
                    // Read compressed samples
                    //
                    reader.SetReadStreamSamples(streamNumbers[i], true);
                }

                //
                // select all streams
                //
                reader.SetStreamsSelected((ushort)streamCount, streamNumbers, streamSelections);

                writer.SetProfile(profile);

                writer.GetInputCount(out inputCount);

                for (uint i = 0; i < inputCount; i++)
                {
                    writer.SetInputProps(i, null);                     // write compressed samples
                }

                writer.SetOutputFilename(outputFilePath);

                Logger.WriteLogMessage("Set output filename [" + outputFilePath + "] for writing.");

                writerAdvanced = (IWMWriterAdvanced)writer;

                // Copy attributes avoided
                // Copy Codec Info avoided
                // Copy all scripts in the header avoided

                writer.BeginWriting();

                //
                // startTime, duration are in 100-nsec ticks
                //
                reader.SetRange(startTime, duration);                 // seek

                Logger.WriteLogMessage("Set range on reader, startTime [" + startTime + "], duration [" + duration + "].");

                for (uint streamsRead = 0; streamsRead < streamCount;)
                {
                    try
                    {
                        streamNum = 0;

                        reader.GetNextSample(0, out sample, out sampleTime, out sampleDuration, out flags, out outputNum, out streamNum);

                        Logger.WriteLogMessage("Grabbed next video sample, sampleTime [" + sampleTime + "], duration [" + sampleDuration + "], flags [" + flags + "], outputNum [" + outputNum + "], streamNum [" + streamNum + "].");

                        writerAdvanced.WriteStreamSample(streamNum, sampleTime, 0, sampleDuration, flags, sample);

                        Logger.WriteLogMessage("Wrote sample, sampleTime [" + sampleTime + "], duration [" + sampleDuration + "], flags [" + flags + "], outputNum [" + outputNum + "], streamNum [" + streamNum + "].");
                    }
                    catch (COMException e)
                    {
                        if (e.ErrorCode == Constants.NS_E_NO_MORE_SAMPLES)
                        {
                            streamsRead++;
                        }
                        else
                        {
                            throw;
                        }
                    }
                }

                writer.EndWriting();
            }
            finally
            {
                reader.Close();
            }
        }
Exemple #19
0
 public WmvWriter()
 {
     _writer = Helpers.CreateWriter();
 }
Exemple #20
0
 public static extern int WMCreateWriter(
     IntPtr pUnkCert,
     out IWMWriter ppWriter
     );
Exemple #21
0
 public override void Close()
 {
     try
     {
         if (m_Writer != null)
         {
             m_Writer.EndWriting();
             var wa = (IWMWriterAdvanced)m_Writer;
             wa.RemoveSink((IWMWriterSink)this);
             Marshal.ReleaseComObject(m_Writer);
             m_Writer = null;
             m_Profile = null;
         }
     }
     finally
     {
         //Purposefully leaving the stream open since it gets passed in,
         //its the responsibility of the calling class to manage its own resources.
         base.Flush();
     }
 }
Exemple #22
0
 public override void Close()
 {
   try
   {
     if (m_Writer != null)
     {
       m_Writer.EndWriting();
       IWMWriterAdvanced wa = (IWMWriterAdvanced)m_Writer;
       wa.RemoveSink((IWMWriterSink)this);
       m_Writer = null;
       m_Profile = null;
     }
   }
   finally 
   {
     base.Close ();
   }
 }
Exemple #23
0
 public static extern uint WMCreateWriter(
     [In, MarshalAs(UnmanagedType.Interface)] object
     pUnkReserved,             // Always null.
     [Out, MarshalAs(UnmanagedType.Interface)] out IWMWriter
     ppWriter);
Exemple #24
0
 public void Dispose()
 {
     try
     {
         if (m_Writer != null)
         {
             m_Writer.EndWriting();
             IWMWriterAdvanced wa = (IWMWriterAdvanced)m_Writer;
             wa.RemoveSink((IWMWriterSink)this);
             m_Writer = null;
             m_Profile = null;
         }
     }
     finally
     {
         m_outputStream.Close();
     }
 }