sipxmedialib  Version 3.3
MprRecorder.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2006-2019 SIPez LLC. All rights reserved.
3 //
4 // Copyright (C) 2004-2009 SIPfoundry Inc.
5 // Licensed by SIPfoundry under the LGPL license.
6 //
7 // Copyright (C) 2004-2006 Pingtel Corp. All rights reserved.
8 // Licensed to SIPfoundry under a Contributor Agreement.
9 //
10 // $$
12 
13 
14 #ifndef _MprRecorder_h_
15 #define _MprRecorder_h_
16 
17 // SYSTEM INCLUDES
18 #ifdef _VXWORKS /* [ */
19 # include <ioLib.h>
20 #endif /* _VXWORKS ] */
21 
22 #if defined(_WIN32) && !defined(WINCE) /* [ */
23 # include <io.h>
24 #endif /* _WIN32 ] */
25 
26 // APPLICATION INCLUDES
27 #include "os/OsMutex.h"
28 #include "mp/MpResourceMsg.h"
29 #include "mp/MpAudioResource.h"
30 #include "utl/CircularBufferPtr.h"
31 #include "os/OsProtectEvent.h"
32 
33 // DEFINES
34 // Note: only tested with 1-4 channels
35 #ifndef MAXIMUM_RECORDER_CHANNELS
36 # define MAXIMUM_RECORDER_CHANNELS 4
37 #endif
38 
39 // MACROS
40 // EXTERNAL FUNCTIONS
41 // EXTERNAL VARIABLES
42 // CONSTANTS
43 // STRUCTS
44 // TYPEDEFS
45 
46 // FORWARD DECLARATIONS
47 class MpEncoderBase;
48 class MpResamplerBase;
49 struct SipxOpusWriteObject;
50 struct OpusHead;
51 
54 {
55 /* //////////////////////////// PUBLIC //////////////////////////////////// */
56 public:
57 
59  typedef enum {
62 
63  // WAV codec codes:
65  WAV_ALAW = 6,
66  WAV_MULAW = 7,
67  WAV_GSM = 49, // 0x31
68 
69  // Ogg codecs:
70  OGG_OPUS = 501
72 
73 /* ============================ CREATORS ================================== */
75 
76 
78  MprRecorder(const UtlString& rName);
79 
81  virtual
82  ~MprRecorder();
83 
85 
86 /* ============================ MANIPULATORS ============================== */
88 
89 
91  static OsStatus startFile(const UtlString& namedResource,
92  OsMsgQ& fgQ,
93  const char *filename,
94  RecordFileFormat recFormat,
95  int time = 0,
96  int silenceLength = -1,
97  UtlBoolean append = FALSE,
98  int numChannels = 1);
124  static OsStatus startBuffer(const UtlString& namedResource,
126  OsMsgQ& fgQ,
127  MpAudioSample *pBuffer,
128  int bufferSize,
129  int time = 0,
130  int silenceLength = -1,
131  int numChannels = 1);
145  static OsStatus pause(const UtlString& namedResource, OsMsgQ& flowgraphQueue);
152  static OsStatus resume(const UtlString& namedResource, OsMsgQ& flowgraphQueue);
159  static OsStatus startCircularBuffer(const UtlString& namedResource,
161  OsMsgQ& fgQ,
162  CircularBufferPtr & buffer,
163  RecordFileFormat recordingFormat,
164  unsigned long recordingBufferNotificationWatermark,
165  int numChannels = 1);
174  static OsStatus stop(const UtlString& namedResource, OsMsgQ& fgQ);
182 
183  static OsStatus validateOpusHeader(int inFileFd, OpusHead& opusHeader);
184 
185 /* ============================ ACCESSORS ================================= */
187 
188 
190 
191 /* ============================ INQUIRY =================================== */
193 
194  static int getMaximumRecoderChannels();
196 
197 /* //////////////////////////// PROTECTED ///////////////////////////////// */
198 protected:
199 
200  typedef enum
201  {
208  } AddlMsgTypes;
209 
210  typedef enum
211  {
215  } FinishCause;
216 
217  typedef enum
218  {
222 
223  } State;
224 
225  typedef enum
226  {
232 
233  typedef enum
234  {
239 
240  State mState;
241  RecordDestination mRecordDestination;
248 
250 
253 
254 
256 
258  int mBufferSize;
260 
261 
265 
268  struct SipxOpusWriteObject* mpOpusStreamObject;
269 
270  SampleInterlaceStage mWhenToInterlace;
271 
273 
274 
276 
277  CircularBufferPtr * mpCircularBuffer;
280 
283 
284  virtual UtlBoolean doProcessFrame(MpBufPtr inBufs[],
285  MpBufPtr outBufs[],
286  int inBufsSize,
287  int outBufsSize,
288  UtlBoolean isEnabled,
289  int samplesPerFrame,
290  int samplesPerSecond);
291 
293  UtlBoolean handleStartFile(int file,
294  RecordFileFormat recFormat,
295  int time,
296  int silenceLength,
297  UtlBoolean append,
298  int numChannels);
309  UtlBoolean handleStartBuffer(MpAudioSample *pBuffer, int bufferSize, int time,
311  int silenceLength, int numChannels);
312 
314  UtlBoolean handleStartCircularBuffer(CircularBufferPtr * buffer,
315  RecordFileFormat recordingFormat,
316  unsigned long recordingBufferNotificationWatermark,
317  int numChannels);
318 
320  UtlBoolean handleStop();
321 
323  virtual UtlBoolean handleDisable();
324 
326  virtual UtlBoolean handleMessage(MpResourceMsg& rMsg);
327 
329  void startRecording(int time, int silenceLength);
330 
332  UtlBoolean finish(FinishCause cause);
333 
335  void closeFile(const char* fromWhereLabel);
336 
337  typedef int (MprRecorder::*WriteMethod)(const char * channelBuffers[], int);
338 
340  inline int writeFileSilence(int numSamples);
341 
343  inline int writeSamples(const MpAudioSample *pBuffer[], int numSamples, WriteMethod writeMethod);
344 
346  inline int writeBufferSilence(int numSamples);
347 
349  inline int writeCircularBufferSilence(int numSamples);
350 
352  inline int writeBufferSpeech(const MpAudioSample *pBuffer, int numSamples);
353 
355  static UtlBoolean writeWaveHeader(int handle, RecordFileFormat format,
356  uint32_t samplesPerSecond = 8000,
357  int16_t numChannels = 1);
358 
360  static OsStatus readWaveHeader(int fileHandle,
361  RecordFileFormat& format,
362  uint16_t& samplesPerSecond,
363  uint16_t& channels);
375  static UtlBoolean updateWaveHeaderLengths(int handle, RecordFileFormat format);
377 
378 /* //////////////////////////// PRIVATE /////////////////////////////////// */
379 private:
380 
382  MprRecorder(const MprRecorder& rMprRecorder);
383 
385  MprRecorder& operator=(const MprRecorder& rhs);
386 
387  int writeFile(const char* channelData[], int dataSize);
388  int writeCircularBuffer(const char* channelData[], int dataSize);
390  void createEncoder(const char * mimeSubtype, unsigned int codecSampleRate);
391  void prepareEncoder(RecordFileFormat recFormat, unsigned int & codecSampleRate);
392  OsStatus createOpusEncoder(int channels,
393  const char* artist,
394  const char* title);
395  void deleteOpusEncoder();
397 
398  static int16_t getBytesPerSample(RecordFileFormat format);
399  static int interlaceSamples(const char* samplesArrays[], int samplesPerChannel, int bytesPerSample, int channels, char* interlacedChannelSamplesArray, int interlacedArrayMaximum);
400 };
401 
402 /* ============================ INLINE METHODS ============================ */
403 
404 #endif // _MprRecorder_h_
Definition: MprRecorder.h:205
Definition: MprRecorder.h:61
void prepareEncoder(RecordFileFormat recFormat, unsigned int &codecSampleRate)
Definition: MprRecorder.cpp:1060
Message object used to communicate with the media processing task.
Definition: MpResourceMsg.h:30
Recording is running.
Definition: MprRecorder.h:220
Definition: MprRecorder.h:207
static UtlBoolean updateWaveHeaderLengths(int handle, RecordFileFormat format)
Update WAV-file&#39;s header with correct recorded length.
Definition: MprRecorder.cpp:2081
int samplesPerFrame
void startRecording(int time, int silenceLength)
Prepare for recording.
Definition: MprRecorder.cpp:1422
virtual UtlBoolean handleMessage(MpResourceMsg &rMsg)
Handle messages for this resource.
Definition: MprRecorder.cpp:1301
unsigned long mRecordingBufferNotificationWatermark
Definition: MprRecorder.h:278
Generic audio resampler.
Definition: MpResampler.h:34
Definition: MpResourceMsg.h:56
int(MprRecorder::* WriteMethod)(const char *channelBuffers[], int)
Definition: MprRecorder.h:337
Definition: MprRecorder.h:237
MpResamplerBase * mpResampler
Resampler for encoding to file.
Definition: MprRecorder.h:272
virtual UtlBoolean handleDisable()
perform the disable operation on the resource
Definition: MprRecorder.cpp:1289
static int16_t getBytesPerSample(RecordFileFormat format)
Definition: MprRecorder.cpp:1906
int mConsecutiveInactive
Definition: MprRecorder.h:246
Definition: MprRecorder.h:204
UtlBoolean finish(FinishCause cause)
Recording has been stopped with given cause.
Definition: MprRecorder.cpp:1457
static int interlaceSamples(const char *samplesArrays[], int samplesPerChannel, int bytesPerSample, int channels, char *interlacedChannelSamplesArray, int interlacedArrayMaximum)
Definition: MprRecorder.cpp:1846
void trimSilenceFromEndOfRecording()
Definition: MprRecorder.cpp:1014
int mLastEncodedFrameSize
Size in bytes of last encoded frame recorded.
Definition: MprRecorder.h:264
void deleteOpusEncoder()
Definition: MprRecorder.cpp:952
int mSamplesPerSecond
Cache sample rate of last processed buffer.
Definition: MprRecorder.h:282
int writeFile(const char *channelData[], int dataSize)
Definition: MprRecorder.cpp:1796
Recording is not running.
Definition: MprRecorder.h:219
Definition: MprRecorder.h:236
Record to a file.
Definition: MprRecorder.h:228
Base class for all media processing encoders.
Definition: MpEncoderBase.h:35
CircularBufferPtr * mpCircularBuffer
Definition: MprRecorder.h:277
RecordDestination
Definition: MprRecorder.h:225
int mNumFramesProcessed
Definition: MprRecorder.h:244
void * mpOpusEncoder
Definition: MprRecorder.h:266
virtual ~MprRecorder()
Destructor.
Definition: MprRecorder.cpp:88
SampleInterlaceStage
Definition: MprRecorder.h:233
int samplesPerSecond
UtlBoolean handleStartCircularBuffer(CircularBufferPtr *buffer, RecordFileFormat recordingFormat, unsigned long recordingBufferNotificationWatermark, int numChannels)
Handle MPRM_START_CIRCULAR_BUFFER message.
Definition: MprRecorder.cpp:1253
static OsStatus startCircularBuffer(const UtlString &namedResource, OsMsgQ &fgQ, CircularBufferPtr &buffer, RecordFileFormat recordingFormat, unsigned long recordingBufferNotificationWatermark, int numChannels=1)
Start recording to a circular-buffer with given parameters.
Definition: MprRecorder.cpp:410
MprRecorder & operator=(const MprRecorder &rhs)
Assignment operator (not implemented for this class)
int writeBufferSpeech(const MpAudioSample *pBuffer, int numSamples)
Write given speech data to the buffer.
Definition: MprRecorder.cpp:1887
Recording has finished automatically.
Definition: MprRecorder.h:212
Definition: MprRecorder.h:235
UtlBoolean handleStartFile(int file, RecordFileFormat recFormat, int time, int silenceLength, UtlBoolean append, int numChannels)
Handle MPRM_START_FILE message.
Definition: MprRecorder.cpp:1169
int mBufferSize
mpBuffer size.
Definition: MprRecorder.h:259
Definition: MprRecorder.h:60
void createEncoder(const char *mimeSubtype, unsigned int codecSampleRate)
Definition: MprRecorder.cpp:681
int16_t MpAudioSample
Definition: MpTypes.h:44
virtual UtlBoolean doProcessFrame(MpBufPtr inBufs[], MpBufPtr outBufs[], int inBufsSize, int outBufsSize, UtlBoolean isEnabled, int samplesPerFrame, int samplesPerSecond)
This method does the real work for the media processing resource and must be defined in each class de...
Definition: MprRecorder.cpp:471
int mSilenceLength
Definition: MprRecorder.h:247
int writeSamples(const MpAudioSample *pBuffer[], int numSamples, WriteMethod writeMethod)
Write given data to the specified target.
Definition: MprRecorder.cpp:1598
State
Definition: MprRecorder.h:217
Record to a buffer.
Definition: MprRecorder.h:229
static OsStatus startFile(const UtlString &namedResource, OsMsgQ &fgQ, const char *filename, RecordFileFormat recFormat, int time=0, int silenceLength=-1, UtlBoolean append=FALSE, int numChannels=1)
Start recording to a file with given parameters.
Definition: MprRecorder.cpp:122
RecordFileFormat mRecFormat
Should data be written in WAV or RAW PCM format.
Definition: MprRecorder.h:252
static OsStatus startBuffer(const UtlString &namedResource, OsMsgQ &fgQ, MpAudioSample *pBuffer, int bufferSize, int time=0, int silenceLength=-1, int numChannels=1)
Start recording to a buffer with given parameters.
Definition: MprRecorder.cpp:382
int mSamplesPerLastFrame
Cache frame size of last processed buffer.
Definition: MprRecorder.h:281
Abstract base class for all audio processing objects.
Definition: MpAudioResource.h:38
Definition: MprRecorder.h:70
Recording is running, but paused.
Definition: MprRecorder.h:221
Definition: MprRecorder.h:227
RecordFileFormat
These match the WAVE compression format codes in the RIFF header for convenience. ...
Definition: MprRecorder.h:59
static OsStatus resume(const UtlString &namedResource, OsMsgQ &flowgraphQueue)
Resume recording if it is already recording.
Definition: MprRecorder.cpp:441
The "Recorder" media processing resource.
Definition: MprRecorder.h:53
Record to a buffer.
Definition: MprRecorder.h:230
OsStatus createOpusEncoder(int channels, const char *artist, const char *title)
Definition: MprRecorder.cpp:859
static OsStatus validateOpusHeader(int inFileFd, OpusHead &opusHeader)
int writeCircularBuffer(const char *channelData[], int dataSize)
Definition: MprRecorder.cpp:640
int writeBufferSilence(int numSamples)
Write silence to the buffer.
Definition: MprRecorder.cpp:1880
FinishCause
Definition: MprRecorder.h:210
Definition: MprRecorder.h:203
int mSamplesRecorded
Definition: MprRecorder.h:245
int writeCircularBufferSilence(int numSamples)
Write silence to the circular buffer.
Definition: MprRecorder.cpp:1894
Definition: MprRecorder.h:67
void * mpOpusComments
Definition: MprRecorder.h:267
Definition: MprRecorder.h:64
Recording has stopped because of error.
Definition: MprRecorder.h:214
void notifyCircularBufferWatermark()
Definition: MprRecorder.cpp:672
int writeFileSilence(int numSamples)
Write silence to the file.
Definition: MprRecorder.cpp:1586
AddlMsgTypes
Definition: MprRecorder.h:200
Definition: MprRecorder.h:65
RecordDestination mRecordDestination
Where to store recorded samples.
Definition: MprRecorder.h:241
static OsStatus readWaveHeader(int fileHandle, RecordFileFormat &format, uint16_t &samplesPerSecond, uint16_t &channels)
Read wave header info.
Definition: MprRecorder.cpp:1929
Smart pointer to MpBuf.
Definition: MpBuf.h:160
SampleInterlaceStage mWhenToInterlace
Definition: MprRecorder.h:270
int mEncodedFrames
number of audio (flowgraph) frames encoded
Definition: MprRecorder.h:263
static OsStatus pause(const UtlString &namedResource, OsMsgQ &flowgraphQueue)
Pause recording if it is already recording.
Definition: MprRecorder.cpp:435
int mChannels
Definition: MprRecorder.h:242
struct SipxOpusWriteObject * mpOpusStreamObject
Definition: MprRecorder.h:268
State mState
Internal recorder state.
Definition: MprRecorder.h:240
MpEncoderBase * mpEncoder
encoder for non-PCM formats saved to file
Definition: MprRecorder.h:262
static OsStatus stop(const UtlString &namedResource, OsMsgQ &fgQ)
Stop recording if it has not stopped automatically yet.
Definition: MprRecorder.cpp:447
MprRecorder(const UtlString &rName)
Constructor.
Definition: MprRecorder.cpp:58
Recording has stopped manually.
Definition: MprRecorder.h:213
static int getMaximumRecoderChannels()
Definition: MprRecorder.cpp:115
UtlBoolean handleStop()
Handle MPRM_STOP message.
Definition: MprRecorder.cpp:1283
void closeFile(const char *fromWhereLabel)
Close file if it is opened and update WAV header if needed.
Definition: MprRecorder.cpp:1503
MpAudioSample * mpBuffer
Definition: MprRecorder.h:257
#define FALSE
Definition: PlgDefsV1.h:37
static UtlBoolean writeWaveHeader(int handle, RecordFileFormat format, uint32_t samplesPerSecond=8000, int16_t numChannels=1)
Write out standard 16bit WAV Header.
Definition: MprRecorder.cpp:1981
int mFileDescriptor
File descriptor to write to.
Definition: MprRecorder.h:251
UtlBoolean handleStartBuffer(MpAudioSample *pBuffer, int bufferSize, int time, int silenceLength, int numChannels)
Handle MPRM_START_BUFFER message.
Definition: MprRecorder.cpp:1227
int mFramesToRecord
Definition: MprRecorder.h:243
Definition: MprRecorder.h:206
Definition: MprRecorder.h:66
UtlBoolean isEnabled() const
Returns TRUE is this resource is currently enabled, FALSE otherwise.
Definition: MpResource.cpp:440
Definition: MprRecorder.h:202