mswinrtdis.cpp 8.01 KB
Newer Older
1
/*
2
mswinrtdis.cpp
3 4 5

mediastreamer2 library - modular sound and video processing and streaming
Windows Audio Session API sound card plugin for mediastreamer2
6
Copyright (C) 2010-2013 Belledonne Communications, Grenoble, France
7 8 9 10 11 12 13 14 15 16 17 18 19

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
20
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21 22 23
*/


24
#include "mswinrtdis.h"
25
#include "VideoBuffer.h"
26

27
using namespace libmswinrtvid;
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
using namespace Microsoft::WRL;
using namespace Windows::Foundation;
using namespace Windows::Media::Core;
using namespace Windows::Media::MediaProperties;


//#define MSWINRTDIS_DEBUG


static void _startMediaElement(Windows::UI::Xaml::Controls::MediaElement^ mediaElement, Windows::Media::Core::MediaStreamSource^ mediaStreamSource)
{
	ms_message("[MSWinRTDis] Play MediaElement");
	mediaElement->RealTimePlayback = true;
	mediaElement->SetMediaStreamSource(mediaStreamSource);
	mediaElement->Play();
}

static void _stopMediaElement(Windows::UI::Xaml::Controls::MediaElement^ mediaElement, MSWinRTDisSampleHandler^ sampleHandler)
{
	ms_message("[MSWinRTDis] Stop MediaElement");
	mediaElement->Stop();
}


MSWinRTDisSampleHandler::MSWinRTDisSampleHandler() :
	mSample(nullptr), mReferenceTime(0), mPixFmt(MS_YUV420P), mWidth(MS_VIDEO_SIZE_CIF_W), mHeight(MS_VIDEO_SIZE_CIF_H), mStarted(false)
{
	mDeferralQueue = ref new Platform::Collections::Vector<MSWinRTDisDeferral^>();
}

MSWinRTDisSampleHandler::~MSWinRTDisSampleHandler()
{
}

void MSWinRTDisSampleHandler::StartMediaElement()
{
	if (mMediaElement != nullptr) {
		VideoEncodingProperties^ videoEncodingProperties;
		videoEncodingProperties = VideoEncodingProperties::CreateUncompressed(MediaEncodingSubtypes::Nv12, this->Width, this->Height);
		VideoStreamDescriptor^ videoStreamDescriptor = ref new VideoStreamDescriptor(videoEncodingProperties);
		MediaStreamSource^ mediaStreamSource = ref new MediaStreamSource(videoStreamDescriptor);
		mediaStreamSource->SampleRequested += ref new Windows::Foundation::TypedEventHandler<Windows::Media::Core::MediaStreamSource ^, Windows::Media::Core::MediaStreamSourceSampleRequestedEventArgs ^>(this, &MSWinRTDisSampleHandler::OnSampleRequested);
		Windows::UI::Xaml::Controls::MediaElement^ mediaElement = mMediaElement;
		bool inUIThread = mediaElement->Dispatcher->HasThreadAccess;
		mReferenceTime = 0;
		if (inUIThread) {
			// We are in the UI thread
			_startMediaElement(mediaElement, mediaStreamSource);
			mStarted = true;
		}
		else {
			// Ask the dispatcher to run this code in the UI thread
			mediaElement->Dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, ref new Windows::UI::Core::DispatchedHandler([mediaElement, mediaStreamSource, this]() {
				_startMediaElement(mediaElement, mediaStreamSource);
				mStarted = true;
			}));
		}
	}
}

void MSWinRTDisSampleHandler::StopMediaElement()
{
	if (mMediaElement != nullptr) {
		Windows::UI::Xaml::Controls::MediaElement^ mediaElement = mMediaElement;
		if (mediaElement->Dispatcher->HasThreadAccess) {
			// We are in the UI thread
			_stopMediaElement(mediaElement, this);
			mDeferralQueue->Clear();
			mStarted = false;
		}
		else {
			// Ask the dispatcher to run this code in the UI thread
			mediaElement->Dispatcher->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, ref new Windows::UI::Core::DispatchedHandler([mediaElement, this]() {
				_stopMediaElement(mediaElement, this);
				mDeferralQueue->Clear();
				mStarted = false;
			}));
		}
	}
	else {
		mDeferralQueue->Clear();
		mStarted = false;
	}
}

void MSWinRTDisSampleHandler::Feed(Windows::Storage::Streams::IBuffer^ pBuffer)
{
	mMutex.lock();
	if (!mStarted) {
		StartMediaElement();
	}
	mSample = pBuffer;
	if (mDeferralQueue->Size > 0) {
#ifdef MSWINRTDIS_DEBUG
		ms_message("[MSWinRTDis] Feed answer deferral");
#endif
		MSWinRTDisDeferral^ deferral = mDeferralQueue->GetAt(0);
		mDeferralQueue->RemoveAt(0);
		AnswerSampleRequest(deferral->Request);
		deferral->Deferral->Complete();
	}
#ifdef MSWINRTDIS_DEBUG
	else {
		ms_message("[MSWinRTDis] Feed");
	}
#endif
	mMutex.unlock();
}

void MSWinRTDisSampleHandler::OnSampleRequested(Windows::Media::Core::MediaStreamSource^ sender, Windows::Media::Core::MediaStreamSourceSampleRequestedEventArgs^ args)
{
	MediaStreamSourceSampleRequest^ request = args->Request;
	VideoStreamDescriptor^ videoStreamDescriptor = dynamic_cast<VideoStreamDescriptor^>(request->StreamDescriptor);
	if (videoStreamDescriptor == nullptr) {
		ms_warning("[MSWinRTDis] OnSampleRequested not for a video stream!");
		return;
	}
	mMutex.lock();
	if (mSample == nullptr) {
#ifdef MSWINRTDIS_DEBUG
		ms_message("[MSWinRTDis] OnSampleRequested defer");
#endif
		mDeferralQueue->Append(ref new MSWinRTDisDeferral(request, request->GetDeferral()));
	} else {
#ifdef MSWINRTDIS_DEBUG
		ms_message("[MSWinRTDis] OnSampleRequested answer");
#endif
		AnswerSampleRequest(request);
	}
	mMutex.unlock();
}

void MSWinRTDisSampleHandler::AnswerSampleRequest(Windows::Media::Core::MediaStreamSourceSampleRequest^ sampleRequest)
{
	TimeSpan ts;
	UINT64 CurrentTime = GetTickCount64() * 10000LL;
	if (mReferenceTime == 0) {
		mReferenceTime = CurrentTime;
	}
	ts.Duration = CurrentTime - mReferenceTime;
	sampleRequest->Sample = MediaStreamSample::CreateFromBuffer(mSample, ts);
	mSample = nullptr;
}

void MSWinRTDisSampleHandler::RequestMediaElementRestart()
{
	mMutex.lock();
	ms_message("[MSWinRTDis] RequestMediaElementRestart");
	if (mReferenceTime != 0) {
		StopMediaElement();
		mReferenceTime = 0;
	}
	mMutex.unlock();
}

Ghislain MARY's avatar
Ghislain MARY committed
183 184


185
MSWinRTDis::MSWinRTDis()
186
	: mIsInitialized(false), mIsActivated(false), mIsStarted(false), mSampleHandler(nullptr)
187
{
188 189
	mSampleHandler = ref new MSWinRTDisSampleHandler();
	mIsInitialized = true;
190 191
}

192
MSWinRTDis::~MSWinRTDis()
193 194 195 196
{
	stop();
}

197
int MSWinRTDis::activate()
198
{
199
	if (!mIsInitialized) return -1;
200 201 202 203
	mIsActivated = true;
	return 0;
}

204
int MSWinRTDis::deactivate()
205 206 207 208 209
{
	mIsActivated = false;
	return 0;
}

210
void MSWinRTDis::start()
211
{
212
	if (!mIsStarted && mIsActivated) {
213
		mIsStarted = true;
214 215 216
	}
}

217
void MSWinRTDis::stop()
218
{
219 220
	if (mIsStarted) {
		mIsStarted = false;
221
		mSampleHandler->StopMediaElement();
Ghislain MARY's avatar
Ghislain MARY committed
222 223 224 225 226
	}
}

int MSWinRTDis::feed(MSFilter *f)
{
227
	if (mIsStarted) {
Ghislain MARY's avatar
Ghislain MARY committed
228
		mblk_t *im;
229
		mblk_t *om;
Ghislain MARY's avatar
Ghislain MARY committed
230

231
		if ((f->inputs[0] != NULL) && ((im = ms_queue_peek_last(f->inputs[0])) != NULL)) {
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
			int size = 0;
			MSPicture inbuf;
			MSPicture outbuf;
			if (ms_yuv_buf_init_from_mblk(&inbuf, im) == 0) {
				if ((inbuf.w != mSampleHandler->Width) || (inbuf.h != mSampleHandler->Height)) {
					mSampleHandler->Width = inbuf.w;
					mSampleHandler->Height = inbuf.h;
					mSampleHandler->RequestMediaElementRestart();
				}
				om = ms_yuv_buf_alloc(&outbuf, inbuf.w, inbuf.h);
				int ysize = inbuf.w * inbuf.h;
				int usize = ysize / 4;
				uint8_t *buffer = outbuf.planes[0];
				memcpy(buffer, inbuf.planes[0], ysize);
				for (int i = 0; i < usize; i++) {
					buffer[ysize + (i * 2)] = inbuf.planes[1][i];
					buffer[ysize + (i * 2) + 1] = inbuf.planes[2][i];
				}
250
				Microsoft::WRL::ComPtr<VideoBuffer> spVideoBuffer = NULL;
251 252
				Microsoft::WRL::MakeAndInitialize<VideoBuffer>(&spVideoBuffer, buffer, (int)msgdsize(om), om);
				mSampleHandler->Feed(VideoBuffer::GetIBuffer(spVideoBuffer));
253
			}
Ghislain MARY's avatar
Ghislain MARY committed
254
		}
255 256
	}

257 258 259
	if (f->inputs[0] != NULL) {
		ms_queue_flush(f->inputs[0]);
	}
260 261
	if (f->inputs[1] != NULL) {
		ms_queue_flush(f->inputs[1]);
262 263 264 265 266
	}

	return 0;
}

267
MSVideoSize MSWinRTDis::getVideoSize()
268 269
{
	MSVideoSize vs;
270 271
	vs.width = mSampleHandler->Width;
	vs.height = mSampleHandler->Height;
272 273 274
	return vs;
}

275
void MSWinRTDis::setVideoSize(MSVideoSize vs)
276
{
277 278
	mSampleHandler->Width = vs.width;
	mSampleHandler->Height = vs.height;
279
}