1 /* 2 * Copyright (C) 2021 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 #ifndef ANDROID_MEDIA_TRANSCODING_LOGGER_H 18 #define ANDROID_MEDIA_TRANSCODING_LOGGER_H 19 20 #include <media/NdkMediaFormat.h> 21 #include <utils/Condition.h> 22 23 #include <chrono> 24 #include <memory> 25 #include <mutex> 26 #include <queue> 27 28 namespace android { 29 30 /** Class for logging transcoding events. */ 31 class TranscodingLogger { 32 public: 33 /** The maximum number of atoms pushed to statsd per day. */ 34 static constexpr int kMaxAtomsPerDay = 50; 35 36 /** The maximum number of successful transcoding atoms pushed to statsd per day. */ 37 static constexpr int kMaxSuccessfulAtomsPerDay = 35; 38 39 /** Reason transcoding session ended. Maps to MediaTranscodingSessionEnded atom's Reason. */ 40 enum SessionEndedReason { 41 UNKNOWN = 0, 42 FINISHED, 43 ERROR, 44 PAUSED, 45 CANCELLED, 46 START_FAILED, 47 RESUME_FAILED, 48 CREATE_FAILED, 49 CONFIG_SRC_FAILED, 50 CONFIG_DST_FAILED, 51 CONFIG_TRACK_FAILED, 52 OPEN_SRC_FD_FAILED, 53 OPEN_DST_FD_FAILED, 54 NO_TRACKS, 55 }; 56 57 TranscodingLogger(); 58 ~TranscodingLogger() = default; 59 60 /** 61 * Logs a transcoding session ended event (MediaTranscodingSessionEnded atom). 62 * @param reason Reason for the transcoding session to end. 63 * @param callingUid UID of the caller connecting to the transcoding service. 64 * @param status Status (error code) of the transcoding session. 65 * @param duration Duration of the transcoding session. 66 * @param srcFormat The source video track format. 67 * @param dstFormat The destination video track format. 68 */ 69 void logSessionEnded(enum SessionEndedReason reason, uid_t callingUid, int status, 70 std::chrono::microseconds duration, AMediaFormat* srcFormat, 71 AMediaFormat* dstFormat); 72 73 private: 74 friend class TranscodingLoggerTest; 75 76 // Function prototype for writing out the session ended atom. 77 using SessionEndedAtomWriter = std::function<int( 78 int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, int32_t, char const*, int32_t, 79 int32_t, int32_t, int32_t, bool arg12, int32_t, int32_t, char const*, bool)>; 80 81 std::mutex mLock; 82 std::queue<std::pair<std::chrono::steady_clock::time_point, int>> mLastLoggedAtoms 83 GUARDED_BY(mLock); 84 uint32_t mSuccessfulCount = 0; 85 SessionEndedAtomWriter mSessionEndedAtomWriter; 86 87 void logSessionEnded(const std::chrono::steady_clock::time_point& now, 88 enum SessionEndedReason reason, uid_t callingUid, int status, 89 std::chrono::microseconds duration, AMediaFormat* srcFormat, 90 AMediaFormat* dstFormat); 91 bool shouldLogAtom(const std::chrono::steady_clock::time_point& now, int status); 92 // Used for testing to validate what gets sent to statsd. 93 void setSessionEndedAtomWriter(const SessionEndedAtomWriter& writer); 94 }; 95 96 } // namespace android 97 #endif // ANDROID_MEDIA_TRANSCODING_LOGGER_H 98