blob: 43e9c0cee112d92daaa9095ac775ced59969e3c5 [file] [log] [blame]
Phil Burk062e67a2015-02-11 13:40:50 -08001/*
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
Mikhail Naganovcbc8f612016-10-11 18:05:13 -070020#include <system/audio.h>
Phil Burk062e67a2015-02-11 13:40:50 -080021#include <utils/Log.h>
22
23#include <audio_utils/spdif/SPDIFEncoder.h>
24
25#include "AudioHwDevice.h"
Phil Burk062e67a2015-02-11 13:40:50 -080026#include "SpdifStreamOut.h"
27
28namespace android {
29
30/**
31 * If the AudioFlinger is processing encoded data and the HAL expects
32 * PCM then we need to wrap the data in an SPDIF wrapper.
33 */
Phil Burk23d89972015-04-06 16:22:23 -070034SpdifStreamOut::SpdifStreamOut(AudioHwDevice *dev,
35 audio_output_flags_t flags,
36 audio_format_t format)
Phil Burk41ae1d62015-08-04 11:11:28 -070037 // Tell the HAL that the data will be compressed audio wrapped in a data burst.
38 : AudioStreamOut(dev, (audio_output_flags_t) (flags | AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO))
Phil Burk23d89972015-04-06 16:22:23 -070039 , mSpdifEncoder(this, format)
Phil Burk062e67a2015-02-11 13:40:50 -080040{
41}
42
43status_t SpdifStreamOut::open(
44 audio_io_handle_t handle,
45 audio_devices_t devices,
46 struct audio_config *config,
47 const char *address)
48{
49 struct audio_config customConfig = *config;
50
Phil Burkca5e6142015-07-14 09:42:29 -070051 mApplicationFormat = config->format;
52 mApplicationSampleRate = config->sample_rate;
53 mApplicationChannelMask = config->channel_mask;
54
Phil Burk062e67a2015-02-11 13:40:50 -080055 // Some data bursts run at a higher sample rate.
Phil Burk23d89972015-04-06 16:22:23 -070056 // TODO Move this into the audio_utils as a static method.
Phil Burk062e67a2015-02-11 13:40:50 -080057 switch(config->format) {
58 case AUDIO_FORMAT_E_AC3:
Dean Wheatleyf14e77f2017-03-23 15:52:01 +110059 case AUDIO_FORMAT_E_AC3_JOC:
Phil Burk062e67a2015-02-11 13:40:50 -080060 mRateMultiplier = 4;
61 break;
62 case AUDIO_FORMAT_AC3:
Phil Burk23d89972015-04-06 16:22:23 -070063 case AUDIO_FORMAT_DTS:
64 case AUDIO_FORMAT_DTS_HD:
Phil Burk062e67a2015-02-11 13:40:50 -080065 mRateMultiplier = 1;
66 break;
67 default:
68 ALOGE("ERROR SpdifStreamOut::open() unrecognized format 0x%08X\n",
69 config->format);
70 return BAD_VALUE;
71 }
72 customConfig.sample_rate = config->sample_rate * mRateMultiplier;
73
Phil Burk23d89972015-04-06 16:22:23 -070074 customConfig.format = AUDIO_FORMAT_PCM_16_BIT;
75 customConfig.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
76
Phil Burk062e67a2015-02-11 13:40:50 -080077 // Always print this because otherwise it could be very confusing if the
78 // HAL and AudioFlinger are using different formats.
79 // Print before open() because HAL may modify customConfig.
80 ALOGI("SpdifStreamOut::open() AudioFlinger requested"
81 " sampleRate %d, format %#x, channelMask %#x",
82 config->sample_rate,
83 config->format,
84 config->channel_mask);
85 ALOGI("SpdifStreamOut::open() HAL configured for"
86 " sampleRate %d, format %#x, channelMask %#x",
87 customConfig.sample_rate,
88 customConfig.format,
89 customConfig.channel_mask);
90
Andy Hung1ef77382023-06-15 14:50:18 -070091 const status_t status = AudioStreamOut::open(
Phil Burk062e67a2015-02-11 13:40:50 -080092 handle,
93 devices,
94 &customConfig,
95 address);
96
97 ALOGI("SpdifStreamOut::open() status = %d", status);
98
99 return status;
100}
101
Phil Burk062e67a2015-02-11 13:40:50 -0800102int SpdifStreamOut::flush()
103{
Phil Burk48e6ea92015-06-18 15:37:08 -0700104 mSpdifEncoder.reset();
Phil Burk062e67a2015-02-11 13:40:50 -0800105 return AudioStreamOut::flush();
106}
107
108int SpdifStreamOut::standby()
109{
Phil Burk48e6ea92015-06-18 15:37:08 -0700110 mSpdifEncoder.reset();
Phil Burk062e67a2015-02-11 13:40:50 -0800111 return AudioStreamOut::standby();
112}
113
Phil Burk062e67a2015-02-11 13:40:50 -0800114ssize_t SpdifStreamOut::writeDataBurst(const void* buffer, size_t bytes)
115{
116 return AudioStreamOut::write(buffer, bytes);
117}
118
Phil Burkca5e6142015-07-14 09:42:29 -0700119ssize_t SpdifStreamOut::write(const void* buffer, size_t numBytes)
Phil Burk062e67a2015-02-11 13:40:50 -0800120{
121 // Write to SPDIF wrapper. It will call back to writeDataBurst().
Phil Burkca5e6142015-07-14 09:42:29 -0700122 return mSpdifEncoder.write(buffer, numBytes);
Phil Burk062e67a2015-02-11 13:40:50 -0800123}
124
125} // namespace android