Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(42)

Side by Side Diff: chrome/browser/media/webrtc_rtp_dump_writer.cc

Issue 264793017: Implements RTP header dumping. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/media/webrtc_rtp_dump_writer.h"
6
7 #include "base/big_endian.h"
8 #include "base/file_util.h"
9 #include "base/logging.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "third_party/zlib/zlib.h"
12
13 using content::BrowserThread;
14
15 namespace {
16
17 // The header of the dump file.
18 struct RtpDumpFileHeader {
19 static const unsigned char kFirstLine[];
20
21 explicit RtpDumpFileHeader(const base::TimeTicks& start)
22 : start_sec(0), start_usec(0), source(0), port(0), padding(0) {
23 base::TimeDelta interval(start - base::TimeTicks());
24 start_sec = interval.InSeconds();
25 start_usec =
26 interval.InMilliseconds() * base::Time::kMicrosecondsPerMillisecond;
27 }
28
29 void WriteBigEndian(std::vector<uint8>* output) {
30 size_t buffer_start_pos = output->size();
31 output->resize(output->size() + sizeof(RtpDumpFileHeader));
32
33 char* buffer = reinterpret_cast<char*>(output->data() + buffer_start_pos);
34
35 base::WriteBigEndian(buffer, start_sec);
36 buffer += sizeof(start_sec);
37
38 base::WriteBigEndian(buffer, start_usec);
39 buffer += sizeof(start_usec);
40
41 base::WriteBigEndian(buffer, source);
42 buffer += sizeof(source);
43
44 base::WriteBigEndian(buffer, port);
45 buffer += sizeof(port);
46
47 base::WriteBigEndian(buffer, padding);
48 }
49
50 uint32 start_sec; // start of recording, the seconds part.
51 uint32 start_usec; // start of recording, the microseconds part.
52 uint32 source; // network source (multicast address). Always 0.
53 uint16 port; // UDP port. Always 0.
54 uint16 padding; // 2 bytes padding.
55 };
56
57 const unsigned char RtpDumpFileHeader::kFirstLine[] =
58 "#!rtpplay1.0 0.0.0.0/0\n";
59
60 // The header for each packet dump.
61 struct PacketDumpHeader {
62 PacketDumpHeader(const base::TimeTicks& start,
63 uint16 dump_length,
64 uint16 packet_length)
65 : packet_dump_length(dump_length),
66 packet_length(packet_length),
67 offset_ms((base::TimeTicks::Now() - start).InMilliseconds()) {}
68
69 void WriteBigEndian(std::vector<uint8>* output) {
70 size_t buffer_start_pos = output->size();
71 output->resize(output->size() + sizeof(PacketDumpHeader));
72
73 char* buffer = reinterpret_cast<char*>(output->data() + buffer_start_pos);
74
75 base::WriteBigEndian(buffer, packet_dump_length);
76 buffer += sizeof(packet_dump_length);
77
78 base::WriteBigEndian(buffer, packet_length);
79 buffer += sizeof(packet_length);
80
81 base::WriteBigEndian(buffer, offset_ms);
82 }
83
84 // Length of the packet dump including this header.
85 uint16 packet_dump_length;
86
87 // Length of header + payload of the RTP packet.
88 uint16 packet_length;
89
90 // Milliseconds since the start of recording.
91 uint32 offset_ms;
92 };
93
94 // Append |src_len| bytes from |src| to |dest|.
95 bool AppendToBuffer(const uint8* src,
96 size_t src_len,
97 std::vector<uint8>* dest) {
98 if (dest->capacity() < dest->size() + src_len)
99 return false;
100
101 for (size_t i = 0; i < src_len; ++i) {
102 dest->push_back(src[i]);
103 }
104 return true;
105 }
106
107 static const size_t kMinimumGzipOutputBufferSize = 256;
108
109 } // namespace
110
111 // This class is running on the FILE thread for compressing and writing the
112 // dump buffer to disk.
113 class WebRtcRtpDumpWriter::FileThreadWorker {
114 public:
115 FileThreadWorker(const base::FilePath& incoming_dump_path,
116 const base::FilePath& outgoing_dump_path,
117 size_t max_dump_size)
118 : incoming_dump_path_(incoming_dump_path),
119 outgoing_dump_path_(outgoing_dump_path),
120 max_dump_size_(max_dump_size),
121 total_dump_size_(0),
122 incoming_stream_initialized_(false),
123 outgoing_stream_initialized_(false) {
124 thread_checker_.DetachFromThread();
125 }
126
127 ~FileThreadWorker() { DCHECK(thread_checker_.CalledOnValidThread()); }
128
129 // Compresses the data in |incoming_buffer| and |outgoing_buffer| and write
130 // to the dump file. If |end_stream| is true, both the incoming and outgoing
131 // compression streams will be ended and the dump file cannot be written to
132 // any more.
133 void CompressAndWriteToFileOnFileThread(
134 scoped_ptr<std::vector<uint8> > incoming_buffer,
135 scoped_ptr<std::vector<uint8> > outgoing_buffer,
136 bool end_stream,
137 FlushResult* result) {
138 DCHECK(thread_checker_.CalledOnValidThread());
139
140 *result = FLUSH_RESULT_SUCCESS;
141
142 if (incoming_buffer->size())
143 CompressAndWriteBufferToFile(incoming_buffer.Pass(), true, result);
144
145 if (*result != FLUSH_RESULT_FAILURE && outgoing_buffer->size())
146 CompressAndWriteBufferToFile(outgoing_buffer.Pass(), false, result);
147
148 if (end_stream) {
149 bool succeeded = false;
150
151 succeeded = EndDumpFile(
152 incoming_dump_path_, incoming_stream_initialized_, &incoming_stream_);
153
154 succeeded = succeeded && EndDumpFile(outgoing_dump_path_,
155 outgoing_stream_initialized_,
156 &outgoing_stream_);
157
158 if (!succeeded)
159 *result = FLUSH_RESULT_FAILURE;
160 }
161 }
162
163 private:
164 // Helper for CompressAndWriteToFileOnFileThread to compress and write one
165 // dump.
166 void CompressAndWriteBufferToFile(scoped_ptr<std::vector<uint8> > buffer,
167 bool incoming,
168 FlushResult* result) {
169 DCHECK(thread_checker_.CalledOnValidThread());
170 DCHECK(buffer->size());
171
172 *result = FLUSH_RESULT_SUCCESS;
173
174 base::FilePath* file_path =
175 incoming ? &incoming_dump_path_ : &outgoing_dump_path_;
176
177 std::vector<uint8> compressed_buffer;
178 if (!Compress(buffer.get(), incoming, &compressed_buffer)) {
179 DVLOG(2) << "Compressing buffer failed.";
180 *result = FLUSH_RESULT_FAILURE;
181 return;
182 }
183
184 int bytes_written = -1;
185
186 if (base::PathExists(*file_path)) {
187 bytes_written = base::AppendToFile(
188 *file_path,
189 reinterpret_cast<const char*>(compressed_buffer.data()),
190 compressed_buffer.size());
191 } else {
192 bytes_written = base::WriteFile(
193 *file_path,
194 reinterpret_cast<const char*>(compressed_buffer.data()),
195 compressed_buffer.size());
196 }
197
198 if (bytes_written == -1) {
199 DVLOG(2) << "Writing file failed: " << file_path->value();
200 *result = FLUSH_RESULT_FAILURE;
201 return;
202 }
203
204 DCHECK_EQ(static_cast<size_t>(bytes_written), compressed_buffer.size());
205
206 total_dump_size_ += bytes_written;
207
208 if (total_dump_size_ > max_dump_size_)
209 *result = FLUSH_RESULT_MAX_SIZE_REACHED;
210 }
211
212 // Compresses |input| into |output| using the incoming or outgoing compression
213 // stream depending on |incoming|.
214 bool Compress(std::vector<uint8>* input,
215 bool incoming,
216 std::vector<uint8>* output) {
217 DCHECK(thread_checker_.CalledOnValidThread());
218 int result = Z_OK;
219
220 output->resize(std::max(kMinimumGzipOutputBufferSize, input->size()));
221
222 z_stream* stream = incoming ? &incoming_stream_ : &outgoing_stream_;
223
224 bool* stream_initialized = incoming ? &incoming_stream_initialized_
225 : &outgoing_stream_initialized_;
226
227 if (!(*stream_initialized)) {
228 memset(stream, 0, sizeof(*stream));
229 result = deflateInit2(stream,
230 Z_DEFAULT_COMPRESSION,
231 Z_DEFLATED,
232 // windowBits = 15 is default, 16 is added to
233 // produce a gzip header + trailer.
234 15 + 16,
235 8, // memLevel = 8 is default.
236 Z_DEFAULT_STRATEGY);
237 DCHECK_EQ(Z_OK, result);
238 *stream_initialized = true;
239 }
240
241 stream->next_in = input->data();
242 stream->avail_in = input->size();
243 stream->next_out = output->data();
244 stream->avail_out = output->size();
245
246 result = deflate(stream, Z_SYNC_FLUSH);
247 DCHECK_EQ(Z_OK, result);
248 DCHECK_EQ(0U, stream->avail_in);
249
250 output->resize(output->size() - stream->avail_out);
251
252 stream->next_in = NULL;
253 stream->next_out = NULL;
254 stream->avail_out = 0;
255 return true;
256 }
257
258 // Ends the compression stream and completes the dump file specified by
259 // |dump_path|.
260 bool EndDumpFile(const base::FilePath& dump_path,
261 bool stream_initialized,
262 z_stream* stream) {
263 DCHECK(thread_checker_.CalledOnValidThread());
264
265 if (!stream_initialized)
266 return true;
267
268 std::vector<uint8> output_buffer;
269 output_buffer.resize(kMinimumGzipOutputBufferSize);
270
271 stream->next_in = NULL;
272 stream->avail_in = 0;
273 stream->next_out = output_buffer.data();
274 stream->avail_out = output_buffer.size();
275
276 int result = deflate(stream, Z_FINISH);
277 DCHECK_EQ(Z_STREAM_END, result);
278
279 result = deflateEnd(stream);
280 DCHECK_EQ(Z_OK, result);
281
282 output_buffer.resize(output_buffer.size() - stream->avail_out);
283
284 int bytes_written =
285 base::AppendToFile(dump_path,
286 reinterpret_cast<const char*>(output_buffer.data()),
287 output_buffer.size());
288
289 return bytes_written > 0;
290 }
291
292 base::FilePath incoming_dump_path_;
293 base::FilePath outgoing_dump_path_;
294
295 size_t max_dump_size_;
296 size_t total_dump_size_;
297
298 z_stream incoming_stream_;
299 z_stream outgoing_stream_;
300 bool incoming_stream_initialized_;
301 bool outgoing_stream_initialized_;
302
303 base::ThreadChecker thread_checker_;
304
305 DISALLOW_COPY_AND_ASSIGN(FileThreadWorker);
306 };
307
308 WebRtcRtpDumpWriter::WebRtcRtpDumpWriter(
309 const base::FilePath& incoming_dump_path,
310 const base::FilePath& outgoing_dump_path,
311 size_t max_dump_size,
312 const base::Closure& max_dump_size_reached_callback)
313 : max_dump_size_(max_dump_size),
314 max_dump_size_reached_callback_(max_dump_size_reached_callback),
315 file_thread_worker_(new FileThreadWorker(incoming_dump_path,
316 outgoing_dump_path,
317 max_dump_size)),
318 weak_ptr_factory_(this) {
319 }
320
321 WebRtcRtpDumpWriter::~WebRtcRtpDumpWriter() {
322 DCHECK(thread_checker_.CalledOnValidThread());
323 if (BrowserThread::DeleteSoon(
324 BrowserThread::FILE, FROM_HERE, file_thread_worker_.get())) {
325 ignore_result(file_thread_worker_.release());
326 } else {
327 file_thread_worker_.reset();
328 }
329 }
330
331 void WebRtcRtpDumpWriter::WriteRtpPacket(const uint8* packet_header,
332 size_t header_length,
333 size_t packet_length,
334 bool incoming) {
335 DCHECK(thread_checker_.CalledOnValidThread());
336
337 static const size_t kMaxInMemoryBufferSize = 65536; // 64KB
338
339 std::vector<uint8>* dest_buffer =
340 incoming ? &incoming_buffer_ : &outgoing_buffer_;
341 bool succeeded = true;
342 if (!dest_buffer->capacity()) {
343 dest_buffer->reserve(std::min(kMaxInMemoryBufferSize, max_dump_size_));
344
345 start_time_ = base::TimeTicks::Now();
346
347 // Writes the dump file header.
348 succeeded = AppendToBuffer(RtpDumpFileHeader::kFirstLine,
349 arraysize(RtpDumpFileHeader::kFirstLine) - 1,
350 dest_buffer);
351 DCHECK(succeeded);
352
353 RtpDumpFileHeader header(start_time_);
354 header.WriteBigEndian(dest_buffer);
355 }
356
357 size_t packet_dump_length = sizeof(PacketDumpHeader) + header_length;
358
359 // Flushes the buffer to disk if the buffer is full.
360 if (dest_buffer->capacity() < dest_buffer->size() + packet_dump_length)
361 FlushBuffers(incoming, !incoming, false, base::Callback<void(bool)>());
362
363 // Writes the packet dump header.
364 PacketDumpHeader packet_dump_header(
365 start_time_, packet_dump_length, packet_length);
366 packet_dump_header.WriteBigEndian(dest_buffer);
367
368 // Writes the actual RTP packet header.
369 succeeded = AppendToBuffer(packet_header, header_length, dest_buffer);
370 DCHECK(succeeded);
371 }
372
373 void WebRtcRtpDumpWriter::EndDump(
374 const base::Callback<void(bool)>& finished_callback) {
375 DCHECK(thread_checker_.CalledOnValidThread());
376
377 FlushBuffers(true, true, true, finished_callback);
378 }
379
380 void WebRtcRtpDumpWriter::FlushBuffers(
381 bool incoming,
382 bool outgoing,
383 bool end_stream,
384 const base::Callback<void(bool)>& callback) {
385 DCHECK(thread_checker_.CalledOnValidThread());
386
387 scoped_ptr<std::vector<uint8> > new_incoming_buffer(new std::vector<uint8>());
388 scoped_ptr<std::vector<uint8> > new_outgoing_buffer(new std::vector<uint8>());
389
390 if (incoming) {
391 new_incoming_buffer->reserve(incoming_buffer_.capacity());
392 new_incoming_buffer->swap(incoming_buffer_);
393 }
394
395 if (outgoing) {
396 new_outgoing_buffer->reserve(outgoing_buffer_.capacity());
397 new_outgoing_buffer->swap(outgoing_buffer_);
398 }
399
400 scoped_ptr<FlushResult> result(new FlushResult(FLUSH_RESULT_FAILURE));
401
402 // OnFlushDone is necessary to avoid running the callback after this object is
403 // gone.
404 BrowserThread::PostTaskAndReply(
405 BrowserThread::FILE,
406 FROM_HERE,
407 base::Bind(&FileThreadWorker::CompressAndWriteToFileOnFileThread,
408 base::Unretained(file_thread_worker_.get()),
409 Passed(&new_incoming_buffer),
410 Passed(&new_outgoing_buffer),
411 end_stream,
412 result.get()),
413 base::Bind(&WebRtcRtpDumpWriter::OnFlushDone,
414 weak_ptr_factory_.GetWeakPtr(),
415 callback,
416 Passed(&result)));
417 }
418
419 void WebRtcRtpDumpWriter::OnFlushDone(
420 const base::Callback<void(bool)>& callback,
421 const scoped_ptr<FlushResult>& result) {
422 DCHECK(thread_checker_.CalledOnValidThread());
423
424 if (*result == FLUSH_RESULT_MAX_SIZE_REACHED &&
425 !max_dump_size_reached_callback_.is_null()) {
426 max_dump_size_reached_callback_.Run();
427 }
428
429 // Returns success for FLUSH_RESULT_MAX_SIZE_REACHED since the dump is still
430 // valid.
431 if (!callback.is_null())
432 callback.Run(*result != FLUSH_RESULT_FAILURE);
433 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698