Chris Cunningham | b771734 | 2017-10-10 22:08:43 | [diff] [blame] | 1 | // 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 Cunningham | 9e66947 | 2017-11-15 21:03:11 | [diff] [blame] | 5 | #ifndef MEDIA_CAPABILITIES_VIDEO_DECODE_STATS_DB_IMPL_H_ |
| 6 | #define MEDIA_CAPABILITIES_VIDEO_DECODE_STATS_DB_IMPL_H_ |
Chris Cunningham | b771734 | 2017-10-10 22:08:43 | [diff] [blame] | 7 | |
| 8 | #include <memory> |
| 9 | |
Chris Cunningham | a5b3801 | 2017-10-28 07:27:18 | [diff] [blame] | 10 | #include "base/files/file_path.h" |
Chris Cunningham | b771734 | 2017-10-10 22:08:43 | [diff] [blame] | 11 | #include "base/memory/weak_ptr.h" |
| 12 | #include "components/leveldb_proto/proto_database.h" |
Chris Cunningham | 9e66947 | 2017-11-15 21:03:11 | [diff] [blame] | 13 | #include "media/base/media_export.h" |
Chris Cunningham | b771734 | 2017-10-10 22:08:43 | [diff] [blame] | 14 | #include "media/base/video_codecs.h" |
Chris Cunningham | 9e66947 | 2017-11-15 21:03:11 | [diff] [blame] | 15 | #include "media/capabilities/video_decode_stats_db.h" |
Chris Cunningham | b771734 | 2017-10-10 22:08:43 | [diff] [blame] | 16 | #include "ui/gfx/geometry/size.h" |
| 17 | |
| 18 | namespace base { |
| 19 | class FilePath; |
| 20 | } // namespace base |
| 21 | |
| 22 | namespace media { |
| 23 | |
| 24 | class DecodeStatsProto; |
| 25 | |
Chris Cunningham | a5b3801 | 2017-10-28 07:27:18 | [diff] [blame] | 26 | // Factory interface to create a DB instance. |
Chris Cunningham | 9e66947 | 2017-11-15 21:03:11 | [diff] [blame] | 27 | class MEDIA_EXPORT VideoDecodeStatsDBImplFactory |
Chris Cunningham | a5b3801 | 2017-10-28 07:27:18 | [diff] [blame] | 28 | : public VideoDecodeStatsDBFactory { |
| 29 | public: |
chcunningham | 9205ea8 | 2018-07-12 18:04:20 | [diff] [blame] | 30 | // |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 Cunningham | a5b3801 | 2017-10-28 07:27:18 | [diff] [blame] | 33 | 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 Cunningham | b771734 | 2017-10-10 22:08:43 | [diff] [blame] | 43 | // 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 Cunningham | 9e66947 | 2017-11-15 21:03:11 | [diff] [blame] | 46 | class MEDIA_EXPORT VideoDecodeStatsDBImpl : public VideoDecodeStatsDB { |
Chris Cunningham | b771734 | 2017-10-10 22:08:43 | [diff] [blame] | 47 | public: |
Chris Cunningham | a5b3801 | 2017-10-28 07:27:18 | [diff] [blame] | 48 | // Constructs the database. NOTE: must call Initialize() before using. |
Chris Cunningham | b771734 | 2017-10-10 22:08:43 | [diff] [blame] | 49 | // |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 Cunningham | a5b3801 | 2017-10-28 07:27:18 | [diff] [blame] | 53 | VideoDecodeStatsDBImpl( |
Chris Cunningham | b771734 | 2017-10-10 22:08:43 | [diff] [blame] | 54 | std::unique_ptr<leveldb_proto::ProtoDatabase<DecodeStatsProto>> db, |
Chris Cunningham | 49934e6 | 2017-11-09 21:49:01 | [diff] [blame] | 55 | const base::FilePath& dir); |
Chris Cunningham | a5b3801 | 2017-10-28 07:27:18 | [diff] [blame] | 56 | ~VideoDecodeStatsDBImpl() override; |
Chris Cunningham | b771734 | 2017-10-10 22:08:43 | [diff] [blame] | 57 | |
Chris Cunningham | a5b3801 | 2017-10-28 07:27:18 | [diff] [blame] | 58 | // Implement VideoDecodeStatsDB. |
chcunningham | 9205ea8 | 2018-07-12 18:04:20 | [diff] [blame] | 59 | void Initialize(InitializeCB init_cb) override; |
Chris Cunningham | b771734 | 2017-10-10 22:08:43 | [diff] [blame] | 60 | void AppendDecodeStats(const VideoDescKey& key, |
Chris Cunningham | 49934e6 | 2017-11-09 21:49:01 | [diff] [blame] | 61 | const DecodeStatsEntry& entry, |
| 62 | AppendDecodeStatsCB append_done_cb) override; |
Chris Cunningham | b771734 | 2017-10-10 22:08:43 | [diff] [blame] | 63 | void GetDecodeStats(const VideoDescKey& key, |
Chris Cunningham | 49934e6 | 2017-11-09 21:49:01 | [diff] [blame] | 64 | GetDecodeStatsCB get_stats_cb) override; |
| 65 | void DestroyStats(base::OnceClosure destroy_done_cb) override; |
Chris Cunningham | b771734 | 2017-10-10 22:08:43 | [diff] [blame] | 66 | |
| 67 | private: |
| 68 | friend class VideoDecodeStatsDBTest; |
| 69 | |
Chris Cunningham | a5b3801 | 2017-10-28 07:27:18 | [diff] [blame] | 70 | // Called when the database has been initialized. Will immediately call |
| 71 | // |init_cb| to forward |success|. |
chcunningham | 9205ea8 | 2018-07-12 18:04:20 | [diff] [blame] | 72 | void OnInit(InitializeCB init_cb, bool success); |
Chris Cunningham | b771734 | 2017-10-10 22:08:43 | [diff] [blame] | 73 | |
Chris Cunningham | a5b3801 | 2017-10-28 07:27:18 | [diff] [blame] | 74 | // Returns true if the DB is successfully initialized. |
| 75 | bool IsInitialized(); |
Chris Cunningham | b771734 | 2017-10-10 22:08:43 | [diff] [blame] | 76 | |
| 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 Cunningham | 49934e6 | 2017-11-09 21:49:01 | [diff] [blame] | 81 | AppendDecodeStatsCB append_done_cb, |
Chris Cunningham | b771734 | 2017-10-10 22:08:43 | [diff] [blame] | 82 | 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 Cunningham | 49934e6 | 2017-11-09 21:49:01 | [diff] [blame] | 86 | // |WriteUpdatedEntry|. Will run |append_done_cb| when done. |
| 87 | void OnEntryUpdated(AppendDecodeStatsCB append_done_cb, bool success); |
Chris Cunningham | b771734 | 2017-10-10 22:08:43 | [diff] [blame] | 88 | |
Chris Cunningham | 49934e6 | 2017-11-09 21:49:01 | [diff] [blame] | 89 | // Called when GetDecodeStats() operation was performed. |get_stats_cb| |
Chris Cunningham | b771734 | 2017-10-10 22:08:43 | [diff] [blame] | 90 | // 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 Cunningham | 49934e6 | 2017-11-09 21:49:01 | [diff] [blame] | 93 | GetDecodeStatsCB get_stats_cb, |
Chris Cunningham | b771734 | 2017-10-10 22:08:43 | [diff] [blame] | 94 | bool success, |
| 95 | std::unique_ptr<DecodeStatsProto> capabilities_info_proto); |
| 96 | |
Chris Cunningham | 49934e6 | 2017-11-09 21:49:01 | [diff] [blame] | 97 | // 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 Cunningham | b771734 | 2017-10-10 22:08:43 | [diff] [blame] | 101 | // Indicates whether initialization is completed. Does not indicate whether it |
Chris Cunningham | 49934e6 | 2017-11-09 21:49:01 | [diff] [blame] | 102 | // was successful. Will be reset upon calling DestroyStats(). Failed |
| 103 | // initialization is signaled by setting |db_| to null. |
Chris Cunningham | b771734 | 2017-10-10 22:08:43 | [diff] [blame] | 104 | bool db_init_ = false; |
| 105 | |
Chris Cunningham | 49934e6 | 2017-11-09 21:49:01 | [diff] [blame] | 106 | // 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 Cunningham | b771734 | 2017-10-10 22:08:43 | [diff] [blame] | 110 | // ProtoDatabase instance. Set to nullptr if fatal database error is |
| 111 | // encountered. |
| 112 | std::unique_ptr<leveldb_proto::ProtoDatabase<DecodeStatsProto>> db_; |
| 113 | |
Chris Cunningham | a5b3801 | 2017-10-28 07:27:18 | [diff] [blame] | 114 | // Directory where levelDB should store database files. |
| 115 | base::FilePath db_dir_; |
| 116 | |
Chris Cunningham | b771734 | 2017-10-10 22:08:43 | [diff] [blame] | 117 | // 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 Cunningham | 9e66947 | 2017-11-15 21:03:11 | [diff] [blame] | 130 | #endif // MEDIA_CAPABILITIES_VIDEO_DECODE_STATS_DB_IMPL_H_ |