AGX Dynamics  2.35.0.1
agxOSG/VideoCapture.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 
18 #ifndef AGXOSG_VIDEOCAPTURE_H
19 #define AGXOSG_VIDEOCAPTURE_H
20 
21 #include <agx/config/AGX_USE_FFMPEG.h>
22 #include <agxOSG/ImageCapture.h>
23 #include <agxData/BinaryData.h>
24 
25 #if AGX_USE_FFMPEG()
26 
27 extern "C"
28 {
29  #include <agx/PushDisableWarnings.h>
30  #include <libavutil/opt.h>
31  #include <libavcodec/avcodec.h>
32  #include <libavutil/channel_layout.h>
33  #include <libavutil/common.h>
34  #include <libavutil/imgutils.h>
35  #include <libavutil/mathematics.h>
36  #include <libavutil/samplefmt.h>
37  #include <libswscale/swscale.h>
38  #include <libavformat/avformat.h>
39  #include <agx/PopDisableWarnings.h>
40 }
41 
42 namespace agxOSG
43 {
44  AGX_DECLARE_POINTER_TYPES(VideoCapture);
45  class AGXOSG_EXPORT VideoCapture : public ImageCaptureBase
46  {
47  public:
48  class DrawCallback;
49 
50  public:
51 
52  VideoCapture(agx::UInt width, agx::UInt height, agx::UInt fps, agxSDK::Simulation *sim = nullptr);
53 
54  void init(agx::UInt width, agx::UInt height, agx::UInt fps, AVCodecID codec);
55 
56  typedef agx::Callback2<agx::UInt8 *, size_t> DataCallback;
57 
58  void setDataCallback(DataCallback callback);
59 
60  agxData::BinaryData *writeHeader();
61 
62  void setFilename(const agx::String& filename);
63 
64  agx::String getFilename() const;
65 
66  void setMovieFPS(agx::UInt fps);
67 
68  void finalizeMovie();
69 
70  protected:
71  virtual ~VideoCapture();
72 
73  private:
74 
75  void finalize();
76 
77  bool encode(AVFrame *frame);
78 
79  virtual void writeImage(osg::Image *image, agx::Index index);
80  static int writePacket(void *opaque, uint8_t *buf, int buf_size);
81 
82  private:
83  AVFormatContext * m_formatContext;
84  AVOutputFormat * m_format;
85  AVCodecContext * m_codecContext;
86  AVCodec * m_codec;
87  AVStream * m_stream;
88  AVFrame * m_frame;
89  SwsContext * m_swsContext;
90  DataCallback m_dataCallback;
91  unsigned char * m_fragmentBuffer;
92  FILE * m_file;
93  bool m_firstFrame;
94  bool m_writingHeader;
95  agxData::BinaryDataRef m_header;
96  agx::String m_filename;
97  agx::UInt m_videoFPS;
98  };
99 
100 }
101 
102 
103 #endif
104 #endif /* AGXOSG_VIDEOCAPTURE_H */
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:248
#define AGXOSG_EXPORT
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition: Simulation.h:127
Templated callback with two arguments.
Definition: Callback.h:140
The agxOSG namespace provides functionality for visualizing AGX simulations with OpenSceneGraph.
uint32_t UInt
Definition: Integer.h:34
void AGXPHYSICS_EXPORT init()
Initialize agx.
UInt32 Index
Definition: Integer.h:52