blob: af040d4bfd5323d6c5d4aab03cfbcd8723227773 [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();
sadiqsada54945062024-01-18 13:39:05 -0800109 void frontendIptvInputThreadLoop(dtv_plugin* interface, dtv_streamer* streamer, void* buf);
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);
sadiqsada54945062024-01-18 13:39:05 -0800133 /**
134 * Stops IPTV playback reading thread.
135 */
136 void stopIptvFrontendInput();
sadiqsada028f2762023-10-31 16:09:25 -0700137
Hongguang4092f2f2021-07-08 18:49:12 -0700138 private:
139 // Tuner service
Hongguang Chenff2c6b02021-08-07 00:12:26 +0000140 std::shared_ptr<Tuner> mTuner;
Hongguang4092f2f2021-07-08 18:49:12 -0700141
142 // Frontend source
143 std::shared_ptr<Frontend> mFrontend;
144
145 // A struct that passes the arguments to a newly created filter thread
146 struct ThreadArgs {
147 Demux* user;
148 int64_t filterId;
149 };
150
151 static void* __threadLoopFrontend(void* user);
152 void frontendInputThreadLoop();
153
154 /**
155 * To create a FilterMQ with the next available Filter ID.
156 * Creating Event Flag at the same time.
157 * Add the successfully created/saved FilterMQ into the local list.
158 *
159 * Return false is any of the above processes fails.
160 */
161 void deleteEventFlag();
162 bool readDataFromMQ();
163
164 int32_t mDemuxId = -1;
165 int32_t mCiCamId;
166 set<int64_t> mPcrFilterIds;
167 /**
168 * Record the last used filter id. Initial value is -1.
169 * Filter Id starts with 0.
170 */
171 int64_t mLastUsedFilterId = -1;
172 /**
173 * Record all the used playback filter Ids.
174 * Any removed filter id should be removed from this set.
175 */
176 set<int64_t> mPlaybackFilterIds;
177 /**
178 * Record all the attached record filter Ids.
179 * Any removed filter id should be removed from this set.
180 */
181 set<int64_t> mRecordFilterIds;
182 /**
183 * A list of created Filter sp.
184 * The array number is the filter ID.
185 */
186 std::map<int64_t, std::shared_ptr<Filter>> mFilters;
187
188 /**
189 * Local reference to the opened Timer Filter instance.
190 */
191 std::shared_ptr<TimeFilter> mTimeFilter;
192
193 /**
194 * Local reference to the opened DVR object.
195 */
196 std::shared_ptr<Dvr> mDvrPlayback;
197 std::shared_ptr<Dvr> mDvrRecord;
198
199 // Thread handlers
Hongguang901aa7b2021-08-26 12:20:56 -0700200 std::thread mFrontendInputThread;
sadiqsada56c98292023-11-02 16:45:31 -0700201 std::thread mDemuxIptvReadThread;
202
203 // track whether the DVR FMQ for IPTV Playback is full
204 bool mIsIptvDvrFMQFull = false;
Hongguang901aa7b2021-08-26 12:20:56 -0700205
Hongguang4092f2f2021-07-08 18:49:12 -0700206 /**
207 * If a specific filter's writing loop is still running
208 */
Hongguang901aa7b2021-08-26 12:20:56 -0700209 std::atomic<bool> mFrontendInputThreadRunning;
210 std::atomic<bool> mKeepFetchingDataFromFrontend;
211
Hongguang4092f2f2021-07-08 18:49:12 -0700212 /**
sadiqsada028f2762023-10-31 16:09:25 -0700213 * Controls IPTV reading thread status
214 */
sadiqsada54945062024-01-18 13:39:05 -0800215 bool mIsIptvReadThreadRunning = false;
216 std::atomic<bool> mIsIptvReadThreadTerminated = false;
sadiqsada028f2762023-10-31 16:09:25 -0700217 std::mutex mIsIptvThreadRunningMutex;
218 std::condition_variable mIsIptvThreadRunningCv;
219
220 /**
Hongguang4092f2f2021-07-08 18:49:12 -0700221 * If the dvr recording is running.
222 */
223 bool mIsRecording = false;
224 /**
225 * Lock to protect writes to the FMQs
226 */
227 std::mutex mWriteLock;
Hongguang4092f2f2021-07-08 18:49:12 -0700228
229 // temp handle single PES filter
230 // TODO handle mulptiple Pes filters
231 int mPesSizeLeft = 0;
232 vector<uint8_t> mPesOutput;
233
234 const bool DEBUG_DEMUX = false;
Kensuke Miyagi73b18ac2022-11-07 10:49:09 -0800235
236 int32_t mFilterTypes;
237 bool mInUse = false;
Hongguang4092f2f2021-07-08 18:49:12 -0700238};
239
240} // namespace tuner
241} // namespace tv
242} // namespace hardware
243} // namespace android
244} // namespace aidl