示例#1
0
        /// <summary>
        /// Close video file.
        /// </summary>
        ///
        public void Close()
        {
            LOG.Debug("Close called");
            lock (this) {
                // release compressed stream
                if (streamCompressed != IntPtr.Zero)
                {
                    LOG.Debug("AVIStreamRelease streamCompressed");
                    Avi32.AVIStreamRelease(streamCompressed);
                    streamCompressed = IntPtr.Zero;
                }

                // release stream
                if (stream != IntPtr.Zero)
                {
                    LOG.Debug("AVIStreamRelease stream");
                    Avi32.AVIStreamRelease(stream);
                    stream = IntPtr.Zero;
                }

                // release file
                if (file != IntPtr.Zero)
                {
                    LOG.Debug("AVIFileRelease file");
                    Avi32.AVIFileRelease(file);
                    file = IntPtr.Zero;
                }
            }
        }
示例#2
0
 /// <summary>
 /// Dispose the object.
 /// </summary>
 ///
 /// <param name="disposing">Indicates if disposing was initiated manually.</param>
 ///
 protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         // dispose managed resources
     }
     // close current AVI file if any opened and uninitialize AVI library
     Close();
     Avi32.AVIFileExit();
 }
示例#3
0
        /// <summary>
        /// Add new frame to the AVI file.
        /// </summary>
        /// <param name="frameData">New frame data.</param>
        public void AddLowLevelFrame(IntPtr frameData)
        {
            lock (this) {
                // write to stream
                if (Avi32.AVIStreamWrite(streamCompressed, position, 1, frameData, stride * height, 0, IntPtr.Zero, IntPtr.Zero) != 0)
                {
                    throw new ApplicationException("Failed adding frame");
                }

                position++;
            }
        }
示例#4
0
        /// <summary>
        /// Create new AVI file and open it for writing.
        /// </summary>
        ///
        /// <param name="fileName">AVI file name to create.</param>
        /// <param name="width">Video width.</param>
        /// <param name="height">Video height.</param>
        ///
        /// <remarks><para>The method opens (creates) a video files, configure video codec and prepares
        /// the stream for saving video frames with a help of <see cref="AddFrame"/> method.</para></remarks>
        ///
        /// <exception cref="ApplicationException">Failure of opening video files (the exception message
        /// specifies the issues).</exception>
        ///
        public bool Open(string fileName, int width, int height)
        {
            lock (this) {
                // calculate stride
                stride = width * 4;
                if ((stride % 4) != 0)
                {
                    stride += (4 - stride % 4);
                }

                this.width  = width;
                this.height = height;

                // describe new stream
                Avi32.AVISTREAMINFO info = new Avi32.AVISTREAMINFO();

                LOG.InfoFormat("Available codecs: {0}", String.Join(", ", Avi32.AvailableCodecs.ToArray()));

                info.type = Avi32.mmioFOURCC("vids");
                if (codec != null)
                {
                    info.handler = Avi32.mmioFOURCC(codec);
                }
                else
                {
                    info.handler = Avi32.mmioFOURCC("DIB ");
                }
                info.scale = 1;
                info.rate  = rate;
                info.suggestedBufferSize = stride * height;

                try {
                    // create new file
                    if (Avi32.AVIFileOpen(out file, fileName, Avi32.OpenFileMode.Create | Avi32.OpenFileMode.Write, IntPtr.Zero) != 0)
                    {
                        throw new ApplicationException("Failed opening file");
                    }

                    // create stream
                    if (Avi32.AVIFileCreateStream(file, out stream, ref info) != 0)
                    {
                        throw new ApplicationException("Failed creating stream");
                    }

                    // describe compression options
                    Avi32.AVICOMPRESSOPTIONS options = new Avi32.AVICOMPRESSOPTIONS();
                    // uncomment if video settings dialog is required to show
                    int retCode = 0;
                    if (codec == null)
                    {
                        retCode = Avi32.AVISaveOptions(stream, ref options);
                        if (retCode == 0)
                        {
                            LOG.Debug("Cancel clicked!");
                            return(false);
                        }
                        codec   = Avi32.decode_mmioFOURCC(options.handler);
                        quality = options.quality;
                    }
                    else
                    {
                        options.handler = Avi32.mmioFOURCC(codec);
                        options.quality = quality;
                    }
                    LOG.DebugFormat("Codec {0} selected with quality {1}.", codec, quality);

                    AviError retval;
                    // create compressed stream
                    try {
                        retval = Avi32.AVIMakeCompressedStream(out streamCompressed, stream, ref options, IntPtr.Zero);
                    } catch (Exception exCompress) {
                        LOG.Warn("Couldn't use compressed stream.", exCompress);
                        retval = AviError.AVIERR_OK;
                    }
                    if (retval != AviError.AVIERR_OK)
                    {
                        throw new ApplicationException(string.Format("Failed creating compressed stream: {0}", retval));
                    }


                    // describe frame format
                    BitmapInfoHeader bitmapInfoHeader = new BitmapInfoHeader(width, height, 32);

                    // set frame format
                    if (streamCompressed != IntPtr.Zero)
                    {
                        retval = Avi32.AVIStreamSetFormat(streamCompressed, 0, ref bitmapInfoHeader, Marshal.SizeOf(bitmapInfoHeader.GetType()));
                    }
                    else
                    {
                        retval = Avi32.AVIStreamSetFormat(stream, 0, ref bitmapInfoHeader, Marshal.SizeOf(bitmapInfoHeader.GetType()));
                    }
                    if (retval != 0)
                    {
                        throw new ApplicationException(string.Format("Failed creating stream: {0}", retval));
                    }
                    position = 0;
                    return(true);
                } catch (Exception ex) {
                    Close();
                    Avi32.AVIFileExit();
                    if (File.Exists(fileName))
                    {
                        File.Delete(fileName);
                    }
                    throw ex;
                }
            }
        }
示例#5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AVIWriter"/> class.
 /// </summary>
 ///
 /// <remarks>Initializes Video for Windows library.</remarks>
 ///
 public AVIWriter()
 {
     Avi32.AVIFileInit();
 }
示例#6
0
        /// <summary>
        /// Create new AVI file and open it for writing.
        /// </summary>
        ///
        /// <param name="fileName">AVI file name to create.</param>
        /// <param name="width">Video width.</param>
        /// <param name="height">Video height.</param>
        ///
        /// <remarks><para>The method opens (creates) a video files, configure video codec and prepares
        /// the stream for saving video frames with a help of <see cref="AddFrame"/> method.</para></remarks>
        ///
        /// <exception cref="ApplicationException">Failure of opening video files (the exception message
        /// specifies the issues).</exception>
        ///
        public void Open(string fileName, int width, int height)
        {
            lock (this)  {
                // calculate stride
                stride = width * 4;
                if ((stride % 4) != 0)
                {
                    stride += (4 - stride % 4);
                }

                // create new file
                if (Avi32.AVIFileOpen(out file, fileName, Avi32.OpenFileMode.Create | Avi32.OpenFileMode.Write, IntPtr.Zero) != 0)
                {
                    throw new ApplicationException("Failed opening file");
                }

                this.width  = width;
                this.height = height;

                // describe new stream
                Avi32.AVISTREAMINFO info = new Avi32.AVISTREAMINFO();

                info.type = Avi32.mmioFOURCC("vids");
                if (codec != null)
                {
                    info.handler = Avi32.mmioFOURCC(codec);
                }
                else
                {
                    info.handler = Avi32.mmioFOURCC("DIB ");
                }
                info.scale = 1;
                info.rate  = rate;
                info.suggestedBufferSize = stride * height;

                // create stream
                if (Avi32.AVIFileCreateStream(file, out stream, ref info) != 0)
                {
                    throw new ApplicationException("Failed creating stream");
                }

                // describe compression options
                Avi32.AVICOMPRESSOPTIONS options = new Avi32.AVICOMPRESSOPTIONS();


                // uncomment if video settings dialog is required to show
                if (codec == null)
                {
                    Avi32.AVISaveOptions(stream, ref options);
                }
                else
                {
                    options.handler = Avi32.mmioFOURCC(codec);
                    options.quality = quality;
                }

                // create compressed stream
                int retval = Avi32.AVIMakeCompressedStream(out streamCompressed, stream, ref options, IntPtr.Zero);
                if (retval != 0)
                {
                    throw new ApplicationException("Failed creating compressed stream: " + retval);
                }

                // describe frame format
                BitmapInfoHeader bitmapInfoHeader = new BitmapInfoHeader(width, height, 32);

                // set frame format
                retval = Avi32.AVIStreamSetFormat(streamCompressed, 0, ref bitmapInfoHeader, Marshal.SizeOf(bitmapInfoHeader.GetType()));
                if (retval != 0)
                {
                    throw new ApplicationException("Failed creating compressed stream: " + retval);
                }
                position = 0;
            }
        }