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