You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
3.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "test/mac/video_renderer_mac.h"
  11. #import <Cocoa/Cocoa.h>
  12. // Creates a Cocoa Window with an OpenGL context, used together with an OpenGL
  13. // renderer.
  14. @interface CocoaWindow : NSObject {
  15. @private
  16. NSWindow *window_;
  17. NSOpenGLContext *context_;
  18. NSString *title_;
  19. int width_;
  20. int height_;
  21. }
  22. - (id)initWithTitle:(NSString *)title width:(int)width height:(int)height;
  23. // 'createWindow' must be called on the main thread.
  24. - (void)createWindow:(NSObject *)ignored;
  25. - (void)makeCurrentContext;
  26. @end
  27. @implementation CocoaWindow
  28. static NSInteger nextXOrigin_;
  29. static NSInteger nextYOrigin_;
  30. - (id)initWithTitle:(NSString *)title width:(int)width height:(int)height {
  31. if (self = [super init]) {
  32. title_ = title;
  33. width_ = width;
  34. height_ = height;
  35. }
  36. return self;
  37. }
  38. - (void)createWindow:(NSObject *)ignored {
  39. NSInteger xOrigin = nextXOrigin_;
  40. NSRect screenFrame = [[NSScreen mainScreen] frame];
  41. if (nextXOrigin_ + width_ < screenFrame.size.width) {
  42. nextXOrigin_ += width_;
  43. } else {
  44. xOrigin = 0;
  45. nextXOrigin_ = 0;
  46. nextYOrigin_ += height_;
  47. }
  48. if (nextYOrigin_ + height_ > screenFrame.size.height) {
  49. xOrigin = 0;
  50. nextXOrigin_ = 0;
  51. nextYOrigin_ = 0;
  52. }
  53. NSInteger yOrigin = nextYOrigin_;
  54. NSRect windowFrame = NSMakeRect(xOrigin, yOrigin, width_, height_);
  55. window_ = [[NSWindow alloc] initWithContentRect:windowFrame
  56. styleMask:NSTitledWindowMask
  57. backing:NSBackingStoreBuffered
  58. defer:NO];
  59. NSRect viewFrame = NSMakeRect(0, 0, width_, height_);
  60. NSOpenGLView *view = [[NSOpenGLView alloc] initWithFrame:viewFrame pixelFormat:nil];
  61. context_ = [view openGLContext];
  62. [[window_ contentView] addSubview:view];
  63. [window_ setTitle:title_];
  64. [window_ makeKeyAndOrderFront:NSApp];
  65. }
  66. - (void)makeCurrentContext {
  67. [context_ makeCurrentContext];
  68. }
  69. @end
  70. namespace webrtc {
  71. namespace test {
  72. VideoRenderer* VideoRenderer::CreatePlatformRenderer(const char* window_title,
  73. size_t width,
  74. size_t height) {
  75. MacRenderer* renderer = new MacRenderer();
  76. if (!renderer->Init(window_title, width, height)) {
  77. delete renderer;
  78. return NULL;
  79. }
  80. return renderer;
  81. }
  82. MacRenderer::MacRenderer()
  83. : window_(NULL) {}
  84. MacRenderer::~MacRenderer() {
  85. GlRenderer::Destroy();
  86. }
  87. bool MacRenderer::Init(const char* window_title, int width, int height) {
  88. window_ = [[CocoaWindow alloc]
  89. initWithTitle:[NSString stringWithUTF8String:window_title]
  90. width:width
  91. height:height];
  92. if (!window_)
  93. return false;
  94. [window_ performSelectorOnMainThread:@selector(createWindow:)
  95. withObject:nil
  96. waitUntilDone:YES];
  97. [window_ makeCurrentContext];
  98. GlRenderer::Init();
  99. GlRenderer::ResizeViewport(width, height);
  100. return true;
  101. }
  102. void MacRenderer::OnFrame(const VideoFrame& frame) {
  103. [window_ makeCurrentContext];
  104. GlRenderer::OnFrame(frame);
  105. }
  106. } // test
  107. } // webrtc