| 1 | /* BoCA - BonkEnc Component Architecture |
| 2 | * Copyright (C) 2007-2019 Robert Kausch <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License as |
| 6 | * published by the Free Software Foundation, either version 2 of |
| 7 | * the License, or (at your option) any later version. |
| 8 | * |
| 9 | * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR |
| 10 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 11 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ |
| 12 | |
| 13 | #include <alsa/asoundlib.h> |
| 14 | |
| 15 | #include "alsa.h" |
| 16 | |
| 17 | const String &BoCA::OutputALSA::GetComponentSpecs() |
| 18 | { |
| 19 | static String componentSpecs; |
| 20 | |
| 21 | snd_pcm_t *playback_handle = NIL; |
| 22 | |
| 23 | if (snd_pcm_open(&playback_handle, "default", SND_PCM_STREAM_PLAYBACK, 0) >= 0) |
| 24 | { |
| 25 | snd_pcm_close(playback_handle); |
| 26 | |
| 27 | componentSpecs = " \ |
| 28 | \ |
| 29 | <?xml version=\"1.0\" encoding=\"UTF-8\"?> \ |
| 30 | <component> \ |
| 31 | <name>ALSA Output Plugin</name> \ |
| 32 | <version>1.0</version> \ |
| 33 | <id>alsa-out</id> \ |
| 34 | <type>output</type> \ |
| 35 | <replace>oss-out</replace> \ |
| 36 | <input bits=\"8\" signed=\"false\"/> \ |
| 37 | <input bits=\"16,32\"/> \ |
| 38 | <input float=\"true\"/> \ |
| 39 | </component> \ |
| 40 | \ |
| 41 | "; |
| 42 | } |
| 43 | |
| 44 | return componentSpecs; |
| 45 | } |
| 46 | |
| 47 | BoCA::OutputALSA::OutputALSA() |
| 48 | { |
| 49 | playback_handle = NIL; |
| 50 | } |
| 51 | |
| 52 | BoCA::OutputALSA::~OutputALSA() |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | Bool BoCA::OutputALSA::Activate() |
| 57 | { |
| 58 | static Endianness endianness = CPU().GetEndianness(); |
| 59 | |
| 60 | const Format &format = track.GetFormat(); |
| 61 | |
| 62 | if (snd_pcm_open(&playback_handle, "default", SND_PCM_STREAM_PLAYBACK, 0) < 0) return False; |
| 63 | |
| 64 | snd_pcm_hw_params_t *hw_params = NIL; |
| 65 | |
| 66 | snd_pcm_hw_params_malloc(&hw_params); |
| 67 | |
| 68 | snd_pcm_hw_params_any(playback_handle, hw_params); |
| 69 | |
| 70 | snd_pcm_hw_params_set_access(playback_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED); |
| 71 | snd_pcm_hw_params_set_buffer_size(playback_handle, hw_params, format.rate * format.channels * (format.bits / 8) / 4); |
| 72 | |
| 73 | if (format.bits == 8 ) snd_pcm_hw_params_set_format(playback_handle, hw_params, SND_PCM_FORMAT_U8); |
| 74 | else if (format.bits == 16 ) snd_pcm_hw_params_set_format(playback_handle, hw_params, endianness == EndianBig ? SND_PCM_FORMAT_S16_BE : SND_PCM_FORMAT_S16_LE); |
| 75 | else if (format.bits == 32 && !format.fp) snd_pcm_hw_params_set_format(playback_handle, hw_params, endianness == EndianBig ? SND_PCM_FORMAT_S32_BE : SND_PCM_FORMAT_S32_LE); |
| 76 | else if (format.bits == 32 && format.fp) snd_pcm_hw_params_set_format(playback_handle, hw_params, endianness == EndianBig ? SND_PCM_FORMAT_FLOAT_BE : SND_PCM_FORMAT_FLOAT_LE); |
| 77 | |
| 78 | snd_pcm_hw_params_set_rate(playback_handle, hw_params, format.rate, 0); |
| 79 | snd_pcm_hw_params_set_channels(playback_handle, hw_params, format.channels); |
| 80 | |
| 81 | snd_pcm_hw_params(playback_handle, hw_params); |
| 82 | |
| 83 | snd_pcm_hw_params_free(hw_params); |
| 84 | |
| 85 | if (snd_pcm_prepare(playback_handle) < 0) return False; |
| 86 | |
| 87 | return True; |
| 88 | } |
| 89 | |
| 90 | Bool BoCA::OutputALSA::Deactivate() |
| 91 | { |
| 92 | snd_pcm_close(playback_handle); |
| 93 | |
| 94 | playback_handle = NIL; |
| 95 | |
| 96 | return True; |
| 97 | } |
| 98 | |
| 99 | Int BoCA::OutputALSA::WriteData(Buffer<UnsignedByte> &data) |
| 100 | { |
| 101 | const Format &format = track.GetFormat(); |
| 102 | snd_pcm_sframes_t frames = -1; |
| 103 | |
| 104 | /* Change to ALSA channel order. |
| 105 | */ |
| 106 | if (format.channels == 5) Utilities::ChangeChannelOrder(data, format, Channel::Default_5_0, Channel::ALSA_5_0); |
| 107 | else if (format.channels == 6) Utilities::ChangeChannelOrder(data, format, Channel::Default_5_1, Channel::ALSA_5_1); |
| 108 | |
| 109 | /* Output samples. |
| 110 | */ |
| 111 | frames = snd_pcm_writei(playback_handle, data, data.Size() / format.channels / (format.bits / 8)); |
| 112 | |
| 113 | if (frames < 0) return 0; |
| 114 | |
| 115 | return frames * format.channels * (format.bits / 8); |
| 116 | } |
| 117 | |
| 118 | Bool BoCA::OutputALSA::Finish() |
| 119 | { |
| 120 | if (playback_handle == NIL) return False; |
| 121 | |
| 122 | if (snd_pcm_drain(playback_handle) < 0) return False; |
| 123 | |
| 124 | return True; |
| 125 | } |
| 126 | |
| 127 | Int BoCA::OutputALSA::CanWrite() |
| 128 | { |
| 129 | if (playback_handle == NIL) return 0; |
| 130 | |
| 131 | if (snd_pcm_state(playback_handle) == SND_PCM_STATE_PAUSED) return 0; |
| 132 | |
| 133 | const Format &format = track.GetFormat(); |
| 134 | snd_pcm_sframes_t frames = snd_pcm_avail(playback_handle); |
| 135 | |
| 136 | if (frames < 0) return 0; |
| 137 | |
| 138 | return frames * format.channels * (format.bits / 8); |
| 139 | } |
| 140 | |
| 141 | Int BoCA::OutputALSA::SetPause(Bool pause) |
| 142 | { |
| 143 | if (playback_handle == NIL) return Error(); |
| 144 | |
| 145 | if (snd_pcm_pause(playback_handle, pause ? 1 : 0) < 0) return Error(); |
| 146 | |
| 147 | return Success(); |
| 148 | } |
| 149 | |
| 150 | Bool BoCA::OutputALSA::IsPlaying() |
| 151 | { |
| 152 | if (playback_handle == NIL) return False; |
| 153 | |
| 154 | if (snd_pcm_state(playback_handle) == SND_PCM_STATE_RUNNING) return True; |
| 155 | |
| 156 | return False; |
| 157 | } |