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 #ifndef CHROME_BROWSER_MEDIA_WEBRTC_RTP_DUMP_HANDLER_H_ | |
6 #define CHROME_BROWSER_MEDIA_WEBRTC_RTP_DUMP_HANDLER_H_ | |
7 | |
8 #include <stddef.h> | |
9 #include <stdint.h> | |
10 | |
11 #include <memory> | |
12 | |
13 #include "base/callback.h" | |
14 #include "base/files/file_path.h" | |
15 #include "base/macros.h" | |
16 #include "base/memory/weak_ptr.h" | |
17 #include "chrome/browser/media/rtp_dump_type.h" | |
18 | |
19 class WebRtcRtpDumpWriter; | |
20 | |
21 // WebRtcRtpDumpHandler handles operations regarding the WebRTC RTP dump: | |
22 // - Starts or stops the RTP dumping on behalf of the client. | |
23 // - Stops the RTP dumping when the max dump file size is reached. | |
24 // - Writes the dump file. | |
25 // - Provides the dump file to the client code to be uploaded when | |
26 // ReleaseRtpDump is called. | |
27 // - Cleans up the dump file if not transferred to the client before the object | |
28 // is destroyed. | |
29 // | |
30 // Must be created/used/destroyed on the browser IO thread. | |
31 class WebRtcRtpDumpHandler { | |
32 public: | |
33 typedef base::Callback<void(bool, const std::string&)> GenericDoneCallback; | |
34 | |
35 struct ReleasedDumps { | |
36 ReleasedDumps(const base::FilePath& incoming_dump, | |
37 const base::FilePath& outgoing_dump) | |
38 : incoming_dump_path(incoming_dump), | |
39 outgoing_dump_path(outgoing_dump) {} | |
40 | |
41 const base::FilePath incoming_dump_path; | |
42 const base::FilePath outgoing_dump_path; | |
43 }; | |
44 | |
45 // The caller must make sure |dump_dir| exists. RTP dump files are saved under | |
46 // |dump_dir| as "rtpdump_$DIRECTION_$TIMESTAMP.gz", where $DIRECTION is | |
47 // 'send' for outgoing dump or 'recv' for incoming dump. $TIMESTAMP is the | |
48 // dump started time converted to a double number in microsecond precision, | |
49 // which should guarantee the uniqueness across tabs and dump streams in | |
50 // practice. | |
51 explicit WebRtcRtpDumpHandler(const base::FilePath& dump_dir); | |
52 ~WebRtcRtpDumpHandler(); | |
53 | |
54 // Starts the specified type of dumping. Incoming/outgoing dumping can be | |
55 // started separately. Returns true if called in a valid state, i.e. the | |
56 // specified type of dump has not been started. | |
57 bool StartDump(RtpDumpType type, std::string* error_message); | |
58 | |
59 // Stops the specified type of dumping. Incoming/outgoing dumping can be | |
60 // stopped separately. Returns asynchronously through |callback|, where | |
61 // |success| is true if StopDump is called in a valid state. The callback is | |
62 // called when the writer finishes writing the dumps. | |
63 void StopDump(RtpDumpType type, const GenericDoneCallback& callback); | |
64 | |
65 // Returns true if it's valid to call ReleaseDumps, i.e. no dumping is ongoing | |
66 // or being stopped. | |
67 bool ReadyToRelease() const; | |
68 | |
69 // Releases all the dumps and resets the state. | |
70 // It should only be called when both incoming and outgoing dumping has been | |
71 // stopped, i.e. ReadyToRelease() returns true. Returns the dump file paths. | |
72 // | |
73 // The caller will own the dump file after the method returns. If ReleaseDump | |
74 // is not called before this object goes away, the dump file will be deleted | |
75 // by this object. | |
76 ReleasedDumps ReleaseDumps(); | |
77 | |
78 // Adds an RTP packet to the dump. The caller must make sure it's a valid RTP | |
79 // packet. | |
80 void OnRtpPacket(const uint8_t* packet_header, | |
81 size_t header_length, | |
82 size_t packet_length, | |
83 bool incoming); | |
84 | |
85 // Stops all ongoing dumps and call |callback| when finished. | |
86 void StopOngoingDumps(const base::Closure& callback); | |
87 | |
88 private: | |
89 friend class WebRtcRtpDumpHandlerTest; | |
90 | |
91 // State transitions: | |
92 // initial --> STATE_NONE | |
93 // StartDump --> STATE_STARTED | |
94 // StopDump --> STATE_STOPPED | |
95 // ReleaseDump --> STATE_RELEASING | |
96 // ReleaseDump done --> STATE_NONE | |
97 enum State { | |
98 STATE_NONE, | |
99 STATE_STARTED, | |
100 STATE_STOPPING, | |
101 STATE_STOPPED, | |
102 }; | |
103 | |
104 // For unit test to inject a fake writer. | |
105 void SetDumpWriterForTesting(std::unique_ptr<WebRtcRtpDumpWriter> writer); | |
106 | |
107 // Callback from the dump writer when the max dump size is reached. | |
108 void OnMaxDumpSizeReached(); | |
109 | |
110 // Callback from the dump writer when ending dumps finishes. Calls |callback| | |
111 // when finished. | |
112 void OnDumpEnded(const base::Closure& callback, | |
113 RtpDumpType ended_type, | |
114 bool incoming_succeeded, | |
115 bool outgoing_succeeded); | |
116 | |
117 // The absolute path to the directory containing the incoming/outgoing dumps. | |
118 const base::FilePath dump_dir_; | |
119 | |
120 // The dump file paths. | |
121 base::FilePath incoming_dump_path_; | |
122 base::FilePath outgoing_dump_path_; | |
123 | |
124 // The states of the incoming and outgoing dump. | |
125 State incoming_state_; | |
126 State outgoing_state_; | |
127 | |
128 // The object used to create and write the dump file. | |
129 std::unique_ptr<WebRtcRtpDumpWriter> dump_writer_; | |
130 | |
131 base::WeakPtrFactory<WebRtcRtpDumpHandler> weak_ptr_factory_; | |
132 | |
133 DISALLOW_COPY_AND_ASSIGN(WebRtcRtpDumpHandler); | |
134 }; | |
135 | |
136 #endif // CHROME_BROWSER_MEDIA_WEBRTC_RTP_DUMP_HANDLER_H_ | |
OLD | NEW |