1/*
2 * Copyright (C) 2015 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 DVB_MANAGER_H_
18#define DVB_MANAGER_H_
19
20#include <jni.h>
21#include <map>
22
23#include "mutex.h"
24#include "tunertvinput_jni.h"
25
26class DvbManager {
27    static const int NUM_POLLFDS = 1;
28    static const int FE_LOCK_CHECK_INTERNAL_US = 100 * 1000;
29    static const int FE_CONSECUTIVE_LOCK_SUCCESS_COUNT = 1;
30    static const int DVB_ERROR_RETRY_INTERVAL_MS = 100 * 1000;
31    static const int DVB_TUNE_STOP_DELAY_MS = 100 * 1000;
32    static const int FE_POLL_TIMEOUT_MS = 100;
33    static const int PAT_PID = 0;
34
35    static const int FILTER_TYPE_OTHER =
36            com_android_tv_tuner_TunerHal_FILTER_TYPE_OTHER;
37    static const int FILTER_TYPE_AUDIO =
38            com_android_tv_tuner_TunerHal_FILTER_TYPE_AUDIO;
39    static const int FILTER_TYPE_VIDEO =
40            com_android_tv_tuner_TunerHal_FILTER_TYPE_VIDEO;
41    static const int FILTER_TYPE_PCR =
42            com_android_tv_tuner_TunerHal_FILTER_TYPE_PCR;
43
44    int mFeFd;
45    int mDemuxFd;
46    int mDvrFd;
47    int mPatFilterFd;
48    bool mFeHasLock;
49    // Flag for pending tune request. Used for canceling the current tune operation.
50    bool volatile mHasPendingTune;
51    std::map<int, int> mPidFilters;
52    Mutex mFilterLock;
53    jmethodID mOpenDvbFrontEndMethodID;
54    jmethodID mOpenDvbDemuxMethodID;
55    jmethodID mOpenDvbDvrMethodID;
56
57public:
58    DvbManager(JNIEnv *env, jobject thiz);
59    ~DvbManager();
60    int tune(JNIEnv *env, jobject thiz,
61            const int frequency, const char *modulationStr, int timeout_ms);
62    int stopTune();
63    int readTsStream(JNIEnv *env, jobject thiz,
64            uint8_t *tsBuffer, int tsBufferSize, int timeout_ms);
65    int startTsPidFilter(JNIEnv *env, jobject thiz, int pid, int filterType);
66    void closeAllDvbPidFilter();
67    void setHasPendingTune(bool hasPendingTune);
68
69private:
70    int openDvbFe(JNIEnv *env, jobject thiz);
71    int openDvbDvr(JNIEnv *env, jobject thiz);
72    void closePatFilter();
73    void closeDvbFe();
74    void closeDvbDvr();
75    void reset();
76    void resetExceptFe();
77    bool isFeLocked();
78    // Call to java method
79    int openDvbFeFromSystemApi(JNIEnv *env, jobject thiz);
80    int openDvbDemuxFromSystemApi(JNIEnv *env, jobject thiz);
81    int openDvbDvrFromSystemApi(JNIEnv *env, jobject thiz);
82};
83
84#endif  // DVB_MANAGER_H_
85