From 4ba5a7d9790022e8aeeb6bacb64618a1df9855dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20M=C3=B6ller?= Date: Thu, 30 Nov 2017 16:50:39 +0100 Subject: [PATCH] Delete unused recording functionality from ModuleFileUtility. A followup to https://webrtc-review.googlesource.com/27381. Bug: None Change-Id: I5e394ba014c0df9d81dce1a139e8ba69eb40070e Reviewed-on: https://webrtc-review.googlesource.com/27600 Reviewed-by: Magnus Flodman Commit-Queue: Niels Moller Cr-Commit-Position: refs/heads/master@{#20968} --- modules/media_file/media_file_utility.cc | 349 +---------------------- modules/media_file/media_file_utility.h | 84 ------ 2 files changed, 1 insertion(+), 432 deletions(-) diff --git a/modules/media_file/media_file_utility.cc b/modules/media_file/media_file_utility.cc index 8c602bcdba..6c7db95643 100644 --- a/modules/media_file/media_file_utility.cc +++ b/modules/media_file/media_file_utility.cc @@ -49,13 +49,11 @@ ModuleFileUtility::ModuleFileUtility() _stopPointInMs(0), _startPointInMs(0), _playoutPositionMs(0), - _bytesWritten(0), codec_info_(), _codecId(kCodecNoCodec), _bytesPerSample(0), _readPos(0), _reading(false), - _writing(false), _tempData() { RTC_LOG(LS_INFO) << "ModuleFileUtility::ModuleFileUtility()"; memset(&codec_info_, 0, sizeof(CodecInst)); @@ -510,105 +508,6 @@ int32_t ModuleFileUtility::ReadWavData(InStream& wav, return bytesRead; } -int32_t ModuleFileUtility::InitWavWriting(OutStream& wav, - const CodecInst& codecInst) { - if (set_codec_info(codecInst) != 0) { - RTC_LOG(LS_ERROR) << "codecInst identifies unsupported codec!"; - return -1; - } - _writing = false; - size_t channels = (codecInst.channels == 0) ? 1 : codecInst.channels; - - if (STR_CASE_CMP(codecInst.plname, "PCMU") == 0) { - _bytesPerSample = 1; - if (WriteWavHeader(wav, 8000, _bytesPerSample, channels, kWavFormatMuLaw, - 0) == -1) { - return -1; - } - } else if (STR_CASE_CMP(codecInst.plname, "PCMA") == 0) { - _bytesPerSample = 1; - if (WriteWavHeader(wav, 8000, _bytesPerSample, channels, kWavFormatALaw, - 0) == -1) { - return -1; - } - } else if (STR_CASE_CMP(codecInst.plname, "L16") == 0) { - _bytesPerSample = 2; - if (WriteWavHeader(wav, codecInst.plfreq, _bytesPerSample, channels, - kWavFormatPcm, 0) == -1) { - return -1; - } - } else { - RTC_LOG(LS_ERROR) << "codecInst identifies unsupported codec for WAV file!"; - return -1; - } - _writing = true; - _bytesWritten = 0; - return 0; -} - -int32_t ModuleFileUtility::WriteWavData(OutStream& out, - const int8_t* buffer, - const size_t dataLength) { - RTC_LOG(LS_VERBOSE) << "ModuleFileUtility::WriteWavData(out= " << &out - << ", buf= " << static_cast(buffer) - << ", dataLen= " << dataLength << ")"; - - if (buffer == NULL) { - RTC_LOG(LS_ERROR) << "WriteWavData: input buffer NULL!"; - return -1; - } - - if (!out.Write(buffer, dataLength)) { - return -1; - } - _bytesWritten += dataLength; - return static_cast(dataLength); -} - -int32_t ModuleFileUtility::WriteWavHeader(OutStream& wav, - uint32_t freq, - size_t bytesPerSample, - size_t channels, - uint32_t format, - size_t lengthInBytes) { - // Frame size in bytes for 10 ms of audio. - // TODO (hellner): 44.1 kHz has 440 samples frame size. Doesn't seem to - // be taken into consideration here! - const size_t frameSize = (freq / 100) * channels; - - // Calculate the number of full frames that the wave file contain. - const size_t dataLengthInBytes = frameSize * (lengthInBytes / frameSize); - - uint8_t buf[kWavHeaderSize]; - webrtc::WriteWavHeader(buf, channels, freq, static_cast(format), - bytesPerSample, dataLengthInBytes / bytesPerSample); - wav.Write(buf, kWavHeaderSize); - return 0; -} - -int32_t ModuleFileUtility::UpdateWavHeader(OutStream& wav) { - int32_t res = -1; - if (wav.Rewind() == -1) { - return -1; - } - size_t channels = (codec_info_.channels == 0) ? 1 : codec_info_.channels; - - if (STR_CASE_CMP(codec_info_.plname, "L16") == 0) { - res = WriteWavHeader(wav, codec_info_.plfreq, 2, channels, kWavFormatPcm, - _bytesWritten); - } else if (STR_CASE_CMP(codec_info_.plname, "PCMU") == 0) { - res = - WriteWavHeader(wav, 8000, 1, channels, kWavFormatMuLaw, _bytesWritten); - } else if (STR_CASE_CMP(codec_info_.plname, "PCMA") == 0) { - res = WriteWavHeader(wav, 8000, 1, channels, kWavFormatALaw, _bytesWritten); - } else { - // Allow calling this API even if not writing to a WAVE file. - // TODO (hellner): why?! - return 0; - } - return res; -} - int32_t ModuleFileUtility::InitPreEncodedReading(InStream& in, const CodecInst& cinst) { uint8_t preEncodedID; @@ -663,46 +562,6 @@ int32_t ModuleFileUtility::ReadPreEncodedData(InStream& in, return in.Read(outData, frameLen); } -int32_t ModuleFileUtility::InitPreEncodedWriting(OutStream& out, - const CodecInst& codecInst) { - if (set_codec_info(codecInst) != 0) { - RTC_LOG(LS_ERROR) << "CodecInst not recognized!"; - return -1; - } - _writing = true; - _bytesWritten = 1; - out.Write(&_codecId, 1); - return 0; -} - -int32_t ModuleFileUtility::WritePreEncodedData(OutStream& out, - const int8_t* buffer, - const size_t dataLength) { - RTC_LOG(LS_VERBOSE) << "ModuleFileUtility::WritePreEncodedData(out= " << &out - << " , inData= " << static_cast(buffer) - << ", dataLen= " << dataLength << ")"; - - if (buffer == NULL) { - RTC_LOG(LS_ERROR) << "buffer NULL"; - } - - size_t bytesWritten = 0; - // The first two bytes is the size of the frame. - int16_t lengthBuf; - lengthBuf = (int16_t)dataLength; - if (dataLength > static_cast(std::numeric_limits::max()) || - !out.Write(&lengthBuf, 2)) { - return -1; - } - bytesWritten = 2; - - if (!out.Write(buffer, dataLength)) { - return -1; - } - bytesWritten += dataLength; - return static_cast(bytesWritten); -} - int32_t ModuleFileUtility::InitCompressedReading(InStream& in, const uint32_t start, const uint32_t stop) { @@ -842,52 +701,6 @@ int32_t ModuleFileUtility::ReadCompressedData(InStream& in, return bytesRead; } -int32_t ModuleFileUtility::InitCompressedWriting(OutStream& out, - const CodecInst& codecInst) { - RTC_LOG(LS_VERBOSE) << "ModuleFileUtility::InitCompressedWriting(out= " - << &out << ", codecName= " << codecInst.plname << ")"; - - _writing = false; - -#ifdef WEBRTC_CODEC_ILBC - if (STR_CASE_CMP(codecInst.plname, "ilbc") == 0) { - if (codecInst.pacsize == 160) { - _codecId = kCodecIlbc20Ms; - out.Write("#!iLBC20\n", 9); - } else if (codecInst.pacsize == 240) { - _codecId = kCodecIlbc30Ms; - out.Write("#!iLBC30\n", 9); - } else { - RTC_LOG(LS_ERROR) << "codecInst defines unsupported compression codec!"; - return -1; - } - memcpy(&codec_info_, &codecInst, sizeof(CodecInst)); - _writing = true; - return 0; - } -#endif - - RTC_LOG(LS_ERROR) << "codecInst defines unsupported compression codec!"; - return -1; -} - -int32_t ModuleFileUtility::WriteCompressedData(OutStream& out, - const int8_t* buffer, - const size_t dataLength) { - RTC_LOG(LS_VERBOSE) << "ModuleFileUtility::WriteCompressedData(out= " << &out - << ", buf= " << static_cast(buffer) - << ", dataLen= " << dataLength << ")"; - - if (buffer == NULL) { - RTC_LOG(LS_ERROR) << "buffer NULL"; - } - - if (!out.Write(buffer, dataLength)) { - return -1; - } - return static_cast(dataLength); -} - int32_t ModuleFileUtility::InitPCMReading(InStream& pcm, const uint32_t start, const uint32_t stop, @@ -1013,78 +826,11 @@ int32_t ModuleFileUtility::ReadPCMData(InStream& pcm, return bytesRead; } -int32_t ModuleFileUtility::InitPCMWriting(OutStream& out, uint32_t freq) { - if (freq == 8000) { - strcpy(codec_info_.plname, "L16"); - codec_info_.pltype = -1; - codec_info_.plfreq = 8000; - codec_info_.pacsize = 160; - codec_info_.channels = 1; - codec_info_.rate = 128000; - - _codecId = kCodecL16_8Khz; - } else if (freq == 16000) { - strcpy(codec_info_.plname, "L16"); - codec_info_.pltype = -1; - codec_info_.plfreq = 16000; - codec_info_.pacsize = 320; - codec_info_.channels = 1; - codec_info_.rate = 256000; - - _codecId = kCodecL16_16kHz; - } else if (freq == 32000) { - strcpy(codec_info_.plname, "L16"); - codec_info_.pltype = -1; - codec_info_.plfreq = 32000; - codec_info_.pacsize = 320; - codec_info_.channels = 1; - codec_info_.rate = 512000; - - _codecId = kCodecL16_32Khz; - } else if (freq == 48000) { - strcpy(codec_info_.plname, "L16"); - codec_info_.pltype = -1; - codec_info_.plfreq = 48000; - codec_info_.pacsize = 480; - codec_info_.channels = 1; - codec_info_.rate = 768000; - - _codecId = kCodecL16_48Khz; - } - if ((_codecId != kCodecL16_8Khz) && (_codecId != kCodecL16_16kHz) && - (_codecId != kCodecL16_32Khz) && (_codecId != kCodecL16_48Khz)) { - RTC_LOG(LS_ERROR) << "CodecInst is not 8KHz, 16KHz, 32kHz or 48kHz PCM!"; - return -1; - } - _writing = true; - _bytesWritten = 0; - return 0; -} - -int32_t ModuleFileUtility::WritePCMData(OutStream& out, - const int8_t* buffer, - const size_t dataLength) { - RTC_LOG(LS_VERBOSE) << "ModuleFileUtility::WritePCMData(out= " << &out - << ", buf= " << static_cast(buffer) - << ", dataLen= " << dataLength << ")"; - - if (buffer == NULL) { - RTC_LOG(LS_ERROR) << "buffer NULL"; - } - - if (!out.Write(buffer, dataLength)) { - return -1; - } - - _bytesWritten += dataLength; - return static_cast(dataLength); -} - int32_t ModuleFileUtility::codec_info(CodecInst& codecInst) { RTC_LOG(LS_VERBOSE) << "ModuleFileUtility::codec_info(codecInst= " << &codecInst << ")"; - if (!_reading && !_writing) { + if (!_reading) { RTC_LOG(LS_ERROR) << "CodecInst: not currently reading audio file!"; return -1; } @@ -1137,99 +883,6 @@ int32_t ModuleFileUtility::set_codec_info(const CodecInst& codecInst) { return 0; } -int32_t ModuleFileUtility::FileDurationMs(const char* fileName, - const FileFormats fileFormat, - const uint32_t freqInHz) { - if (fileName == NULL) { - RTC_LOG(LS_ERROR) << "filename NULL"; - return -1; - } - - int32_t time_in_ms = -1; - struct stat file_size; - if (stat(fileName, &file_size) == -1) { - RTC_LOG(LS_ERROR) << "failed to retrieve file size with stat!"; - return -1; - } - FileWrapper* inStreamObj = FileWrapper::Create(); - if (inStreamObj == NULL) { - RTC_LOG(LS_INFO) << "failed to create InStream object!"; - return -1; - } - if (!inStreamObj->OpenFile(fileName, true)) { - delete inStreamObj; - RTC_LOG(LS_ERROR) << "failed to open file " << fileName << "!"; - return -1; - } - - switch (fileFormat) { - case kFileFormatWavFile: { - if (ReadWavHeader(*inStreamObj) == -1) { - RTC_LOG(LS_ERROR) << "failed to read WAV file header!"; - return -1; - } - time_in_ms = - ((file_size.st_size - 44) / (_wavFormatObj.nAvgBytesPerSec / 1000)); - break; - } - case kFileFormatPcm16kHzFile: { - // 16 samples per ms. 2 bytes per sample. - int32_t denominator = 16 * 2; - time_in_ms = (file_size.st_size) / denominator; - break; - } - case kFileFormatPcm8kHzFile: { - // 8 samples per ms. 2 bytes per sample. - int32_t denominator = 8 * 2; - time_in_ms = (file_size.st_size) / denominator; - break; - } - case kFileFormatCompressedFile: { - int32_t cnt = 0; - int read_len = 0; - char buf[64]; - do { - read_len = inStreamObj->Read(&buf[cnt++], 1); - if (read_len != 1) { - return -1; - } - } while ((buf[cnt - 1] != '\n') && (64 > cnt)); - - if (cnt == 64) { - return -1; - } else { - buf[cnt] = 0; - } -#ifdef WEBRTC_CODEC_ILBC - if (!strcmp("#!iLBC20\n", buf)) { - // 20 ms is 304 bits - time_in_ms = ((file_size.st_size) * 160) / 304; - break; - } - if (!strcmp("#!iLBC30\n", buf)) { - // 30 ms takes 400 bits. - // file size in bytes * 8 / 400 is the number of - // 30 ms frames in the file -> - // time_in_ms = file size * 8 / 400 * 30 - time_in_ms = ((file_size.st_size) * 240) / 400; - break; - } -#endif - break; - } - case kFileFormatPreencodedFile: { - RTC_LOG(LS_ERROR) << "cannot determine duration of Pre-Encoded file!"; - break; - } - default: - RTC_LOG(LS_ERROR) << "unsupported file format " << fileFormat << "!"; - break; - } - inStreamObj->CloseFile(); - delete inStreamObj; - return time_in_ms; -} - uint32_t ModuleFileUtility::PlayoutPositionMs() { RTC_LOG(LS_VERBOSE) << "ModuleFileUtility::PlayoutPosition()"; diff --git a/modules/media_file/media_file_utility.h b/modules/media_file/media_file_utility.h index 94785be64c..0b56140887 100644 --- a/modules/media_file/media_file_utility.h +++ b/modules/media_file/media_file_utility.h @@ -57,27 +57,6 @@ public: int8_t* audioBufferRight, const size_t bufferLength); - // Prepare for recording audio to stream. - // codecInst specifies the encoding of the audio data. - // Note: codecInst.channels should be set to 2 for stereo (and 1 for - // mono). Stereo is only supported for WAV files. - int32_t InitWavWriting(OutStream& stream, const CodecInst& codecInst); - - // Write one audio frame, i.e. the bufferLength first bytes of audioBuffer, - // to file. The audio frame size is determined by the codecInst.pacsize - // parameter of the last sucessfull StartRecordingAudioFile(..) call. - // The return value is the number of bytes written to audioBuffer. - int32_t WriteWavData(OutStream& stream, - const int8_t* audioBuffer, - const size_t bufferLength); - - // Finalizes the WAV header so that it is correct if nothing more will be - // written to stream. - // Note: this API must be called before closing stream to ensure that the - // WAVE header is updated with the file size. Don't call this API - // if more samples are to be written to stream. - int32_t UpdateWavHeader(OutStream& stream); - // Prepare for playing audio from stream. // startPointMs and stopPointMs, unless zero, specify what part of the file // should be read. From startPointMs ms to stopPointMs ms. @@ -94,19 +73,6 @@ public: int32_t ReadPCMData(InStream& stream, int8_t* audioBuffer, const size_t dataLengthInBytes); - // Prepare for recording audio to stream. - // freqInHz is the PCM sampling frequency. - // NOTE, allowed frequencies are 8000, 16000 and 32000 (Hz) - int32_t InitPCMWriting(OutStream& stream, const uint32_t freqInHz = 16000); - - // Write one 10ms audio frame, i.e. the bufferLength first bytes of - // audioBuffer, to file. The audio frame size is determined by the freqInHz - // parameter of the last sucessfull InitPCMWriting(..) call. - // The return value is the number of bytes written to audioBuffer. - int32_t WritePCMData(OutStream& stream, - const int8_t* audioBuffer, - size_t bufferLength); - // Prepare for playing audio from stream. // startPointMs and stopPointMs, unless zero, specify what part of the file // should be read. From startPointMs ms to stopPointMs ms. @@ -121,20 +87,6 @@ public: int8_t* audioBuffer, const size_t dataLengthInBytes); - // Prepare for recording audio to stream. - // codecInst specifies the encoding of the audio data. - int32_t InitCompressedWriting(OutStream& stream, - const CodecInst& codecInst); - - // Write one audio frame, i.e. the bufferLength first bytes of audioBuffer, - // to file. The audio frame size is determined by the codecInst.pacsize - // parameter of the last sucessfull InitCompressedWriting(..) call. - // The return value is the number of bytes written to stream. - // Note: bufferLength must be exactly one frame. - int32_t WriteCompressedData(OutStream& stream, - const int8_t* audioBuffer, - const size_t bufferLength); - // Prepare for playing audio from stream. // codecInst specifies the encoding of the audio data. int32_t InitPreEncodedReading(InStream& stream, @@ -147,26 +99,6 @@ public: int8_t* audioBuffer, const size_t dataLengthInBytes); - // Prepare for recording audio to stream. - // codecInst specifies the encoding of the audio data. - int32_t InitPreEncodedWriting(OutStream& stream, - const CodecInst& codecInst); - - // Write one audio frame, i.e. the bufferLength first bytes of audioBuffer, - // to stream. The audio frame size is determined by the codecInst.pacsize - // parameter of the last sucessfull InitPreEncodedWriting(..) call. - // The return value is the number of bytes written to stream. - // Note: bufferLength must be exactly one frame. - int32_t WritePreEncodedData(OutStream& stream, - const int8_t* inData, - const size_t dataLengthInBytes); - - // Set durationMs to the size of the file (in ms) specified by fileName. - // freqInHz specifies the sampling frequency of the file. - int32_t FileDurationMs(const char* fileName, - const FileFormats fileFormat, - const uint32_t freqInHz = 16000); - // Return the number of ms that have been played so far. uint32_t PlayoutPositionMs(); @@ -187,19 +119,6 @@ private: // Parse the WAV header in stream. int32_t ReadWavHeader(InStream& stream); - // Update the WAV header. freqInHz, bytesPerSample, channels, format, - // lengthInBytes specify characterists of the audio data. - // freqInHz is the sampling frequency. bytesPerSample is the sample size in - // bytes. channels is the number of channels, e.g. 1 is mono and 2 is - // stereo. format is the encode format (e.g. PCMU, PCMA, PCM etc). - // lengthInBytes is the number of bytes the audio samples are using up. - int32_t WriteWavHeader(OutStream& stream, - uint32_t freqInHz, - size_t bytesPerSample, - size_t channels, - uint32_t format, - size_t lengthInBytes); - // Put dataLengthInBytes of audio data from stream into the audioBuffer. // The return value is the number of bytes written to audioBuffer. int32_t ReadWavData(InStream& stream, uint8_t* audioBuffer, @@ -261,7 +180,6 @@ private: uint32_t _stopPointInMs; uint32_t _startPointInMs; uint32_t _playoutPositionMs; - size_t _bytesWritten; CodecInst codec_info_; MediaFileUtility_CodecType _codecId; @@ -270,9 +188,7 @@ private: size_t _bytesPerSample; size_t _readPos; - // Only reading or writing can be enabled, not both. bool _reading; - bool _writing; // Scratch buffer used for turning stereo audio to mono. uint8_t _tempData[WAV_MAX_BUFFER_SIZE];