blob: ad7b7a77a1ad6fd602bd1628a50d06cfad77004e [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
sadiqsada56c98292023-11-02 16:45:31 -070059class DvrPlaybackCallback : public BnDvrCallback {
60 public:
61 virtual ::ndk::ScopedAStatus onPlaybackStatus(PlaybackStatus status) override {
62 ALOGD("demux.h: playback status %d", status);
63 return ndk::ScopedAStatus::ok();
64 }
65
66 virtual ::ndk::ScopedAStatus onRecordStatus(RecordStatus status) override {
67 ALOGD("Record Status %hhd", status);
68 return ndk::ScopedAStatus::ok();
69 }
70};
71
Hongguang4092f2f2021-07-08 18:49:12 -070072class Demux : public BnDemux {
73 public:
Kensuke Miyagi73b18ac2022-11-07 10:49:09 -080074 Demux(int32_t demuxId, uint32_t filterTypes);
Hongguang4092f2f2021-07-08 18:49:12 -070075 ~Demux();
76
77 ::ndk::ScopedAStatus setFrontendDataSource(int32_t in_frontendId) override;
78 ::ndk::ScopedAStatus openFilter(const DemuxFilterType& in_type, int32_t in_bufferSize,
79 const std::shared_ptr<IFilterCallback>& in_cb,
80 std::shared_ptr<IFilter>* _aidl_return) override;
81 ::ndk::ScopedAStatus openTimeFilter(std::shared_ptr<ITimeFilter>* _aidl_return) override;
82 ::ndk::ScopedAStatus getAvSyncHwId(const std::shared_ptr<IFilter>& in_filter,
83 int32_t* _aidl_return) override;
84 ::ndk::ScopedAStatus getAvSyncTime(int32_t in_avSyncHwId, int64_t* _aidl_return) override;
85 ::ndk::ScopedAStatus close() override;
86 ::ndk::ScopedAStatus openDvr(DvrType in_type, int32_t in_bufferSize,
87 const std::shared_ptr<IDvrCallback>& in_cb,
88 std::shared_ptr<IDvr>* _aidl_return) override;
89 ::ndk::ScopedAStatus connectCiCam(int32_t in_ciCamId) override;
90 ::ndk::ScopedAStatus disconnectCiCam() override;
91
Hongguang2ecfc392021-11-23 10:29:15 -080092 binder_status_t dump(int fd, const char** args, uint32_t numArgs) override;
93
Hongguang4092f2f2021-07-08 18:49:12 -070094 // Functions interacts with Tuner Service
95 void stopFrontendInput();
96 ::ndk::ScopedAStatus removeFilter(int64_t filterId);
97 bool attachRecordFilter(int64_t filterId);
98 bool detachRecordFilter(int64_t filterId);
99 ::ndk::ScopedAStatus startFilterHandler(int64_t filterId);
100 void updateFilterOutput(int64_t filterId, vector<int8_t> data);
101 void updateMediaFilterOutput(int64_t filterId, vector<int8_t> data, uint64_t pts);
102 uint16_t getFilterTpid(int64_t filterId);
103 void setIsRecording(bool isRecording);
104 bool isRecording();
105 void startFrontendInputLoop();
sadiqsada028f2762023-10-31 16:09:25 -0700106 void readIptvThreadLoop(dtv_plugin* interface, dtv_streamer* streamer, size_t size,
sadiqsada56c98292023-11-02 16:45:31 -0700107 int timeout_ms, int buffer_timeout);
Hongguang4092f2f2021-07-08 18:49:12 -0700108
109 /**
110 * A dispatcher to read and dispatch input data to all the started filters.
111 * Each filter handler handles the data filtering/output writing/filterEvent updating.
112 * Note that recording filters are not included.
113 */
114 bool startBroadcastFilterDispatcher();
115 void startBroadcastTsFilter(vector<int8_t> data);
116
117 void sendFrontendInputToRecord(vector<int8_t> data);
118 void sendFrontendInputToRecord(vector<int8_t> data, uint16_t pid, uint64_t pts);
119 bool startRecordFilterDispatcher();
120
Kensuke Miyagi73b18ac2022-11-07 10:49:09 -0800121 void getDemuxInfo(DemuxInfo* demuxInfo);
122 int32_t getDemuxId();
123 bool isInUse();
124 void setInUse(bool inUse);
125 void setTunerService(std::shared_ptr<Tuner> tuner);
126
sadiqsada028f2762023-10-31 16:09:25 -0700127 /**
128 * Setter for IPTV Reading thread
129 */
130 void setIptvThreadRunning(bool isIptvThreadRunning);
131
Hongguang4092f2f2021-07-08 18:49:12 -0700132 private:
133 // Tuner service
Hongguang Chenff2c6b02021-08-07 00:12:26 +0000134 std::shared_ptr<Tuner> mTuner;
Hongguang4092f2f2021-07-08 18:49:12 -0700135
136 // Frontend source
137 std::shared_ptr<Frontend> mFrontend;
138
139 // A struct that passes the arguments to a newly created filter thread
140 struct ThreadArgs {
141 Demux* user;
142 int64_t filterId;
143 };
144
145 static void* __threadLoopFrontend(void* user);
146 void frontendInputThreadLoop();
147
148 /**
149 * To create a FilterMQ with the next available Filter ID.
150 * Creating Event Flag at the same time.
151 * Add the successfully created/saved FilterMQ into the local list.
152 *
153 * Return false is any of the above processes fails.
154 */
155 void deleteEventFlag();
156 bool readDataFromMQ();
157
158 int32_t mDemuxId = -1;
159 int32_t mCiCamId;
160 set<int64_t> mPcrFilterIds;
161 /**
162 * Record the last used filter id. Initial value is -1.
163 * Filter Id starts with 0.
164 */
165 int64_t mLastUsedFilterId = -1;
166 /**
167 * Record all the used playback filter Ids.
168 * Any removed filter id should be removed from this set.
169 */
170 set<int64_t> mPlaybackFilterIds;
171 /**
172 * Record all the attached record filter Ids.
173 * Any removed filter id should be removed from this set.
174 */
175 set<int64_t> mRecordFilterIds;
176 /**
177 * A list of created Filter sp.
178 * The array number is the filter ID.
179 */
180 std::map<int64_t, std::shared_ptr<Filter>> mFilters;
181
182 /**
183 * Local reference to the opened Timer Filter instance.
184 */
185 std::shared_ptr<TimeFilter> mTimeFilter;
186
187 /**
188 * Local reference to the opened DVR object.
189 */
190 std::shared_ptr<Dvr> mDvrPlayback;
191 std::shared_ptr<Dvr> mDvrRecord;
192
193 // Thread handlers
Hongguang901aa7b2021-08-26 12:20:56 -0700194 std::thread mFrontendInputThread;
sadiqsada56c98292023-11-02 16:45:31 -0700195 std::thread mDemuxIptvReadThread;
196
197 // track whether the DVR FMQ for IPTV Playback is full
198 bool mIsIptvDvrFMQFull = false;
Hongguang901aa7b2021-08-26 12:20:56 -0700199
Hongguang4092f2f2021-07-08 18:49:12 -0700200 /**
201 * If a specific filter's writing loop is still running
202 */
Hongguang901aa7b2021-08-26 12:20:56 -0700203 std::atomic<bool> mFrontendInputThreadRunning;
204 std::atomic<bool> mKeepFetchingDataFromFrontend;
205
Hongguang4092f2f2021-07-08 18:49:12 -0700206 /**
sadiqsada028f2762023-10-31 16:09:25 -0700207 * Controls IPTV reading thread status
208 */
209 bool mIsIptvReadThreadRunning;
210 std::mutex mIsIptvThreadRunningMutex;
211 std::condition_variable mIsIptvThreadRunningCv;
212
213 /**
Hongguang4092f2f2021-07-08 18:49:12 -0700214 * If the dvr recording is running.
215 */
216 bool mIsRecording = false;
217 /**
218 * Lock to protect writes to the FMQs
219 */
220 std::mutex mWriteLock;
Hongguang4092f2f2021-07-08 18:49:12 -0700221
222 // temp handle single PES filter
223 // TODO handle mulptiple Pes filters
224 int mPesSizeLeft = 0;
225 vector<uint8_t> mPesOutput;
226
227 const bool DEBUG_DEMUX = false;
Kensuke Miyagi73b18ac2022-11-07 10:49:09 -0800228
229 int32_t mFilterTypes;
230 bool mInUse = false;
Hongguang4092f2f2021-07-08 18:49:12 -0700231};
232
233} // namespace tuner
234} // namespace tv
235} // namespace hardware
236} // namespace android
237} // namespace aidl