diff --git a/src/modules/audio_processing/interface/audio_processing.h b/src/modules/audio_processing/interface/audio_processing.h index e263696183..ee4d06f71b 100644 --- a/src/modules/audio_processing/interface/audio_processing.h +++ b/src/modules/audio_processing/interface/audio_processing.h @@ -191,7 +191,7 @@ class AudioProcessing : public Module { // a NULL-terminated string. If there is an ongoing recording, the old file // will be closed, and recording will continue in the newly specified file. // An already existing file will be overwritten without warning. - static const int kMaxFilenameSize = 1024; + static const size_t kMaxFilenameSize = 1024; virtual int StartDebugRecording(const char filename[kMaxFilenameSize]) = 0; // Stops recording debugging information, and closes the file. Recording diff --git a/src/modules/utility/source/rtp_dump_impl.cc b/src/modules/utility/source/rtp_dump_impl.cc index 14fdeb1948..6ba0c0ba9e 100644 --- a/src/modules/utility/source/rtp_dump_impl.cc +++ b/src/modules/utility/source/rtp_dump_impl.cc @@ -114,7 +114,12 @@ WebRtc_Word32 RtpDumpImpl::Start(const WebRtc_Word8* fileNameUTF8) // All rtp dump files start with #!rtpplay. WebRtc_Word8 magic[16]; sprintf(magic, "#!rtpplay%s \n", RTPFILE_VERSION); - _file.WriteText(magic); + if (_file.WriteText(magic) == -1) + { + WEBRTC_TRACE(kTraceError, kTraceUtility, -1, + "error writing to file"); + return -1; + } // The header according to the rtpdump documentation is sizeof(RD_hdr_t) // which is 8 + 4 + 2 = 14 bytes for 32-bit architecture (and 22 bytes on @@ -125,7 +130,12 @@ WebRtc_Word32 RtpDumpImpl::Start(const WebRtc_Word8* fileNameUTF8) // of padding should be added to the header. WebRtc_Word8 dummyHdr[16]; memset(dummyHdr, 0, 16); - _file.Write(dummyHdr, sizeof(dummyHdr)); + if (!_file.Write(dummyHdr, sizeof(dummyHdr))) + { + WEBRTC_TRACE(kTraceError, kTraceUtility, -1, + "error writing to file"); + return -1; + } return 0; } @@ -190,8 +200,20 @@ WebRtc_Word32 RtpDumpImpl::DumpPacket(const WebRtc_UWord8* packet, { hdr.plen = RtpDumpHtons((WebRtc_UWord16)packetLength); } - _file.Write(&hdr, sizeof(hdr)); - _file.Write(packet, packetLength); + + if (!_file.Write(&hdr, sizeof(hdr))) + { + WEBRTC_TRACE(kTraceError, kTraceUtility, -1, + "error writing to file"); + return -1; + } + if (!_file.Write(packet, packetLength)) + { + WEBRTC_TRACE(kTraceError, kTraceUtility, -1, + "error writing to file"); + return -1; + } + return 0; } diff --git a/src/system_wrappers/interface/file_wrapper.h b/src/system_wrappers/interface/file_wrapper.h index 123ab65eac..d8112ee6d2 100644 --- a/src/system_wrappers/interface/file_wrapper.h +++ b/src/system_wrappers/interface/file_wrapper.h @@ -11,6 +11,8 @@ #ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_FILE_WRAPPER_H_ #define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_FILE_WRAPPER_H_ +#include + #include "common_types.h" #include "typedefs.h" @@ -18,10 +20,11 @@ // write from/to a file. namespace webrtc { + class FileWrapper : public InStream, public OutStream { public: - enum { kMaxFileNameSize = 1024}; + static const size_t kMaxFileNameSize = 1024; // Factory method. Constructor disabled. static FileWrapper* Create(); @@ -30,41 +33,44 @@ public: virtual bool Open() const = 0; // Opens a file in read or write mode, decided by the readOnly parameter. - virtual WebRtc_Word32 OpenFile(const WebRtc_Word8* fileNameUTF8, - const bool readOnly, - const bool loop = false, - const bool text = false) = 0; + virtual int OpenFile(const char* fileNameUTF8, + bool readOnly, + bool loop = false, + bool text = false) = 0; - virtual WebRtc_Word32 CloseFile() = 0; + virtual int CloseFile() = 0; // Limits the file size. - virtual WebRtc_Word32 SetMaxFileSize(WebRtc_Word32 bytes) = 0; + virtual int SetMaxFileSize(size_t bytes) = 0; // Flush any pending writes. - virtual WebRtc_Word32 Flush() = 0; + virtual int Flush() = 0; - // Returns the opened file's name in fileNameUTF8. size is the allocated - // size of fileNameUTF8. The name will be truncated if the size of - // fileNameUTF8 is to small. - virtual WebRtc_Word32 FileName(WebRtc_Word8* fileNameUTF8, - WebRtc_UWord32 size) const = 0; + // Returns the opened file's name in |fileNameUTF8|. Provide the size of + // the buffer in bytes in |size|. The name will be truncated if |size| is + // too small. + virtual int FileName(char* fileNameUTF8, + size_t size) const = 0; // Write text to the opened file. The written text can contain plain text // and text with type specifiers in the same way as sprintf works. - virtual WebRtc_Word32 WriteText(const char* format, ...) = 0; + virtual int WriteText(const char* format, ...) = 0; - // Reads len number of bytes from buf to file. + // Inherited from Instream. + // Reads |len| bytes from file to |buf|. virtual int Read(void* buf, int len) = 0; - // Writes len number of bytes to buf from file. The actual writing to file - // may happen some time later. Call flush to force a write to take effect. + // Inherited from OutStream. + // Writes |len| bytes from |buf| to file. The actual writing may happen + // some time later. Call Flush() to force a write. virtual bool Write(const void *buf, int len) = 0; + // Inherited from both Instream and OutStream. // Rewinds the file to the start. Only available when OpenFile() has been - // called with loop argument set to true. Or readOnly argument has been set - // to false. + // called with |loop| == true or |readOnly| == true. virtual int Rewind() = 0; }; + } // namespace webrtc #endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_FILE_WRAPPER_H_ diff --git a/src/system_wrappers/source/file_impl.cc b/src/system_wrappers/source/file_impl.cc index f252fe2125..3fc913707f 100644 --- a/src/system_wrappers/source/file_impl.cc +++ b/src/system_wrappers/source/file_impl.cc @@ -20,6 +20,7 @@ #endif namespace webrtc { + FileWrapper* FileWrapper::Create() { return new FileWrapperImpl(); @@ -30,8 +31,7 @@ FileWrapperImpl::FileWrapperImpl() _open(false), _looping(false), _readOnly(false), - _text(false), - _maxSizeInBytes(-1), + _maxSizeInBytes(0), _sizeInBytes(0) { memset(_fileNameUTF8, 0, kMaxFileNameSize); @@ -45,7 +45,7 @@ FileWrapperImpl::~FileWrapperImpl() } } -WebRtc_Word32 FileWrapperImpl::CloseFile() +int FileWrapperImpl::CloseFile() { if (_id != NULL) { @@ -70,13 +70,13 @@ int FileWrapperImpl::Rewind() return -1; } -WebRtc_Word32 FileWrapperImpl::SetMaxFileSize(WebRtc_Word32 bytes) +int FileWrapperImpl::SetMaxFileSize(size_t bytes) { _maxSizeInBytes = bytes; return 0; } -WebRtc_Word32 FileWrapperImpl::Flush() +int FileWrapperImpl::Flush() { if (_id != NULL) { @@ -85,10 +85,10 @@ WebRtc_Word32 FileWrapperImpl::Flush() return -1; } -WebRtc_Word32 FileWrapperImpl::FileName(WebRtc_Word8* fileNameUTF8, - WebRtc_UWord32 size) const +int FileWrapperImpl::FileName(char* fileNameUTF8, + size_t size) const { - WebRtc_Word32 len = static_cast(strlen(_fileNameUTF8)); + size_t len = strlen(_fileNameUTF8); if(len > kMaxFileNameSize) { assert(false); @@ -99,7 +99,7 @@ WebRtc_Word32 FileWrapperImpl::FileName(WebRtc_Word8* fileNameUTF8, return -1; } // Make sure to NULL terminate - if(size < (WebRtc_UWord32)len) + if(size < len) { len = size - 1; } @@ -108,24 +108,21 @@ WebRtc_Word32 FileWrapperImpl::FileName(WebRtc_Word8* fileNameUTF8, return 0; } -bool -FileWrapperImpl::Open() const +bool FileWrapperImpl::Open() const { return _open; } -WebRtc_Word32 FileWrapperImpl::OpenFile(const WebRtc_Word8 *fileNameUTF8, - const bool readOnly, const bool loop, - const bool text) +int FileWrapperImpl::OpenFile(const char *fileNameUTF8, bool readOnly, + bool loop, bool text) { - WebRtc_Word32 length = (WebRtc_Word32)strlen(fileNameUTF8); + size_t length = strlen(fileNameUTF8); if (length > kMaxFileNameSize) { return -1; } _readOnly = readOnly; - _text = text; FILE *tmpId = NULL; #if defined _WIN32 @@ -191,10 +188,6 @@ WebRtc_Word32 FileWrapperImpl::OpenFile(const WebRtc_Word8 *fileNameUTF8, int FileWrapperImpl::Read(void *buf, int len) { - if(len < 0) - { - return 0; - } if (_id != NULL) { int res = static_cast(fread(buf, 1, len, _id)); @@ -205,19 +198,16 @@ int FileWrapperImpl::Read(void *buf, int len) CloseFile(); } } - return res; + return 0; } return -1; } -WebRtc_Word32 FileWrapperImpl::WriteText(const char* format, ...) +int FileWrapperImpl::WriteText(const char* format, ...) { if (_readOnly) return -1; - if (!_text) - return -1; - if (_id == NULL) return -1; @@ -226,10 +216,10 @@ WebRtc_Word32 FileWrapperImpl::WriteText(const char* format, ...) va_list args; va_start(args, format); - int num_bytes = vfprintf(_id, format, args); + int num_chars = vfprintf(_id, format, args); va_end(args); - if (num_bytes > 0) + if (num_chars > 0) { return 0; } @@ -250,21 +240,21 @@ bool FileWrapperImpl::Write(const void* buf, int len) if (_id != NULL) { // Check if it's time to stop writing. - if ((_maxSizeInBytes != -1) && - _sizeInBytes + len > (WebRtc_UWord32)_maxSizeInBytes) + if (_sizeInBytes + len > _maxSizeInBytes) { Flush(); return false; } - size_t nBytes = fwrite((WebRtc_UWord8*)buf, 1, len, _id); - if (nBytes > 0) + size_t num_bytes = fwrite(buf, 1, len, _id); + if (num_bytes > 0) { - _sizeInBytes += static_cast(nBytes); + _sizeInBytes += num_bytes; return true; } CloseFile(); } return false; } + } // namespace webrtc diff --git a/src/system_wrappers/source/file_impl.h b/src/system_wrappers/source/file_impl.h index 457b11026f..00dcb6100b 100644 --- a/src/system_wrappers/source/file_impl.h +++ b/src/system_wrappers/source/file_impl.h @@ -16,42 +16,42 @@ #include namespace webrtc { + class FileWrapperImpl : public FileWrapper { public: FileWrapperImpl(); virtual ~FileWrapperImpl(); - virtual WebRtc_Word32 FileName(WebRtc_Word8* fileNameUTF8, - WebRtc_UWord32 size) const; + virtual int FileName(char* fileNameUTF8, + size_t size) const; virtual bool Open() const; - virtual WebRtc_Word32 OpenFile(const WebRtc_Word8* fileNameUTF8, - const bool readOnly, - const bool loop = false, - const bool text = false); + virtual int OpenFile(const char* fileNameUTF8, + bool readOnly, + bool loop = false, + bool text = false); - virtual WebRtc_Word32 CloseFile(); - virtual WebRtc_Word32 SetMaxFileSize(WebRtc_Word32 bytes); - virtual WebRtc_Word32 Flush(); + virtual int CloseFile(); + virtual int SetMaxFileSize(size_t bytes); + virtual int Flush(); virtual int Read(void* buf, int len); virtual bool Write(const void *buf, int len); + virtual int WriteText(const char* format, ...); virtual int Rewind(); - virtual WebRtc_Word32 WriteText(const char* format, ...); - private: - FILE* _id; - bool _open; - bool _looping; - bool _readOnly; - bool _text; - WebRtc_Word32 _maxSizeInBytes; // -1 indicates file size limitation is off - WebRtc_UWord32 _sizeInBytes; - WebRtc_Word8 _fileNameUTF8[kMaxFileNameSize]; + FILE* _id; + bool _open; + bool _looping; + bool _readOnly; + size_t _maxSizeInBytes; // -1 indicates file size limitation is off + size_t _sizeInBytes; + char _fileNameUTF8[kMaxFileNameSize]; }; + } // namespace webrtc #endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_FILE_IMPL_H_