blob: 8b002669dddecd6ea60c22e0c33ffdaa8e2a604d [file] [log] [blame]
Amyfd4243a2019-08-16 16:01:27 -07001/*
2 * Copyright (C) 2019 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_HARDWARE_TV_TUNER_V1_0_DEMUX_H_
18#define ANDROID_HARDWARE_TV_TUNER_V1_0_DEMUX_H_
19
20#include <android/hardware/tv/tuner/1.0/IDemux.h>
Amya609d5a2019-08-23 14:38:31 -070021#include <fmq/MessageQueue.h>
Amyfd4243a2019-08-16 16:01:27 -070022
23using namespace std;
24
25namespace android {
26namespace hardware {
27namespace tv {
28namespace tuner {
29namespace V1_0 {
30namespace implementation {
31
Amya609d5a2019-08-23 14:38:31 -070032using ::android::hardware::EventFlag;
33using ::android::hardware::kSynchronizedReadWrite;
34using ::android::hardware::MessageQueue;
35using ::android::hardware::MQDescriptorSync;
Amyfd4243a2019-08-16 16:01:27 -070036using ::android::hardware::tv::tuner::V1_0::IDemux;
Amya609d5a2019-08-23 14:38:31 -070037using ::android::hardware::tv::tuner::V1_0::IDemuxCallback;
Amyfd4243a2019-08-16 16:01:27 -070038using ::android::hardware::tv::tuner::V1_0::Result;
39
Amya609d5a2019-08-23 14:38:31 -070040using FilterMQ = MessageQueue<uint8_t, kSynchronizedReadWrite>;
41
Amyfd4243a2019-08-16 16:01:27 -070042class Demux : public IDemux {
43 public:
44 Demux(uint32_t demuxId);
45
46 virtual Return<Result> setFrontendDataSource(uint32_t frontendId) override;
47
48 virtual Return<Result> close() override;
49
Amya609d5a2019-08-23 14:38:31 -070050 virtual Return<void> addFilter(DemuxFilterType type, uint32_t bufferSize,
51 const sp<IDemuxCallback>& cb, addFilter_cb _hidl_cb) override;
52
53 virtual Return<void> getFilterQueueDesc(uint32_t filterId,
54 getFilterQueueDesc_cb _hidl_cb) override;
55
56 virtual Return<Result> configureFilter(uint32_t filterId,
57 const DemuxFilterSettings& settings) override;
58
59 virtual Return<Result> startFilter(uint32_t filterId) override;
60
61 virtual Return<Result> stopFilter(uint32_t filterId) override;
62
63 virtual Return<Result> flushFilter(uint32_t filterId) override;
64
65 virtual Return<Result> removeFilter(uint32_t filterId) override;
66
67 virtual Return<void> getAvSyncHwId(uint32_t filterId, getAvSyncHwId_cb _hidl_cb) override;
68
69 virtual Return<void> getAvSyncTime(AvSyncHwId avSyncHwId, getAvSyncTime_cb _hidl_cb) override;
70
Amyfd4243a2019-08-16 16:01:27 -070071 private:
72 virtual ~Demux();
Amya609d5a2019-08-23 14:38:31 -070073 /**
74 * To create a FilterMQ with the the next available Filter ID.
75 * Creating Event Flag at the same time.
76 * Add the successfully created/saved FilterMQ into the local list.
77 *
78 * Return false is any of the above processes fails.
79 */
80 bool createAndSaveMQ(uint32_t bufferSize, uint32_t filterId);
81 void deleteEventFlag();
82 bool writeDataToFilterMQ(const std::vector<uint8_t>& data, uint32_t filterId);
83 Result startSectionFilterHandler(DemuxFilterEvent event);
84 Result startPesFilterHandler(DemuxFilterEvent& event);
85 Result startTsFilterHandler();
86 Result startMediaFilterHandler(DemuxFilterEvent& event);
87 Result startRecordFilterHandler(DemuxFilterEvent& event);
88 Result startPcrFilterHandler();
89 bool writeSectionsAndCreateEvent(DemuxFilterEvent& event, uint32_t sectionNum);
90 void filterThreadLoop(DemuxFilterEvent* event);
91 static void* __threadLoop(void* data);
92
Amyfd4243a2019-08-16 16:01:27 -070093 uint32_t mDemuxId;
94 uint32_t mSourceFrontendId;
Amya609d5a2019-08-23 14:38:31 -070095 /**
96 * Record the last used filer id. Initial value is -1.
97 * Filter Id starts with 0.
98 */
99 uint32_t mLastUsedFilterId = -1;
100 /**
101 * A list of created FilterMQ ptrs.
102 * The array number is the filter ID.
103 */
104 vector<unique_ptr<FilterMQ>> mFilterMQs;
105 vector<DemuxFilterType> mFilterTypes;
106 vector<EventFlag*> mFilterEventFlags;
107 /**
108 * Demux callbacks used on filter events or IO buffer status
109 */
110 vector<sp<IDemuxCallback>> mDemuxCallbacks;
111 /**
112 * How many times a specific filter has written since started
113 */
114 vector<uint16_t> mFilterWriteCount;
115 pthread_t mThreadId = 0;
116 /**
117 * If a specific filter's writing loop is still running
118 */
119 vector<bool> mThreadRunning;
120 /**
121 * Lock to protect writes to the FMQs
122 */
123 std::mutex mWriteLock;
124 /**
125 * How many times a filter should write
126 * TODO make this dynamic/random/can take as a parameter
127 */
128 const uint16_t SECTION_WRITE_COUNT = 10;
129 // A struct that passes the arguments to a newly created filter thread
130 struct ThreadArgs {
131 Demux* user;
132 DemuxFilterEvent* event;
133 };
Amyfd4243a2019-08-16 16:01:27 -0700134};
135
136} // namespace implementation
137} // namespace V1_0
138} // namespace tuner
139} // namespace tv
140} // namespace hardware
141} // namespace android
142
143#endif // ANDROID_HARDWARE_TV_TUNER_V1_0_DEMUX_H_