Skip to content

Commit

Permalink
switch to new AdaptFrame/OnFrame interface
Browse files Browse the repository at this point in the history
  • Loading branch information
radioman committed Oct 14, 2016
1 parent f9a8c02 commit d43a9df
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 84 deletions.
6 changes: 3 additions & 3 deletions WebRtc.NET.Utils/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ 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))
{
f.Seek(0, SeekOrigin.End);

using (var b = new BinaryWriter(f))
{
b.Write((int)lDataLen);
//b.Write((int)lDataLen);

using (UnmanagedMemoryStream ms = new UnmanagedMemoryStream(pData, lDataLen))
{
Expand Down
2 changes: 1 addition & 1 deletion WebRtc.NET/WebRtc.NET.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">NotUsing</PrecompiledHeader>
</ClCompile>
<ClCompile Include="src\internals\vp8_impl.cc">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">NotUsing</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">NotUsing</PrecompiledHeader>
</ClCompile>
<ClCompile Include="src\internals\yuvframegenerator.cc">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">NotUsing</PrecompiledHeader>
Expand Down
6 changes: 3 additions & 3 deletions WebRtc.NET/WebRtc.NET.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@
<ClCompile Include="src\TJpeg.cpp">
<Filter>Source Files\src</Filter>
</ClCompile>
<ClCompile Include="src\internals\vp8_impl.cc">
<Filter>Source Files\src\internals</Filter>
</ClCompile>
<ClCompile Include="src\internals\yuvframegenerator.cc">
<Filter>Source Files\src\internals</Filter>
</ClCompile>
<ClCompile Include="src\filters.cpp">
<Filter>Source Files\src</Filter>
</ClCompile>
<ClCompile Include="src\internals\vp8_impl.cc">
<Filter>Source Files\src\internals</Filter>
</ClCompile>
</ItemGroup>
</Project>
5 changes: 4 additions & 1 deletion WebRtc.NET/internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
8 changes: 1 addition & 7 deletions WebRtc.NET/src/conductor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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<webrtc::MediaStreamInterface*>(stream);
//stream->Release();
}

void Conductor::OnIceCandidate(const webrtc::IceCandidateInterface* candidate)
Expand Down
94 changes: 40 additions & 54 deletions WebRtc.NET/src/defaults.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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);
Expand All @@ -112,7 +103,6 @@ YuvFramesCapturer2::YuvFramesCapturer2(Conductor & c)
YuvFramesCapturer2::~YuvFramesCapturer2()
{
Stop();
delete[] static_cast<char*>(captured_frame_.data);
}

cricket::CaptureState YuvFramesCapturer2::Start(const cricket::VideoFormat& capture_format)
Expand All @@ -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);
Expand All @@ -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<uint32_t>* fourccs)
Expand All @@ -174,40 +161,39 @@ bool YuvFramesCapturer2::GetPreferredFourccs(std::vector<uint32_t>* 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<void>(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<int32_t>(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<int32_t>(captured_frame_.time_stamp - barcode_reference_timestamp_millis_);
}
16 changes: 5 additions & 11 deletions WebRtc.NET/src/defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,20 @@ 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<webrtc::I420Buffer> video_buffer;
cricket::VideoFrame * video_frame;

int width_;
int height_;
uint32_t frame_data_size_;
uint32_t frame_index_;

int64_t barcode_reference_timestamp_millis_;
int32_t barcode_interval_;
int32_t GetBarcodeValue();

RTC_DISALLOW_COPY_AND_ASSIGN(YuvFramesCapturer2);
};
Expand Down
4 changes: 2 additions & 2 deletions WebRtc.NET/src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
7 changes: 5 additions & 2 deletions WebRtc.NET/src/managed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d43a9df

Please sign in to comment.