blob: 1f8715dfa504cc74ca0431f8caec5ef5af72aff6 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
xians@webrtc.org6bde7a82012-02-20 08:39:25 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
pbos@webrtc.org8b062002013-07-12 08:28:10 +000011#include "webrtc/modules/utility/source/rtp_dump_impl.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <assert.h>
niklase@google.com470e71d2011-07-07 08:21:25 +000014#include <stdio.h>
15
pbos@webrtc.org8b062002013-07-12 08:28:10 +000016#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
17#include "webrtc/system_wrappers/interface/trace.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000018
19#if defined(_WIN32)
20#include <Windows.h>
21#include <mmsystem.h>
22#elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC)
23#include <string.h>
24#include <sys/time.h>
25#include <time.h>
26#endif
27
28#if (defined(_DEBUG) && defined(_WIN32))
29#define DEBUG_PRINT(expr) OutputDebugString(##expr)
30#define DEBUG_PRINTP(expr, p) \
31{ \
32 char msg[128]; \
33 sprintf(msg, ##expr, p); \
34 OutputDebugString(msg); \
35}
36#else
37#define DEBUG_PRINT(expr) ((void)0)
38#define DEBUG_PRINTP(expr,p) ((void)0)
39#endif // defined(_DEBUG) && defined(_WIN32)
40
41namespace webrtc {
leozwang@webrtc.org2559cbf2012-02-27 19:18:25 +000042const char RTPFILE_VERSION[] = "1.0";
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000043const uint32_t MAX_UWORD32 = 0xffffffff;
niklase@google.com470e71d2011-07-07 08:21:25 +000044
45// This stucture is specified in the rtpdump documentation.
46// This struct corresponds to RD_packet_t in
47// http://www.cs.columbia.edu/irt/software/rtptools/
48typedef struct
49{
50 // Length of packet, including this header (may be smaller than plen if not
51 // whole packet recorded).
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000052 uint16_t length;
niklase@google.com470e71d2011-07-07 08:21:25 +000053 // Actual header+payload length for RTP, 0 for RTCP.
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000054 uint16_t plen;
niklase@google.com470e71d2011-07-07 08:21:25 +000055 // Milliseconds since the start of recording.
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000056 uint32_t offset;
niklase@google.com470e71d2011-07-07 08:21:25 +000057} rtpDumpPktHdr_t;
58
59RtpDump* RtpDump::CreateRtpDump()
60{
niklase@google.com470e71d2011-07-07 08:21:25 +000061 return new RtpDumpImpl();
62}
63
64void RtpDump::DestroyRtpDump(RtpDump* object)
65{
niklase@google.com470e71d2011-07-07 08:21:25 +000066 delete object;
67}
68
69RtpDumpImpl::RtpDumpImpl()
henrike@webrtc.org105e0712011-12-16 19:53:46 +000070 : _critSect(CriticalSectionWrapper::CreateCriticalSection()),
niklase@google.com470e71d2011-07-07 08:21:25 +000071 _file(*FileWrapper::Create()),
72 _startTime(0)
73{
74 WEBRTC_TRACE(kTraceMemory, kTraceUtility, -1, "%s created", __FUNCTION__);
75}
76
77RtpDump::~RtpDump()
78{
79}
80
81RtpDumpImpl::~RtpDumpImpl()
82{
83 _file.Flush();
84 _file.CloseFile();
85 delete &_file;
henrike@webrtc.org105e0712011-12-16 19:53:46 +000086 delete _critSect;
niklase@google.com470e71d2011-07-07 08:21:25 +000087 WEBRTC_TRACE(kTraceMemory, kTraceUtility, -1, "%s deleted", __FUNCTION__);
88}
89
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +000090int32_t RtpDumpImpl::Start(const char* fileNameUTF8)
niklase@google.com470e71d2011-07-07 08:21:25 +000091{
niklase@google.com470e71d2011-07-07 08:21:25 +000092
93 if (fileNameUTF8 == NULL)
94 {
95 return -1;
96 }
97
98 CriticalSectionScoped lock(_critSect);
99 _file.Flush();
100 _file.CloseFile();
101 if (_file.OpenFile(fileNameUTF8, false, false, false) == -1)
102 {
103 WEBRTC_TRACE(kTraceError, kTraceUtility, -1,
104 "failed to open the specified file");
105 return -1;
106 }
107
108 // Store start of RTP dump (to be used for offset calculation later).
109 _startTime = GetTimeInMS();
110
111 // All rtp dump files start with #!rtpplay.
leozwang@webrtc.org2559cbf2012-02-27 19:18:25 +0000112 char magic[16];
niklase@google.com470e71d2011-07-07 08:21:25 +0000113 sprintf(magic, "#!rtpplay%s \n", RTPFILE_VERSION);
andrew@webrtc.org5ae19de2011-12-13 22:59:33 +0000114 if (_file.WriteText(magic) == -1)
115 {
116 WEBRTC_TRACE(kTraceError, kTraceUtility, -1,
117 "error writing to file");
118 return -1;
119 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000120
121 // The header according to the rtpdump documentation is sizeof(RD_hdr_t)
122 // which is 8 + 4 + 2 = 14 bytes for 32-bit architecture (and 22 bytes on
123 // 64-bit architecture). However, Wireshark use 16 bytes for the header
124 // regardless of if the binary is 32-bit or 64-bit. Go by the same approach
125 // as Wireshark since it makes more sense.
126 // http://wiki.wireshark.org/rtpdump explains that an additional 2 bytes
127 // of padding should be added to the header.
leozwang@webrtc.org2559cbf2012-02-27 19:18:25 +0000128 char dummyHdr[16];
niklase@google.com470e71d2011-07-07 08:21:25 +0000129 memset(dummyHdr, 0, 16);
andrew@webrtc.org5ae19de2011-12-13 22:59:33 +0000130 if (!_file.Write(dummyHdr, sizeof(dummyHdr)))
131 {
132 WEBRTC_TRACE(kTraceError, kTraceUtility, -1,
133 "error writing to file");
134 return -1;
135 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000136 return 0;
137}
138
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000139int32_t RtpDumpImpl::Stop()
niklase@google.com470e71d2011-07-07 08:21:25 +0000140{
niklase@google.com470e71d2011-07-07 08:21:25 +0000141 CriticalSectionScoped lock(_critSect);
142 _file.Flush();
143 _file.CloseFile();
144 return 0;
145}
146
147bool RtpDumpImpl::IsActive() const
148{
149 CriticalSectionScoped lock(_critSect);
150 return _file.Open();
151}
152
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000153int32_t RtpDumpImpl::DumpPacket(const uint8_t* packet, uint16_t packetLength)
niklase@google.com470e71d2011-07-07 08:21:25 +0000154{
155 CriticalSectionScoped lock(_critSect);
156 if (!IsActive())
157 {
158 return 0;
159 }
160
161 if (packet == NULL)
162 {
163 return -1;
164 }
165
166 if (packetLength < 1)
167 {
168 return -1;
169 }
170
171 // If the packet doesn't contain a valid RTCP header the packet will be
172 // considered RTP (without further verification).
173 bool isRTCP = RTCP(packet);
174
175 rtpDumpPktHdr_t hdr;
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000176 uint32_t offset;
niklase@google.com470e71d2011-07-07 08:21:25 +0000177
178 // Offset is relative to when recording was started.
179 offset = GetTimeInMS();
180 if (offset < _startTime)
181 {
182 // Compensate for wraparound.
183 offset += MAX_UWORD32 - _startTime + 1;
184 } else {
185 offset -= _startTime;
186 }
187 hdr.offset = RtpDumpHtonl(offset);
188
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000189 hdr.length = RtpDumpHtons((uint16_t)(packetLength + sizeof(hdr)));
niklase@google.com470e71d2011-07-07 08:21:25 +0000190 if (isRTCP)
191 {
192 hdr.plen = 0;
193 }
194 else
195 {
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000196 hdr.plen = RtpDumpHtons((uint16_t)packetLength);
niklase@google.com470e71d2011-07-07 08:21:25 +0000197 }
andrew@webrtc.org5ae19de2011-12-13 22:59:33 +0000198
199 if (!_file.Write(&hdr, sizeof(hdr)))
200 {
201 WEBRTC_TRACE(kTraceError, kTraceUtility, -1,
202 "error writing to file");
203 return -1;
204 }
205 if (!_file.Write(packet, packetLength))
206 {
207 WEBRTC_TRACE(kTraceError, kTraceUtility, -1,
208 "error writing to file");
209 return -1;
210 }
211
niklase@google.com470e71d2011-07-07 08:21:25 +0000212 return 0;
213}
214
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000215bool RtpDumpImpl::RTCP(const uint8_t* packet) const
niklase@google.com470e71d2011-07-07 08:21:25 +0000216{
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000217 const uint8_t payloadType = packet[1];
niklase@google.com470e71d2011-07-07 08:21:25 +0000218 bool is_rtcp = false;
219
220 switch(payloadType)
221 {
222 case 192:
223 is_rtcp = true;
224 break;
225 case 193: case 195:
226 break;
227 case 200: case 201: case 202: case 203:
228 case 204: case 205: case 206: case 207:
229 is_rtcp = true;
230 break;
231 }
232 return is_rtcp;
233}
234
235// TODO (hellner): why is TickUtil not used here?
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000236inline uint32_t RtpDumpImpl::GetTimeInMS() const
niklase@google.com470e71d2011-07-07 08:21:25 +0000237{
238#if defined(_WIN32)
239 return timeGetTime();
240#elif defined(WEBRTC_LINUX) || defined(WEBRTC_MAC)
241 struct timeval tv;
242 struct timezone tz;
243 unsigned long val;
244
245 gettimeofday(&tv, &tz);
246 val = tv.tv_sec * 1000 + tv.tv_usec / 1000;
247 return val;
248#else
249 #error Either _WIN32 or LINUX or WEBRTC_MAC has to be defined!
250 assert(false);
251 return 0;
252#endif
253}
254
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000255inline uint32_t RtpDumpImpl::RtpDumpHtonl(uint32_t x) const
niklase@google.com470e71d2011-07-07 08:21:25 +0000256{
257#if defined(WEBRTC_BIG_ENDIAN)
258 return x;
259#elif defined(WEBRTC_LITTLE_ENDIAN)
260 return (x >> 24) + ((((x >> 16) & 0xFF) << 8) + ((((x >> 8) & 0xFF) << 16) +
261 ((x & 0xFF) << 24)));
262#else
263#error Either WEBRTC_BIG_ENDIAN or WEBRTC_LITTLE_ENDIAN has to be defined!
264 assert(false);
265 return 0;
266#endif
267}
268
pbos@webrtc.orgc75102e2013-04-09 13:32:55 +0000269inline uint16_t RtpDumpImpl::RtpDumpHtons(uint16_t x) const
niklase@google.com470e71d2011-07-07 08:21:25 +0000270{
271#if defined(WEBRTC_BIG_ENDIAN)
272 return x;
273#elif defined(WEBRTC_LITTLE_ENDIAN)
274 return (x >> 8) + ((x & 0xFF) << 8);
275#else
276 #error Either WEBRTC_BIG_ENDIAN or WEBRTC_LITTLE_ENDIAN has to be defined!
277 assert(false);
278 return 0;
279#endif
280}
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000281} // namespace webrtc