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