summaryrefslogtreecommitdiff
path: root/setup.c
blob: 8a1697fcf76a6b6baefe061583f3bb02a6413c76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * See the README file for copyright information and how to reach the author.
 *
 * $Id$
 */

#include "setup.h"

#include <vdr/tools.h>
#include <vdr/menuitems.h>

#include <bcm_host.h>
#include "interface/vchiq_arm/vchiq_if.h"
#include "interface/vmcs_host/vc_tvservice.h"

cRpiSetup* cRpiSetup::s_instance = 0;

cRpiSetup* cRpiSetup::GetInstance(void)
{
	if (!s_instance)
		s_instance = new cRpiSetup();

	return s_instance;
}

void cRpiSetup::DropInstance(void)
{
	delete s_instance;
	s_instance = 0;

	bcm_host_deinit();
}

class cRpiSetupPage : public cMenuSetupPage
{

public:

	cRpiSetupPage(int *audioPort, int *passthrough, bool *audioSetupChanged) :
		m_audioPort(audioPort),
		m_passthrough(passthrough),
		m_audioSetupChanged(audioSetupChanged)
	{
	    static const char *const audioport[] = { tr("analog"), tr("HDMI") };

		m_newAudioPort = *m_audioPort;
		m_newPassthrough = *m_passthrough;

		Add(new cMenuEditStraItem(
				tr("Audio Port"), &m_newAudioPort, 2, audioport));
		Add(new cMenuEditBoolItem(
				tr("Digital Audio Pass-Through"), &m_newPassthrough));
	}

protected:

	virtual void Store(void)
	{
		*m_audioSetupChanged =
			(*m_audioPort != m_newAudioPort) ||
			(*m_passthrough != m_newPassthrough);

		SetupStore("AudioPort", *m_audioPort = m_newAudioPort);
		SetupStore("PassThrough", *m_passthrough = m_newPassthrough);
	}

private:

	int m_newAudioPort;
	int m_newPassthrough;

	int *m_audioPort;
	int *m_passthrough;

	bool *m_audioSetupChanged;

};

bool cRpiSetup::HwInit(void)
{
	bcm_host_init();

	if (!vc_gencmd_send("codec_enabled MPG2"))
	{
		char buffer[1024];
		if (!vc_gencmd_read_response(buffer, sizeof(buffer)))
		{
			if (!strcasecmp(buffer,"MPG2=enabled"))
				GetInstance()->m_mpeg2Enabled = true;
		}
	}

	int height = 0, width = 0;
	bool progressive = false;
	cVideoPort::ePort port = cVideoPort::eComposite;

	TV_DISPLAY_STATE_T tvstate;
	memset(&tvstate, 0, sizeof(TV_DISPLAY_STATE_T));
	if (!vc_tv_get_display_state(&tvstate))
	{
		// HDMI
		if ((tvstate.state & (VC_HDMI_HDMI | VC_HDMI_DVI)))
		{
			progressive = tvstate.display.hdmi.scan_mode == 0;
			height = tvstate.display.hdmi.height;
			width = tvstate.display.hdmi.width;
			port = cVideoPort::eHDMI;
		}
		else
		{
			height = tvstate.display.sdtv.height;
			width = tvstate.display.sdtv.width;
		}

		ILOG("using %s video output at %dx%d%s",
				cVideoPort::Str(port), width, height, progressive ? "p" : "i");

		GetInstance()->m_isProgressive = progressive;
		GetInstance()->m_displayHeight = height;
		GetInstance()->m_displayWidth = width;
	}
	else
	{
		ELOG("failed to get display parameters!");
		return false;
	}

	return true;
}

bool cRpiSetup::IsAudioFormatSupported(cAudioCodec::eCodec codec,
		int channels, int samplingRate)
{
	if (vc_tv_hdmi_audio_supported(
			codec == cAudioCodec::eMPG  ? EDID_AudioFormat_eMPEG1 :
			codec == cAudioCodec::eAC3  ? EDID_AudioFormat_eAC3   :
			codec == cAudioCodec::eEAC3 ? EDID_AudioFormat_eEAC3  :
			codec == cAudioCodec::eAAC ? EDID_AudioFormat_eAAC   :
					EDID_AudioFormat_ePCM, channels,
			samplingRate ==  32000 ? EDID_AudioSampleRate_e32KHz  :
			samplingRate ==  44100 ? EDID_AudioSampleRate_e44KHz  :
			samplingRate ==  88000 ? EDID_AudioSampleRate_e88KHz  :
			samplingRate ==  96000 ? EDID_AudioSampleRate_e96KHz  :
			samplingRate == 176000 ? EDID_AudioSampleRate_e176KHz :
			samplingRate == 192000 ? EDID_AudioSampleRate_e192KHz :
					EDID_AudioSampleRate_e48KHz,
					EDID_AudioSampleSize_16bit) == 0)
		return true;

	DLOG("%dch %s, %d.%dkHz not supported by HDMI device",
			channels, cAudioCodec::Str(codec),
			samplingRate / 1000, (samplingRate % 1000) / 100);

	return false;
}

int cRpiSetup::GetDisplaySize(int &width, int &height, double &aspect)
{
	height = GetInstance()->m_displayHeight;
	width = GetInstance()->m_displayWidth;
	aspect = (double)width / height;
	return 0;
}

bool cRpiSetup::HasAudioSetupChanged(void)
{
	if (!GetInstance()->m_audioSetupChanged)
		return false;

	GetInstance()->m_audioSetupChanged = false;
	return true;
}

cMenuSetupPage* cRpiSetup::GetSetupPage(void)
{
	return new cRpiSetupPage(&m_audioPort, &m_passthrough, &m_audioSetupChanged);
}

bool cRpiSetup::Parse(const char *name, const char *value)
{
	if (!strcasecmp(name, "AudioPort")) m_audioPort = atoi(value);
	else if (!strcasecmp(name, "PassThrough")) m_passthrough = atoi(value);
	else return false;

	return true;
}