diff --git a/WebRtc.NET.Utils/Util.cs b/WebRtc.NET.Utils/Util.cs index b7f5c84c..5e47e0af 100644 --- a/WebRtc.NET.Utils/Util.cs +++ b/WebRtc.NET.Utils/Util.cs @@ -21,9 +21,9 @@ public class Util static UInt64 i = 0; - public unsafe static void OnFillBuffer(byte * pData, long lDataLen) + public unsafe static void OnFillBuffer(byte * pData, long lDataLen, int part_idx, bool keyFrame) { - Trace.WriteLine(++i + ": _EncodeInternal: " + lDataLen); + Trace.WriteLine($"{i++}: Encode[{keyFrame}|{part_idx}]: {lDataLen}"); using (var f = File.Open("dump.bin", FileMode.OpenOrCreate, FileAccess.ReadWrite)) { @@ -31,7 +31,7 @@ public unsafe static void OnFillBuffer(byte * pData, long lDataLen) using (var b = new BinaryWriter(f)) { - b.Write((int)lDataLen); + //b.Write((int)lDataLen); using (UnmanagedMemoryStream ms = new UnmanagedMemoryStream(pData, lDataLen)) { diff --git a/WebRtc.NET/WebRtc.NET.vcxproj b/WebRtc.NET/WebRtc.NET.vcxproj index ea5bda92..d503bd8c 100644 --- a/WebRtc.NET/WebRtc.NET.vcxproj +++ b/WebRtc.NET/WebRtc.NET.vcxproj @@ -184,8 +184,8 @@ NotUsing - NotUsing NotUsing + NotUsing NotUsing diff --git a/WebRtc.NET/WebRtc.NET.vcxproj.filters b/WebRtc.NET/WebRtc.NET.vcxproj.filters index c2af2b85..bf976260 100644 --- a/WebRtc.NET/WebRtc.NET.vcxproj.filters +++ b/WebRtc.NET/WebRtc.NET.vcxproj.filters @@ -62,14 +62,14 @@ Source Files\src - - Source Files\src\internals - Source Files\src\internals Source Files\src + + Source Files\src\internals + \ No newline at end of file diff --git a/WebRtc.NET/internals.h b/WebRtc.NET/internals.h index 005d926e..9f589267 100644 --- a/WebRtc.NET/internals.h +++ b/WebRtc.NET/internals.h @@ -11,7 +11,10 @@ extern bool CFG_quality_scaler_enabled_; void _InitializeSSL(); void _CleanupSSL(); -//void _EncodeInternal(unsigned char * data, unsigned int size); +namespace Internal +{ + void Encode(unsigned char * data, unsigned int size, int part_idx, bool keyFrame); +} namespace Native { diff --git a/WebRtc.NET/src/conductor.cc b/WebRtc.NET/src/conductor.cc index ca7c3ceb..f43e6a98 100644 --- a/WebRtc.NET/src/conductor.cc +++ b/WebRtc.NET/src/conductor.cc @@ -2,7 +2,7 @@ #include "defaults.h" #include "conductor.h" -#include "webrtc/api/videosourceinterface.h" +//#include "webrtc/api/videosourceinterface.h" #include "webrtc/api/test/fakeconstraints.h" #include "webrtc/video_encoder.h" @@ -291,12 +291,6 @@ void Conductor::OnAddStream(webrtc::MediaStreamInterface* stream) void Conductor::OnRemoveStream(webrtc::MediaStreamInterface* stream) { LOG(INFO) << __FUNCTION__ << " " << stream->label(); - //stream->AddRef(); - //main_wnd_->QueueUIThreadCallback(STREAM_REMOVED, stream); - - // Remote peer stopped sending a stream. - //webrtc::MediaStreamInterface* stream = reinterpret_cast(stream); - //stream->Release(); } void Conductor::OnIceCandidate(const webrtc::IceCandidateInterface* candidate) diff --git a/WebRtc.NET/src/defaults.cc b/WebRtc.NET/src/defaults.cc index a99f772b..ad6acdfc 100644 --- a/WebRtc.NET/src/defaults.cc +++ b/WebRtc.NET/src/defaults.cc @@ -3,12 +3,6 @@ #include "internals.h" #include "conductor.h" -#include "webrtc/base/bind.h" - -/////////////////////////////////////////////////////////////////////// -// Definition of private class YuvFramesThread that periodically generates -// frames. -/////////////////////////////////////////////////////////////////////// class YuvFramesCapturer2::YuvFramesThread : public rtc::Thread, public rtc::MessageHandler { public: @@ -73,31 +67,28 @@ class YuvFramesCapturer2::YuvFramesThread : public rtc::Thread, public rtc::Mess RTC_DISALLOW_COPY_AND_ASSIGN(YuvFramesThread); }; -///////////////////////////////////////////////////////////////////// -// Implementation of class YuvFramesCapturer. -///////////////////////////////////////////////////////////////////// +namespace +{ + int I420DataSize(int height, int stride_y, int stride_u, int stride_v) + { + return stride_y * height + (stride_u + stride_v) * ((height + 1) / 2); + } +} -// TODO(shaowei): allow width_ and height_ to be configurable. YuvFramesCapturer2::YuvFramesCapturer2(Conductor & c) : frames_generator_thread(NULL), width_(640), height_(360), frame_index_(0), barcode_interval_(1), - startThread_(NULL), con(&c) { - int size = width_ * height_; - int qsize = size / 4; frame_generator_ = new cricket::YuvFrameGenerator(width_, height_, true); - frame_data_size_ = size + 2 * qsize; - captured_frame_.data = new char[frame_data_size_]; - captured_frame_.fourcc = cricket::FOURCC_IYUV; - captured_frame_.pixel_height = 1; - captured_frame_.pixel_width = 1; - captured_frame_.width = width_; - captured_frame_.height = height_; - captured_frame_.data_size = frame_data_size_; + + video_buffer = webrtc::I420Buffer::Create(width_, height_); + frame_data_size_ = I420DataSize(height_, video_buffer->StrideY(), video_buffer->StrideU(), video_buffer->StrideV()); + + video_frame = new cricket::WebRtcVideoFrame(video_buffer, webrtc::VideoRotation::kVideoRotation_0, 0, 0); // Enumerate the supported formats. We have only one supported format. cricket::VideoFormat format(width_, height_, cricket::VideoFormat::FpsToInterval(con->caputureFps), cricket::FOURCC_IYUV); @@ -112,7 +103,6 @@ YuvFramesCapturer2::YuvFramesCapturer2(Conductor & c) YuvFramesCapturer2::~YuvFramesCapturer2() { Stop(); - delete[] static_cast(captured_frame_.data); } cricket::CaptureState YuvFramesCapturer2::Start(const cricket::VideoFormat& capture_format) @@ -124,9 +114,7 @@ cricket::CaptureState YuvFramesCapturer2::Start(const cricket::VideoFormat& capt } SetCaptureFormat(&capture_format); - barcode_reference_timestamp_millis_ = rtc::TimeNanos(); - - startThread_ = rtc::Thread::Current(); + barcode_reference_timestamp_millis_ = rtc::TimeNanos(); // Create a thread to generate frames. frames_generator_thread = new YuvFramesThread(this); @@ -153,12 +141,11 @@ void YuvFramesCapturer2::Stop() if (frames_generator_thread) { frames_generator_thread->Stop(); + delete frames_generator_thread; frames_generator_thread = NULL; LOG(LS_INFO) << "Yuv Frame Generator stopped"; } SetCaptureFormat(NULL); - - startThread_ = NULL; } bool YuvFramesCapturer2::GetPreferredFourccs(std::vector* fourccs) @@ -174,40 +161,39 @@ bool YuvFramesCapturer2::GetPreferredFourccs(std::vector* fourccs) // Executed in the context of YuvFramesThread. void YuvFramesCapturer2::ReadFrame(bool first_frame) { - // 1. Signal the previously read frame to downstream. - if (!first_frame) - { - //OnFrameCaptured(this, &captured_frame_); - - if (startThread_->IsCurrent()) - { - SignalFrameCaptured(this, &captured_frame_); - } - else - { - startThread_->Invoke(RTC_FROM_HERE, rtc::Bind(&YuvFramesCapturer2::SignalFrameCapturedOnStartThread, this, &captured_frame_)); - } - } - //else + int64_t camera_time_us = rtc::TimeMicros(); + int64_t system_time_us = camera_time_us; + int out_width; + int out_height; + int crop_width; + int crop_height; + int crop_x; + int crop_y; + int64_t translated_camera_time_us; + + if (AdaptFrame(width_, + height_, + camera_time_us, + system_time_us, + &out_width, + &out_height, + &crop_width, + &crop_height, + &crop_x, + &crop_y, + &translated_camera_time_us)) { - captured_frame_.time_stamp = rtc::TimeNanos(); - if (con->barcodeEnabled) { - frame_generator_->GenerateNextFrame((uint8_t*)captured_frame_.data, GetBarcodeValue()); + frame_generator_->GenerateNextFrame((uint8_t*)video_buffer->DataY(), static_cast(rtc::TimeNanos() - barcode_reference_timestamp_millis_)); } else { - con->OnFillBuffer((uint8_t*)captured_frame_.data, captured_frame_.data_size); + con->OnFillBuffer((uint8_t*)video_buffer->DataY(), frame_data_size_); } - } -} -int32_t YuvFramesCapturer2::GetBarcodeValue() -{ - if (barcode_reference_timestamp_millis_ == -1 || frame_index_ % barcode_interval_ != 0) - { - return -1; + video_frame->set_timestamp_us(translated_camera_time_us); + + OnFrame(*video_frame, width_, height_); } - return static_cast(captured_frame_.time_stamp - barcode_reference_timestamp_millis_); } \ No newline at end of file diff --git a/WebRtc.NET/src/defaults.h b/WebRtc.NET/src/defaults.h index ec3bd8f6..e5b2b45a 100644 --- a/WebRtc.NET/src/defaults.h +++ b/WebRtc.NET/src/defaults.h @@ -34,18 +34,13 @@ class YuvFramesCapturer2 : public cricket::VideoCapturer private: class YuvFramesThread; // Forward declaration, defined in .cc. - rtc::Thread* startThread_; // Set in Start(), unset in Stop(). - // Used to signal frame capture on the thread that capturer was started on. - void SignalFrameCapturedOnStartThread(const cricket::CapturedFrame* frame) - { - SignalFrameCaptured(this, frame); - } - Conductor * con; - - cricket::YuvFrameGenerator* frame_generator_; - cricket::CapturedFrame captured_frame_; YuvFramesThread* frames_generator_thread; + cricket::YuvFrameGenerator* frame_generator_; + + rtc::scoped_refptr video_buffer; + cricket::VideoFrame * video_frame; + int width_; int height_; uint32_t frame_data_size_; @@ -53,7 +48,6 @@ class YuvFramesCapturer2 : public cricket::VideoCapturer int64_t barcode_reference_timestamp_millis_; int32_t barcode_interval_; - int32_t GetBarcodeValue(); RTC_DISALLOW_COPY_AND_ASSIGN(YuvFramesCapturer2); }; diff --git a/WebRtc.NET/src/main.cc b/WebRtc.NET/src/main.cc index a0ab6266..cd8a8d4c 100644 --- a/WebRtc.NET/src/main.cc +++ b/WebRtc.NET/src/main.cc @@ -152,8 +152,8 @@ #pragma comment(lib,"libvpx_intrinsics_ssse3.lib") #pragma comment(lib,"libvpx_yasm.lib") -#pragma comment(lib,"webrtc_vp8.lib") // original -//#pragma comment(lib,"webrtc_vp8_no_impl.lib") // for hacking vp8_impl.cc +//#pragma comment(lib,"webrtc_vp8.lib") // original +#pragma comment(lib,"webrtc_vp8_no_impl.lib") // for hacking vp8_impl.cc #include "stdafx.h" diff --git a/WebRtc.NET/src/managed.cpp b/WebRtc.NET/src/managed.cpp index b02d56de..5fca2f27 100644 --- a/WebRtc.NET/src/managed.cpp +++ b/WebRtc.NET/src/managed.cpp @@ -18,9 +18,12 @@ using namespace msclr::interop; [assembly:System::Runtime::Versioning::TargetFrameworkAttribute(L".NETFramework,Version=v4.0", FrameworkDisplayName = L".NET Framework 4")]; -void _EncodeInternal(unsigned char * data, unsigned int size) +namespace Internal { - //Util::OnFillBuffer(data, size); + void Encode(unsigned char * data, unsigned int size, int part_idx, bool keyFrame) + { + //Util::OnFillBuffer(data, size, part_idx, keyFrame); + } } namespace WebRtc