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 "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"
13
14class WebRtcRtpDumpWriter;
15
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
23//   is destroyed.
24//
25// Must be created/used/destroyed on the browser IO thread.
26class WebRtcRtpDumpHandler {
27 public:
28  typedef base::Callback<void(bool, const std::string&)> GenericDoneCallback;
29
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) {}
35
36    const base::FilePath incoming_dump_path;
37    const base::FilePath outgoing_dump_path;
38  };
39
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
45  // practice.
46  explicit WebRtcRtpDumpHandler(const base::FilePath& dump_dir);
47  ~WebRtcRtpDumpHandler();
48
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);
53
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);
59
60  // Returns true if it's valid to call ReleaseDumps, i.e. no dumping is ongoing
61  // or being stopped.
62  bool ReadyToRelease() const;
63
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.
67  //
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
70  // by this object.
71  ReleasedDumps ReleaseDumps();
72
73  // Adds an RTP packet to the dump. The caller must make sure it's a valid RTP
74  // packet.
75  void OnRtpPacket(const uint8* packet_header,
76                   size_t header_length,
77                   size_t packet_length,
78                   bool incoming);
79
80  // Stops all ongoing dumps and call |callback| when finished.
81  void StopOngoingDumps(const base::Closure& callback);
82
83 private:
84  friend class WebRtcRtpDumpHandlerTest;
85
86  // State transitions:
87  // initial --> STATE_NONE
88  // StartDump --> STATE_STARTED
89  // StopDump --> STATE_STOPPED
90  // ReleaseDump --> STATE_RELEASING
91  // ReleaseDump done --> STATE_NONE
92  enum State {
93    STATE_NONE,
94    STATE_STARTED,
95    STATE_STOPPING,
96    STATE_STOPPED,
97  };
98
99  // For unit test to inject a fake writer.
100  void SetDumpWriterForTesting(scoped_ptr<WebRtcRtpDumpWriter> writer);
101
102  // Callback from the dump writer when the max dump size is reached.
103  void OnMaxDumpSizeReached();
104
105  // Callback from the dump writer when ending dumps finishes. Calls |callback|
106  // when finished.
107  void OnDumpEnded(const base::Closure& callback,
108                   RtpDumpType ended_type,
109                   bool incoming_succeeded,
110                   bool outgoing_succeeded);
111
112  // The absolute path to the directory containing the incoming/outgoing dumps.
113  const base::FilePath dump_dir_;
114
115  // The dump file paths.
116  base::FilePath incoming_dump_path_;
117  base::FilePath outgoing_dump_path_;
118
119  // The states of the incoming and outgoing dump.
120  State incoming_state_;
121  State outgoing_state_;
122
123  // The object used to create and write the dump file.
124  scoped_ptr<WebRtcRtpDumpWriter> dump_writer_;
125
126  base::WeakPtrFactory<WebRtcRtpDumpHandler> weak_ptr_factory_;
127
128  DISALLOW_COPY_AND_ASSIGN(WebRtcRtpDumpHandler);
129};
130
131#endif  // CHROME_BROWSER_MEDIA_WEBRTC_RTP_DUMP_HANDLER_H_
132