mswinrtdis.cpp 8.66 KB
Newer Older
1
/*
2
mswinrtdis.cpp
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

mediastreamer2 library - modular sound and video processing and streaming
Windows Audio Session API sound card plugin for mediastreamer2
Copyright (C) 2010-2013 Belledonne Communications, Grenoble, France

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
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/


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

Ghislain MARY's avatar
Ghislain MARY committed
27
using namespace libmswinrtvid;
28
using namespace Microsoft::WRL;
Ghislain MARY's avatar
Ghislain MARY committed
29 30 31
using namespace Windows::Foundation;
using namespace Windows::Media::Core;
using namespace Windows::Media::MediaProperties;
32

33 34


Ghislain MARY's avatar
Ghislain MARY committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
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();
}


50
bool MSWinRTDis::smInstantiated = false;
51 52


53 54
MSWinRTDisSampleHandler::MSWinRTDisSampleHandler() :
	mLastPresentationTime(0), mLastFilterTime(0), mPixFmt(MS_YUV420P), mWidth(MS_VIDEO_SIZE_CIF_W), mHeight(MS_VIDEO_SIZE_CIF_H)
Ghislain MARY's avatar
Ghislain MARY committed
55 56 57 58 59 60 61 62 63 64 65 66
{
	mSampleQueue = ref new Platform::Collections::Vector<MSWinRTDisSample^>();
}

MSWinRTDisSampleHandler::~MSWinRTDisSampleHandler()
{
}

void MSWinRTDisSampleHandler::StartMediaElement()
{
	if (mMediaElement != nullptr) {
		VideoEncodingProperties^ videoEncodingProperties;
67
		videoEncodingProperties = VideoEncodingProperties::CreateUncompressed(MediaEncodingSubtypes::Yv12, this->Width, this->Height);
Ghislain MARY's avatar
Ghislain MARY committed
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
		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;
		if (mediaElement->Dispatcher->HasThreadAccess) {
			// We are in the UI thread
			_startMediaElement(mediaElement, mediaStreamSource);
		}
		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]() {
				_startMediaElement(mediaElement, mediaStreamSource);
			}));
		}
	}
}

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);
		}
		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);
			}));
		}
	}
}

103
void MSWinRTDisSampleHandler::Feed(Windows::Storage::Streams::IBuffer^ pBuffer, UINT64 filterTime)
Ghislain MARY's avatar
Ghislain MARY committed
104
{
105
	MSWinRTDisSample^ sample = ref new MSWinRTDisSample(pBuffer, filterTime);
Ghislain MARY's avatar
Ghislain MARY committed
106 107 108 109 110 111 112 113
	mMutex.lock();
	mSampleQueue->Append(sample);

	if ((mSampleRequestDeferral != nullptr) && (mSampleRequest != nullptr)) {
		AnswerSampleRequest(mSampleRequest);
		mSampleRequestDeferral->Complete();
		mSampleRequest = nullptr;
		mSampleRequestDeferral = nullptr;
114 115
#ifdef _DEBUG
		ms_message("OnSampleReceived fill sample [queue: %d, lastFilterTime=%llu, lastPresentationTime=%llu]", mSampleQueue->Size, mLastFilterTime, mLastPresentationTime);
Ghislain MARY's avatar
Ghislain MARY committed
116 117
	} else {
		ms_message("OnSampleReceived queue sample [queue: %d]", mSampleQueue->Size);
118
#endif
Ghislain MARY's avatar
Ghislain MARY committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
	}
	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("OnSampleRequested not for a video stream!");
		return;
	}
	mMutex.lock();
	if (mSampleQueue->Size > 0) {
		AnswerSampleRequest(request);
134 135 136
#ifdef _DEBUG
		ms_message("OnSampleRequested fill sample [queue: %d, lastFilterTime=%llu, lastPresentationTime=%llu]", mSampleQueue->Size, mLastFilterTime, mLastPresentationTime);
#endif
Ghislain MARY's avatar
Ghislain MARY committed
137
	} else {
138
#ifdef _DEBUG
Ghislain MARY's avatar
Ghislain MARY committed
139
		ms_message("OnSampleRequested wait for sample [queue: %d]", mSampleQueue->Size);
140
#endif
Ghislain MARY's avatar
Ghislain MARY committed
141 142 143 144 145 146 147 148 149 150 151 152
		mSampleRequestDeferral = request->GetDeferral();
		mSampleRequest = request;
	}
	mMutex.unlock();
}

void MSWinRTDisSampleHandler::AnswerSampleRequest(Windows::Media::Core::MediaStreamSourceSampleRequest^ sampleRequest)
{
	MSWinRTDisSample^ sample = mSampleQueue->GetAt(0);
	mSampleQueue->RemoveAt(0);
	if (sample->Buffer != nullptr) {
		TimeSpan ts;
153 154 155 156 157
		if ((mLastFilterTime == 0) || (mSampleQueue->Size > 0)) {
			ts.Duration = mLastPresentationTime;
		} else {
			ts.Duration = mLastPresentationTime + ((sample->FilterTime - mLastFilterTime) * 10000LL);
		}
Ghislain MARY's avatar
Ghislain MARY committed
158 159
		MediaStreamSample^ streamSample = MediaStreamSample::CreateFromBuffer(sample->Buffer, ts);
		sampleRequest->Sample = streamSample;
160 161
		mLastFilterTime = sample->FilterTime;
		mLastPresentationTime = ts.Duration;
Ghislain MARY's avatar
Ghislain MARY committed
162 163 164
	} else {
		// This is a request to restart the media element
		StopMediaElement();
165
		mLastPresentationTime = mLastFilterTime = 0;
Ghislain MARY's avatar
Ghislain MARY committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179
		StartMediaElement();
	}
}

void MSWinRTDisSampleHandler::RequestMediaElementRestart()
{
	MSWinRTDisSample^ sample = ref new MSWinRTDisSample(nullptr, 0);
	mMutex.lock();
	mSampleQueue->Append(sample);
	mMutex.unlock();
}



180
MSWinRTDis::MSWinRTDis()
181
	: mIsInitialized(false), mIsActivated(false), mIsStarted(false), mBuffer(NULL), mSampleHandler(nullptr)
182 183
{
	if (smInstantiated) {
184
		ms_error("[MSWinRTDis] A video display filter is already instantiated. A second one can not be created.");
185 186 187
		return;
	}

Ghislain MARY's avatar
Ghislain MARY committed
188
	mSampleHandler = ref new MSWinRTDisSampleHandler();
189 190 191 192
	mIsInitialized = true;
	smInstantiated = true;
}

193
MSWinRTDis::~MSWinRTDis()
194 195 196 197 198
{
	stop();
	smInstantiated = false;
}

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

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

212
void MSWinRTDis::start()
213
{
214
	if (!mIsStarted && mIsActivated) {
215
		mIsStarted = true;
Ghislain MARY's avatar
Ghislain MARY committed
216
		mSampleHandler->StartMediaElement();
217 218 219
	}
}

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

int MSWinRTDis::feed(MSFilter *f)
{
230
	if (mIsStarted) {
Ghislain MARY's avatar
Ghislain MARY committed
231 232
		mblk_t *im;

233 234
		while ((im = ms_queue_get(f->inputs[0])) != NULL) {
			int size = 0;
235 236 237 238 239 240 241
			MSPicture buf;
			ms_yuv_buf_init_from_mblk(&buf, im);
			if ((buf.w != mSampleHandler->Width) || (buf.h != mSampleHandler->Height)) {
				mSampleHandler->Width = buf.w;
				mSampleHandler->Height = buf.h;
				mSampleHandler->RequestMediaElementRestart();
				if (mBuffer) ms_free(mBuffer);
242
			}
243 244 245 246 247 248 249 250 251
			size = (buf.w * buf.h * 3) / 2;
			if (!mBuffer) mBuffer = (uint8_t *)ms_malloc(size);
			int ysize = buf.w * buf.h;
			int usize = ysize / 4;
			memcpy(mBuffer, buf.planes[0], ysize);
			memcpy(mBuffer + ysize, buf.planes[2], usize);
			memcpy(mBuffer + ysize + usize, buf.planes[1], usize);
			freemsg(im);
			if (size > 0) {
Ghislain MARY's avatar
Ghislain MARY committed
252
				ComPtr<VideoBuffer> spVideoBuffer = NULL;
253
				MakeAndInitialize<VideoBuffer>(&spVideoBuffer, mBuffer, size);
254
				mSampleHandler->Feed(VideoBuffer::GetIBuffer(spVideoBuffer), f->ticker->time);
255
			}
Ghislain MARY's avatar
Ghislain MARY committed
256
		}
257 258 259 260 261 262 263 264
	} else {
		if (f->inputs[0] != NULL) {
			ms_queue_flush(f->inputs[0]);
		}
	}

	if (f->inputs[1] != NULL) {
		ms_queue_flush(f->inputs[1]);
265 266 267 268 269
	}

	return 0;
}

270
MSVideoSize MSWinRTDis::getVideoSize()
271 272
{
	MSVideoSize vs;
Ghislain MARY's avatar
Ghislain MARY committed
273 274
	vs.width = mSampleHandler->Width;
	vs.height = mSampleHandler->Height;
275 276 277
	return vs;
}

278
void MSWinRTDis::setVideoSize(MSVideoSize vs)
279
{
Ghislain MARY's avatar
Ghislain MARY committed
280 281
	mSampleHandler->Width = vs.width;
	mSampleHandler->Height = vs.height;
282
}