1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved. 2a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 3a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// found in the LICENSE file. 4a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#ifndef CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_LEVEL_CALCULATOR_H_ 6a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#define CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_LEVEL_CALCULATOR_H_ 7a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/threading/thread_checker.h" 9a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace content { 11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// This class is used by the WebRtcLocalAudioTrack to calculate the level of 13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// the audio signal. And the audio level will be eventually used by the volume 14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// animation UI. 15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// The algorithm used by this class is the same as how it is done in 16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// third_party/webrtc/voice_engine/level_indicator.cc. 17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class MediaStreamAudioLevelCalculator { 18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) public: 19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) MediaStreamAudioLevelCalculator(); 20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) ~MediaStreamAudioLevelCalculator(); 21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Calculates the signal level of the audio data. 23effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch // Returns the absolute value of the amplitude of the signal. 24ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch // |force_report_nonzero_energy| is a flag forcing the calculator to 25ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch // report nonzero energy even if the energy of the processed audio is zero. 26ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch // Since |audio_data| is post processed data, and the audio processing might 27ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch // zero all the audio data, when the caller detects the pre processed data 28ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch // contain energy, it could pass |force_report_nonzero_energy| as true to 29ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch // force calculator to report 1 as energy when |audio_data| is all zero. 30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) int Calculate(const int16* audio_data, int number_of_channels, 31ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch int number_of_frames, bool force_report_nonzero_energy); 32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) private: 34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Used to DCHECK that the constructor and Calculate() are always called on 35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // the same audio thread. Note that the destructor will be called on a 36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // different thread, which can be either the main render thread or a new 37a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // audio thread where WebRtcLocalAudioTrack::OnSetFormat() is called. 38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) base::ThreadChecker thread_checker_; 39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) int counter_; 41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) int max_amplitude_; 42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) int level_; 43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}; 44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} // namespace content 46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 47a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif // CONTENT_RENDERER_MEDIA_MEDIA_STREAM_AUDIO_LEVEL_CALCULATOR_H_ 48