OLD | NEW |
---|---|
(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_handler.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/strings/string_number_conversions.h" | |
9 #include "base/time/time.h" | |
10 #include "chrome/browser/media/webrtc_rtp_dump_writer.h" | |
11 #include "content/public/browser/browser_thread.h" | |
12 | |
13 using content::BrowserThread; | |
14 | |
15 WebRtcRtpDumpHandler::WebRtcRtpDumpHandler(const base::FilePath& dump_dir) | |
16 : dump_dir_(dump_dir), | |
17 incoming_state_(STATE_NONE), | |
18 outgoing_state_(STATE_NONE) { | |
19 } | |
20 | |
21 WebRtcRtpDumpHandler::~WebRtcRtpDumpHandler() { | |
Henrik Grunell
2014/05/07 09:38:19
On which thread should this class be created and d
| |
22 // TODO: delete the dump file if not released yet. | |
23 | |
24 if (dump_writer_) { | |
25 if (BrowserThread::DeleteSoon(BrowserThread::IO, | |
26 FROM_HERE, | |
27 dump_writer_.get())) { | |
28 ignore_result(dump_writer_.release()); | |
29 } | |
30 } | |
31 } | |
32 | |
33 bool WebRtcRtpDumpHandler::StartDump(const PacketType& type) { | |
34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
35 | |
36 bool succeeded = false; | |
37 if (type.incoming && incoming_state_ == STATE_NONE) { | |
Henrik Grunell
2014/05/07 09:38:19
If both incoming and outgoing are set, incoming st
| |
38 incoming_state_ = STATE_STARTED; | |
39 succeeded = true; | |
40 } | |
41 if (type.outgoing && outgoing_state_ == STATE_NONE) { | |
42 outgoing_state_ = STATE_STARTED; | |
43 succeeded = true; | |
44 } | |
45 | |
46 if (succeeded && !dump_writer_) { | |
47 static const char kDumpFilePrefix[] = "rtpdump_"; | |
48 static const char kDumpFileExtension[] = ".gz"; | |
49 static const size_t kMaxDumpSize = 8 * 1024 * 1024; | |
50 | |
51 std::string dump_id = base::DoubleToString(base::Time::Now().ToDoubleT()); | |
52 dump_path_ = dump_dir_.AppendASCII(std::string(kDumpFilePrefix) + dump_id) | |
53 .AddExtension(FILE_PATH_LITERAL(kDumpFileExtension)); | |
54 | |
55 dump_writer_.reset(new WebRtcRtpDumpWriter( | |
56 dump_path_, | |
57 kMaxDumpSize, | |
58 base::Bind(&WebRtcRtpDumpHandler::OnMaxDumpSizeReached, | |
59 base::Unretained(this)))); | |
60 } | |
61 return succeeded; | |
62 } | |
63 | |
64 bool WebRtcRtpDumpHandler::StopDump(const PacketType& type) { | |
65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
66 | |
67 bool succeeded = false; | |
68 if (type.incoming && incoming_state_ == STATE_STARTED) { | |
69 incoming_state_ = STATE_STOPPED; | |
70 succeeded = true; | |
71 } | |
72 if (type.outgoing && outgoing_state_ == STATE_STARTED) { | |
73 outgoing_state_ = STATE_STOPPED; | |
74 succeeded = true; | |
75 } | |
76 return succeeded; | |
77 } | |
78 | |
79 bool WebRtcRtpDumpHandler::ReleaseDump(const ReleaseDumpCallback& callback) { | |
80 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
81 | |
82 // All types of dumps must have been stopped or not started. | |
83 if (incoming_state_ == STATE_STARTED || outgoing_state_ == STATE_STARTED || | |
84 (incoming_state_ == STATE_NONE && outgoing_state_ == STATE_NONE)) | |
85 return false; | |
86 | |
87 incoming_state_ = STATE_NONE; | |
88 outgoing_state_ = STATE_NONE; | |
89 | |
90 dump_writer_->Flush(base::Bind(&WebRtcRtpDumpHandler::OnWriterFlushDone, | |
91 base::Unretained(this), | |
92 callback)); | |
93 | |
94 return true; | |
95 } | |
96 | |
97 void WebRtcRtpDumpHandler::OnRtpPacket(const uint8* packet, | |
98 size_t length, | |
99 bool incoming) { | |
100 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
101 | |
102 if ((incoming && incoming_state_ != STATE_STARTED) || | |
103 (!incoming && outgoing_state_ != STATE_STARTED)) | |
104 return; | |
105 | |
106 dump_writer_->WriteRtpPacket(packet, length); | |
107 } | |
108 | |
109 void WebRtcRtpDumpHandler::OnMaxDumpSizeReached() { | |
110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
111 | |
112 PacketType type = {true, true}; | |
113 StopDump(type); | |
114 } | |
115 | |
116 void WebRtcRtpDumpHandler::OnWriterFlushDone( | |
117 const ReleaseDumpCallback& callback, bool succeeded) { | |
118 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
119 | |
120 callback.Run(succeeded, dump_path_); | |
121 } | |
OLD | NEW |