Fix error in RtpDump::Start due to r1156.

- r1156 fixed a check on the _text member of FileWrapper. Turns out this
  was incompatibile with the RTP dumps, which want to write both binary
  and text data. Writing text data to a file open as "b" isn't actually
  an error, so I simply removed the check.
- Also cleans up the interface, most notably removing all WebRtc types.

TEST=vie_auto_test

Review URL: http://webrtc-codereview.appspot.com/317005

git-svn-id: http://webrtc.googlecode.com/svn/trunk@1175 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
andrew@webrtc.org 2011-12-13 22:59:33 +00:00
parent 832cacacff
commit 5ae19de3ec
5 changed files with 93 additions and 75 deletions

View File

@ -191,7 +191,7 @@ class AudioProcessing : public Module {
// a NULL-terminated string. If there is an ongoing recording, the old file // 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. // will be closed, and recording will continue in the newly specified file.
// An already existing file will be overwritten without warning. // 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; virtual int StartDebugRecording(const char filename[kMaxFilenameSize]) = 0;
// Stops recording debugging information, and closes the file. Recording // Stops recording debugging information, and closes the file. Recording

View File

@ -114,7 +114,12 @@ WebRtc_Word32 RtpDumpImpl::Start(const WebRtc_Word8* fileNameUTF8)
// All rtp dump files start with #!rtpplay. // All rtp dump files start with #!rtpplay.
WebRtc_Word8 magic[16]; WebRtc_Word8 magic[16];
sprintf(magic, "#!rtpplay%s \n", RTPFILE_VERSION); 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) // 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 // 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. // of padding should be added to the header.
WebRtc_Word8 dummyHdr[16]; WebRtc_Word8 dummyHdr[16];
memset(dummyHdr, 0, 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; return 0;
} }
@ -190,8 +200,20 @@ WebRtc_Word32 RtpDumpImpl::DumpPacket(const WebRtc_UWord8* packet,
{ {
hdr.plen = RtpDumpHtons((WebRtc_UWord16)packetLength); 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; return 0;
} }

View File

@ -11,6 +11,8 @@
#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_FILE_WRAPPER_H_ #ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_FILE_WRAPPER_H_
#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_FILE_WRAPPER_H_ #define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_FILE_WRAPPER_H_
#include <stddef.h>
#include "common_types.h" #include "common_types.h"
#include "typedefs.h" #include "typedefs.h"
@ -18,10 +20,11 @@
// write from/to a file. // write from/to a file.
namespace webrtc { namespace webrtc {
class FileWrapper : public InStream, public OutStream class FileWrapper : public InStream, public OutStream
{ {
public: public:
enum { kMaxFileNameSize = 1024}; static const size_t kMaxFileNameSize = 1024;
// Factory method. Constructor disabled. // Factory method. Constructor disabled.
static FileWrapper* Create(); static FileWrapper* Create();
@ -30,41 +33,44 @@ public:
virtual bool Open() const = 0; virtual bool Open() const = 0;
// Opens a file in read or write mode, decided by the readOnly parameter. // Opens a file in read or write mode, decided by the readOnly parameter.
virtual WebRtc_Word32 OpenFile(const WebRtc_Word8* fileNameUTF8, virtual int OpenFile(const char* fileNameUTF8,
const bool readOnly, bool readOnly,
const bool loop = false, bool loop = false,
const bool text = false) = 0; bool text = false) = 0;
virtual WebRtc_Word32 CloseFile() = 0; virtual int CloseFile() = 0;
// Limits the file size. // Limits the file size.
virtual WebRtc_Word32 SetMaxFileSize(WebRtc_Word32 bytes) = 0; virtual int SetMaxFileSize(size_t bytes) = 0;
// Flush any pending writes. // 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 // Returns the opened file's name in |fileNameUTF8|. Provide the size of
// size of fileNameUTF8. The name will be truncated if the size of // the buffer in bytes in |size|. The name will be truncated if |size| is
// fileNameUTF8 is to small. // too small.
virtual WebRtc_Word32 FileName(WebRtc_Word8* fileNameUTF8, virtual int FileName(char* fileNameUTF8,
WebRtc_UWord32 size) const = 0; size_t size) const = 0;
// Write text to the opened file. The written text can contain plain text // 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. // 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; virtual int Read(void* buf, int len) = 0;
// Writes len number of bytes to buf from file. The actual writing to file // Inherited from OutStream.
// may happen some time later. Call flush to force a write to take effect. // 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; 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 // 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 // called with |loop| == true or |readOnly| == true.
// to false.
virtual int Rewind() = 0; virtual int Rewind() = 0;
}; };
} // namespace webrtc } // namespace webrtc
#endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_FILE_WRAPPER_H_ #endif // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_FILE_WRAPPER_H_

View File

@ -20,6 +20,7 @@
#endif #endif
namespace webrtc { namespace webrtc {
FileWrapper* FileWrapper::Create() FileWrapper* FileWrapper::Create()
{ {
return new FileWrapperImpl(); return new FileWrapperImpl();
@ -30,8 +31,7 @@ FileWrapperImpl::FileWrapperImpl()
_open(false), _open(false),
_looping(false), _looping(false),
_readOnly(false), _readOnly(false),
_text(false), _maxSizeInBytes(0),
_maxSizeInBytes(-1),
_sizeInBytes(0) _sizeInBytes(0)
{ {
memset(_fileNameUTF8, 0, kMaxFileNameSize); memset(_fileNameUTF8, 0, kMaxFileNameSize);
@ -45,7 +45,7 @@ FileWrapperImpl::~FileWrapperImpl()
} }
} }
WebRtc_Word32 FileWrapperImpl::CloseFile() int FileWrapperImpl::CloseFile()
{ {
if (_id != NULL) if (_id != NULL)
{ {
@ -70,13 +70,13 @@ int FileWrapperImpl::Rewind()
return -1; return -1;
} }
WebRtc_Word32 FileWrapperImpl::SetMaxFileSize(WebRtc_Word32 bytes) int FileWrapperImpl::SetMaxFileSize(size_t bytes)
{ {
_maxSizeInBytes = bytes; _maxSizeInBytes = bytes;
return 0; return 0;
} }
WebRtc_Word32 FileWrapperImpl::Flush() int FileWrapperImpl::Flush()
{ {
if (_id != NULL) if (_id != NULL)
{ {
@ -85,10 +85,10 @@ WebRtc_Word32 FileWrapperImpl::Flush()
return -1; return -1;
} }
WebRtc_Word32 FileWrapperImpl::FileName(WebRtc_Word8* fileNameUTF8, int FileWrapperImpl::FileName(char* fileNameUTF8,
WebRtc_UWord32 size) const size_t size) const
{ {
WebRtc_Word32 len = static_cast<WebRtc_Word32>(strlen(_fileNameUTF8)); size_t len = strlen(_fileNameUTF8);
if(len > kMaxFileNameSize) if(len > kMaxFileNameSize)
{ {
assert(false); assert(false);
@ -99,7 +99,7 @@ WebRtc_Word32 FileWrapperImpl::FileName(WebRtc_Word8* fileNameUTF8,
return -1; return -1;
} }
// Make sure to NULL terminate // Make sure to NULL terminate
if(size < (WebRtc_UWord32)len) if(size < len)
{ {
len = size - 1; len = size - 1;
} }
@ -108,24 +108,21 @@ WebRtc_Word32 FileWrapperImpl::FileName(WebRtc_Word8* fileNameUTF8,
return 0; return 0;
} }
bool bool FileWrapperImpl::Open() const
FileWrapperImpl::Open() const
{ {
return _open; return _open;
} }
WebRtc_Word32 FileWrapperImpl::OpenFile(const WebRtc_Word8 *fileNameUTF8, int FileWrapperImpl::OpenFile(const char *fileNameUTF8, bool readOnly,
const bool readOnly, const bool loop, bool loop, bool text)
const bool text)
{ {
WebRtc_Word32 length = (WebRtc_Word32)strlen(fileNameUTF8); size_t length = strlen(fileNameUTF8);
if (length > kMaxFileNameSize) if (length > kMaxFileNameSize)
{ {
return -1; return -1;
} }
_readOnly = readOnly; _readOnly = readOnly;
_text = text;
FILE *tmpId = NULL; FILE *tmpId = NULL;
#if defined _WIN32 #if defined _WIN32
@ -191,10 +188,6 @@ WebRtc_Word32 FileWrapperImpl::OpenFile(const WebRtc_Word8 *fileNameUTF8,
int FileWrapperImpl::Read(void *buf, int len) int FileWrapperImpl::Read(void *buf, int len)
{ {
if(len < 0)
{
return 0;
}
if (_id != NULL) if (_id != NULL)
{ {
int res = static_cast<int>(fread(buf, 1, len, _id)); int res = static_cast<int>(fread(buf, 1, len, _id));
@ -205,19 +198,16 @@ int FileWrapperImpl::Read(void *buf, int len)
CloseFile(); CloseFile();
} }
} }
return res; return 0;
} }
return -1; return -1;
} }
WebRtc_Word32 FileWrapperImpl::WriteText(const char* format, ...) int FileWrapperImpl::WriteText(const char* format, ...)
{ {
if (_readOnly) if (_readOnly)
return -1; return -1;
if (!_text)
return -1;
if (_id == NULL) if (_id == NULL)
return -1; return -1;
@ -226,10 +216,10 @@ WebRtc_Word32 FileWrapperImpl::WriteText(const char* format, ...)
va_list args; va_list args;
va_start(args, format); va_start(args, format);
int num_bytes = vfprintf(_id, format, args); int num_chars = vfprintf(_id, format, args);
va_end(args); va_end(args);
if (num_bytes > 0) if (num_chars > 0)
{ {
return 0; return 0;
} }
@ -250,21 +240,21 @@ bool FileWrapperImpl::Write(const void* buf, int len)
if (_id != NULL) if (_id != NULL)
{ {
// Check if it's time to stop writing. // Check if it's time to stop writing.
if ((_maxSizeInBytes != -1) && if (_sizeInBytes + len > _maxSizeInBytes)
_sizeInBytes + len > (WebRtc_UWord32)_maxSizeInBytes)
{ {
Flush(); Flush();
return false; return false;
} }
size_t nBytes = fwrite((WebRtc_UWord8*)buf, 1, len, _id); size_t num_bytes = fwrite(buf, 1, len, _id);
if (nBytes > 0) if (num_bytes > 0)
{ {
_sizeInBytes += static_cast<WebRtc_Word32>(nBytes); _sizeInBytes += num_bytes;
return true; return true;
} }
CloseFile(); CloseFile();
} }
return false; return false;
} }
} // namespace webrtc } // namespace webrtc

View File

@ -16,42 +16,42 @@
#include <stdio.h> #include <stdio.h>
namespace webrtc { namespace webrtc {
class FileWrapperImpl : public FileWrapper class FileWrapperImpl : public FileWrapper
{ {
public: public:
FileWrapperImpl(); FileWrapperImpl();
virtual ~FileWrapperImpl(); virtual ~FileWrapperImpl();
virtual WebRtc_Word32 FileName(WebRtc_Word8* fileNameUTF8, virtual int FileName(char* fileNameUTF8,
WebRtc_UWord32 size) const; size_t size) const;
virtual bool Open() const; virtual bool Open() const;
virtual WebRtc_Word32 OpenFile(const WebRtc_Word8* fileNameUTF8, virtual int OpenFile(const char* fileNameUTF8,
const bool readOnly, bool readOnly,
const bool loop = false, bool loop = false,
const bool text = false); bool text = false);
virtual WebRtc_Word32 CloseFile(); virtual int CloseFile();
virtual WebRtc_Word32 SetMaxFileSize(WebRtc_Word32 bytes); virtual int SetMaxFileSize(size_t bytes);
virtual WebRtc_Word32 Flush(); virtual int Flush();
virtual int Read(void* buf, int len); virtual int Read(void* buf, int len);
virtual bool Write(const void *buf, int len); virtual bool Write(const void *buf, int len);
virtual int WriteText(const char* format, ...);
virtual int Rewind(); virtual int Rewind();
virtual WebRtc_Word32 WriteText(const char* format, ...);
private: private:
FILE* _id; FILE* _id;
bool _open; bool _open;
bool _looping; bool _looping;
bool _readOnly; bool _readOnly;
bool _text; size_t _maxSizeInBytes; // -1 indicates file size limitation is off
WebRtc_Word32 _maxSizeInBytes; // -1 indicates file size limitation is off size_t _sizeInBytes;
WebRtc_UWord32 _sizeInBytes; char _fileNameUTF8[kMaxFileNameSize];
WebRtc_Word8 _fileNameUTF8[kMaxFileNameSize];
}; };
} // namespace webrtc } // namespace webrtc
#endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_FILE_IMPL_H_ #endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_FILE_IMPL_H_