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 MEDIA_BASE_AUDIO_LATENCY_H_
6 #define MEDIA_BASE_AUDIO_LATENCY_H_
7 
8 #include "media/base/media_shmem_export.h"
9 
10 namespace base {
11 class TimeDelta;
12 }
13 
14 namespace media {
15 
16 class MEDIA_SHMEM_EXPORT AudioLatency {
17  public:
18   // Categories of expected latencies for input/output audio. Do not change
19   // existing values, they are used for UMA histogram reporting.
20   enum LatencyType {
21     // Specific latency in milliseconds.
22     LATENCY_EXACT_MS = 0,
23     // Lowest possible latency which does not cause glitches.
24     LATENCY_INTERACTIVE = 1,
25     // Latency optimized for real time communication.
26     LATENCY_RTC = 2,
27     // Latency optimized for continuous playback and power saving.
28     LATENCY_PLAYBACK = 3,
29     // For validation only.
30     LATENCY_LAST = LATENCY_PLAYBACK,
31     LATENCY_COUNT = LATENCY_LAST + 1
32   };
33 
34   // Indicates if the OS does not require resampling for playback.
35   static bool IsResamplingPassthroughSupported(LatencyType type);
36 
37   // |preferred_buffer_size| should be set to 0 if a client has no preference.
38   static int GetHighLatencyBufferSize(int sample_rate,
39                                       int preferred_buffer_size);
40 
41   // |hardware_buffer_size| should be set to 0 if unknown/invalid/not preferred.
42   static int GetRtcBufferSize(int sample_rate, int hardware_buffer_size);
43 
44   static int GetInteractiveBufferSize(int hardware_buffer_size);
45 
46   // Return the closest buffer size for this platform that will result in a
47   // latency not less than |duration| for the given |sample_rate|.
48   //
49   // Requirements:
50   // - |hardware_buffer_size| > 0 and |max_allowed_buffer_size| > 0.
51   // - If |min_hardware_buffer_size| and |max_hardware_buffer_size| are > 0 then
52   //   the following must be true: |min_hardware_buffer_size| <=
53   //   |hardware_buffer_size| <= |max_hardware_buffer_size| <=
54   //   |max_allowed_buffer_size|.
55   //
56   // The returned buffer size is guaranteed to be between
57   // |min_hardware_buffer_size| and |max_allowed_buffer_size|.
58   // |max_hardware_buffer_size| is used to help determine a buffer size that
59   // won't cause the caller and the hardware to run at unsynchronized buffer
60   // sizes (e.g. hardware running at 4096 and caller running at 4224).
61   // |hardware_buffer_size| is the platform's preferred buffer size.
62   //
63   // It is valid for both the min and max to be zero in which case only
64   // |hardware_buffer_size| and multiples of it will be used.
65   static int GetExactBufferSize(base::TimeDelta duration,
66                                 int sample_rate,
67                                 int hardware_buffer_size,
68                                 int min_hardware_buffer_size,
69                                 int max_hardware_buffer_size,
70                                 int max_allowed_buffer_size);
71 };
72 
73 }  // namespace media
74 
75 #endif  // MEDIA_BASE_AUDIO_LATENCY_H_
76