views:

75

answers:

2

I have such .H file (from here)

/*
  FFmpeg simple Encoder
 */


#ifndef __VIDEO_ENCODER_H__
#define __VIDEO_ENCODER_H__

#include "ffmpegInclude.h"
#include <Windows.h>
#include <string>

class VideoEncoder
{
  private:

  // output file name
  std::string     outputFilename;
  // output format.
  AVOutputFormat  *pOutFormat;
  // format context
  AVFormatContext *pFormatContext;
  // video stream context
  AVStream * pVideoStream;
  // audio streams context
  AVStream * pAudioStream;
  // convert context context
  struct SwsContext *pImgConvertCtx;
  // encode buffer and size
  uint8_t * pVideoEncodeBuffer;
  int nSizeVideoEncodeBuffer;

  // audio buffer and size
  uint8_t * pAudioEncodeBuffer;
  int nSizeAudioEncodeBuffer;


  // count of sample
  int audioInputSampleSize;
  // current picture
  AVFrame *pCurrentPicture;

  // audio buffer
  char* audioBuffer;
  int   nAudioBufferSize;
  int   nAudioBufferSizeCurrent;

  public:

  VideoEncoder() 
  {
    pOutFormat = NULL;
    pFormatContext = NULL;
    pVideoStream = NULL;
    pImgConvertCtx = NULL;
    pCurrentPicture = NULL;
    pVideoEncodeBuffer = NULL;
    nSizeVideoEncodeBuffer = 0;
    pAudioEncodeBuffer = NULL;
    nSizeAudioEncodeBuffer = 0;
    nAudioBufferSize = 1024 * 1024 * 4;
    audioBuffer      = new char[nAudioBufferSize];
    nAudioBufferSizeCurrent = 0;
  }

  virtual ~VideoEncoder() 
  {
    Finish();
  }

  // init output file
  bool InitFile(std::string& inputFile, std::string& container);
  // Add video and audio data
  bool AddFrame(AVFrame* frame, const char* soundBuffer, int soundBufferSize);
  // end of output
  bool Finish();

  private: 

  // Add video stream
  AVStream *AddVideoStream(AVFormatContext *pContext, CodecID codec_id);
  // Open Video Stream
  bool OpenVideo(AVFormatContext *oc, AVStream *pStream);
  // Allocate memory
  AVFrame * CreateFFmpegPicture(int pix_fmt, int nWidth, int nHeight);
  // Close video stream
  void CloseVideo(AVFormatContext *pContext, AVStream *pStream);
  // Add audio stream
  AVStream * AddAudioStream(AVFormatContext *pContext, CodecID codec_id);
  // Open audio stream
  bool OpenAudio(AVFormatContext *pContext, AVStream *pStream);
  // close audio stream
  void CloseAudio(AVFormatContext *pContext, AVStream *pStream);
  // Add video frame
  bool AddVideoFrame(AVFrame * frame, AVCodecContext *pVideoCodec);
  // Add audio samples
  bool AddAudioSample(AVFormatContext *pFormatContext, 
    AVStream *pStream, const char* soundBuffer, int soundBufferSize);
  // Free resourses.
  void Free();
  bool NeedConvert();
};

#endif // __VIDEO_ENCODER_H__

I want to encapsulate this "Video Encoder" into some namespace and compile it so that I would be able to acsess that video encoder functions from C# (so I turned that project (link provided above) into dll\library and try to compile it with clr and clr:old... it compiles and dll is acsessible from C# but not its functions/classes... I just do not know what to modify in this class to turn it into acsessible one from .net)

A: 

You need to look into P/Invoke, which enables native DLL methods to be called from managed code. It has many gotchas and pitfalls, so be careful, and understand it won't be easy.

abelenky
my main idea is not to use C# for creating real .net dll but create real .net dll in visual C++
Blender
+1  A: 

I believe what you're looking for is C++/CLI ( http://en.wikipedia.org/wiki/C++/CLI and http://msdn.microsoft.com/en-us/library/xey702bw.aspx ). While I'm no expert myself, I think you might be able to get away with changing the class declaration to

namespace MyNamespace
{
    public ref class VideoEncoder
    {
        // Existing contents of VideoEncoder class
    }
}

(note that you'll also have to edit the matching .cpp file to use the namespace).

If that doesn't work, you should be able to able to write a managed class that wraps the original C++ class.

Dave Kilian
in general I like your idea but it gave me 100 errors so I went on to another way of doind what I intended...
Blender