~ubuntu-branches/debian/experimental/kopete/experimental

« back to all changes in this revision

Viewing changes to protocols/jabber/googletalk/libjingle/talk/session/phone/rtpdump.h

  • Committer: Package Import Robot
  • Author(s): Maximiliano Curia
  • Date: 2015-02-24 11:32:57 UTC
  • mfrom: (1.1.41 vivid)
  • Revision ID: package-import@ubuntu.com-20150224113257-gnupg4v7lzz18ij0
Tags: 4:14.12.2-1
* New upstream release (14.12.2).
* Bump Standards-Version to 3.9.6, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * libjingle
3
 
 * Copyright 2010, Google Inc.
4
 
 *
5
 
 * Redistribution and use in source and binary forms, with or without
6
 
 * modification, are permitted provided that the following conditions are met:
7
 
 *
8
 
 *  1. Redistributions of source code must retain the above copyright notice,
9
 
 *     this list of conditions and the following disclaimer.
10
 
 *  2. Redistributions in binary form must reproduce the above copyright notice,
11
 
 *     this list of conditions and the following disclaimer in the documentation
12
 
 *     and/or other materials provided with the distribution.
13
 
 *  3. The name of the author may not be used to endorse or promote products
14
 
 *     derived from this software without specific prior written permission.
15
 
 *
16
 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17
 
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18
 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19
 
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20
 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
 
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22
 
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23
 
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24
 
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25
 
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 
 */
27
 
 
28
 
#ifndef TALK_SESSION_PHONE_RTPDUMP_H_
29
 
#define TALK_SESSION_PHONE_RTPDUMP_H_
30
 
 
31
 
#include <cstring>
32
 
#include <string>
33
 
#include <vector>
34
 
 
35
 
#include "talk/base/basictypes.h"
36
 
#include "talk/base/bytebuffer.h"
37
 
#include "talk/base/stream.h"
38
 
 
39
 
namespace cricket {
40
 
 
41
 
// We use the RTP dump file format compatible to the format used by rtptools
42
 
// (http://www.cs.columbia.edu/irt/software/rtptools/) and Wireshark
43
 
// (http://wiki.wireshark.org/rtpdump). In particular, the file starts with the
44
 
// first line "#!rtpplay1.0 address/port\n", followed by a 16 byte file header.
45
 
// For each packet, the file contains a 8 byte dump packet header, followed by
46
 
// the actual RTP or RTCP packet.
47
 
 
48
 
enum RtpDumpPacketFilter {
49
 
  PF_NONE = 0x0,
50
 
  PF_RTPHEADER = 0x1,
51
 
  PF_RTPPACKET = 0x3,  // includes header
52
 
  // PF_RTCPHEADER = 0x4,  // TODO
53
 
  PF_RTCPPACKET = 0xC,  // includes header
54
 
  PF_ALL = 0xF
55
 
};
56
 
 
57
 
struct RtpDumpFileHeader {
58
 
  RtpDumpFileHeader(uint32 start_ms, uint32 s, uint16 p);
59
 
  void WriteToByteBuffer(talk_base::ByteBuffer* buf);
60
 
 
61
 
  static const char kFirstLine[];
62
 
  static const size_t kHeaderLength = 16;
63
 
  uint32 start_sec;   // start of recording, the seconds part.
64
 
  uint32 start_usec;  // start of recording, the microseconds part.
65
 
  uint32 source;      // network source (multicast address).
66
 
  uint16 port;        // UDP port.
67
 
  uint16 padding;     // 2 bytes padding.
68
 
};
69
 
 
70
 
struct RtpDumpPacket {
71
 
  RtpDumpPacket() {}
72
 
 
73
 
  RtpDumpPacket(const void* d, size_t s, uint32 elapsed, bool rtcp)
74
 
      : elapsed_time(elapsed),
75
 
        is_rtcp(rtcp) {
76
 
    data.resize(s);
77
 
    memcpy(&data[0], d, s);
78
 
  }
79
 
 
80
 
  bool IsValidRtpPacket() const;
81
 
  bool IsValidRtcpPacket() const;
82
 
  // Get the payload type, sequence number, timestampe, and SSRC of the RTP
83
 
  // packet. Return true and set the output parameter if successful.
84
 
  bool GetRtpPayloadType(int* pt) const;
85
 
  bool GetRtpSeqNum(int* seq_num) const;
86
 
  bool GetRtpTimestamp(uint32* ts) const;
87
 
  bool GetRtpSsrc(uint32* ssrc) const;
88
 
  bool GetRtpHeaderLen(size_t* len) const;
89
 
  // Get the type of the RTCP packet. Return true and set the output parameter
90
 
  // if successful.
91
 
  bool GetRtcpType(int* type) const;
92
 
 
93
 
  static const size_t kHeaderLength = 8;
94
 
  uint32 elapsed_time;      // Milliseconds since the start of recording.
95
 
  bool is_rtcp;             // True if the data below is a RTCP packet.
96
 
  std::vector<uint8> data;  // The actual RTP or RTCP packet.
97
 
};
98
 
 
99
 
class RtpDumpReader {
100
 
 public:
101
 
  explicit RtpDumpReader(talk_base::StreamInterface* stream)
102
 
      : stream_(stream),
103
 
        file_header_read_(false),
104
 
        first_line_and_file_header_len_(0),
105
 
        start_time_ms_(0) {
106
 
  }
107
 
  virtual ~RtpDumpReader() {}
108
 
 
109
 
  // Use the specified ssrc, rather than the ssrc from dump, for RTP packets.
110
 
  void SetSsrc(uint32 ssrc);
111
 
  virtual talk_base::StreamResult ReadPacket(RtpDumpPacket* packet);
112
 
 
113
 
 protected:
114
 
  talk_base::StreamResult ReadFileHeader();
115
 
  bool RewindToFirstDumpPacket() {
116
 
    return stream_->SetPosition(first_line_and_file_header_len_);
117
 
  }
118
 
 
119
 
 private:
120
 
  // Check if its matches "#!rtpplay1.0 address/port\n".
121
 
  bool CheckFirstLine(const std::string& first_line);
122
 
 
123
 
  talk_base::StreamInterface* stream_;
124
 
  bool file_header_read_;
125
 
  size_t first_line_and_file_header_len_;
126
 
  uint32 start_time_ms_;
127
 
  talk_base::ByteBuffer ssrc_buffer_;
128
 
 
129
 
  DISALLOW_COPY_AND_ASSIGN(RtpDumpReader);
130
 
};
131
 
 
132
 
// RtpDumpLoopReader reads RTP dump packets from the input stream and rewinds
133
 
// the stream when it ends. RtpDumpLoopReader maintains the elapsed time, the
134
 
// RTP sequence number and the RTP timestamp properly. RtpDumpLoopReader can
135
 
// handle both RTP dump and RTCP dump. We assume that the dump does not mix
136
 
// RTP packets and RTCP packets.
137
 
class RtpDumpLoopReader : public RtpDumpReader {
138
 
 public:
139
 
  explicit RtpDumpLoopReader(talk_base::StreamInterface* stream);
140
 
  virtual talk_base::StreamResult ReadPacket(RtpDumpPacket* packet);
141
 
 
142
 
 private:
143
 
  // During the first loop, update the statistics, including packet count, frame
144
 
  // count, timestamps, and sequence number, of the input stream.
145
 
  void UpdateStreamStatistics(const RtpDumpPacket& packet);
146
 
 
147
 
  // At the end of first loop, calculate elapsed_time_increases_,
148
 
  // rtp_seq_num_increase_, and rtp_timestamp_increase_.
149
 
  void CalculateIncreases();
150
 
 
151
 
  // During the second and later loops, update the elapsed time of the dump
152
 
  // packet. If the dumped packet is a RTP packet, update its RTP sequence
153
 
  // number and timestamp as well.
154
 
  void UpdateDumpPacket(RtpDumpPacket* packet);
155
 
 
156
 
  int loop_count_;
157
 
  // How much to increase the elapsed time, RTP sequence number, RTP timestampe
158
 
  // for each loop. They are calcualted with the variables below during the
159
 
  // first loop.
160
 
  uint32 elapsed_time_increases_;
161
 
  int rtp_seq_num_increase_;
162
 
  uint32 rtp_timestamp_increase_;
163
 
  // How many RTP packets and how many payload frames in the input stream. RTP
164
 
  // packets belong to the same frame have the same RTP timestamp, different
165
 
  // dump timestamp, and different RTP sequence number.
166
 
  uint32 packet_count_;
167
 
  uint32 frame_count_;
168
 
  // The elapsed time, RTP sequence number, and RTP timestamp of the first and
169
 
  // the previous dump packets in the input stream.
170
 
  uint32 first_elapsed_time_;
171
 
  int first_rtp_seq_num_;
172
 
  uint32 first_rtp_timestamp_;
173
 
  uint32 prev_elapsed_time_;
174
 
  int prev_rtp_seq_num_;
175
 
  uint32 prev_rtp_timestamp_;
176
 
 
177
 
  DISALLOW_COPY_AND_ASSIGN(RtpDumpLoopReader);
178
 
};
179
 
 
180
 
class RtpDumpWriter {
181
 
 public:
182
 
  explicit RtpDumpWriter(talk_base::StreamInterface* stream);
183
 
 
184
 
  // Filter to control what packets we actually record.
185
 
  void set_packet_filter(int filter);
186
 
  // Write a RTP or RTCP packet. The parameters data points to the packet and
187
 
  // data_len is its length.
188
 
  talk_base::StreamResult WriteRtpPacket(const void* data, size_t data_len) {
189
 
    return WritePacket(data, data_len, GetElapsedTime(), false);
190
 
  }
191
 
  talk_base::StreamResult WriteRtcpPacket(const void* data, size_t data_len) {
192
 
    return WritePacket(data, data_len, GetElapsedTime(), true);
193
 
  }
194
 
  talk_base::StreamResult WritePacket(const RtpDumpPacket& packet) {
195
 
    return WritePacket(&packet.data[0], packet.data.size(), packet.elapsed_time,
196
 
                       packet.is_rtcp);
197
 
  }
198
 
  uint32 GetElapsedTime() const;
199
 
 
200
 
  bool GetDumpSize(size_t* size) {
201
 
    // Note that we use GetPosition(), rather than GetSize(), to avoid flush the
202
 
    // stream per write.
203
 
    return stream_ && size && stream_->GetPosition(size);
204
 
  }
205
 
 
206
 
 protected:
207
 
  talk_base::StreamResult WriteFileHeader();
208
 
 
209
 
 private:
210
 
  talk_base::StreamResult WritePacket(const void* data, size_t data_len,
211
 
                                      uint32 elapsed, bool rtcp);
212
 
  size_t FilterPacket(const void* data, size_t data_len, bool rtcp);
213
 
 
214
 
  talk_base::StreamInterface* stream_;
215
 
  int packet_filter_;
216
 
  bool file_header_written_;
217
 
  uint32 start_time_ms_;  // Time when the record starts.
218
 
  DISALLOW_COPY_AND_ASSIGN(RtpDumpWriter);
219
 
};
220
 
 
221
 
}  // namespace cricket
222
 
 
223
 
#endif  // TALK_SESSION_PHONE_RTPDUMP_H_