• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 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_MEDIA_TV_FILTER_CLIENT_H_
18 #define _ANDROID_MEDIA_TV_FILTER_CLIENT_H_
19 
20 #include <aidl/android/media/tv/tuner/ITunerFilter.h>
21 #include <aidl/android/media/tv/tuner/BnTunerFilterCallback.h>
22 #include <aidl/android/media/tv/tuner/TunerFilterEvent.h>
23 #include <aidl/android/media/tv/tuner/TunerFilterSettings.h>
24 #include <aidlcommonsupport/NativeHandle.h>
25 #include <android/hardware/tv/tuner/1.1/IFilter.h>
26 #include <android/hardware/tv/tuner/1.1/IFilterCallback.h>
27 #include <android/hardware/tv/tuner/1.1/types.h>
28 #include <fmq/AidlMessageQueue.h>
29 #include <fmq/MessageQueue.h>
30 
31 #include "ClientHelper.h"
32 #include "FilterClientCallback.h"
33 
34 using Status = ::ndk::ScopedAStatus;
35 using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite;
36 using ::aidl::android::media::tv::tuner::BnTunerFilterCallback;
37 using ::aidl::android::media::tv::tuner::ITunerFilter;
38 using ::aidl::android::media::tv::tuner::TunerDemuxIpAddress;
39 using ::aidl::android::media::tv::tuner::TunerFilterAvSettings;
40 using ::aidl::android::media::tv::tuner::TunerFilterConfiguration;
41 using ::aidl::android::media::tv::tuner::TunerFilterDownloadSettings;
42 using ::aidl::android::media::tv::tuner::TunerFilterEvent;
43 using ::aidl::android::media::tv::tuner::TunerFilterPesDataSettings;
44 using ::aidl::android::media::tv::tuner::TunerFilterRecordSettings;
45 using ::aidl::android::media::tv::tuner::TunerFilterSectionSettings;
46 using ::aidl::android::media::tv::tuner::TunerFilterSettings;
47 
48 using ::android::hardware::EventFlag;
49 using ::android::hardware::MessageQueue;
50 using ::android::hardware::MQDescriptorSync;
51 using ::android::hardware::Return;
52 using ::android::hardware::Void;
53 using ::android::hardware::hidl_handle;
54 using ::android::hardware::tv::tuner::V1_0::DemuxAlpFilterSettings;
55 using ::android::hardware::tv::tuner::V1_0::DemuxMmtpFilterSettings;
56 using ::android::hardware::tv::tuner::V1_0::DemuxFilterAvSettings;
57 using ::android::hardware::tv::tuner::V1_0::DemuxFilterDownloadSettings;
58 using ::android::hardware::tv::tuner::V1_0::DemuxFilterPesDataSettings;
59 using ::android::hardware::tv::tuner::V1_0::DemuxFilterRecordSettings;
60 using ::android::hardware::tv::tuner::V1_0::DemuxFilterSectionSettings;
61 using ::android::hardware::tv::tuner::V1_0::DemuxFilterSettings;
62 using ::android::hardware::tv::tuner::V1_0::DemuxFilterType;
63 using ::android::hardware::tv::tuner::V1_0::DemuxIpAddress;
64 using ::android::hardware::tv::tuner::V1_0::DemuxIpFilterSettings;
65 using ::android::hardware::tv::tuner::V1_0::DemuxTlvFilterSettings;
66 using ::android::hardware::tv::tuner::V1_0::DemuxTsFilterSettings;
67 using ::android::hardware::tv::tuner::V1_0::IFilter;
68 using ::android::hardware::tv::tuner::V1_0::Result;
69 using ::android::hardware::tv::tuner::V1_1::AvStreamType;
70 using ::android::hardware::tv::tuner::V1_1::IFilterCallback;
71 
72 using namespace std;
73 
74 namespace android {
75 
76 using MQ = MessageQueue<uint8_t, kSynchronizedReadWrite>;
77 using MQDesc = MQDescriptorSync<uint8_t>;
78 using AidlMQ = AidlMessageQueue<int8_t, SynchronizedReadWrite>;
79 using AidlMQDesc = MQDescriptor<int8_t, SynchronizedReadWrite>;
80 
81 struct SharedHandleInfo {
82     native_handle_t* sharedHandle;
83     uint64_t size;
84 };
85 
86 class TunerFilterCallback : public BnTunerFilterCallback {
87 
88 public:
89     TunerFilterCallback(sp<FilterClientCallback> filterClientCallback);
90     Status onFilterStatus(int status);
91     Status onFilterEvent(const vector<TunerFilterEvent>& filterEvents);
92 
93 private:
94     void getHidlFilterEvent(const vector<TunerFilterEvent>& filterEvents,
95             DemuxFilterEvent& event, DemuxFilterEventExt& eventExt);
96     void getHidlMediaEvent(
97             const vector<TunerFilterEvent>& filterEvents, DemuxFilterEvent& event);
98     void getHidlSectionEvent(
99             const vector<TunerFilterEvent>& filterEvents, DemuxFilterEvent& event);
100     void getHidlPesEvent(
101             const vector<TunerFilterEvent>& filterEvents, DemuxFilterEvent& event);
102     void getHidlTsRecordEvent(const vector<TunerFilterEvent>& filterEvents,
103             DemuxFilterEvent& event, DemuxFilterEventExt& eventExt);
104     void getHidlMmtpRecordEvent(const vector<TunerFilterEvent>& filterEvents,
105             DemuxFilterEvent& event, DemuxFilterEventExt& eventExt);
106     void getHidlDownloadEvent(
107             const vector<TunerFilterEvent>& filterEvents, DemuxFilterEvent& event);
108     void getHidlIpPayloadEvent(
109             const vector<TunerFilterEvent>& filterEvents, DemuxFilterEvent& event);
110     void getHidlTemiEvent(
111             const vector<TunerFilterEvent>& filterEvents, DemuxFilterEvent& event);
112     void getHidlMonitorEvent(
113             const vector<TunerFilterEvent>& filterEvents, DemuxFilterEventExt& eventExt);
114     void getHidlRestartEvent(
115             const vector<TunerFilterEvent>& filterEvents, DemuxFilterEventExt& eventExt);
116 
117     sp<FilterClientCallback> mFilterClientCallback;
118 };
119 
120 struct HidlFilterCallback : public IFilterCallback {
121 
122 public:
123     HidlFilterCallback(sp<FilterClientCallback> filterClientCallback);
124     virtual Return<void> onFilterEvent_1_1(const DemuxFilterEvent& filterEvent,
125             const DemuxFilterEventExt& filterEventExt);
126     virtual Return<void> onFilterEvent(const DemuxFilterEvent& filterEvent);
127     virtual Return<void> onFilterStatus(const DemuxFilterStatus status);
128 
129 private:
130     sp<FilterClientCallback> mFilterClientCallback;
131 };
132 
133 struct FilterClient : public RefBase {
134 
135 public:
136     FilterClient(DemuxFilterType type, shared_ptr<ITunerFilter> tunerFilter);
137     ~FilterClient();
138 
139     // TODO: remove after migration to Tuner Service is done.
140     void setHidlFilter(sp<IFilter> filter);
141 
142     /**
143      * Read size of data from filter FMQ into buffer.
144      *
145      * @return the actual reading size. -1 if failed to read.
146      */
147     int read(int8_t* buffer, int size);
148 
149     /**
150      * Get the a/v shared memory handle information
151      */
152     SharedHandleInfo getAvSharedHandleInfo();
153 
154     /**
155      * Configure the filter.
156      */
157     Result configure(DemuxFilterSettings configure);
158 
159     /**
160      * Configure the monitor event of the Filter.
161      */
162     Result configureMonitorEvent(int monitorEventType);
163 
164     /**
165      * Configure the context id of the IP Filter.
166      */
167     Result configureIpFilterContextId(int cid);
168 
169     /**
170      * Configure the stream type of the media Filter.
171      */
172     Result configureAvStreamType(AvStreamType avStreamType);
173 
174     /**
175      * Start the filter.
176      */
177     Result start();
178 
179     /**
180      * Stop the filter.
181      */
182     Result stop();
183 
184     /**
185      * Flush the filter.
186      */
187     Result flush();
188 
189     /**
190      * Get the 32-bit filter Id.
191      */
192     Result getId(uint32_t& id);
193 
194     /**
195      * Get the 64-bit filter Id.
196      */
197     Result getId64Bit(uint64_t& id);
198 
199     /**
200      * Release the handle reported by the HAL for AV memory.
201      */
202     Result releaseAvHandle(native_handle_t* handle, uint64_t avDataId);
203 
204     /**
205      * Set the filter's data source.
206      */
207     Result setDataSource(sp<FilterClient> filterClient);
208 
209     /**
210      * Get the Hal filter to build up filter linkage.
211      */
getHalFilterFilterClient212     sp<IFilter> getHalFilter() { return mFilter; }
213 
214     /**
215      * Get the Aidl filter to build up filter linkage.
216      */
getAidlFilterFilterClient217     shared_ptr<ITunerFilter> getAidlFilter() { return mTunerFilter; }
218 
219     /**
220      * Close a new interface of ITunerFilter.
221      */
222     Result close();
223 
224 private:
225     TunerFilterConfiguration getAidlFilterSettings(DemuxFilterSettings configure);
226 
227     TunerFilterConfiguration getAidlTsSettings(DemuxTsFilterSettings configure);
228     TunerFilterConfiguration getAidlMmtpSettings(DemuxMmtpFilterSettings mmtp);
229     TunerFilterConfiguration getAidlIpSettings(DemuxIpFilterSettings ip);
230     TunerFilterConfiguration getAidlTlvSettings(DemuxTlvFilterSettings tlv);
231     TunerFilterConfiguration getAidlAlpSettings(DemuxAlpFilterSettings alp);
232 
233     TunerFilterAvSettings getAidlAvSettings(DemuxFilterAvSettings hidlAv);
234     TunerFilterSectionSettings getAidlSectionSettings(DemuxFilterSectionSettings hidlSection);
235     TunerFilterPesDataSettings getAidlPesDataSettings(DemuxFilterPesDataSettings hidlPesData);
236     TunerFilterRecordSettings getAidlRecordSettings(DemuxFilterRecordSettings hidlRecord);
237     TunerFilterDownloadSettings getAidlDownloadSettings(DemuxFilterDownloadSettings hidlDownload);
238 
239     void getAidlIpAddress(DemuxIpAddress ipAddr,
240             TunerDemuxIpAddress& srcIpAddress, TunerDemuxIpAddress& dstIpAddress);
241     Result getFilterMq();
242     int copyData(int8_t* buffer, int size);
243     void checkIsMediaFilter(DemuxFilterType type);
244     void checkIsPassthroughFilter(DemuxFilterSettings configure);
245     void handleAvShareMemory();
246     void closeAvSharedMemory();
247 
248     /**
249      * An AIDL Tuner Filter Singleton assigned at the first time when the Tuner Client
250      * opens a filter. Default null when Tuner Service does not exist.
251      */
252     shared_ptr<ITunerFilter> mTunerFilter;
253 
254     /**
255      * A 1.0 Filter HAL interface that is ready before migrating to the TunerFilter.
256      * This is a temprary interface before Tuner Framework migrates to use TunerService.
257      * Default null when the HAL service does not exist.
258      */
259     sp<IFilter> mFilter;
260 
261     /**
262      * A 1.1 Filter HAL interface that is ready before migrating to the TunerFilter.
263      * This is a temprary interface before Tuner Framework migrates to use TunerService.
264      * Default null when the HAL service does not exist.
265      */
266     sp<::android::hardware::tv::tuner::V1_1::IFilter> mFilter_1_1;
267 
268     AidlMQ* mFilterMQ = NULL;
269     EventFlag* mFilterMQEventFlag = NULL;
270 
271     native_handle_t* mAvSharedHandle;
272     uint64_t mAvSharedMemSize;
273     bool mIsMediaFilter;
274     bool mIsPassthroughFilter;
275 };
276 }  // namespace android
277 
278 #endif  // _ANDROID_MEDIA_TV_FILTER_CLIENT_H_
279