1 /*
2  * rtp2wav.cxx
3  *
4  * Open Phone Abstraction Library (OPAL)
5  * Formally known as the Open H323 project.
6  *
7  * Copyright (c) 2001 Equivalence Pty. Ltd.
8  *
9  * The contents of this file are subject to the Mozilla Public License
10  * Version 1.0 (the "License"); you may not use this file except in
11  * compliance with the License. You may obtain a copy of the License at
12  * http://www.mozilla.org/MPL/
13  *
14  * Software distributed under the License is distributed on an "AS IS"
15  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
16  * the License for the specific language governing rights and limitations
17  * under the License.
18  *
19  * The Original Code is Open Phone Abstraction Library.
20  *
21  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
22  *
23  * Contributor(s): ______________________________________.
24  *
25  * $Id$
26  *
27  */
28 
29 #include <ptlib.h>
30 
31 #ifdef __GNUC__
32 #pragma implementation "rtp2wav.h"
33 #endif
34 
35 #include "rtp2wav.h"
36 
37 
38 #define new PNEW
39 
40 
41 ///////////////////////////////////////////////////////////////////////////////
42 
OpalRtpToWavFile()43 OpalRtpToWavFile::OpalRtpToWavFile()
44 #ifdef _MSC_VER
45 #pragma warning(disable:4355)
46 #endif
47   : receiveHandler(PCREATE_NOTIFIER(ReceivedPacket))
48 #ifdef _MSC_VER
49 #pragma warning(default:4355)
50 #endif
51 {
52   payloadType = RTP_DataFrame::IllegalPayloadType;
53   lastPayloadSize = 0;
54 }
55 
56 
OpalRtpToWavFile(const PString & filename)57 OpalRtpToWavFile::OpalRtpToWavFile(const PString & filename)
58 #ifdef _MSC_VER
59 #pragma warning(disable:4355)
60 #endif
61   :  receiveHandler(PCREATE_NOTIFIER(ReceivedPacket))
62 #ifdef _MSC_VER
63 #pragma warning(default:4355)
64 #endif
65 {
66   SetFilePath(filename);
67   payloadType = RTP_DataFrame::IllegalPayloadType;
68   lastPayloadSize = 0;
69 }
70 
71 
OnFirstPacket(RTP_DataFrame & frame)72 PBoolean OpalRtpToWavFile::OnFirstPacket(RTP_DataFrame & frame)
73 {
74   static int SupportedTypes[] = {
75     PWAVFile::fmt_uLaw,
76     0, 0,
77     PWAVFile::fmt_GSM,
78     PWAVFile::fmt_VivoG7231,
79     0, 0, 0,
80     PWAVFile::fmt_ALaw,
81     0, 0,
82     PWAVFile::fmt_PCM
83   };
84 
85   payloadType = frame.GetPayloadType();
86 
87   if (payloadType >= PARRAYSIZE(SupportedTypes) || SupportedTypes[payloadType] == 0) {
88     PTRACE(1, "rtp2wav\tUnsupported payload type: " << payloadType);
89     return FALSE;
90   }
91 
92   if (!SetFormat(SupportedTypes[payloadType])) {
93     PTRACE(1, "rtp2wav\tCould not set WAV file format: " << SupportedTypes[payloadType]);
94     return FALSE;
95   }
96 
97   if (!Open(PFile::WriteOnly)) {
98     PTRACE(1, "rtp2wav\tCould not open WAV file: " << GetErrorText());
99     return FALSE;
100   }
101 
102   PTRACE(3, "rtp2wav\tStarted recording payload type " << payloadType
103          << " to " << GetFilePath());
104   return TRUE;
105 }
106 
107 
ReceivedPacket(RTP_DataFrame & frame,H323_INT)108 void OpalRtpToWavFile::ReceivedPacket(RTP_DataFrame & frame,  H323_INT)
109 {
110   PINDEX payloadSize = frame.GetPayloadSize();
111 
112   if (payloadType == RTP_DataFrame::IllegalPayloadType) {
113     // Ignore packets until actually get something of jitter buffer
114     if (payloadSize == 0)
115       return;
116     if (!OnFirstPacket(frame))
117       return;
118   }
119 
120   if (payloadType != frame.GetPayloadType())
121     return;
122 
123   if (!IsOpen())
124     return;
125 
126   if (payloadSize > 0) {
127     if (Write(frame.GetPayloadPtr(), payloadSize)) {
128       lastPayloadSize = payloadSize;
129       memcpy(lastFrame.GetPointer(lastPayloadSize), frame.GetPayloadPtr(), payloadSize);
130       return;
131     }
132   }
133 
134   else if (lastPayloadSize == 0)
135     return;
136 
137   else if (Write(lastFrame.GetPointer(), lastPayloadSize))
138     return;
139 
140   PTRACE(1, "rtp2wav\tError writing to WAV file: " << GetErrorText(PChannel::LastWriteError));
141   Close();
142 }
143 
144 
145 /////////////////////////////////////////////////////////////////////////////
146