SurfaceMediaSource_test.cpp revision f2e55f1f42180f61f2bac90cc60f23f3509ce95d
1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17//#define LOG_NDEBUG 0 18#define LOG_TAG "SurfaceMediaSource_test" 19 20#include <gtest/gtest.h> 21#include <utils/String8.h> 22#include <utils/Errors.h> 23#include <fcntl.h> 24#include <unistd.h> 25 26#include <GLES2/gl2.h> 27 28#include <media/stagefright/SurfaceMediaSource.h> 29#include <media/mediarecorder.h> 30 31#include <ui/GraphicBuffer.h> 32#include <gui/Surface.h> 33#include <gui/ISurfaceComposer.h> 34#include <gui/Surface.h> 35#include <gui/SurfaceComposerClient.h> 36 37#include <binder/ProcessState.h> 38 39#include <media/stagefright/foundation/ADebug.h> 40#include <media/stagefright/MediaBufferGroup.h> 41#include <media/stagefright/MediaDefs.h> 42#include <media/stagefright/MetaData.h> 43#include <media/stagefright/OMXClient.h> 44#include <media/stagefright/OMXCodec.h> 45#include <OMX_Component.h> 46 47#include "DummyRecorder.h" 48 49 50namespace android { 51 52class GLTest : public ::testing::Test { 53protected: 54 55 GLTest(): 56 mEglDisplay(EGL_NO_DISPLAY), 57 mEglSurface(EGL_NO_SURFACE), 58 mEglContext(EGL_NO_CONTEXT) { 59 } 60 61 virtual void SetUp() { 62 ALOGV("GLTest::SetUp()"); 63 mEglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); 64 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 65 ASSERT_NE(EGL_NO_DISPLAY, mEglDisplay); 66 67 EGLint majorVersion; 68 EGLint minorVersion; 69 EXPECT_TRUE(eglInitialize(mEglDisplay, &majorVersion, &minorVersion)); 70 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 71 RecordProperty("EglVersionMajor", majorVersion); 72 RecordProperty("EglVersionMajor", minorVersion); 73 74 EGLint numConfigs = 0; 75 EXPECT_TRUE(eglChooseConfig(mEglDisplay, getConfigAttribs(), &mGlConfig, 76 1, &numConfigs)); 77 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 78 79 char* displaySecsEnv = getenv("GLTEST_DISPLAY_SECS"); 80 if (displaySecsEnv != NULL) { 81 mDisplaySecs = atoi(displaySecsEnv); 82 if (mDisplaySecs < 0) { 83 mDisplaySecs = 0; 84 } 85 } else { 86 mDisplaySecs = 0; 87 } 88 89 if (mDisplaySecs > 0) { 90 mComposerClient = new SurfaceComposerClient; 91 ASSERT_EQ(NO_ERROR, mComposerClient->initCheck()); 92 93 mSurfaceControl = mComposerClient->createSurface( 94 String8("Test Surface"), 95 getSurfaceWidth(), getSurfaceHeight(), 96 PIXEL_FORMAT_RGB_888, 0); 97 98 ASSERT_TRUE(mSurfaceControl != NULL); 99 ASSERT_TRUE(mSurfaceControl->isValid()); 100 101 SurfaceComposerClient::openGlobalTransaction(); 102 ASSERT_EQ(NO_ERROR, mSurfaceControl->setLayer(0x7FFFFFFF)); 103 ASSERT_EQ(NO_ERROR, mSurfaceControl->show()); 104 SurfaceComposerClient::closeGlobalTransaction(); 105 106 sp<ANativeWindow> window = mSurfaceControl->getSurface(); 107 mEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig, 108 window.get(), NULL); 109 } else { 110 ALOGV("No actual display. Choosing EGLSurface based on SurfaceMediaSource"); 111 sp<IGraphicBufferProducer> sms = (new SurfaceMediaSource( 112 getSurfaceWidth(), getSurfaceHeight()))->getBufferQueue(); 113 sp<Surface> stc = new Surface(sms); 114 sp<ANativeWindow> window = stc; 115 116 mEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig, 117 window.get(), NULL); 118 } 119 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 120 ASSERT_NE(EGL_NO_SURFACE, mEglSurface); 121 122 mEglContext = eglCreateContext(mEglDisplay, mGlConfig, EGL_NO_CONTEXT, 123 getContextAttribs()); 124 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 125 ASSERT_NE(EGL_NO_CONTEXT, mEglContext); 126 127 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, 128 mEglContext)); 129 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 130 131 EGLint w, h; 132 EXPECT_TRUE(eglQuerySurface(mEglDisplay, mEglSurface, EGL_WIDTH, &w)); 133 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 134 EXPECT_TRUE(eglQuerySurface(mEglDisplay, mEglSurface, EGL_HEIGHT, &h)); 135 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 136 RecordProperty("EglSurfaceWidth", w); 137 RecordProperty("EglSurfaceHeight", h); 138 139 glViewport(0, 0, w, h); 140 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 141 } 142 143 virtual void TearDown() { 144 // Display the result 145 if (mDisplaySecs > 0 && mEglSurface != EGL_NO_SURFACE) { 146 eglSwapBuffers(mEglDisplay, mEglSurface); 147 sleep(mDisplaySecs); 148 } 149 150 if (mComposerClient != NULL) { 151 mComposerClient->dispose(); 152 } 153 if (mEglContext != EGL_NO_CONTEXT) { 154 eglDestroyContext(mEglDisplay, mEglContext); 155 } 156 if (mEglSurface != EGL_NO_SURFACE) { 157 eglDestroySurface(mEglDisplay, mEglSurface); 158 } 159 if (mEglDisplay != EGL_NO_DISPLAY) { 160 eglTerminate(mEglDisplay); 161 } 162 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 163 } 164 165 virtual EGLint const* getConfigAttribs() { 166 ALOGV("GLTest getConfigAttribs"); 167 static EGLint sDefaultConfigAttribs[] = { 168 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT, 169 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, 170 EGL_RED_SIZE, 8, 171 EGL_GREEN_SIZE, 8, 172 EGL_BLUE_SIZE, 8, 173 EGL_ALPHA_SIZE, 8, 174 EGL_DEPTH_SIZE, 16, 175 EGL_STENCIL_SIZE, 8, 176 EGL_NONE }; 177 178 return sDefaultConfigAttribs; 179 } 180 181 virtual EGLint const* getContextAttribs() { 182 static EGLint sDefaultContextAttribs[] = { 183 EGL_CONTEXT_CLIENT_VERSION, 2, 184 EGL_NONE }; 185 186 return sDefaultContextAttribs; 187 } 188 189 virtual EGLint getSurfaceWidth() { 190 return 512; 191 } 192 193 virtual EGLint getSurfaceHeight() { 194 return 512; 195 } 196 197 void loadShader(GLenum shaderType, const char* pSource, GLuint* outShader) { 198 GLuint shader = glCreateShader(shaderType); 199 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 200 if (shader) { 201 glShaderSource(shader, 1, &pSource, NULL); 202 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 203 glCompileShader(shader); 204 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 205 GLint compiled = 0; 206 glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); 207 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 208 if (!compiled) { 209 GLint infoLen = 0; 210 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen); 211 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 212 if (infoLen) { 213 char* buf = (char*) malloc(infoLen); 214 if (buf) { 215 glGetShaderInfoLog(shader, infoLen, NULL, buf); 216 printf("Shader compile log:\n%s\n", buf); 217 free(buf); 218 FAIL(); 219 } 220 } else { 221 char* buf = (char*) malloc(0x1000); 222 if (buf) { 223 glGetShaderInfoLog(shader, 0x1000, NULL, buf); 224 printf("Shader compile log:\n%s\n", buf); 225 free(buf); 226 FAIL(); 227 } 228 } 229 glDeleteShader(shader); 230 shader = 0; 231 } 232 } 233 ASSERT_TRUE(shader != 0); 234 *outShader = shader; 235 } 236 237 void createProgram(const char* pVertexSource, const char* pFragmentSource, 238 GLuint* outPgm) { 239 GLuint vertexShader, fragmentShader; 240 { 241 SCOPED_TRACE("compiling vertex shader"); 242 loadShader(GL_VERTEX_SHADER, pVertexSource, &vertexShader); 243 if (HasFatalFailure()) { 244 return; 245 } 246 } 247 { 248 SCOPED_TRACE("compiling fragment shader"); 249 loadShader(GL_FRAGMENT_SHADER, pFragmentSource, &fragmentShader); 250 if (HasFatalFailure()) { 251 return; 252 } 253 } 254 255 GLuint program = glCreateProgram(); 256 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 257 if (program) { 258 glAttachShader(program, vertexShader); 259 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 260 glAttachShader(program, fragmentShader); 261 ASSERT_EQ(GLenum(GL_NO_ERROR), glGetError()); 262 glLinkProgram(program); 263 GLint linkStatus = GL_FALSE; 264 glGetProgramiv(program, GL_LINK_STATUS, &linkStatus); 265 if (linkStatus != GL_TRUE) { 266 GLint bufLength = 0; 267 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength); 268 if (bufLength) { 269 char* buf = (char*) malloc(bufLength); 270 if (buf) { 271 glGetProgramInfoLog(program, bufLength, NULL, buf); 272 printf("Program link log:\n%s\n", buf); 273 free(buf); 274 FAIL(); 275 } 276 } 277 glDeleteProgram(program); 278 program = 0; 279 } 280 } 281 glDeleteShader(vertexShader); 282 glDeleteShader(fragmentShader); 283 ASSERT_TRUE(program != 0); 284 *outPgm = program; 285 } 286 287 static int abs(int value) { 288 return value > 0 ? value : -value; 289 } 290 291 ::testing::AssertionResult checkPixel(int x, int y, int r, 292 int g, int b, int a, int tolerance=2) { 293 GLubyte pixel[4]; 294 String8 msg; 295 glReadPixels(x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixel); 296 GLenum err = glGetError(); 297 if (err != GL_NO_ERROR) { 298 msg += String8::format("error reading pixel: %#x", err); 299 while ((err = glGetError()) != GL_NO_ERROR) { 300 msg += String8::format(", %#x", err); 301 } 302 fprintf(stderr, "pixel check failure: %s\n", msg.string()); 303 return ::testing::AssertionFailure( 304 ::testing::Message(msg.string())); 305 } 306 if (r >= 0 && abs(r - int(pixel[0])) > tolerance) { 307 msg += String8::format("r(%d isn't %d)", pixel[0], r); 308 } 309 if (g >= 0 && abs(g - int(pixel[1])) > tolerance) { 310 if (!msg.isEmpty()) { 311 msg += " "; 312 } 313 msg += String8::format("g(%d isn't %d)", pixel[1], g); 314 } 315 if (b >= 0 && abs(b - int(pixel[2])) > tolerance) { 316 if (!msg.isEmpty()) { 317 msg += " "; 318 } 319 msg += String8::format("b(%d isn't %d)", pixel[2], b); 320 } 321 if (a >= 0 && abs(a - int(pixel[3])) > tolerance) { 322 if (!msg.isEmpty()) { 323 msg += " "; 324 } 325 msg += String8::format("a(%d isn't %d)", pixel[3], a); 326 } 327 if (!msg.isEmpty()) { 328 fprintf(stderr, "pixel check failure: %s\n", msg.string()); 329 return ::testing::AssertionFailure( 330 ::testing::Message(msg.string())); 331 } else { 332 return ::testing::AssertionSuccess(); 333 } 334 } 335 336 int mDisplaySecs; 337 sp<SurfaceComposerClient> mComposerClient; 338 sp<SurfaceControl> mSurfaceControl; 339 340 EGLDisplay mEglDisplay; 341 EGLSurface mEglSurface; 342 EGLContext mEglContext; 343 EGLConfig mGlConfig; 344}; 345 346/////////////////////////////////////////////////////////////////////// 347// Class for the NON-GL tests 348/////////////////////////////////////////////////////////////////////// 349class SurfaceMediaSourceTest : public ::testing::Test { 350public: 351 352 SurfaceMediaSourceTest( ): mYuvTexWidth(176), mYuvTexHeight(144) { } 353 void oneBufferPass(int width, int height ); 354 void oneBufferPassNoFill(int width, int height ); 355 static void fillYV12Buffer(uint8_t* buf, int w, int h, int stride) ; 356 static void fillYV12BufferRect(uint8_t* buf, int w, int h, 357 int stride, const android_native_rect_t& rect) ; 358protected: 359 360 virtual void SetUp() { 361 android::ProcessState::self()->startThreadPool(); 362 mSMS = new SurfaceMediaSource(mYuvTexWidth, mYuvTexHeight); 363 364 // Manual cast is required to avoid constructor ambiguity 365 mSTC = new Surface(static_cast<sp<IGraphicBufferProducer> >( mSMS->getBufferQueue())); 366 mANW = mSTC; 367 } 368 369 virtual void TearDown() { 370 mSMS.clear(); 371 mSTC.clear(); 372 mANW.clear(); 373 } 374 375 const int mYuvTexWidth; 376 const int mYuvTexHeight; 377 378 sp<SurfaceMediaSource> mSMS; 379 sp<Surface> mSTC; 380 sp<ANativeWindow> mANW; 381}; 382 383/////////////////////////////////////////////////////////////////////// 384// Class for the GL tests 385/////////////////////////////////////////////////////////////////////// 386class SurfaceMediaSourceGLTest : public GLTest { 387public: 388 389 SurfaceMediaSourceGLTest( ): mYuvTexWidth(176), mYuvTexHeight(144) { } 390 virtual EGLint const* getConfigAttribs(); 391 void oneBufferPassGL(int num = 0); 392 static sp<MediaRecorder> setUpMediaRecorder(int fileDescriptor, int videoSource, 393 int outputFormat, int videoEncoder, int width, int height, int fps); 394protected: 395 396 virtual void SetUp() { 397 ALOGV("SMS-GLTest::SetUp()"); 398 android::ProcessState::self()->startThreadPool(); 399 mSMS = new SurfaceMediaSource(mYuvTexWidth, mYuvTexHeight); 400 mSTC = new Surface(static_cast<sp<IGraphicBufferProducer> >( mSMS->getBufferQueue())); 401 mANW = mSTC; 402 403 // Doing the setup related to the GL Side 404 GLTest::SetUp(); 405 } 406 407 virtual void TearDown() { 408 mSMS.clear(); 409 mSTC.clear(); 410 mANW.clear(); 411 GLTest::TearDown(); 412 } 413 414 void setUpEGLSurfaceFromMediaRecorder(sp<MediaRecorder>& mr); 415 416 const int mYuvTexWidth; 417 const int mYuvTexHeight; 418 419 sp<SurfaceMediaSource> mSMS; 420 sp<Surface> mSTC; 421 sp<ANativeWindow> mANW; 422}; 423 424///////////////////////////////////////////////////////////////////// 425// Methods in SurfaceMediaSourceGLTest 426///////////////////////////////////////////////////////////////////// 427EGLint const* SurfaceMediaSourceGLTest::getConfigAttribs() { 428 ALOGV("SurfaceMediaSourceGLTest getConfigAttribs"); 429 static EGLint sDefaultConfigAttribs[] = { 430 EGL_SURFACE_TYPE, EGL_WINDOW_BIT, 431 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, 432 EGL_RED_SIZE, 8, 433 EGL_GREEN_SIZE, 8, 434 EGL_BLUE_SIZE, 8, 435 EGL_RECORDABLE_ANDROID, EGL_TRUE, 436 EGL_NONE }; 437 438 return sDefaultConfigAttribs; 439} 440 441// One pass of dequeuing and queuing a GLBuffer 442void SurfaceMediaSourceGLTest::oneBufferPassGL(int num) { 443 int d = num % 50; 444 float f = 0.2f; // 0.1f * d; 445 446 glClearColor(0, 0.3, 0, 0.6); 447 glClear(GL_COLOR_BUFFER_BIT); 448 449 glEnable(GL_SCISSOR_TEST); 450 glScissor(4 + d, 4 + d, 4, 4); 451 glClearColor(1.0 - f, f, f, 1.0); 452 glClear(GL_COLOR_BUFFER_BIT); 453 454 glScissor(24 + d, 48 + d, 4, 4); 455 glClearColor(f, 1.0 - f, f, 1.0); 456 glClear(GL_COLOR_BUFFER_BIT); 457 458 glScissor(37 + d, 17 + d, 4, 4); 459 glClearColor(f, f, 1.0 - f, 1.0); 460 glClear(GL_COLOR_BUFFER_BIT); 461 462 // The following call dequeues and queues the buffer 463 eglSwapBuffers(mEglDisplay, mEglSurface); 464 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 465 glDisable(GL_SCISSOR_TEST); 466} 467 468// Set up the MediaRecorder which runs in the same process as mediaserver 469sp<MediaRecorder> SurfaceMediaSourceGLTest::setUpMediaRecorder(int fd, int videoSource, 470 int outputFormat, int videoEncoder, int width, int height, int fps) { 471 sp<MediaRecorder> mr = new MediaRecorder(); 472 mr->setVideoSource(videoSource); 473 mr->setOutputFormat(outputFormat); 474 mr->setVideoEncoder(videoEncoder); 475 mr->setOutputFile(fd, 0, 0); 476 mr->setVideoSize(width, height); 477 mr->setVideoFrameRate(fps); 478 mr->prepare(); 479 ALOGV("Starting MediaRecorder..."); 480 CHECK_EQ((status_t)OK, mr->start()); 481 return mr; 482} 483 484// query the mediarecorder for a surfacemeidasource and create an egl surface with that 485void SurfaceMediaSourceGLTest::setUpEGLSurfaceFromMediaRecorder(sp<MediaRecorder>& mr) { 486 sp<IGraphicBufferProducer> iST = mr->querySurfaceMediaSourceFromMediaServer(); 487 mSTC = new Surface(iST); 488 mANW = mSTC; 489 490 if (mEglSurface != EGL_NO_SURFACE) { 491 EXPECT_TRUE(eglDestroySurface(mEglDisplay, mEglSurface)); 492 mEglSurface = EGL_NO_SURFACE; 493 } 494 mEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig, 495 mANW.get(), NULL); 496 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 497 ASSERT_NE(EGL_NO_SURFACE, mEglSurface) ; 498 499 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, 500 mEglContext)); 501 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 502} 503 504 505///////////////////////////////////////////////////////////////////// 506// Methods in SurfaceMediaSourceTest 507///////////////////////////////////////////////////////////////////// 508 509// One pass of dequeuing and queuing the buffer. Fill it in with 510// cpu YV12 buffer 511void SurfaceMediaSourceTest::oneBufferPass(int width, int height ) { 512 ANativeWindowBuffer* anb; 513 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(), &anb)); 514 ASSERT_TRUE(anb != NULL); 515 516 517 // Fill the buffer with the a checkerboard pattern 518 uint8_t* img = NULL; 519 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false)); 520 buf->lock(GRALLOC_USAGE_SW_WRITE_OFTEN, (void**)(&img)); 521 SurfaceMediaSourceTest::fillYV12Buffer(img, width, height, buf->getStride()); 522 buf->unlock(); 523 524 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer(), 525 -1)); 526} 527 528// Dequeuing and queuing the buffer without really filling it in. 529void SurfaceMediaSourceTest::oneBufferPassNoFill( 530 int /* width */, int /* height */) { 531 ANativeWindowBuffer* anb; 532 ASSERT_EQ(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(), &anb)); 533 ASSERT_TRUE(anb != NULL); 534 535 // We do not fill the buffer in. Just queue it back. 536 sp<GraphicBuffer> buf(new GraphicBuffer(anb, false)); 537 ASSERT_EQ(NO_ERROR, mANW->queueBuffer(mANW.get(), buf->getNativeBuffer(), 538 -1)); 539} 540 541// Fill a YV12 buffer with a multi-colored checkerboard pattern 542void SurfaceMediaSourceTest::fillYV12Buffer(uint8_t* buf, int w, int h, int stride) { 543 const int blockWidth = w > 16 ? w / 16 : 1; 544 const int blockHeight = h > 16 ? h / 16 : 1; 545 const int yuvTexOffsetY = 0; 546 int yuvTexStrideY = stride; 547 int yuvTexOffsetV = yuvTexStrideY * h; 548 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf; 549 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * h/2; 550 int yuvTexStrideU = yuvTexStrideV; 551 for (int x = 0; x < w; x++) { 552 for (int y = 0; y < h; y++) { 553 int parityX = (x / blockWidth) & 1; 554 int parityY = (y / blockHeight) & 1; 555 unsigned char intensity = (parityX ^ parityY) ? 63 : 191; 556 buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = intensity; 557 if (x < w / 2 && y < h / 2) { 558 buf[yuvTexOffsetU + (y * yuvTexStrideU) + x] = intensity; 559 if (x * 2 < w / 2 && y * 2 < h / 2) { 560 buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 0] = 561 buf[yuvTexOffsetV + (y*2 * yuvTexStrideV) + x*2 + 1] = 562 buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 0] = 563 buf[yuvTexOffsetV + ((y*2+1) * yuvTexStrideV) + x*2 + 1] = 564 intensity; 565 } 566 } 567 } 568 } 569} 570 571// Fill a YV12 buffer with red outside a given rectangle and green inside it. 572void SurfaceMediaSourceTest::fillYV12BufferRect(uint8_t* buf, int w, 573 int h, int stride, const android_native_rect_t& rect) { 574 const int yuvTexOffsetY = 0; 575 int yuvTexStrideY = stride; 576 int yuvTexOffsetV = yuvTexStrideY * h; 577 int yuvTexStrideV = (yuvTexStrideY/2 + 0xf) & ~0xf; 578 int yuvTexOffsetU = yuvTexOffsetV + yuvTexStrideV * h/2; 579 int yuvTexStrideU = yuvTexStrideV; 580 for (int x = 0; x < w; x++) { 581 for (int y = 0; y < h; y++) { 582 bool inside = rect.left <= x && x < rect.right && 583 rect.top <= y && y < rect.bottom; 584 buf[yuvTexOffsetY + (y * yuvTexStrideY) + x] = inside ? 240 : 64; 585 if (x < w / 2 && y < h / 2) { 586 bool inside = rect.left <= 2*x && 2*x < rect.right && 587 rect.top <= 2*y && 2*y < rect.bottom; 588 buf[yuvTexOffsetU + (y * yuvTexStrideU) + x] = 16; 589 buf[yuvTexOffsetV + (y * yuvTexStrideV) + x] = 590 inside ? 16 : 255; 591 } 592 } 593 } 594} ///////// End of class SurfaceMediaSourceTest 595 596/////////////////////////////////////////////////////////////////// 597// Class to imitate the recording ///////////////////////////// 598// //////////////////////////////////////////////////////////////// 599struct SimpleDummyRecorder { 600 sp<MediaSource> mSource; 601 602 SimpleDummyRecorder 603 (const sp<MediaSource> &source): mSource(source) {} 604 605 status_t start() { return mSource->start();} 606 status_t stop() { return mSource->stop();} 607 608 // fakes reading from a media source 609 status_t readFromSource() { 610 MediaBuffer *buffer; 611 status_t err = mSource->read(&buffer); 612 if (err != OK) { 613 return err; 614 } 615 buffer->release(); 616 buffer = NULL; 617 return OK; 618 } 619}; 620/////////////////////////////////////////////////////////////////// 621// TESTS 622// SurfaceMediaSourceTest class contains tests that fill the buffers 623// using the cpu calls 624// SurfaceMediaSourceGLTest class contains tests that fill the buffers 625// using the GL calls. 626// TODO: None of the tests actually verify the encoded images.. so at this point, 627// these are mostly functionality tests + visual inspection 628////////////////////////////////////////////////////////////////////// 629 630// Just pass one buffer from the native_window to the SurfaceMediaSource 631// Dummy Encoder 632static int testId = 1; 633TEST_F(SurfaceMediaSourceTest, DISABLED_DummyEncodingFromCpuFilledYV12BufferNpotOneBufferPass) { 634 ALOGV("Test # %d", testId++); 635 ALOGV("Testing OneBufferPass ******************************"); 636 637 ASSERT_EQ(NO_ERROR, native_window_set_buffers_format(mANW.get(), 638 HAL_PIXEL_FORMAT_YV12)); 639 oneBufferPass(mYuvTexWidth, mYuvTexHeight); 640} 641 642// Pass the buffer with the wrong height and weight and should not be accepted 643// Dummy Encoder 644TEST_F(SurfaceMediaSourceTest, DISABLED_DummyEncodingFromCpuFilledYV12BufferNpotWrongSizeBufferPass) { 645 ALOGV("Test # %d", testId++); 646 ALOGV("Testing Wrong size BufferPass ******************************"); 647 648 // setting the client side buffer size different than the server size 649 ASSERT_EQ(NO_ERROR, native_window_set_buffers_dimensions(mANW.get(), 650 10, 10)); 651 ASSERT_EQ(NO_ERROR, native_window_set_buffers_format(mANW.get(), 652 HAL_PIXEL_FORMAT_YV12)); 653 654 ANativeWindowBuffer* anb; 655 656 // Note: make sure we get an ERROR back when dequeuing! 657 ASSERT_NE(NO_ERROR, native_window_dequeue_buffer_and_wait(mANW.get(), &anb)); 658} 659 660// pass multiple buffers from the native_window the SurfaceMediaSource 661// Dummy Encoder 662TEST_F(SurfaceMediaSourceTest, DISABLED_DummyEncodingFromCpuFilledYV12BufferNpotMultiBufferPass) { 663 ALOGV("Test # %d", testId++); 664 ALOGV("Testing MultiBufferPass, Dummy Recorder *********************"); 665 ASSERT_EQ(NO_ERROR, native_window_set_buffers_format(mANW.get(), 666 HAL_PIXEL_FORMAT_YV12)); 667 668 SimpleDummyRecorder writer(mSMS); 669 writer.start(); 670 671 int32_t nFramesCount = 0; 672 while (nFramesCount < 300) { 673 oneBufferPass(mYuvTexWidth, mYuvTexHeight); 674 675 ASSERT_EQ(NO_ERROR, writer.readFromSource()); 676 677 nFramesCount++; 678 } 679 writer.stop(); 680} 681 682// Delayed pass of multiple buffers from the native_window the SurfaceMediaSource 683// Dummy Encoder 684TEST_F(SurfaceMediaSourceTest, DummyLagEncodingFromCpuFilledYV12BufferNpotMultiBufferPass) { 685 ALOGV("Test # %d", testId++); 686 ALOGV("Testing MultiBufferPass, Dummy Recorder Lagging **************"); 687 688 ASSERT_EQ(NO_ERROR, native_window_set_buffers_format(mANW.get(), 689 HAL_PIXEL_FORMAT_YV12)); 690 691 SimpleDummyRecorder writer(mSMS); 692 writer.start(); 693 694 int32_t nFramesCount = 1; 695 const int FRAMES_LAG = SurfaceMediaSource::MIN_UNDEQUEUED_BUFFERS; 696 697 while (nFramesCount <= 300) { 698 ALOGV("Frame: %d", nFramesCount); 699 oneBufferPass(mYuvTexWidth, mYuvTexHeight); 700 // Forcing the writer to lag behind a few frames 701 if (nFramesCount > FRAMES_LAG) { 702 ASSERT_EQ(NO_ERROR, writer.readFromSource()); 703 } 704 nFramesCount++; 705 } 706 writer.stop(); 707} 708 709// pass multiple buffers from the native_window the SurfaceMediaSource 710// A dummy writer (MULTITHREADED) is used to simulate actual MPEG4Writer 711TEST_F(SurfaceMediaSourceTest, DummyThreadedEncodingFromCpuFilledYV12BufferNpotMultiBufferPass) { 712 ALOGV("Test # %d", testId++); 713 ALOGV("Testing MultiBufferPass, Dummy Recorder Multi-Threaded **********"); 714 ASSERT_EQ(NO_ERROR, native_window_set_buffers_format(mANW.get(), 715 HAL_PIXEL_FORMAT_YV12)); 716 717 DummyRecorder writer(mSMS); 718 writer.start(); 719 720 int32_t nFramesCount = 0; 721 while (nFramesCount <= 300) { 722 ALOGV("Frame: %d", nFramesCount); 723 oneBufferPass(mYuvTexWidth, mYuvTexHeight); 724 725 nFramesCount++; 726 } 727 writer.stop(); 728} 729 730// Test to examine actual encoding using mediarecorder 731// We use the mediaserver to create a mediarecorder and send 732// it back to us. So SurfaceMediaSource lives in the same process 733// as the mediaserver. 734// Very close to the actual camera, except that the 735// buffers are filled and queueud by the CPU instead of GL. 736TEST_F(SurfaceMediaSourceTest, DISABLED_EncodingFromCpuYV12BufferNpotWriteMediaServer) { 737 ALOGV("Test # %d", testId++); 738 ALOGV("************** Testing the whole pipeline with actual MediaRecorder ***********"); 739 ALOGV("************** SurfaceMediaSource is same process as mediaserver ***********"); 740 741 const char *fileName = "/sdcard/outputSurfEncMSource.mp4"; 742 int fd = open(fileName, O_RDWR | O_CREAT, 0744); 743 if (fd < 0) { 744 ALOGE("ERROR: Could not open the the file %s, fd = %d !!", fileName, fd); 745 } 746 CHECK(fd >= 0); 747 748 sp<MediaRecorder> mr = SurfaceMediaSourceGLTest::setUpMediaRecorder(fd, 749 VIDEO_SOURCE_SURFACE, OUTPUT_FORMAT_MPEG_4, VIDEO_ENCODER_H264, 750 mYuvTexWidth, mYuvTexHeight, 30); 751 // get the reference to the surfacemediasource living in 752 // mediaserver that is created by stagefrightrecorder 753 sp<IGraphicBufferProducer> iST = mr->querySurfaceMediaSourceFromMediaServer(); 754 mSTC = new Surface(iST); 755 mANW = mSTC; 756 ASSERT_EQ(NO_ERROR, native_window_api_connect(mANW.get(), NATIVE_WINDOW_API_CPU)); 757 ASSERT_EQ(NO_ERROR, native_window_set_buffers_format(mANW.get(), 758 HAL_PIXEL_FORMAT_YV12)); 759 760 int32_t nFramesCount = 0; 761 while (nFramesCount <= 300) { 762 oneBufferPassNoFill(mYuvTexWidth, mYuvTexHeight); 763 nFramesCount++; 764 ALOGV("framesCount = %d", nFramesCount); 765 } 766 767 ASSERT_EQ(NO_ERROR, native_window_api_disconnect(mANW.get(), NATIVE_WINDOW_API_CPU)); 768 ALOGV("Stopping MediaRecorder..."); 769 CHECK_EQ((status_t)OK, mr->stop()); 770 mr.clear(); 771 close(fd); 772} 773 774////////////////////////////////////////////////////////////////////// 775// GL tests 776///////////////////////////////////////////////////////////////////// 777 778// Test to examine whether we can choose the Recordable Android GLConfig 779// DummyRecorder used- no real encoding here 780TEST_F(SurfaceMediaSourceGLTest, ChooseAndroidRecordableEGLConfigDummyWriter) { 781 ALOGV("Test # %d", testId++); 782 ALOGV("Verify creating a surface w/ right config + dummy writer*********"); 783 784 mSMS = new SurfaceMediaSource(mYuvTexWidth, mYuvTexHeight); 785 mSTC = new Surface(static_cast<sp<IGraphicBufferProducer> >( mSMS->getBufferQueue())); 786 mANW = mSTC; 787 788 DummyRecorder writer(mSMS); 789 writer.start(); 790 791 if (mEglSurface != EGL_NO_SURFACE) { 792 EXPECT_TRUE(eglDestroySurface(mEglDisplay, mEglSurface)); 793 mEglSurface = EGL_NO_SURFACE; 794 } 795 796 mEglSurface = eglCreateWindowSurface(mEglDisplay, mGlConfig, 797 mANW.get(), NULL); 798 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 799 ASSERT_NE(EGL_NO_SURFACE, mEglSurface) ; 800 801 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, 802 mEglContext)); 803 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 804 805 int32_t nFramesCount = 0; 806 while (nFramesCount <= 300) { 807 oneBufferPassGL(); 808 nFramesCount++; 809 ALOGV("framesCount = %d", nFramesCount); 810 } 811 812 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, 813 EGL_NO_CONTEXT)); 814 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 815 eglDestroySurface(mEglDisplay, mEglSurface); 816 mEglSurface = EGL_NO_SURFACE; 817 818 writer.stop(); 819} 820// Test to examine whether we can render GL buffers in to the surface 821// created with the native window handle 822TEST_F(SurfaceMediaSourceGLTest, RenderingToRecordableEGLSurfaceWorks) { 823 ALOGV("Test # %d", testId++); 824 ALOGV("RenderingToRecordableEGLSurfaceWorks *********************"); 825 // Do the producer side of things 826 glClearColor(0.6, 0.6, 0.6, 0.6); 827 glClear(GL_COLOR_BUFFER_BIT); 828 829 glEnable(GL_SCISSOR_TEST); 830 glScissor(4, 4, 4, 4); 831 glClearColor(1.0, 0.0, 0.0, 1.0); 832 glClear(GL_COLOR_BUFFER_BIT); 833 834 glScissor(24, 48, 4, 4); 835 glClearColor(0.0, 1.0, 0.0, 1.0); 836 glClear(GL_COLOR_BUFFER_BIT); 837 838 glScissor(37, 17, 4, 4); 839 glClearColor(0.0, 0.0, 1.0, 1.0); 840 glClear(GL_COLOR_BUFFER_BIT); 841 842 EXPECT_TRUE(checkPixel( 0, 0, 153, 153, 153, 153)); 843 EXPECT_TRUE(checkPixel(63, 0, 153, 153, 153, 153)); 844 EXPECT_TRUE(checkPixel(63, 63, 153, 153, 153, 153)); 845 EXPECT_TRUE(checkPixel( 0, 63, 153, 153, 153, 153)); 846 847 EXPECT_TRUE(checkPixel( 4, 7, 255, 0, 0, 255)); 848 EXPECT_TRUE(checkPixel(25, 51, 0, 255, 0, 255)); 849 EXPECT_TRUE(checkPixel(40, 19, 0, 0, 255, 255)); 850 EXPECT_TRUE(checkPixel(29, 51, 153, 153, 153, 153)); 851 EXPECT_TRUE(checkPixel( 5, 32, 153, 153, 153, 153)); 852 EXPECT_TRUE(checkPixel(13, 8, 153, 153, 153, 153)); 853 EXPECT_TRUE(checkPixel(46, 3, 153, 153, 153, 153)); 854 EXPECT_TRUE(checkPixel(30, 33, 153, 153, 153, 153)); 855 EXPECT_TRUE(checkPixel( 6, 52, 153, 153, 153, 153)); 856 EXPECT_TRUE(checkPixel(55, 33, 153, 153, 153, 153)); 857 EXPECT_TRUE(checkPixel(16, 29, 153, 153, 153, 153)); 858 EXPECT_TRUE(checkPixel( 1, 30, 153, 153, 153, 153)); 859 EXPECT_TRUE(checkPixel(41, 37, 153, 153, 153, 153)); 860 EXPECT_TRUE(checkPixel(46, 29, 153, 153, 153, 153)); 861 EXPECT_TRUE(checkPixel(15, 25, 153, 153, 153, 153)); 862 EXPECT_TRUE(checkPixel( 3, 52, 153, 153, 153, 153)); 863} 864 865// Test to examine the actual encoding with GL buffers 866// Actual encoder, Actual GL Buffers Filled SurfaceMediaSource 867// The same pattern is rendered every frame 868TEST_F(SurfaceMediaSourceGLTest, EncodingFromGLRgbaSameImageEachBufNpotWrite) { 869 ALOGV("Test # %d", testId++); 870 ALOGV("************** Testing the whole pipeline with actual Recorder ***********"); 871 ALOGV("************** GL Filling the buffers ***********"); 872 // Note: No need to set the colorformat for the buffers. The colorformat is 873 // in the GRAlloc buffers itself. 874 875 const char *fileName = "/sdcard/outputSurfEncMSourceGL.mp4"; 876 int fd = open(fileName, O_RDWR | O_CREAT, 0744); 877 if (fd < 0) { 878 ALOGE("ERROR: Could not open the the file %s, fd = %d !!", fileName, fd); 879 } 880 CHECK(fd >= 0); 881 882 sp<MediaRecorder> mr = setUpMediaRecorder(fd, VIDEO_SOURCE_SURFACE, 883 OUTPUT_FORMAT_MPEG_4, VIDEO_ENCODER_H264, mYuvTexWidth, mYuvTexHeight, 30); 884 885 // get the reference to the surfacemediasource living in 886 // mediaserver that is created by stagefrightrecorder 887 setUpEGLSurfaceFromMediaRecorder(mr); 888 889 int32_t nFramesCount = 0; 890 while (nFramesCount <= 300) { 891 oneBufferPassGL(); 892 nFramesCount++; 893 ALOGV("framesCount = %d", nFramesCount); 894 } 895 896 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, 897 EGL_NO_CONTEXT)); 898 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 899 eglDestroySurface(mEglDisplay, mEglSurface); 900 mEglSurface = EGL_NO_SURFACE; 901 902 ALOGV("Stopping MediaRecorder..."); 903 CHECK_EQ((status_t)OK, mr->stop()); 904 mr.clear(); 905 close(fd); 906} 907 908// Test to examine the actual encoding from the GL Buffers 909// Actual encoder, Actual GL Buffers Filled SurfaceMediaSource 910// A different pattern is rendered every frame 911TEST_F(SurfaceMediaSourceGLTest, EncodingFromGLRgbaDiffImageEachBufNpotWrite) { 912 ALOGV("Test # %d", testId++); 913 ALOGV("************** Testing the whole pipeline with actual Recorder ***********"); 914 ALOGV("************** Diff GL Filling the buffers ***********"); 915 // Note: No need to set the colorformat for the buffers. The colorformat is 916 // in the GRAlloc buffers itself. 917 918 const char *fileName = "/sdcard/outputSurfEncMSourceGLDiff.mp4"; 919 int fd = open(fileName, O_RDWR | O_CREAT, 0744); 920 if (fd < 0) { 921 ALOGE("ERROR: Could not open the the file %s, fd = %d !!", fileName, fd); 922 } 923 CHECK(fd >= 0); 924 925 sp<MediaRecorder> mr = setUpMediaRecorder(fd, VIDEO_SOURCE_SURFACE, 926 OUTPUT_FORMAT_MPEG_4, VIDEO_ENCODER_H264, mYuvTexWidth, mYuvTexHeight, 30); 927 928 // get the reference to the surfacemediasource living in 929 // mediaserver that is created by stagefrightrecorder 930 setUpEGLSurfaceFromMediaRecorder(mr); 931 932 int32_t nFramesCount = 0; 933 while (nFramesCount <= 300) { 934 oneBufferPassGL(nFramesCount); 935 nFramesCount++; 936 ALOGV("framesCount = %d", nFramesCount); 937 } 938 939 EXPECT_TRUE(eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, 940 EGL_NO_CONTEXT)); 941 ASSERT_EQ(EGL_SUCCESS, eglGetError()); 942 eglDestroySurface(mEglDisplay, mEglSurface); 943 mEglSurface = EGL_NO_SURFACE; 944 945 ALOGV("Stopping MediaRecorder..."); 946 CHECK_EQ((status_t)OK, mr->stop()); 947 mr.clear(); 948 close(fd); 949} 950} // namespace android 951