blob: 1b789bd5d08b23a7a7e1658b7f0d1e6813c74f6c [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>
20
21#include <fmq/AidlMessageQueue.h>
22#include <math.h>
Hongguang901aa7b2021-08-26 12:20:56 -070023#include <atomic>
Hongguang4092f2f2021-07-08 18:49:12 -070024#include <set>
Hongguang901aa7b2021-08-26 12:20:56 -070025#include <thread>
26
Hongguang4092f2f2021-07-08 18:49:12 -070027#include "Dvr.h"
28#include "Filter.h"
29#include "Frontend.h"
30#include "TimeFilter.h"
31#include "Tuner.h"
32
33using namespace std;
34
35namespace aidl {
36namespace android {
37namespace hardware {
38namespace tv {
39namespace tuner {
40
41using ::aidl::android::hardware::common::fmq::MQDescriptor;
42using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite;
43using ::android::AidlMessageQueue;
44using ::android::hardware::EventFlag;
45
46using FilterMQ = AidlMessageQueue<int8_t, SynchronizedReadWrite>;
47
48class Dvr;
49class Filter;
50class Frontend;
51class TimeFilter;
52class Tuner;
53
54class Demux : public BnDemux {
55 public:
Hongguang Chenff2c6b02021-08-07 00:12:26 +000056 Demux(int32_t demuxId, std::shared_ptr<Tuner> tuner);
Hongguang4092f2f2021-07-08 18:49:12 -070057 ~Demux();
58
59 ::ndk::ScopedAStatus setFrontendDataSource(int32_t in_frontendId) override;
60 ::ndk::ScopedAStatus openFilter(const DemuxFilterType& in_type, int32_t in_bufferSize,
61 const std::shared_ptr<IFilterCallback>& in_cb,
62 std::shared_ptr<IFilter>* _aidl_return) override;
63 ::ndk::ScopedAStatus openTimeFilter(std::shared_ptr<ITimeFilter>* _aidl_return) override;
64 ::ndk::ScopedAStatus getAvSyncHwId(const std::shared_ptr<IFilter>& in_filter,
65 int32_t* _aidl_return) override;
66 ::ndk::ScopedAStatus getAvSyncTime(int32_t in_avSyncHwId, int64_t* _aidl_return) override;
67 ::ndk::ScopedAStatus close() override;
68 ::ndk::ScopedAStatus openDvr(DvrType in_type, int32_t in_bufferSize,
69 const std::shared_ptr<IDvrCallback>& in_cb,
70 std::shared_ptr<IDvr>* _aidl_return) override;
71 ::ndk::ScopedAStatus connectCiCam(int32_t in_ciCamId) override;
72 ::ndk::ScopedAStatus disconnectCiCam() override;
73
74 // Functions interacts with Tuner Service
75 void stopFrontendInput();
76 ::ndk::ScopedAStatus removeFilter(int64_t filterId);
77 bool attachRecordFilter(int64_t filterId);
78 bool detachRecordFilter(int64_t filterId);
79 ::ndk::ScopedAStatus startFilterHandler(int64_t filterId);
80 void updateFilterOutput(int64_t filterId, vector<int8_t> data);
81 void updateMediaFilterOutput(int64_t filterId, vector<int8_t> data, uint64_t pts);
82 uint16_t getFilterTpid(int64_t filterId);
83 void setIsRecording(bool isRecording);
84 bool isRecording();
85 void startFrontendInputLoop();
86
87 /**
88 * A dispatcher to read and dispatch input data to all the started filters.
89 * Each filter handler handles the data filtering/output writing/filterEvent updating.
90 * Note that recording filters are not included.
91 */
92 bool startBroadcastFilterDispatcher();
93 void startBroadcastTsFilter(vector<int8_t> data);
94
95 void sendFrontendInputToRecord(vector<int8_t> data);
96 void sendFrontendInputToRecord(vector<int8_t> data, uint16_t pid, uint64_t pts);
97 bool startRecordFilterDispatcher();
98
99 private:
100 // Tuner service
Hongguang Chenff2c6b02021-08-07 00:12:26 +0000101 std::shared_ptr<Tuner> mTuner;
Hongguang4092f2f2021-07-08 18:49:12 -0700102
103 // Frontend source
104 std::shared_ptr<Frontend> mFrontend;
105
106 // A struct that passes the arguments to a newly created filter thread
107 struct ThreadArgs {
108 Demux* user;
109 int64_t filterId;
110 };
111
112 static void* __threadLoopFrontend(void* user);
113 void frontendInputThreadLoop();
114
115 /**
116 * To create a FilterMQ with the next available Filter ID.
117 * Creating Event Flag at the same time.
118 * Add the successfully created/saved FilterMQ into the local list.
119 *
120 * Return false is any of the above processes fails.
121 */
122 void deleteEventFlag();
123 bool readDataFromMQ();
124
125 int32_t mDemuxId = -1;
126 int32_t mCiCamId;
127 set<int64_t> mPcrFilterIds;
128 /**
129 * Record the last used filter id. Initial value is -1.
130 * Filter Id starts with 0.
131 */
132 int64_t mLastUsedFilterId = -1;
133 /**
134 * Record all the used playback filter Ids.
135 * Any removed filter id should be removed from this set.
136 */
137 set<int64_t> mPlaybackFilterIds;
138 /**
139 * Record all the attached record filter Ids.
140 * Any removed filter id should be removed from this set.
141 */
142 set<int64_t> mRecordFilterIds;
143 /**
144 * A list of created Filter sp.
145 * The array number is the filter ID.
146 */
147 std::map<int64_t, std::shared_ptr<Filter>> mFilters;
148
149 /**
150 * Local reference to the opened Timer Filter instance.
151 */
152 std::shared_ptr<TimeFilter> mTimeFilter;
153
154 /**
155 * Local reference to the opened DVR object.
156 */
157 std::shared_ptr<Dvr> mDvrPlayback;
158 std::shared_ptr<Dvr> mDvrRecord;
159
160 // Thread handlers
Hongguang901aa7b2021-08-26 12:20:56 -0700161 std::thread mFrontendInputThread;
162
Hongguang4092f2f2021-07-08 18:49:12 -0700163 /**
164 * If a specific filter's writing loop is still running
165 */
Hongguang901aa7b2021-08-26 12:20:56 -0700166 std::atomic<bool> mFrontendInputThreadRunning;
167 std::atomic<bool> mKeepFetchingDataFromFrontend;
168
Hongguang4092f2f2021-07-08 18:49:12 -0700169 /**
170 * If the dvr recording is running.
171 */
172 bool mIsRecording = false;
173 /**
174 * Lock to protect writes to the FMQs
175 */
176 std::mutex mWriteLock;
Hongguang4092f2f2021-07-08 18:49:12 -0700177
178 // temp handle single PES filter
179 // TODO handle mulptiple Pes filters
180 int mPesSizeLeft = 0;
181 vector<uint8_t> mPesOutput;
182
183 const bool DEBUG_DEMUX = false;
184};
185
186} // namespace tuner
187} // namespace tv
188} // namespace hardware
189} // namespace android
190} // namespace aidl