Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | #ifndef ANDROID_MEDIAANALYTICSSERVICE_H |
| 19 | #define ANDROID_MEDIAANALYTICSSERVICE_H |
| 20 | |
| 21 | #include <arpa/inet.h> |
| 22 | |
| 23 | #include <utils/threads.h> |
| 24 | #include <utils/Errors.h> |
| 25 | #include <utils/KeyedVector.h> |
| 26 | #include <utils/String8.h> |
| 27 | #include <utils/List.h> |
| 28 | |
| 29 | #include <media/IMediaAnalyticsService.h> |
| 30 | |
| 31 | |
| 32 | namespace android { |
| 33 | |
| 34 | class MediaAnalyticsService : public BnMediaAnalyticsService |
| 35 | { |
| 36 | |
| 37 | public: |
| 38 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame^] | 39 | // on this side, caller surrenders ownership |
| 40 | virtual int64_t submit(MediaAnalyticsItem *item, bool forcenew); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 41 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame^] | 42 | virtual List<MediaAnalyticsItem *> |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 43 | *getMediaAnalyticsItemList(bool finished, int64_t ts); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame^] | 44 | virtual List<MediaAnalyticsItem *> |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 45 | *getMediaAnalyticsItemList(bool finished, int64_t ts, MediaAnalyticsItem::Key key); |
| 46 | |
| 47 | |
| 48 | static void instantiate(); |
| 49 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 50 | |
| 51 | MediaAnalyticsService(); |
| 52 | virtual ~MediaAnalyticsService(); |
| 53 | |
| 54 | private: |
| 55 | MediaAnalyticsItem::SessionID_t generateUniqueSessionID(); |
| 56 | |
| 57 | // statistics about our analytics |
| 58 | int64_t mItemsSubmitted; |
| 59 | int64_t mItemsFinalized; |
| 60 | int64_t mItemsDiscarded; |
| 61 | MediaAnalyticsItem::SessionID_t mLastSessionID; |
| 62 | |
| 63 | // partitioned a bit so we don't over serialize |
| 64 | mutable Mutex mLock; |
| 65 | mutable Mutex mLock_ids; |
| 66 | |
| 67 | // the most we hold in memory |
| 68 | // up to this many in each queue (open, finalized) |
| 69 | int32_t mMaxRecords; |
| 70 | |
| 71 | // input validation after arrival from client |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame^] | 72 | bool contentValid(MediaAnalyticsItem *); |
| 73 | bool rateLimited(MediaAnalyticsItem *); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 74 | |
| 75 | // the ones that are still open |
| 76 | // (newest at front) since we keep looking for them |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame^] | 77 | List<MediaAnalyticsItem *> *mOpen; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 78 | // the ones we've finalized |
| 79 | // (oldest at front) so it prints nicely for dumpsys |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame^] | 80 | List<MediaAnalyticsItem *> *mFinalized; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 81 | // searching within these queues: queue, key |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame^] | 82 | MediaAnalyticsItem *findItem(List<MediaAnalyticsItem *> *, |
| 83 | MediaAnalyticsItem *, bool removeit); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 84 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame^] | 85 | void saveItem(MediaAnalyticsItem); |
| 86 | void saveItem(List<MediaAnalyticsItem *> *, MediaAnalyticsItem *, int); |
| 87 | void deleteItem(List<MediaAnalyticsItem *> *, MediaAnalyticsItem *); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 88 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame^] | 89 | String8 dumpQueue(List<MediaAnalyticsItem*> *); |
| 90 | String8 dumpQueue(List<MediaAnalyticsItem*> *, nsecs_t); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 91 | |
| 92 | }; |
| 93 | |
| 94 | // ---------------------------------------------------------------------------- |
| 95 | |
| 96 | }; // namespace android |
| 97 | |
| 98 | #endif // ANDROID_MEDIAANALYTICSSERVICE_H |