blob: f83289c1fc8a950e054913489a112d549ef807be [file] [log] [blame]
Chris Cunninghamb7717342017-10-10 22:08:431// Copyright 2017 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
Chris Cunningham9e669472017-11-15 21:03:115#ifndef MEDIA_CAPABILITIES_VIDEO_DECODE_STATS_DB_IMPL_H_
6#define MEDIA_CAPABILITIES_VIDEO_DECODE_STATS_DB_IMPL_H_
Chris Cunninghamb7717342017-10-10 22:08:437
8#include <memory>
9
Chris Cunninghama5b38012017-10-28 07:27:1810#include "base/files/file_path.h"
Chris Cunninghamb7717342017-10-10 22:08:4311#include "base/memory/weak_ptr.h"
12#include "components/leveldb_proto/proto_database.h"
Chris Cunningham9e669472017-11-15 21:03:1113#include "media/base/media_export.h"
Chris Cunninghamb7717342017-10-10 22:08:4314#include "media/base/video_codecs.h"
Chris Cunningham9e669472017-11-15 21:03:1115#include "media/capabilities/video_decode_stats_db.h"
Chris Cunninghamb7717342017-10-10 22:08:4316#include "ui/gfx/geometry/size.h"
17
18namespace base {
19class FilePath;
20} // namespace base
21
22namespace media {
23
24class DecodeStatsProto;
25
Chris Cunninghama5b38012017-10-28 07:27:1826// Factory interface to create a DB instance.
Chris Cunningham9e669472017-11-15 21:03:1127class MEDIA_EXPORT VideoDecodeStatsDBImplFactory
Chris Cunninghama5b38012017-10-28 07:27:1828 : public VideoDecodeStatsDBFactory {
29 public:
chcunningham9205ea82018-07-12 18:04:2030 // |db_dir| specifies where to store LevelDB files to disk. LevelDB generates
31 // a handful of files, so its recommended to provide a dedicated directory to
32 // keep them isolated.
Chris Cunninghama5b38012017-10-28 07:27:1833 explicit VideoDecodeStatsDBImplFactory(base::FilePath db_dir);
34 ~VideoDecodeStatsDBImplFactory() override;
35 std::unique_ptr<VideoDecodeStatsDB> CreateDB() override;
36
37 private:
38 base::FilePath db_dir_;
39
40 DISALLOW_COPY_AND_ASSIGN(VideoDecodeStatsDBImplFactory);
41};
42
Chris Cunninghamb7717342017-10-10 22:08:4343// LevelDB implementation of VideoDecodeStatsDB. This class is not
44// thread safe. All API calls should happen on the same sequence used for
45// construction. API callbacks will also occur on this sequence.
Chris Cunningham9e669472017-11-15 21:03:1146class MEDIA_EXPORT VideoDecodeStatsDBImpl : public VideoDecodeStatsDB {
Chris Cunninghamb7717342017-10-10 22:08:4347 public:
Chris Cunninghama5b38012017-10-28 07:27:1848 // Constructs the database. NOTE: must call Initialize() before using.
Chris Cunninghamb7717342017-10-10 22:08:4349 // |db| injects the level_db database instance for storing capabilities info.
50 // |dir| specifies where to store LevelDB files to disk. LevelDB generates a
51 // handful of files, so its recommended to provide a dedicated directory to
52 // keep them isolated.
Chris Cunninghama5b38012017-10-28 07:27:1853 VideoDecodeStatsDBImpl(
Chris Cunninghamb7717342017-10-10 22:08:4354 std::unique_ptr<leveldb_proto::ProtoDatabase<DecodeStatsProto>> db,
Chris Cunningham49934e62017-11-09 21:49:0155 const base::FilePath& dir);
Chris Cunninghama5b38012017-10-28 07:27:1856 ~VideoDecodeStatsDBImpl() override;
Chris Cunninghamb7717342017-10-10 22:08:4357
Chris Cunninghama5b38012017-10-28 07:27:1858 // Implement VideoDecodeStatsDB.
chcunningham9205ea82018-07-12 18:04:2059 void Initialize(InitializeCB init_cb) override;
Chris Cunninghamb7717342017-10-10 22:08:4360 void AppendDecodeStats(const VideoDescKey& key,
Chris Cunningham49934e62017-11-09 21:49:0161 const DecodeStatsEntry& entry,
62 AppendDecodeStatsCB append_done_cb) override;
Chris Cunninghamb7717342017-10-10 22:08:4363 void GetDecodeStats(const VideoDescKey& key,
Chris Cunningham49934e62017-11-09 21:49:0164 GetDecodeStatsCB get_stats_cb) override;
65 void DestroyStats(base::OnceClosure destroy_done_cb) override;
Chris Cunninghamb7717342017-10-10 22:08:4366
67 private:
68 friend class VideoDecodeStatsDBTest;
69
Chris Cunninghama5b38012017-10-28 07:27:1870 // Called when the database has been initialized. Will immediately call
71 // |init_cb| to forward |success|.
chcunningham9205ea82018-07-12 18:04:2072 void OnInit(InitializeCB init_cb, bool success);
Chris Cunninghamb7717342017-10-10 22:08:4373
Chris Cunninghama5b38012017-10-28 07:27:1874 // Returns true if the DB is successfully initialized.
75 bool IsInitialized();
Chris Cunninghamb7717342017-10-10 22:08:4376
77 // Passed as the callback for |OnGotDecodeStats| by |AppendDecodeStats| to
78 // update the database once we've read the existing stats entry.
79 void WriteUpdatedEntry(const VideoDescKey& key,
80 const DecodeStatsEntry& entry,
Chris Cunningham49934e62017-11-09 21:49:0181 AppendDecodeStatsCB append_done_cb,
Chris Cunninghamb7717342017-10-10 22:08:4382 bool read_success,
83 std::unique_ptr<DecodeStatsProto> prev_stats_proto);
84
85 // Called when the database has been modified after a call to
Chris Cunningham49934e62017-11-09 21:49:0186 // |WriteUpdatedEntry|. Will run |append_done_cb| when done.
87 void OnEntryUpdated(AppendDecodeStatsCB append_done_cb, bool success);
Chris Cunninghamb7717342017-10-10 22:08:4388
Chris Cunningham49934e62017-11-09 21:49:0189 // Called when GetDecodeStats() operation was performed. |get_stats_cb|
Chris Cunninghamb7717342017-10-10 22:08:4390 // will be run with |success| and a |DecodeStatsEntry| created from
91 // |stats_proto| or nullptr if no entry was found for the requested key.
92 void OnGotDecodeStats(
Chris Cunningham49934e62017-11-09 21:49:0193 GetDecodeStatsCB get_stats_cb,
Chris Cunninghamb7717342017-10-10 22:08:4394 bool success,
95 std::unique_ptr<DecodeStatsProto> capabilities_info_proto);
96
Chris Cunningham49934e62017-11-09 21:49:0197 // Internal callback for ClearStats that logs |success| and runs
98 // |destroy_done_cb|
99 void OnDestroyedStats(base::OnceClosure destroy_done_cb, bool success);
100
Chris Cunninghamb7717342017-10-10 22:08:43101 // Indicates whether initialization is completed. Does not indicate whether it
Chris Cunningham49934e62017-11-09 21:49:01102 // was successful. Will be reset upon calling DestroyStats(). Failed
103 // initialization is signaled by setting |db_| to null.
Chris Cunninghamb7717342017-10-10 22:08:43104 bool db_init_ = false;
105
Chris Cunningham49934e62017-11-09 21:49:01106 // Tracks whether db_->Destroy() is in progress. Used to assert that
107 // Initialize() is not called until db destruction is complete.
108 bool db_destroy_pending_ = false;
109
Chris Cunninghamb7717342017-10-10 22:08:43110 // ProtoDatabase instance. Set to nullptr if fatal database error is
111 // encountered.
112 std::unique_ptr<leveldb_proto::ProtoDatabase<DecodeStatsProto>> db_;
113
Chris Cunninghama5b38012017-10-28 07:27:18114 // Directory where levelDB should store database files.
115 base::FilePath db_dir_;
116
Chris Cunninghamb7717342017-10-10 22:08:43117 // Ensures all access to class members come on the same sequence. API calls
118 // and callbacks should occur on the same sequence used during construction.
119 // LevelDB operations happen on a separate task runner, but all LevelDB
120 // callbacks to this happen on the checked sequence.
121 SEQUENCE_CHECKER(sequence_checker_);
122
123 base::WeakPtrFactory<VideoDecodeStatsDBImpl> weak_ptr_factory_;
124
125 DISALLOW_COPY_AND_ASSIGN(VideoDecodeStatsDBImpl);
126};
127
128} // namespace media
129
Chris Cunningham9e669472017-11-15 21:03:11130#endif // MEDIA_CAPABILITIES_VIDEO_DECODE_STATS_DB_IMPL_H_