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