blob: b8d57dffea360ede79b645c9efe4176eed92c062 [file] [log] [blame]
Hongguang4092f2f2021-07-08 18:49:12 -07001/*
2 * Copyright 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#pragma once
18
19#include <aidl/android/hardware/tv/tuner/BnDemux.h>
sadiqsada56c98292023-11-02 16:45:31 -070020#include <aidl/android/hardware/tv/tuner/BnDvrCallback.h>
Hongguang4092f2f2021-07-08 18:49:12 -070021
22#include <fmq/AidlMessageQueue.h>
23#include <math.h>
Hongguang901aa7b2021-08-26 12:20:56 -070024#include <atomic>
Hongguang4092f2f2021-07-08 18:49:12 -070025#include <set>
Hongguang901aa7b2021-08-26 12:20:56 -070026#include <thread>
27
Hongguang4092f2f2021-07-08 18:49:12 -070028#include "Dvr.h"
29#include "Filter.h"
30#include "Frontend.h"
31#include "TimeFilter.h"
sadiqsada56c98292023-11-02 16:45:31 -070032#include "Timer.h"
Hongguang4092f2f2021-07-08 18:49:12 -070033#include "Tuner.h"
sadiqsada56c98292023-11-02 16:45:31 -070034#include "dtv_plugin.h"
Hongguang4092f2f2021-07-08 18:49:12 -070035
36using namespace std;
37
38namespace aidl {
39namespace android {
40namespace hardware {
41namespace tv {
42namespace tuner {
43
44using ::aidl::android::hardware::common::fmq::MQDescriptor;
45using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite;
46using ::android::AidlMessageQueue;
47using ::android::hardware::EventFlag;
48
49using FilterMQ = AidlMessageQueue<int8_t, SynchronizedReadWrite>;
sadiqsada56c98292023-11-02 16:45:31 -070050using AidlMQ = AidlMessageQueue<int8_t, SynchronizedReadWrite>;
51using AidlMQDesc = MQDescriptor<int8_t, SynchronizedReadWrite>;
Hongguang4092f2f2021-07-08 18:49:12 -070052
53class Dvr;
54class Filter;
55class Frontend;
56class TimeFilter;
57class Tuner;
58
sadiqsada25a0f6f2024-01-16 15:16:10 -080059const int IPTV_PLAYBACK_TIMEOUT = 20; // ms
60const int IPTV_PLAYBACK_BUFFER_TIMEOUT = 20000; // ms
61
sadiqsada56c98292023-11-02 16:45:31 -070062class DvrPlaybackCallback : public BnDvrCallback {
63 public:
64 virtual ::ndk::ScopedAStatus onPlaybackStatus(PlaybackStatus status) override {
65 ALOGD("demux.h: playback status %d", status);
66 return ndk::ScopedAStatus::ok();
67 }
68
69 virtual ::ndk::ScopedAStatus onRecordStatus(RecordStatus status) override {
70 ALOGD("Record Status %hhd", status);
71 return ndk::ScopedAStatus::ok();
72 }
73};
74
Hongguang4092f2f2021-07-08 18:49:12 -070075class Demux : public BnDemux {
76 public:
Kensuke Miyagi73b18ac2022-11-07 10:49:09 -080077 Demux(int32_t demuxId, uint32_t filterTypes);
Hongguang4092f2f2021-07-08 18:49:12 -070078 ~Demux();
79
80 ::ndk::ScopedAStatus setFrontendDataSource(int32_t in_frontendId) override;
81 ::ndk::ScopedAStatus openFilter(const DemuxFilterType& in_type, int32_t in_bufferSize,
82 const std::shared_ptr<IFilterCallback>& in_cb,
83 std::shared_ptr<IFilter>* _aidl_return) override;
84 ::ndk::ScopedAStatus openTimeFilter(std::shared_ptr<ITimeFilter>* _aidl_return) override;
85 ::ndk::ScopedAStatus getAvSyncHwId(const std::shared_ptr<IFilter>& in_filter,
86 int32_t* _aidl_return) override;
87 ::ndk::ScopedAStatus getAvSyncTime(int32_t in_avSyncHwId, int64_t* _aidl_return) override;
88 ::ndk::ScopedAStatus close() override;
89 ::ndk::ScopedAStatus openDvr(DvrType in_type, int32_t in_bufferSize,
90 const std::shared_ptr<IDvrCallback>& in_cb,
91 std::shared_ptr<IDvr>* _aidl_return) override;
92 ::ndk::ScopedAStatus connectCiCam(int32_t in_ciCamId) override;
93 ::ndk::ScopedAStatus disconnectCiCam() override;
94
Hongguang2ecfc392021-11-23 10:29:15 -080095 binder_status_t dump(int fd, const char** args, uint32_t numArgs) override;
96
Hongguang4092f2f2021-07-08 18:49:12 -070097 // Functions interacts with Tuner Service
98 void stopFrontendInput();
99 ::ndk::ScopedAStatus removeFilter(int64_t filterId);
100 bool attachRecordFilter(int64_t filterId);
101 bool detachRecordFilter(int64_t filterId);
102 ::ndk::ScopedAStatus startFilterHandler(int64_t filterId);
103 void updateFilterOutput(int64_t filterId, vector<int8_t> data);
104 void updateMediaFilterOutput(int64_t filterId, vector<int8_t> data, uint64_t pts);
105 uint16_t getFilterTpid(int64_t filterId);
106 void setIsRecording(bool isRecording);
107 bool isRecording();
108 void startFrontendInputLoop();
sadiqsada25a0f6f2024-01-16 15:16:10 -0800109 void frontendIptvInputThreadLoop(dtv_plugin* interface, dtv_streamer* streamer);
Hongguang4092f2f2021-07-08 18:49:12 -0700110
111 /**
112 * A dispatcher to read and dispatch input data to all the started filters.
113 * Each filter handler handles the data filtering/output writing/filterEvent updating.
114 * Note that recording filters are not included.
115 */
116 bool startBroadcastFilterDispatcher();
117 void startBroadcastTsFilter(vector<int8_t> data);
118
119 void sendFrontendInputToRecord(vector<int8_t> data);
120 void sendFrontendInputToRecord(vector<int8_t> data, uint16_t pid, uint64_t pts);
121 bool startRecordFilterDispatcher();
122
Kensuke Miyagi73b18ac2022-11-07 10:49:09 -0800123 void getDemuxInfo(DemuxInfo* demuxInfo);
124 int32_t getDemuxId();
125 bool isInUse();
126 void setInUse(bool inUse);
127 void setTunerService(std::shared_ptr<Tuner> tuner);
128
sadiqsada028f2762023-10-31 16:09:25 -0700129 /**
130 * Setter for IPTV Reading thread
131 */
132 void setIptvThreadRunning(bool isIptvThreadRunning);
133
Hongguang4092f2f2021-07-08 18:49:12 -0700134 private:
135 // Tuner service
Hongguang Chenff2c6b02021-08-07 00:12:26 +0000136 std::shared_ptr<Tuner> mTuner;
Hongguang4092f2f2021-07-08 18:49:12 -0700137
138 // Frontend source
139 std::shared_ptr<Frontend> mFrontend;
140
141 // A struct that passes the arguments to a newly created filter thread
142 struct ThreadArgs {
143 Demux* user;
144 int64_t filterId;
145 };
146
147 static void* __threadLoopFrontend(void* user);
148 void frontendInputThreadLoop();
149
150 /**
151 * To create a FilterMQ with the next available Filter ID.
152 * Creating Event Flag at the same time.
153 * Add the successfully created/saved FilterMQ into the local list.
154 *
155 * Return false is any of the above processes fails.
156 */
157 void deleteEventFlag();
158 bool readDataFromMQ();
159
160 int32_t mDemuxId = -1;
161 int32_t mCiCamId;
162 set<int64_t> mPcrFilterIds;
163 /**
164 * Record the last used filter id. Initial value is -1.
165 * Filter Id starts with 0.
166 */
167 int64_t mLastUsedFilterId = -1;
168 /**
169 * Record all the used playback filter Ids.
170 * Any removed filter id should be removed from this set.
171 */
172 set<int64_t> mPlaybackFilterIds;
173 /**
174 * Record all the attached record filter Ids.
175 * Any removed filter id should be removed from this set.
176 */
177 set<int64_t> mRecordFilterIds;
178 /**
179 * A list of created Filter sp.
180 * The array number is the filter ID.
181 */
182 std::map<int64_t, std::shared_ptr<Filter>> mFilters;
183
184 /**
185 * Local reference to the opened Timer Filter instance.
186 */
187 std::shared_ptr<TimeFilter> mTimeFilter;
188
189 /**
190 * Local reference to the opened DVR object.
191 */
192 std::shared_ptr<Dvr> mDvrPlayback;
193 std::shared_ptr<Dvr> mDvrRecord;
194
195 // Thread handlers
Hongguang901aa7b2021-08-26 12:20:56 -0700196 std::thread mFrontendInputThread;
sadiqsada56c98292023-11-02 16:45:31 -0700197 std::thread mDemuxIptvReadThread;
198
199 // track whether the DVR FMQ for IPTV Playback is full
200 bool mIsIptvDvrFMQFull = false;
Hongguang901aa7b2021-08-26 12:20:56 -0700201
Hongguang4092f2f2021-07-08 18:49:12 -0700202 /**
203 * If a specific filter's writing loop is still running
204 */
Hongguang901aa7b2021-08-26 12:20:56 -0700205 std::atomic<bool> mFrontendInputThreadRunning;
206 std::atomic<bool> mKeepFetchingDataFromFrontend;
207
Hongguang4092f2f2021-07-08 18:49:12 -0700208 /**
sadiqsada028f2762023-10-31 16:09:25 -0700209 * Controls IPTV reading thread status
210 */
211 bool mIsIptvReadThreadRunning;
212 std::mutex mIsIptvThreadRunningMutex;
213 std::condition_variable mIsIptvThreadRunningCv;
214
215 /**
Hongguang4092f2f2021-07-08 18:49:12 -0700216 * If the dvr recording is running.
217 */
218 bool mIsRecording = false;
219 /**
220 * Lock to protect writes to the FMQs
221 */
222 std::mutex mWriteLock;
Hongguang4092f2f2021-07-08 18:49:12 -0700223
224 // temp handle single PES filter
225 // TODO handle mulptiple Pes filters
226 int mPesSizeLeft = 0;
227 vector<uint8_t> mPesOutput;
228
229 const bool DEBUG_DEMUX = false;
Kensuke Miyagi73b18ac2022-11-07 10:49:09 -0800230
231 int32_t mFilterTypes;
232 bool mInUse = false;
Hongguang4092f2f2021-07-08 18:49:12 -0700233};
234
235} // namespace tuner
236} // namespace tv
237} // namespace hardware
238} // namespace android
239} // namespace aidl