| 1 | /* BoCA - BonkEnc Component Architecture |
| 2 | * Copyright (C) 2007-2024 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 <boca/application/external/encodercomponentstdio.h> |
| 14 | #include <boca/common/utilities.h> |
| 15 | |
| 16 | #include <smooth/io/drivers/driver_win32.h> |
| 17 | |
| 18 | #include <windows.h> |
| 19 | #include <mmreg.h> |
| 20 | |
| 21 | using namespace smooth::IO; |
| 22 | |
| 23 | BoCA::AS::EncoderComponentExternalStdIO::EncoderComponentExternalStdIO(ComponentSpecs *specs) : EncoderComponentExternal(specs) |
| 24 | { |
| 25 | out = NIL; |
| 26 | |
| 27 | driver_stdin = NIL; |
| 28 | |
| 29 | rPipe = NIL; |
| 30 | wPipe = NIL; |
| 31 | |
| 32 | hProcess = NIL; |
| 33 | } |
| 34 | |
| 35 | BoCA::AS::EncoderComponentExternalStdIO::~EncoderComponentExternalStdIO() |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | Bool BoCA::AS::EncoderComponentExternalStdIO::Activate() |
| 40 | { |
| 41 | if (!EncoderComponentExternal::Activate()) return False; |
| 42 | |
| 43 | /* Build output file name. |
| 44 | */ |
| 45 | encFileName = Utilities::GetNonUnicodeTempFileName(track.outputFile).Append(".").Append(GetOutputFileExtension()); |
| 46 | |
| 47 | /* Remove temporary file if it exists. |
| 48 | * Might be a leftover of a previous encoding attempt. |
| 49 | */ |
| 50 | File(encFileName).Delete(); |
| 51 | |
| 52 | /* Set up security attributes. |
| 53 | */ |
| 54 | SECURITY_ATTRIBUTES secAttr; |
| 55 | |
| 56 | ZeroMemory(&secAttr, sizeof(secAttr)); |
| 57 | |
| 58 | secAttr.nLength = sizeof(secAttr); |
| 59 | secAttr.bInheritHandle = True; |
| 60 | |
| 61 | CreatePipe(&rPipe, &wPipe, &secAttr, 32768 * format.channels * (format.bits / 8)); |
| 62 | SetHandleInformation(wPipe, HANDLE_FLAG_INHERIT, 0); |
| 63 | |
| 64 | /* Start 3rd party command line encoder. |
| 65 | */ |
| 66 | const Info &info = track.GetInfo(); |
| 67 | |
| 68 | String command = String("\"").Append(specs->external_command).Append("\"").Replace("/", Directory::GetDirectoryDelimiter()); |
| 69 | String arguments = String(specs->external_arguments).Replace("%OPTIONS", specs->GetExternalArgumentsString(GetConfiguration())) |
| 70 | .Replace("%OUTFILE", String("\"").Append(encFileName).Append("\"")) |
| 71 | .Replace("%ARTIST", String("\"").Append((char *) info.artist).Append("\"")) |
| 72 | .Replace("%ALBUM", String("\"").Append((char *) info.album).Append("\"")) |
| 73 | .Replace("%TITLE", String("\"").Append((char *) info.title).Append("\"")) |
| 74 | .Replace("%TRACK", String("\"").Append(String::FromInt(info.track)).Append("\"")) |
| 75 | .Replace("%YEAR", String("\"").Append(String::FromInt(info.year)).Append("\"")) |
| 76 | .Replace("%GENRE", String("\"").Append((char *) info.genre).Append("\"")); |
| 77 | |
| 78 | if (specs->debug) AllocConsole(); |
| 79 | |
| 80 | STARTUPINFOA startupInfo; |
| 81 | |
| 82 | ZeroMemory(&startupInfo, sizeof(startupInfo)); |
| 83 | |
| 84 | startupInfo.cb = sizeof(startupInfo); |
| 85 | startupInfo.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES; |
| 86 | startupInfo.wShowWindow = specs->debug ? SW_SHOW : SW_HIDE; |
| 87 | startupInfo.hStdInput = rPipe; |
| 88 | startupInfo.hStdOutput = specs->debug ? GetStdHandle(STD_OUTPUT_HANDLE) : NIL; |
| 89 | startupInfo.hStdError = specs->debug ? GetStdHandle(STD_ERROR_HANDLE) : NIL; |
| 90 | |
| 91 | PROCESS_INFORMATION processInfo; |
| 92 | |
| 93 | ZeroMemory(&processInfo, sizeof(processInfo)); |
| 94 | |
| 95 | CreateProcessA(NIL, String(command).Append(" ").Append(arguments), NIL, NIL, True, 0, NIL, NIL, &startupInfo, &processInfo); |
| 96 | |
| 97 | hProcess = processInfo.hProcess; |
| 98 | |
| 99 | /* Close stdio pipe read handle. |
| 100 | */ |
| 101 | CloseHandle(rPipe); |
| 102 | |
| 103 | /* Check process handle. |
| 104 | */ |
| 105 | if (processInfo.hProcess == NIL) |
| 106 | { |
| 107 | CloseHandle(wPipe); |
| 108 | |
| 109 | errorState = True; |
| 110 | errorString = String("Unable to run encoder ").Append(command).Append("."); |
| 111 | |
| 112 | return False; |
| 113 | } |
| 114 | |
| 115 | /* Open pipe handle. |
| 116 | */ |
| 117 | driver_stdin = new DriverWin32(wPipe); |
| 118 | out = new OutStream(STREAM_DRIVER, driver_stdin); |
| 119 | |
| 120 | /* Output WAVE header. |
| 121 | */ |
| 122 | out->OutputString("RIFF"); |
| 123 | out->OutputNumber(36, 4); |
| 124 | out->OutputString("WAVE"); |
| 125 | out->OutputString("fmt "); |
| 126 | |
| 127 | out->OutputNumber(16, 4); |
| 128 | out->OutputNumber(format.fp ? WAVE_FORMAT_IEEE_FLOAT : WAVE_FORMAT_PCM, 2); |
| 129 | out->OutputNumber(format.channels, 2); |
| 130 | out->OutputNumber(format.rate, 4); |
| 131 | out->OutputNumber(format.rate * format.channels * (format.bits / 8), 4); |
| 132 | out->OutputNumber(format.channels * (format.bits / 8), 2); |
| 133 | |
| 134 | out->OutputNumber(format.bits, 2); |
| 135 | out->OutputString("data"); |
| 136 | out->OutputNumber(0, 4); |
| 137 | |
| 138 | out->Flush(); |
| 139 | |
| 140 | return True; |
| 141 | } |
| 142 | |
| 143 | Bool BoCA::AS::EncoderComponentExternalStdIO::Deactivate() |
| 144 | { |
| 145 | EncoderComponentExternal::Deactivate(); |
| 146 | |
| 147 | /* Close stdio pipe. |
| 148 | */ |
| 149 | delete out; |
| 150 | delete driver_stdin; |
| 151 | |
| 152 | CloseHandle(wPipe); |
| 153 | |
| 154 | /* Wait until the encoder exits. |
| 155 | */ |
| 156 | while (WaitForSingleObject(hProcess, 0) == WAIT_TIMEOUT) S::System::System::Sleep(10); |
| 157 | |
| 158 | if (specs->debug) |
| 159 | { |
| 160 | BoCA::Utilities::InfoMessage("Click OK to close console window."); |
| 161 | |
| 162 | FreeConsole(); |
| 163 | } |
| 164 | |
| 165 | /* Check if anything went wrong. |
| 166 | */ |
| 167 | unsigned long exitCode = 0; |
| 168 | |
| 169 | GetExitCodeProcess(hProcess, &exitCode); |
| 170 | |
| 171 | if (!specs->external_ignoreExitCode && exitCode != 0) |
| 172 | { |
| 173 | /* Remove output file. |
| 174 | */ |
| 175 | File(encFileName).Delete(); |
| 176 | |
| 177 | errorState = True; |
| 178 | errorString = String("Encoder returned exit code ").Append(String::FromInt((signed) exitCode)).Append("."); |
| 179 | |
| 180 | return False; |
| 181 | } |
| 182 | |
| 183 | /* Create tag buffers. |
| 184 | */ |
| 185 | Buffer<UnsignedByte> tagBufferPrepend; |
| 186 | Buffer<UnsignedByte> tagBufferAppend; |
| 187 | |
| 188 | RenderTags(encFileName, track, tagBufferPrepend, tagBufferAppend); |
| 189 | |
| 190 | /* Prepend tags. |
| 191 | */ |
| 192 | driver->WriteData(tagBufferPrepend, tagBufferPrepend.Size()); |
| 193 | |
| 194 | /* Stream contents of created file to output driver. |
| 195 | */ |
| 196 | InStream in(STREAM_FILE, encFileName, IS_READ); |
| 197 | Buffer<UnsignedByte> buffer(128 * 1024); |
| 198 | Int64 bytesLeft = in.Size(); |
| 199 | |
| 200 | while (bytesLeft) |
| 201 | { |
| 202 | Int bytes = Math::Min(buffer.Size(), bytesLeft); |
| 203 | |
| 204 | in.InputData(buffer, bytes); |
| 205 | |
| 206 | driver->WriteData(buffer, bytes); |
| 207 | |
| 208 | bytesLeft -= bytes; |
| 209 | } |
| 210 | |
| 211 | /* Append tags. |
| 212 | */ |
| 213 | driver->WriteData(tagBufferAppend, tagBufferAppend.Size()); |
| 214 | |
| 215 | in.Close(); |
| 216 | |
| 217 | File(encFileName).Delete(); |
| 218 | |
| 219 | /* Look for a companion file. |
| 220 | */ |
| 221 | File companionFile = GetCompanionFile(encFileName); |
| 222 | |
| 223 | if (companionFile.Exists()) companionFile.Move(GetCompanionFile(track.outputFile)); |
| 224 | |
| 225 | return True; |
| 226 | } |
| 227 | |
| 228 | Int BoCA::AS::EncoderComponentExternalStdIO::WriteData(const Buffer<UnsignedByte> &data) |
| 229 | { |
| 230 | Buffer<UnsignedByte> &buffer = const_cast<Buffer<UnsignedByte> &>(data); |
| 231 | |
| 232 | ProcessData(buffer); |
| 233 | |
| 234 | /* Convert to little-endian byte order. |
| 235 | */ |
| 236 | static Endianness endianness = CPU().GetEndianness(); |
| 237 | |
| 238 | if (endianness != EndianLittle) BoCA::Utilities::SwitchBufferByteOrder(buffer, format.bits / 8); |
| 239 | |
| 240 | /* Convert 8 bit samples to unsigned. |
| 241 | */ |
| 242 | if (format.bits == 8 && format.sign == True) |
| 243 | { |
| 244 | for (Int i = 0; i < buffer.Size(); i++) buffer[i] = SignedByte(buffer[i]) + 128; |
| 245 | } |
| 246 | |
| 247 | /* Check if external encoder still exists. |
| 248 | */ |
| 249 | if (WaitForSingleObject(hProcess, 0) != WAIT_TIMEOUT) |
| 250 | { |
| 251 | errorState = True; |
| 252 | errorString = "Encoder quit prematurely."; |
| 253 | |
| 254 | return -1; |
| 255 | } |
| 256 | |
| 257 | /* Hand data over to the encoder using the stdio pipe. |
| 258 | */ |
| 259 | out->OutputData(buffer, buffer.Size()); |
| 260 | out->Flush(); |
| 261 | |
| 262 | return buffer.Size(); |
| 263 | } |