1 /*
2  *
3  * Copyright 2015, 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 "AudioFlinger"
19 //#define LOG_NDEBUG 0
20 #include "Configuration.h"
21 #include <system/audio.h>
22 #include <utils/Log.h>
23 
24 #include <audio_utils/spdif/SPDIFEncoder.h>
25 
26 #include "AudioHwDevice.h"
27 #include "SpdifStreamOut.h"
28 
29 namespace android {
30 
31 /**
32  * If the AudioFlinger is processing encoded data and the HAL expects
33  * PCM then we need to wrap the data in an SPDIF wrapper.
34  */
SpdifStreamOut(AudioHwDevice * dev,audio_output_flags_t flags,audio_format_t format)35 SpdifStreamOut::SpdifStreamOut(AudioHwDevice *dev,
36             audio_output_flags_t flags,
37             audio_format_t format)
38         // Tell the HAL that the data will be compressed audio wrapped in a data burst.
39         : AudioStreamOut(dev, (audio_output_flags_t) (flags | AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO))
40         , mSpdifEncoder(this, format)
41 {
42 }
43 
open(audio_io_handle_t handle,audio_devices_t devices,struct audio_config * config,const char * address)44 status_t SpdifStreamOut::open(
45         audio_io_handle_t handle,
46         audio_devices_t devices,
47         struct audio_config *config,
48         const char *address)
49 {
50     struct audio_config customConfig = *config;
51 
52     mApplicationConfig.format = config->format;
53     mApplicationConfig.sample_rate = config->sample_rate;
54     mApplicationConfig.channel_mask = config->channel_mask;
55 
56     mRateMultiplier = spdif_rate_multiplier(config->format);
57     if (mRateMultiplier <= 0) {
58         ALOGE("ERROR SpdifStreamOut::open() unrecognized format 0x%08X\n", config->format);
59         return BAD_VALUE;
60     }
61     customConfig.sample_rate = config->sample_rate * mRateMultiplier;
62 
63     customConfig.format = AUDIO_FORMAT_PCM_16_BIT;
64     customConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
65 
66     // Always print this because otherwise it could be very confusing if the
67     // HAL and AudioFlinger are using different formats.
68     // Print before open() because HAL may modify customConfig.
69     ALOGI("SpdifStreamOut::open() AudioFlinger requested sampleRate %d, format %#x,"
70             " channelMask %#x", config->sample_rate, config->format, config->channel_mask);
71     ALOGI("SpdifStreamOut::open() HAL configured for sampleRate %d, format %#x, channelMask %#x",
72             customConfig.sample_rate, customConfig.format, customConfig.channel_mask);
73 
74     const status_t status = AudioStreamOut::open(
75             handle,
76             devices,
77             &customConfig,
78             address);
79 
80     ALOGI("SpdifStreamOut::open() status = %d", status);
81 
82 #ifdef TEE_SINK
83     if (status == OK) {
84         // Don't use PCM 16-bit format to avoid WAV encoding IEC61937 data.
85         mTee.set(customConfig.sample_rate,
86                 audio_channel_count_from_out_mask(customConfig.channel_mask),
87                 AUDIO_FORMAT_IEC61937, NBAIO_Tee::TEE_FLAG_OUTPUT_THREAD);
88         mTee.setId(std::string("_") + std::to_string(handle) + "_D");
89     }
90 #endif
91 
92     return status;
93 }
94 
flush()95 int SpdifStreamOut::flush()
96 {
97     mSpdifEncoder.reset();
98     return AudioStreamOut::flush();
99 }
100 
standby()101 int SpdifStreamOut::standby()
102 {
103     mSpdifEncoder.reset();
104     return AudioStreamOut::standby();
105 }
106 
writeDataBurst(const void * buffer,size_t bytes)107 ssize_t SpdifStreamOut::writeDataBurst(const void* buffer, size_t bytes)
108 {
109     const ssize_t written = AudioStreamOut::write(buffer, bytes);
110 
111 #ifdef TEE_SINK
112     if (written > 0) {
113         mTee.write(reinterpret_cast<const char *>(buffer),
114                 written / AudioStreamOut::getFrameSize());
115     }
116 #endif
117     return written;
118 }
119 
write(const void * buffer,size_t numBytes)120 ssize_t SpdifStreamOut::write(const void* buffer, size_t numBytes)
121 {
122     // Write to SPDIF wrapper. It will call back to writeDataBurst().
123     return mSpdifEncoder.write(buffer, numBytes);
124 }
125 
126 } // namespace android
127