PTLib  Version 2.18.8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mediafile.h
Go to the documentation of this file.
1 /*
2  * mediafile.h
3  *
4  * Media file declaration
5  *
6  * Portable Windows Library
7  *
8  * Copyright (C) 2017 Vox Lucida Pty. Ltd.
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Portable Tools Library.
21  *
22  * All Rights Reserved.
23  *
24  * Contributor(s): ______________________________________.
25  */
26 
27 #ifndef PTLIB_PMEDIAFILE_H
28 #define PTLIB_PMEDIAFILE_H
29 
30 #ifdef P_USE_PRAGMA
31 #pragma interface
32 #endif
33 
34 #include <ptlib.h>
35 
36 #if P_MEDIAFILE
37 
38 #include <ptlib/pfactory.h>
39 #include <ptlib/smartptr.h>
40 #include <ptlib/sound.h>
41 #include <ptlib/videoio.h>
42 #include <ptclib/delaychan.h>
43 
44 
47 class PMediaFile : public PSmartObject
48 {
49  PCLASSINFO(PMediaFile, PSmartObject);
50  protected:
53  PMediaFile();
54 
55  bool m_reading;
56  PFilePath m_filePath;
57  PString m_lastErrorText;
58 
59  bool CheckOpenAndTrack(unsigned track) const;
60  bool CheckMode(bool reading) const;
61  bool CheckOpenTrackAndMode(unsigned track, bool reading) const;
62  bool CheckModeAndTrackType(bool reading, const PString & expectedTrackType, const PString & actualTrackType) const;
63 
64  bool SetErrorText(const PString & error);
65 
66  public:
67  ~PMediaFile();
68 
70 
71  typedef PSmartPtr<PMediaFile> Ptr;
72 
76  static PMediaFile * Create(
77  const PFilePath & file
78  ) { return Factory::CreateInstance(file.GetType()); }
79 
82  static PStringSet GetAllFileTypes();
83 
85  static const PString & Audio();
86 
88  static const PString & Video();
89 
93  virtual bool IsSupported(
94  const PString & format
95  ) const = 0;
96 
99  virtual bool OpenForReading(
100  const PFilePath & filePath
101  ) = 0;
102 
106  virtual bool OpenForWriting(
107  const PFilePath & filePath
108  ) = 0;
109 
112  virtual bool IsOpen() const = 0;
113 
116  virtual bool Close() = 0;
117 
120  bool IsReading() const { return m_reading; }
121 
124  const PFilePath & GetFilePath() const { return m_filePath; }
125 
128  const PString & GetErrorText() const { return m_lastErrorText; }
129 
131  struct TrackInfo
132  {
133  TrackInfo(const PString & type = PString::Empty(), const PString & format = PString::Empty());
134  TrackInfo(unsigned rate, unsigned channels); // Audio track
135 #if P_VIDEO
136  TrackInfo(unsigned width, unsigned height, double rate); // Video track
137 #endif
138 
139  bool operator==(const TrackInfo & other) const;
140  bool operator!=(const TrackInfo & other) const { return !operator==(other); }
141 
142  PCaselessString m_type;
143  PString m_format;
144  unsigned m_size;
145  int64_t m_frames;
146  double m_rate;
147  unsigned m_channels;
148  unsigned m_width, m_height;
149  PStringOptions m_options;
150  };
151  typedef std::vector<TrackInfo> TracksInfo;
152 
158  virtual bool GetDefaultTrackInfo(
159  const PCaselessString & type,
160  TrackInfo & info
161  ) const = 0;
162 
166  virtual unsigned GetTrackCount() const = 0;
167 
171  virtual bool GetTracks(
172  TracksInfo & tracks
173  ) = 0;
174 
179  virtual bool SetTracks(
180  const TracksInfo & tracks
181  ) = 0;
182 
186  virtual bool ReadNative(
187  unsigned track,
188  void * data,
189  PINDEX & size,
190  unsigned & frames
191  ) = 0;
192 
196  virtual bool WriteNative(
197  unsigned track,
198  const void * data,
199  PINDEX & size,
200  unsigned & frames
201  ) = 0;
202 
207  virtual bool ConfigureAudio(
208  unsigned track,
209  unsigned channels,
210  unsigned sampleRate
211  ) = 0;
212 
217  virtual bool ReadAudio(
218  unsigned track,
219  void * pcm,
220  PINDEX size,
221  PINDEX & length
222  ) = 0;
223 
228  virtual bool WriteAudio(
229  unsigned track,
230  const void * pcm,
231  PINDEX length,
232  PINDEX & written
233  ) = 0;
234 
235 #if P_VIDEO
236 
243  virtual bool ConfigureVideo(
244  unsigned track,
245  const PVideoFrameInfo & frameInfo
246  ) = 0;
247 
252  virtual bool ReadVideo(
253  unsigned track,
254  void * data,
255  PTimeInterval * sampleTime = NULL
256  ) = 0;
257 
262  virtual bool WriteVideo(
263  unsigned track,
264  const void * data,
265  const PTimeInterval & sampleTime = -1
266  ) = 0;
267 #endif
268 
269 
270  class SoundChannel : public PSoundChannelEmulation
271  {
272  PCLASSINFO(SoundChannel, PSoundChannelEmulation);
273  public:
274  explicit SoundChannel(const Ptr & mediaFile = Ptr(), unsigned track = 0);
275  ~SoundChannel();
276 
277  virtual bool Open(const Params & params);
278  virtual PString GetName() const;
279  virtual PBoolean Close();
280  virtual PBoolean IsOpen() const;
281 
282  protected:
283  virtual bool RawWrite(const void * buf, PINDEX len);
284  virtual bool RawRead(void * buf, PINDEX len);
285  virtual bool Rewind();
286 
287  Ptr m_mediaFile;
288  unsigned m_track;
289  };
290 
291 #if P_VIDEO
292  class VideoInputDevice : public PVideoInputEmulatedDevice
293  {
294  PCLASSINFO(VideoInputDevice, PVideoInputEmulatedDevice);
295  public:
296  explicit VideoInputDevice(const Ptr & mediaFile = Ptr(), unsigned track = 0);
297  ~VideoInputDevice();
298 
299  virtual PStringArray GetDeviceNames() const;
300  virtual PBoolean Open(const PString & deviceName, PBoolean startImmediate = true);
301  virtual PBoolean IsOpen();
302  virtual PBoolean Close();
303 
304  protected:
305  virtual bool InternalReadFrameData(BYTE * frame);
306 
307  Ptr m_mediaFile;
308  unsigned m_track;
309  };
310 #endif // P_VIDEO
311 };
312 
313 
314 PFACTORY_LOAD(PMediaFile_WAV);
315 
316 
317 #if P_VIDEO
318 
320 //
321 // This class defines a video display (output) device that writes video to a raw YUV file
322 //
323 
324 class PVideoOutputDevice_MediaFile : public PVideoOutputDevice
325 {
326  PCLASSINFO(PVideoOutputDevice_MediaFile, PVideoOutputDevice);
327 
328  public:
331  PVideoOutputDevice_MediaFile();
332 
335  virtual ~PVideoOutputDevice_MediaFile();
336 
339  static PStringArray GetOutputDeviceNames();
340 
341  virtual PStringArray GetDeviceNames() const
342  { return GetOutputDeviceNames(); }
343 
346  virtual PBoolean Open(
347  const PString & deviceName,
348  PBoolean startImmediate = true
349  );
350 
353  PBoolean Start();
354 
357  PBoolean Stop();
358 
361  virtual PBoolean Close();
362 
365  virtual PBoolean IsOpen();
366 
372  virtual PBoolean SetColourFormat(
373  const PString & colourFormat // New colour format for device.
374  );
375 
378  virtual PBoolean SetFrameData(const FrameData & frameData);
379 
380  protected:
381  PMediaFile * m_file;
382 };
383 
384 #endif // P_VIDEO
385 
386 #endif // P_MEDIAFILE
387 
388 #endif // PTLIB_PMEDIAFILE_H
389 
390 
391 // End Of File ///////////////////////////////////////////////////////////////
This class defines an arbitrary time interval to millisecond accuracy.
Definition: timeint.h:51
#define PCLASSINFO(cls, par)
Declare all the standard PTLib class information.
Definition: object.h:2164
virtual PBoolean Open(const PString &deviceName, PBoolean startImmediate=true)=0
Open the device given the device name.
This class describes a full description for a file on the particular platform.
Definition: filepath.h:61
This class is a variation of a string that ignores case.
Definition: pstring.h:2012
Definition: sound.h:762
This is an array collection class of PString objects.
Definition: pstring.h:2365
virtual PBoolean Start()
Start the video device I/O display.
Definition: videoio.h:789
Definition: videoio.h:49
Specialised version of PStringToString to contain a dictionary of options/attributes.
Definition: pstring.h:3249
virtual PBoolean Stop()
Stop the video device I/O display.
Definition: videoio.h:793
virtual PBoolean IsOpen()=0
Determine if the device is currently open.
#define PFACTORY_LOAD(ConcreteType)
Definition: pfactory.h:557
bool PBoolean
Definition: object.h:174
The character string class.
Definition: pstring.h:108
This is a set collection class of PString objects.
Definition: pstring.h:2821
virtual PStringArray GetDeviceNames() const =0
Get a list of all of the drivers available.
virtual PBoolean SetFrameData(const FrameData &frameData)=0
Set a section of the output frame buffer.
This template class creates a type safe version of PSmartPointer.
Definition: smartptr.h:197
PFilePathString GetType() const
Get the file type of the file.
static const PString & Empty()
Return an empty string.
Class for a factory to create concrete class instances without parameters during construction.
Definition: pfactory.h:396
This is the base class for objects that use the smart pointer system.
Definition: smartptr.h:49
virtual PBoolean SetColourFormat(const PString &colourFormat)
Set the colour format to be used.
virtual PBoolean Close()
Close the device.
Definition: videoio.h:785
This class defines a video output device.
Definition: videoio.h:712
Definition: videoio.h:1246