1 // Copyright 2016 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_AUDIO_DEBUG_RECORDINGS_HANDLER_H_
6 #define CHROME_BROWSER_MEDIA_WEBRTC_AUDIO_DEBUG_RECORDINGS_HANDLER_H_
7 
8 #include <stddef.h>
9 #include <stdint.h>
10 
11 #include <memory>
12 #include <string>
13 
14 #include "base/callback.h"
15 #include "base/files/file_path.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/time/time.h"
18 
19 namespace content {
20 class BrowserContext;
21 class RenderProcessHost;
22 }
23 
24 namespace media {
25 class AudioDebugRecordingSession;
26 }
27 
28 // AudioDebugRecordingsHandler provides an interface to start and stop
29 // AudioDebugRecordings, including WebRTC AEC dumps. Lives on the UI thread.
30 class AudioDebugRecordingsHandler
31     : public base::RefCountedThreadSafe<AudioDebugRecordingsHandler> {
32  public:
33   typedef base::OnceCallback<void(bool, const std::string&)>
34       GenericDoneCallback;
35   typedef base::OnceCallback<void(const std::string&)> RecordingErrorCallback;
36   typedef base::OnceCallback<void(const std::string&, bool, bool)>
37       RecordingDoneCallback;
38 
39   // Key used to attach the handler to the RenderProcessHost
40   static const char kAudioDebugRecordingsHandlerKey[];
41 
42   explicit AudioDebugRecordingsHandler(
43       content::BrowserContext* browser_context);
44 
45   // Starts an audio debug recording. The recording lasts the given |delay|,
46   // unless |delay| is zero, in which case recording will continue until
47   // StopAudioDebugRecordings() is explicitly invoked.
48   // |callback| is invoked once recording stops. If |delay| is zero
49   // |callback| is invoked once recording starts.
50   // If a recording was already in progress, |error_callback| is invoked instead
51   // of |callback|.
52   void StartAudioDebugRecordings(content::RenderProcessHost* host,
53                                  base::TimeDelta delay,
54                                  RecordingDoneCallback callback,
55                                  RecordingErrorCallback error_callback);
56 
57   // Stops an audio debug recording. |callback| is invoked once recording
58   // stops. If no recording was in progress, |error_callback| is invoked instead
59   // of |callback|.
60   void StopAudioDebugRecordings(content::RenderProcessHost* host,
61                                 RecordingDoneCallback callback,
62                                 RecordingErrorCallback error_callback);
63 
64  private:
65   friend class base::RefCountedThreadSafe<AudioDebugRecordingsHandler>;
66 
67   virtual ~AudioDebugRecordingsHandler();
68 
69   // Helper for starting audio debug recordings.
70   void DoStartAudioDebugRecordings(content::RenderProcessHost* host,
71                                    base::TimeDelta delay,
72                                    RecordingDoneCallback callback,
73                                    RecordingErrorCallback error_callback,
74                                    const base::FilePath& log_directory);
75 
76   // Helper for stopping audio debug recordings.
77   void DoStopAudioDebugRecordings(content::RenderProcessHost* host,
78                                   bool is_manual_stop,
79                                   uint64_t audio_debug_recordings_id,
80                                   RecordingDoneCallback callback,
81                                   RecordingErrorCallback error_callback,
82                                   const base::FilePath& log_directory);
83 
84   // The browser context associated with our renderer process.
85   content::BrowserContext* const browser_context_;
86 
87   // This counter allows saving each debug recording in separate files.
88   uint64_t current_audio_debug_recordings_id_;
89 
90   // Used for controlling debug recordings.
91   std::unique_ptr<media::AudioDebugRecordingSession>
92       audio_debug_recording_session_;
93 
94   DISALLOW_COPY_AND_ASSIGN(AudioDebugRecordingsHandler);
95 };
96 
97 #endif  // CHROME_BROWSER_MEDIA_WEBRTC_AUDIO_DEBUG_RECORDINGS_HANDLER_H_
98