blob: 30695866ca7d2c0c39d0c9e69e890c77a4d6a6b7 [file] [log] [blame]
Amyb4b68012019-10-15 17:38:19 -07001/*
2 * Copyright (C) 2019 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_HARDWARE_TV_TUNER_V1_0_DVR_H_
18#define ANDROID_HARDWARE_TV_TUNER_V1_0_DVR_H_
19
20#include <android/hardware/tv/tuner/1.0/IDvr.h>
21#include <fmq/MessageQueue.h>
22#include <math.h>
23#include <set>
24#include "Demux.h"
25#include "Frontend.h"
26#include "Tuner.h"
27
28using namespace std;
29
30namespace android {
31namespace hardware {
32namespace tv {
33namespace tuner {
34namespace V1_0 {
35namespace implementation {
36
37using ::android::hardware::EventFlag;
38using ::android::hardware::kSynchronizedReadWrite;
39using ::android::hardware::MessageQueue;
40using ::android::hardware::MQDescriptorSync;
41using ::android::hardware::tv::tuner::V1_0::IDemux;
42using ::android::hardware::tv::tuner::V1_0::IDvrCallback;
43using ::android::hardware::tv::tuner::V1_0::Result;
44
45using DvrMQ = MessageQueue<uint8_t, kSynchronizedReadWrite>;
46
Amy Zhang85a9ab32020-06-19 16:44:52 -070047struct MediaEsMetaData {
48 bool isAudio;
49 int startIndex;
50 int len;
51 int pts;
52};
53
Amyb4b68012019-10-15 17:38:19 -070054class Demux;
55class Filter;
56class Frontend;
57class Tuner;
58
59class Dvr : public IDvr {
60 public:
61 Dvr();
62
63 Dvr(DvrType type, uint32_t bufferSize, const sp<IDvrCallback>& cb, sp<Demux> demux);
64
65 ~Dvr();
66
67 virtual Return<void> getQueueDesc(getQueueDesc_cb _hidl_cb) override;
68
69 virtual Return<Result> configure(const DvrSettings& settings) override;
70
71 virtual Return<Result> attachFilter(const sp<IFilter>& filter) override;
72
73 virtual Return<Result> detachFilter(const sp<IFilter>& filter) override;
74
75 virtual Return<Result> start() override;
76
77 virtual Return<Result> stop() override;
78
79 virtual Return<Result> flush() override;
80
81 virtual Return<Result> close() override;
82
83 /**
84 * To create a DvrMQ and its Event Flag.
85 *
86 * Return false is any of the above processes fails.
87 */
88 bool createDvrMQ();
Amy5ed13572019-12-11 15:33:51 -080089 void sendBroadcastInputToDvrRecord(vector<uint8_t> byteBuffer);
90 bool writeRecordFMQ(const std::vector<uint8_t>& data);
Amy Zhang6bda6392020-05-26 19:00:46 -070091 bool addPlaybackFilter(uint32_t filterId, sp<IFilter> filter);
92 bool removePlaybackFilter(uint32_t filterId);
93 bool readPlaybackFMQ(bool isVirtualFrontend, bool isRecording);
Amy Zhang85a9ab32020-06-19 16:44:52 -070094 bool processEsDataOnPlayback(bool isVirtualFrontend, bool isRecording);
Amy Zhang6bda6392020-05-26 19:00:46 -070095 bool startFilterDispatcher(bool isVirtualFrontend, bool isRecording);
96 EventFlag* getDvrEventFlag();
Amy Zhang85a9ab32020-06-19 16:44:52 -070097 DvrSettings getSettings() { return mDvrSettings; }
Amyb4b68012019-10-15 17:38:19 -070098
99 private:
100 // Demux service
101 sp<Demux> mDemux;
102
103 DvrType mType;
104 uint32_t mBufferSize;
105 sp<IDvrCallback> mCallback;
106 std::map<uint32_t, sp<IFilter>> mFilters;
107
108 void deleteEventFlag();
109 bool readDataFromMQ();
Amy Zhang85a9ab32020-06-19 16:44:52 -0700110 void getMetaDataValue(int& index, uint8_t* dataOutputBuffer, int& value);
Amyb4b68012019-10-15 17:38:19 -0700111 void maySendPlaybackStatusCallback();
112 void maySendRecordStatusCallback();
113 PlaybackStatus checkPlaybackStatusChange(uint32_t availableToWrite, uint32_t availableToRead,
114 uint32_t highThreshold, uint32_t lowThreshold);
Amy5ed13572019-12-11 15:33:51 -0800115 RecordStatus checkRecordStatusChange(uint32_t availableToWrite, uint32_t availableToRead,
116 uint32_t highThreshold, uint32_t lowThreshold);
Amyb4b68012019-10-15 17:38:19 -0700117 /**
118 * A dispatcher to read and dispatch input data to all the started filters.
119 * Each filter handler handles the data filtering/output writing/filterEvent updating.
120 */
Amyb4b68012019-10-15 17:38:19 -0700121 void startTpidFilter(vector<uint8_t> data);
Amyb4b68012019-10-15 17:38:19 -0700122 static void* __threadLoopPlayback(void* user);
Amy5ed13572019-12-11 15:33:51 -0800123 static void* __threadLoopRecord(void* user);
Amyb4b68012019-10-15 17:38:19 -0700124 void playbackThreadLoop();
Amy5ed13572019-12-11 15:33:51 -0800125 void recordThreadLoop();
Amyb4b68012019-10-15 17:38:19 -0700126
127 unique_ptr<DvrMQ> mDvrMQ;
128 EventFlag* mDvrEventFlag;
129 /**
130 * Demux callbacks used on filter events or IO buffer status
131 */
132 bool mDvrConfigured = false;
133 DvrSettings mDvrSettings;
134
135 // Thread handlers
136 pthread_t mDvrThread;
Amyb4b68012019-10-15 17:38:19 -0700137
138 // FMQ status local records
139 PlaybackStatus mPlaybackStatus;
Amy5ed13572019-12-11 15:33:51 -0800140 RecordStatus mRecordStatus;
Amyb4b68012019-10-15 17:38:19 -0700141 /**
142 * If a specific filter's writing loop is still running
143 */
144 bool mDvrThreadRunning;
Amyb4b68012019-10-15 17:38:19 -0700145 bool mKeepFetchingDataFromFrontend;
146 /**
147 * Lock to protect writes to the FMQs
148 */
149 std::mutex mWriteLock;
150 /**
151 * Lock to protect writes to the input status
152 */
153 std::mutex mPlaybackStatusLock;
Amy5ed13572019-12-11 15:33:51 -0800154 std::mutex mRecordStatusLock;
Amyb4b68012019-10-15 17:38:19 -0700155 std::mutex mDvrThreadLock;
156
157 const bool DEBUG_DVR = false;
Amy5ed13572019-12-11 15:33:51 -0800158
159 // Booleans to check if recording is running.
160 // Recording is ready when both of the following are set to true.
161 bool mIsRecordStarted = false;
Amyb4b68012019-10-15 17:38:19 -0700162};
163
164} // namespace implementation
165} // namespace V1_0
166} // namespace tuner
167} // namespace tv
168} // namespace hardware
169} // namespace android
170
171#endif // ANDROID_HARDWARE_TV_TUNER_V1_0_DVR_H_