blob: 2ae30993f6b0a96f5631c169d75170eea8ea3ef9 [file] [log] [blame]
// Copyright 2023 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef COBALT_MEDIA_BASE_CVAL_STATS_H_
#define COBALT_MEDIA_BASE_CVAL_STATS_H_
#include <algorithm>
#include <map>
#include <string>
#include <utility>
#include <vector>
#include "cobalt/base/c_val.h"
namespace cobalt {
namespace media {
enum class MediaTiming {
SbPlayerCreate,
SbPlayerDestroy,
SbPlayerWriteSamples
};
// size of the sample array
constexpr size_t kMaxSamples = 100;
// number of samples taken before we trigger historical calculations
constexpr int kMediaDefaultMaxSamplesBeforeCalculation = 5;
constexpr int kMediaHighFrequencyMaxSamplesBeforeCalculation = 25;
class CValContainer {
public:
CValContainer(std::string name, int max_samples_before_calculation);
~CValContainer() = default;
void StartTimer();
void StopTimer();
void UpdateCVal(SbTime event_time);
// for testing only
size_t GetSampleIndex() { return sample_write_index_; }
size_t GetNumberSamples() {
return std::min(accumulated_sample_count_, kMaxSamples);
}
public:
int max_samples_before_calculation_{0};
std::string cval_name_;
private:
base::CVal<SbTime, base::CValPublic> latest_;
base::CVal<SbTime, base::CValPublic> average_;
base::CVal<SbTime, base::CValPublic> maximum_;
base::CVal<SbTime, base::CValPublic> median_;
base::CVal<SbTime, base::CValPublic> minimum_;
SbTime samples_[kMaxSamples];
size_t sample_write_index_{0};
size_t accumulated_sample_count_{0};
bool first_sample_added_{false};
SbTime latest_time_start_{0};
SbTime latest_time_stop_{0};
};
class CValStats {
public:
CValStats();
~CValStats() = default;
void StartTimer(MediaTiming event_type, std::string pipeline_identifier);
void StopTimer(MediaTiming event_type, std::string pipeline_identifier);
void Enable(bool enabled) { enabled_ = enabled; }
// just for testing
size_t GetBufferIndexForTest();
size_t GetBufferSampleSizeForTest();
private:
std::map<MediaTiming, CValContainer> cval_containers_;
std::map<std::pair<MediaTiming, std::string>, SbTime> running_timers_;
bool enabled_{false};
};
} // namespace media
} // namespace cobalt
#endif // COBALT_MEDIA_BASE_CVAL_STATS_H_