1 /*
2 **
3 ** Copyright 2014, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 **     http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17 
18 #define LOG_TAG "AudioHAL_HDMIAudioOutput"
19 
20 #include <utils/Log.h>
21 
22 #include <stdint.h>
23 #include <sound/asound.h> // bionic
24 
25 #include "AudioHardwareOutput.h"
26 #include "AudioStreamOut.h"
27 #include "HDMIAudioOutput.h"
28 
29 namespace android {
30 
31 extern AudioHardwareOutput gAudioHardwareOutput;
32 
HDMIAudioOutput()33 HDMIAudioOutput::HDMIAudioOutput()
34     : AudioOutput(kHDMI_ALSADeviceName, PCM_FORMAT_S24_LE)
35 {
36 }
37 
~HDMIAudioOutput()38 HDMIAudioOutput::~HDMIAudioOutput()
39 {
40 }
41 
setupForStream(const AudioStreamOut & stream)42 status_t HDMIAudioOutput::setupForStream(const AudioStreamOut& stream)
43 {
44     mFramesPerChunk = stream.framesPerChunk();
45     mFramesPerSec = stream.outputSampleRate();
46     mBufferChunks = stream.nomChunksInFlight();
47     mChannelCnt = audio_channel_count_from_out_mask(stream.chanMask());
48 
49     ALOGI("setupForStream format %08x, rate = %u", stream.format(), mFramesPerSec);
50 
51     if (!gAudioHardwareOutput.getHDMIAudioCaps().supportsFormat(
52             stream.format(),
53             stream.sampleRate(),
54             mChannelCnt,
55             stream.isIec958NonAudio())) {
56         ALOGE("HDMI Sink does not support format = 0x%0X, srate = %d, #channels = 0%d",
57                 stream.format(), mFramesPerSec, mChannelCnt);
58         return BAD_VALUE;
59     }
60 
61     setupInternal();
62 
63     setChannelStatusToCompressed(stream.isIec958NonAudio());
64 
65     return initCheck();
66 }
67 
applyPendingVolParams()68 void HDMIAudioOutput::applyPendingVolParams()
69 {
70 }
71 
72 #define IEC958_AES0_NONAUDIO      (1<<1)   /* 0 = audio, 1 = non-audio */
73 
setChannelStatusToCompressed(bool compressed)74 void HDMIAudioOutput::setChannelStatusToCompressed(bool compressed)
75 {
76     struct snd_aes_iec958  iec958;
77     struct mixer* mixer;
78     int err;
79     const size_t count = 1;
80 
81     ALOGI("setChannelStatusToCompressed %d", compressed);
82 
83     mixer = mixer_open(mALSACardID);
84     if (mixer == NULL) {
85         ALOGE("Couldn't open mixer on alsa id %d", mALSACardID);
86         return;
87     }
88 
89     const char *ctlName = "IEC958 Playback Default";
90     struct mixer_ctl *ctl = mixer_get_ctl_by_name(mixer, ctlName);
91     if (ctl == NULL) {
92         ALOGE("Couldn't get mixer ctl %s", ctlName);
93         goto finish;
94     }
95 
96     // Set count to 1 so we get one complete iec958 structure.
97     err = mixer_ctl_get_array(ctl, &iec958, count);
98     if (err < 0) {
99         ALOGE("Channel Status bit get has failed\n");
100         goto finish;
101     }
102 
103     if (compressed) {
104         iec958.status[0] |= IEC958_AES0_NONAUDIO;
105     } else {
106         iec958.status[0] &= ~IEC958_AES0_NONAUDIO;
107     }
108 
109     err = mixer_ctl_set_array(ctl, &iec958, count);
110     if (err < 0) {
111         ALOGE("Channel Status bit set has failed\n");
112     }
113 
114 finish:
115     mixer_close(mixer);
116 }
117 
dump(String8 & result)118 void HDMIAudioOutput::dump(String8& result)
119 {
120     const size_t SIZE = 256;
121     char buffer[SIZE];
122 
123     snprintf(buffer, SIZE,
124             "\t%s Audio Output\n"
125             "\t\tSample Rate       : %d\n"
126             "\t\tChannel Count     : %d\n"
127             "\t\tState             : %d\n",
128             getOutputName(),
129             mFramesPerSec,
130             mChannelCnt,
131             mState);
132     result.append(buffer);
133 }
134 
135 } // namespace android
136