blob: fbb778ca6cfe488388eaa2f0055fda4a5da69e5d [file] [log] [blame]
Amyb4b68012019-10-15 17:38:19 -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_DVR_H_
18#define ANDROID_HARDWARE_TV_TUNER_V1_0_DVR_H_
19
20#include <android/hardware/tv/tuner/1.0/IDvr.h>
21#include <fmq/MessageQueue.h>
22#include <math.h>
23#include <set>
24#include "Demux.h"
25#include "Frontend.h"
26#include "Tuner.h"
27
28using namespace std;
29
30namespace android {
31namespace hardware {
32namespace tv {
33namespace tuner {
34namespace V1_0 {
35namespace implementation {
36
37using ::android::hardware::EventFlag;
38using ::android::hardware::kSynchronizedReadWrite;
39using ::android::hardware::MessageQueue;
40using ::android::hardware::MQDescriptorSync;
41using ::android::hardware::tv::tuner::V1_0::IDemux;
42using ::android::hardware::tv::tuner::V1_0::IDvrCallback;
43using ::android::hardware::tv::tuner::V1_0::Result;
44
45using DvrMQ = MessageQueue<uint8_t, kSynchronizedReadWrite>;
46
47class Demux;
48class Filter;
49class Frontend;
50class Tuner;
51
52class Dvr : public IDvr {
53 public:
54 Dvr();
55
56 Dvr(DvrType type, uint32_t bufferSize, const sp<IDvrCallback>& cb, sp<Demux> demux);
57
58 ~Dvr();
59
60 virtual Return<void> getQueueDesc(getQueueDesc_cb _hidl_cb) override;
61
62 virtual Return<Result> configure(const DvrSettings& settings) override;
63
64 virtual Return<Result> attachFilter(const sp<IFilter>& filter) override;
65
66 virtual Return<Result> detachFilter(const sp<IFilter>& filter) override;
67
68 virtual Return<Result> start() override;
69
70 virtual Return<Result> stop() override;
71
72 virtual Return<Result> flush() override;
73
74 virtual Return<Result> close() override;
75
76 /**
77 * To create a DvrMQ and its Event Flag.
78 *
79 * Return false is any of the above processes fails.
80 */
81 bool createDvrMQ();
82
83 private:
84 // Demux service
85 sp<Demux> mDemux;
86
87 DvrType mType;
88 uint32_t mBufferSize;
89 sp<IDvrCallback> mCallback;
90 std::map<uint32_t, sp<IFilter>> mFilters;
91
92 void deleteEventFlag();
93 bool readDataFromMQ();
94 void maySendPlaybackStatusCallback();
95 void maySendRecordStatusCallback();
96 PlaybackStatus checkPlaybackStatusChange(uint32_t availableToWrite, uint32_t availableToRead,
97 uint32_t highThreshold, uint32_t lowThreshold);
98 /**
99 * A dispatcher to read and dispatch input data to all the started filters.
100 * Each filter handler handles the data filtering/output writing/filterEvent updating.
101 */
102 bool readPlaybackFMQ();
103 void startTpidFilter(vector<uint8_t> data);
104 bool startFilterDispatcher();
105 static void* __threadLoopPlayback(void* user);
106 static void* __threadLoopBroadcast(void* user);
107 void playbackThreadLoop();
108 void broadcastInputThreadLoop();
109
110 unique_ptr<DvrMQ> mDvrMQ;
111 EventFlag* mDvrEventFlag;
112 /**
113 * Demux callbacks used on filter events or IO buffer status
114 */
115 bool mDvrConfigured = false;
116 DvrSettings mDvrSettings;
117
118 // Thread handlers
119 pthread_t mDvrThread;
120 pthread_t mBroadcastInputThread;
121
122 // FMQ status local records
123 PlaybackStatus mPlaybackStatus;
124 /**
125 * If a specific filter's writing loop is still running
126 */
127 bool mDvrThreadRunning;
128 bool mBroadcastInputThreadRunning;
129 bool mKeepFetchingDataFromFrontend;
130 /**
131 * Lock to protect writes to the FMQs
132 */
133 std::mutex mWriteLock;
134 /**
135 * Lock to protect writes to the input status
136 */
137 std::mutex mPlaybackStatusLock;
138 std::mutex mBroadcastInputThreadLock;
139 std::mutex mDvrThreadLock;
140
141 const bool DEBUG_DVR = false;
142};
143
144} // namespace implementation
145} // namespace V1_0
146} // namespace tuner
147} // namespace tv
148} // namespace hardware
149} // namespace android
150
151#endif // ANDROID_HARDWARE_TV_TUNER_V1_0_DVR_H_