CoreSDK  v1.0.0.b1304
r5data.h
1 /*
2  * Copyright (c) 2022-2023 Infrared5, Inc.
3  * License information: https://account.red5.net/assets/LICENSE.txt
4  *
5  */
6 
7 #pragma once
8 
9 #include <memory>
10 #include <string>
11 #include <cstring>
12 #include <vector>
13 
14 #include "r5common.h"
15 
16 namespace r5::common {
21 enum class VideoFmt : int {
22  Unknown = 0,
23  Yuv420,
24  Yuy2,
25  Nv12,
26  Rgba,
27  Argb,
28  Bgra,
29  Abgr,
30  H264,
31  Unsupported,
32 };
37 enum class AudioFmt : int {
38  Unknown = 0,
39  FltP,
40  S16P,
41  Flt,
42  S16,
43  Aac,
44  Opus,
45  Unsupported,
46 };
51 enum class DataType {
52  Unknown = -1,
53  Audio = 0,
54  Video = 1,
55  Rtsp,
56  DataTypeCount
57 };
64 inline bool IsEncodedVideoFormat(VideoFmt fmt)
65 {
66  return fmt == VideoFmt::H264;
67 }
74 inline bool IsRawVideoFormat(VideoFmt fmt)
75 {
76  return !(fmt == VideoFmt::Unknown || fmt == VideoFmt::Unsupported) && !IsEncodedVideoFormat(fmt);
77 }
78 
85 inline bool IsEncodedAudioFormat(AudioFmt fmt)
86 {
87  return fmt == AudioFmt::Aac || fmt == AudioFmt::Opus;
88 }
89 
96 inline bool IsRawAudioFormat(AudioFmt fmt)
97 {
98  return !(fmt == AudioFmt::Unknown || fmt == AudioFmt::Unsupported) && !IsEncodedAudioFormat(fmt);
99 }
100 
105 typedef struct VideoDesc
106 {
107  union {
108  VideoFmt fmt = VideoFmt::Unknown;
109  int fmtFull;
110  };
111  int width = 0;
112  int height = 0;
113  int fps = 0;
114 } VideoDesc;
119 typedef struct AudioDesc
120 {
121  union {
122  AudioFmt fmt = AudioFmt::Unknown;
123  int fmtFull;
124  };
125  int numChannels = 0;
126  int sampleRate = 0;
127 } AudioDesc;
132 typedef struct MediaDesc
133 {
139  MediaDesc(DataType t = DataType::Unknown) : type(t)
140  {
141  video.fmt = VideoFmt::Unknown;
142  video.width = 0;
143  video.height = 0;
144  video.fps = 0;
145  }
151  MediaDesc(const MediaDesc &other)
152  {
153  type = other.type;
154  if (type == DataType::Audio)
155  audio = other.audio;
156  else if (type == DataType::Video)
157  video = other.video;
158  }
166  {
167  type = other.type;
168  if (type == DataType::Audio)
169  audio = other.audio;
170  else if (type == DataType::Video)
171  video = other.video;
172  return *this;
173  }
182  MediaDesc(VideoFmt fmt, int32_t w = 0, int32_t h = 0, int32_t fps = 0) : type(DataType::Video)
183  {
184  video.fmt = fmt;
185  video.width = w;
186  video.height = h;
187  video.fps = fps;
188  }
196  MediaDesc(AudioFmt fmt, int32_t ch = 0, int32_t sr = 0) : type(DataType::Audio)
197  {
198  audio.fmt = fmt;
199  audio.numChannels = ch;
200  audio.sampleRate = sr;
201  }
202 
203  DataType type;
204 
205  union {
206  struct VideoDesc video;
207  struct AudioDesc audio;
208  };
209 } MediaDesc;
220 inline bool operator==(const MediaDesc &lhs, const MediaDesc &rhs)
221 {
222  if (lhs.type == DataType::Unknown || rhs.type == DataType::Unknown)
223  return false; // Unknown types not identical
224  if (lhs.type != rhs.type)
225  return false;
226 
227  if (lhs.type == DataType::Audio) {
228  if (lhs.audio.fmt == AudioFmt::Unknown || rhs.audio.fmt == AudioFmt::Unknown || lhs.audio.fmt == rhs.audio.fmt)
229  return true;
230  else
231  return false;
232  } else if (lhs.type == DataType::Video) {
233  if (lhs.video.fmt == VideoFmt::Unknown || rhs.video.fmt == VideoFmt::Unknown || lhs.video.fmt == rhs.video.fmt)
234  return true;
235  else
236  return false;
237  }
238  return false;
239 }
243 inline bool operator!=(const MediaDesc &lhs, const MediaDesc &rhs)
244 {
245  bool eq = lhs == rhs;
246  return !eq;
247 }
253 typedef struct MediaTransform
254 {
255 
261  MediaTransform(DataType t = DataType::Unknown) : in(t), out(t) {}
268  {
269  in = other.in;
270  out = other.out;
271  friendlyName = other.friendlyName;
272  }
280  {
281  in = other.in;
282  out = other.out;
283  friendlyName = other.friendlyName;
284  return *this;
285  }
293  MediaTransform(VideoFmt fmtIn, VideoFmt fmtOut, std::string name = "") : in(fmtIn), out(fmtOut), friendlyName(name) {}
301  MediaTransform(AudioFmt fmtIn, AudioFmt fmtOut, std::string name = "") : in(fmtIn), out(fmtOut), friendlyName(name) {}
310  MediaTransform(MediaDesc descIn, MediaDesc descOut, std::string name = "") : in(descIn), out(descOut), friendlyName(name)
311  {
312  if (in.type == DataType::Audio) {
313  if (in.audio.numChannels != 0 && out.audio.numChannels == 0)
314  out.audio.numChannels = in.audio.numChannels;
315  else if (in.audio.numChannels == 0 && out.audio.numChannels != 0)
316  in.audio.numChannels = out.audio.numChannels;
317 
318  if (in.audio.sampleRate != 0 && out.audio.sampleRate == 0)
319  out.audio.sampleRate = in.audio.sampleRate;
320  else if (in.audio.sampleRate == 0 && out.audio.sampleRate != 0)
321  in.audio.sampleRate = out.audio.sampleRate;
322  } else if (in.type == DataType::Video) {
323  if (in.video.width != 0 && out.video.width == 0)
324  out.video.width = in.video.width;
325  else if (in.video.width == 0 && out.video.width != 0)
326  in.video.width = out.video.width;
327 
328  if (in.video.height != 0 && out.video.height == 0)
329  out.video.height = in.video.height;
330  else if (in.video.height == 0 && out.video.height != 0)
331  in.video.height = out.video.height;
332  }
333  }
334 
337  std::string friendlyName;
342 template<typename T, typename U>
343 inline std::unique_ptr<T[]> reinterpret_unique_pointer_cast(std::unique_ptr<U[]> &&old)
344 {
345  return std::unique_ptr<T[]>(reinterpret_cast<T *>(old.release()));
346 }
351 typedef struct MediaSample
352 {
357  {
358  timestamp = 0;
359  size = 0;
360  buffer = nullptr;
361  }
369  MediaSample(MediaDesc mediaType, uint32_t dataSize, uint32_t ts)
370  {
371  buffer = std::make_unique<uint8_t[]>(dataSize);
372  size = dataSize;
373  type = mediaType;
374  timestamp = ts;
375  }
384  MediaSample(MediaDesc mediaType, uint8_t *data, uint32_t dataSize, uint32_t ts)
385  {
386  buffer = std::make_unique<uint8_t[]>(dataSize);
387  memcpy(buffer.get(), data, dataSize);
388  size = dataSize;
389  type = mediaType;
390  timestamp = ts;
391  }
400  MediaSample(MediaDesc mediaType, char *data, uint32_t dataSize, uint32_t ts)
401  {
402  buffer = std::make_unique<uint8_t[]>(dataSize);
403  memcpy(buffer.get(), data, dataSize);
404  size = dataSize;
405  type = mediaType;
406  timestamp = ts;
407  }
417  MediaSample(MediaDesc mediaType, std::unique_ptr<uint8_t[]> data, uint32_t dataSize, uint32_t ts)
418  {
419  buffer = std::move(data);
420  size = dataSize;
421  type = mediaType;
422  timestamp = ts;
423  }
433  MediaSample(MediaDesc mediaType, std::unique_ptr<char[]> data, uint32_t dataSize, uint32_t ts)
434  {
435  buffer = reinterpret_unique_pointer_cast<uint8_t>(std::move(data));
436  size = dataSize;
437  type = mediaType;
438  timestamp = ts;
439  }
450  {
451  type = other.type;
452  timestamp = other.timestamp;
453  size = other.size;
454  buffer = std::move(other.buffer);
455  }
463  {
464  type = other.type;
465  timestamp = other.timestamp;
466  size = other.size;
467  buffer = std::move(other.buffer);
468 
469  return *this;
470  }
476  MediaSample(const MediaSample &other)
477  {
478  type = other.type;
479  timestamp = other.timestamp;
480  size = other.size;
481  buffer = std::make_unique<uint8_t[]>(size);
482  memcpy(buffer.get(), other.buffer.get(), size);
483  }
491  {
492  type = other.type;
493  timestamp = other.timestamp;
494  size = other.size;
495  buffer = std::make_unique<uint8_t[]>(size);
496  memcpy(buffer.get(), other.buffer.get(), size);
497 
498  return *this;
499  }
500 
502  uint32_t timestamp;
503  uint32_t size;
504  std::unique_ptr<uint8_t[]> buffer;
505 } MediaSample;
510 typedef struct ProvisionDesc
511 {
512  int width;
513  int height;
514  int fps;
515  int bitrate;
516 } ProvisionDesc;
524 int R5_SDK_CORE_EXPORT parseProvisions(const std::string &jsonData, std::vector<ProvisionDesc> &provisions);
532 int R5_SDK_CORE_EXPORT writeProvisions(const std::vector<ProvisionDesc> &provisions, std::string &jsonData);
533 }
MediaSample()
Default constructor.
Definition: r5data.h:356
MediaSample(MediaSample &other)
Copying constructor.
Definition: r5data.h:449
MediaDesc & operator=(MediaDesc other)
Assign MediaDesc value.
Definition: r5data.h:165
std::unique_ptr< uint8_t[]> buffer
sample data
Definition: r5data.h:504
MediaSample(MediaDesc mediaType, std::unique_ptr< uint8_t[]> data, uint32_t dataSize, uint32_t ts)
Initializing constructor Will set values, allocate and initialize internal buffer based on parameters...
Definition: r5data.h:417
VideoFmt fmt
video pixel format
Definition: r5data.h:108
MediaSample(MediaDesc mediaType, char *data, uint32_t dataSize, uint32_t ts)
Initializing constructor (char variant) Will set values, allocate and initialize internal buffer base...
Definition: r5data.h:400
MediaDesc(VideoFmt fmt, int32_t w=0, int32_t h=0, int32_t fps=0)
Overriden constructor to create video data description Contructs object with type set to Video...
Definition: r5data.h:182
MediaDesc(const MediaDesc &other)
Copying constructor.
Definition: r5data.h:151
MediaDesc in
media description of transformation input
Definition: r5data.h:335
MediaSample(MediaDesc mediaType, std::unique_ptr< char[]> data, uint32_t dataSize, uint32_t ts)
Initializing constructor (char variant) Will set values, allocate and initialize internal buffer base...
Definition: r5data.h:433
~MediaSample()
Default destructor.
Definition: r5data.h:443
MediaTransform(const MediaTransform &other)
Copying constructor.
Definition: r5data.h:267
MediaTransform(AudioFmt fmtIn, AudioFmt fmtOut, std::string name="")
Overriden constructor to create audio transform description Contructs object with types set to Ausio...
Definition: r5data.h:301
MediaDesc(AudioFmt fmt, int32_t ch=0, int32_t sr=0)
Overriden constructor to create audio data description Contructs object with type set to Audio...
Definition: r5data.h:196
MediaTransform(DataType t=DataType::Unknown)
Default constructor.
Definition: r5data.h:261
MediaSample(const MediaSample &other)
Copying constructor.
Definition: r5data.h:476
Description of media sample used.
Definition: r5data.h:351
int fmtFull
negative values for ffmpeg codec id
Definition: r5data.h:123
int height
transcoding video height
Definition: r5data.h:513
uint32_t timestamp
timestamp of sample in milliseconds
Definition: r5data.h:502
struct AudioDesc audio
Description for Audio data type.
Definition: r5data.h:207
DataType type
Type of media described by structure. Defines what option of union to use.
Definition: r5data.h:203
Description of media data transformation This structure can specify input, output or both media descr...
Definition: r5data.h:253
MediaSample(MediaDesc mediaType, uint32_t dataSize, uint32_t ts)
Allocation constrructor Will set values and allocate internal buffer based on paramters.
Definition: r5data.h:369
Video data description.
Definition: r5data.h:105
MediaTransform & operator=(MediaTransform other)
Assign MediaTransform value.
Definition: r5data.h:279
MediaDesc out
media description of transformation output
Definition: r5data.h:336
MediaDesc type
media description of sample data
Definition: r5data.h:501
AudioFmt fmt
audio sample format
Definition: r5data.h:122
MediaTransform(VideoFmt fmtIn, VideoFmt fmtOut, std::string name="")
Overriden constructor to create video transform description Contructs object with types set to Video...
Definition: r5data.h:293
struct VideoDesc video
Description for Video data type.
Definition: r5data.h:206
Audio data description.
Definition: r5data.h:119
MediaDesc(DataType t=DataType::Unknown)
Default constructor Can specify data type and set all other fields to zero.
Definition: r5data.h:139
Unified media data description.
Definition: r5data.h:132
int fps
transcoding frame rate
Definition: r5data.h:514
int fmtFull
negative values for ffmpeg codec id
Definition: r5data.h:109
uint32_t size
size of sample data in bytes
Definition: r5data.h:503
Definition: r5codec.h:12
MediaSample(MediaDesc mediaType, uint8_t *data, uint32_t dataSize, uint32_t ts)
Initializing constructor Will set values, allocate and initialize internal buffer based on parameters...
Definition: r5data.h:384
int bitrate
transcoding bitrate
Definition: r5data.h:515
MediaSample & operator=(const MediaSample &other)
Assign MediaSample value.
Definition: r5data.h:490
MediaSample & operator=(MediaSample &other)
Assign MediaSample value.
Definition: r5data.h:462
Transcoding provision level description It can be used to control transcoding instance by Red5Pro Str...
Definition: r5data.h:510
std::string friendlyName
name of transfromation
Definition: r5data.h:337
int width
transcoding video width
Definition: r5data.h:512
MediaTransform(MediaDesc descIn, MediaDesc descOut, std::string name="")
Overriden constructor to create transform description based on provided media descriptions Note: Tran...
Definition: r5data.h:310