From b08a12d6e81aee11e0d77df978e2eccbb4d268b0 Mon Sep 17 00:00:00 2001 From: "andresp@webrtc.org" Date: Wed, 8 Jan 2014 12:38:22 +0000 Subject: [PATCH] Isolate debug recording from video sender into a thread safe small class. R=marpan@webrtc.org Review URL: https://webrtc-codereview.appspot.com/6649004 git-svn-id: http://webrtc.googlecode.com/svn/trunk@5353 4adac7df-926f-26a2-2b94-8c16560cd09d --- .../main/source/video_coding_impl.cc | 3 +- .../main/source/video_coding_impl.h | 7 ++- .../video_coding/main/source/video_sender.cc | 62 ++++++++++++------- 3 files changed, 48 insertions(+), 24 deletions(-) diff --git a/webrtc/modules/video_coding/main/source/video_coding_impl.cc b/webrtc/modules/video_coding/main/source/video_coding_impl.cc index 1decc2f112..f1ec0eb8ff 100644 --- a/webrtc/modules/video_coding/main/source/video_coding_impl.cc +++ b/webrtc/modules/video_coding/main/source/video_coding_impl.cc @@ -194,7 +194,8 @@ class VideoCodingModuleImpl : public VideoCodingModule { } virtual int StopDebugRecording() OVERRIDE { - return sender_->StopDebugRecording(); + sender_->StopDebugRecording(); + return VCM_OK; } virtual void SuspendBelowMinBitrate() { diff --git a/webrtc/modules/video_coding/main/source/video_coding_impl.h b/webrtc/modules/video_coding/main/source/video_coding_impl.h index 5ecb02aad3..9f22c0b692 100644 --- a/webrtc/modules/video_coding/main/source/video_coding_impl.h +++ b/webrtc/modules/video_coding/main/source/video_coding_impl.h @@ -32,6 +32,8 @@ class EncodedFrameObserver; namespace vcm { +class DebugRecorder; + class VCMProcessTimer { public: VCMProcessTimer(uint32_t periodMs, Clock* clock) @@ -96,7 +98,7 @@ class VideoSender { int SetSenderKeyFramePeriod(int periodMs); int StartDebugRecording(const char* file_name_utf8); - int StopDebugRecording(); + void StopDebugRecording(); void SuspendBelowMinBitrate(); bool VideoSuspended() const; @@ -111,6 +113,8 @@ class VideoSender { int32_t _id; Clock* clock_; + scoped_ptr recorder_; + scoped_ptr process_crit_sect_; CriticalSectionWrapper* _sendCritSect; VCMGenericEncoder* _encoder; @@ -118,7 +122,6 @@ class VideoSender { std::vector _nextFrameTypes; media_optimization::MediaOptimization _mediaOpt; VCMSendStatisticsCallback* _sendStatsCallback; - FILE* _encoderInputFile; VCMCodecDataBase _codecDataBase; bool frame_dropper_enabled_; VCMProcessTimer _sendStatsTimer; diff --git a/webrtc/modules/video_coding/main/source/video_sender.cc b/webrtc/modules/video_coding/main/source/video_sender.cc index d19896666d..5ac842e6c8 100644 --- a/webrtc/modules/video_coding/main/source/video_sender.cc +++ b/webrtc/modules/video_coding/main/source/video_sender.cc @@ -21,9 +21,46 @@ namespace webrtc { namespace vcm { +class DebugRecorder { + public: + DebugRecorder() + : cs_(CriticalSectionWrapper::CreateCriticalSection()), file_(NULL) {} + + ~DebugRecorder() { Stop(); } + + int Start(const char* file_name_utf8) { + CriticalSectionScoped cs(cs_.get()); + if (file_) + fclose(file_); + file_ = fopen(file_name_utf8, "wb"); + if (!file_) + return VCM_GENERAL_ERROR; + return VCM_OK; + } + + void Stop() { + CriticalSectionScoped cs(cs_.get()); + if (file_) { + fclose(file_); + file_ = NULL; + } + } + + void Add(const I420VideoFrame& frame) { + CriticalSectionScoped cs(cs_.get()); + if (file_) + PrintI420VideoFrame(frame, file_); + } + + private: + scoped_ptr cs_; + FILE* file_ GUARDED_BY(cs_); +}; + VideoSender::VideoSender(const int32_t id, Clock* clock) : _id(id), clock_(clock), + recorder_(new DebugRecorder()), process_crit_sect_(CriticalSectionWrapper::CreateCriticalSection()), _sendCritSect(CriticalSectionWrapper::CreateCriticalSection()), _encoder(), @@ -31,7 +68,6 @@ VideoSender::VideoSender(const int32_t id, Clock* clock) _nextFrameTypes(1, kVideoFrameDelta), _mediaOpt(id, clock_), _sendStatsCallback(NULL), - _encoderInputFile(NULL), _codecDataBase(id), frame_dropper_enabled_(true), _sendStatsTimer(1000, clock_), @@ -40,9 +76,6 @@ VideoSender::VideoSender(const int32_t id, Clock* clock) VideoSender::~VideoSender() { delete _sendCritSect; - if (_encoderInputFile != NULL) { - fclose(_encoderInputFile); - } } int32_t VideoSender::Process() { @@ -336,11 +369,7 @@ int32_t VideoSender::AddVideoFrame(const I420VideoFrame& videoFrame, _mediaOpt.UpdateContentData(contentMetrics); int32_t ret = _encoder->Encode(videoFrame, codecSpecificInfo, _nextFrameTypes); - if (_encoderInputFile != NULL) { - if (PrintI420VideoFrame(videoFrame, _encoderInputFile) < 0) { - return -1; - } - } + recorder_->Add(videoFrame); if (ret < 0) { WEBRTC_TRACE(webrtc::kTraceError, webrtc::kTraceVideoCoding, @@ -412,20 +441,11 @@ int VideoSender::SetSenderKeyFramePeriod(int periodMs) { } int VideoSender::StartDebugRecording(const char* file_name_utf8) { - CriticalSectionScoped cs(_sendCritSect); - _encoderInputFile = fopen(file_name_utf8, "wb"); - if (_encoderInputFile == NULL) - return VCM_GENERAL_ERROR; - return VCM_OK; + return recorder_->Start(file_name_utf8); } -int VideoSender::StopDebugRecording() { - CriticalSectionScoped cs(_sendCritSect); - if (_encoderInputFile != NULL) { - fclose(_encoderInputFile); - _encoderInputFile = NULL; - } - return VCM_OK; +void VideoSender::StopDebugRecording() { + recorder_->Stop(); } void VideoSender::SuspendBelowMinBitrate() {