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 PAT_PID = 0;
33
34    static const int FILTER_TYPE_OTHER =
35            com_android_usbtuner_TunerHal_FILTER_TYPE_OTHER;
36    static const int FILTER_TYPE_AUDIO =
37            com_android_usbtuner_TunerHal_FILTER_TYPE_AUDIO;
38    static const int FILTER_TYPE_VIDEO =
39            com_android_usbtuner_TunerHal_FILTER_TYPE_VIDEO;
40    static const int FILTER_TYPE_PCR =
41            com_android_usbtuner_TunerHal_FILTER_TYPE_PCR;
42
43    int mFeFd;
44    int mDemuxFd;
45    int mDvrFd;
46    int mPatFilterFd;
47    bool mFeHasLock;
48    std::map<int, int> mPidFilters;
49    Mutex mFilterLock;
50    jmethodID mOpenDvbFrontEndMethodID;
51    jmethodID mOpenDvbDemuxMethodID;
52    jmethodID mOpenDvbDvrMethodID;
53
54public:
55    DvbManager(JNIEnv *env, jobject thiz);
56    ~DvbManager();
57    int tune(JNIEnv *env, jobject thiz,
58            const int frequency, const char *modulationStr, int timeout_ms);
59    int stopTune();
60    int readTsStream(JNIEnv *env, jobject thiz,
61            uint8_t *tsBuffer, int tsBufferSize, int timeout_ms);
62    int startTsPidFilter(JNIEnv *env, jobject thiz, int pid, int filterType);
63    void closeAllDvbPidFilter();
64
65private:
66    int openDvbFe(JNIEnv *env, jobject thiz);
67    int openDvbDvr(JNIEnv *env, jobject thiz);
68    void closePatFilter();
69    void closeDvbFe();
70    void closeDvbDvr();
71    void reset();
72    void resetExceptFe();
73    bool isFeLocked();
74    // Call to java method
75    int openDvbFeFromSystemApi(JNIEnv *env, jobject thiz);
76    int openDvbDemuxFromSystemApi(JNIEnv *env, jobject thiz);
77    int openDvbDvrFromSystemApi(JNIEnv *env, jobject thiz);
78};
79
80#endif  // DVB_MANAGER_H_
81