summaryrefslogtreecommitdiffstats
path: root/media/cast/test/linux_output_window.cc
blob: 735e0dd687f2b636fa007dc64c66703a5fcbd925 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/cast/test/linux_output_window.h"

#include "base/logging.h"
#include "media/base/video_frame.h"
#include "third_party/libyuv/include/libyuv/convert.h"
#include "ui/gfx/size.h"

namespace media {
namespace cast {
namespace test {

LinuxOutputWindow::LinuxOutputWindow(int x_pos,
                                     int y_pos,
                                     int width,
                                     int height,
                                     const std::string& name) {
  CreateWindow(x_pos, y_pos, width, height, name);
}

LinuxOutputWindow::~LinuxOutputWindow() {
  if (display_ && window_) {
    XUnmapWindow(display_, window_);
    XDestroyWindow(display_, window_);
    XSync(display_, false);
    if (gc_)
      XFreeGC(display_, gc_);
    XCloseDisplay(display_);
  }
}

void LinuxOutputWindow::CreateWindow(int x_pos,
                                     int y_pos,
                                     int width,
                                     int height,
                                     const std::string& name) {
  display_ = XOpenDisplay(NULL);
  if (display_ == NULL) {
    // There's no point to continue if this happens: nothing will work anyway.
    VLOG(1) << "Failed to connect to X server: X environment likely broken";
    NOTREACHED();
  }

  int screen = DefaultScreen(display_);

  // Try to establish a 24-bit TrueColor display.
  // (our environment must allow this).
  XVisualInfo visual_info;
  if (XMatchVisualInfo(display_, screen, 24, TrueColor, &visual_info) == 0) {
    VLOG(1) << "Failed to establish 24-bit TrueColor in X environment.";
    NOTREACHED();
  }

  // Create suitable window attributes.
  XSetWindowAttributes window_attributes;
  window_attributes.colormap = XCreateColormap(
      display_, DefaultRootWindow(display_), visual_info.visual, AllocNone);
  window_attributes.event_mask = StructureNotifyMask | ExposureMask;
  window_attributes.background_pixel = 0;
  window_attributes.border_pixel = 0;

  unsigned long attribute_mask =
      CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;

  window_ = XCreateWindow(display_,
                          DefaultRootWindow(display_),
                          x_pos,
                          y_pos,
                          width,
                          height,
                          0,
                          visual_info.depth,
                          InputOutput,
                          visual_info.visual,
                          attribute_mask,
                          &window_attributes);

  // Set window name.
  XStoreName(display_, window_, name.c_str());
  XSetIconName(display_, window_, name.c_str());

  // Make x report events for mask.
  XSelectInput(display_, window_, StructureNotifyMask);

  // Map the window to the display.
  XMapWindow(display_, window_);

  // Wait for map event.
  XEvent event;
  do {
    XNextEvent(display_, &event);
  } while (event.type != MapNotify || event.xmap.event != window_);

  gc_ = XCreateGC(display_, window_, 0, 0);

  // create shared memory image
  image_ = XShmCreateImage(
      display_, CopyFromParent, 24, ZPixmap, NULL, &shminfo_, width, height);
  shminfo_.shmid = shmget(
      IPC_PRIVATE, (image_->bytes_per_line * image_->height), IPC_CREAT | 0777);
  shminfo_.shmaddr = image_->data = (char*)shmat(shminfo_.shmid, 0, 0);
  if (image_->data == reinterpret_cast<char*>(-1)) {
    VLOG(1) << "XShmCreateImage failed";
    NOTREACHED();
  }
  render_buffer_ = reinterpret_cast<uint8_t*>(image_->data);
  shminfo_.readOnly = false;

  // Attach image to display.
  if (!XShmAttach(display_, &shminfo_)) {
    VLOG(1) << "XShmAttach failed";
    NOTREACHED();
  }
  XSync(display_, false);
}

void LinuxOutputWindow::RenderFrame(
    const scoped_refptr<media::VideoFrame>& video_frame) {
  libyuv::I420ToARGB(video_frame->data(VideoFrame::kYPlane),
                     video_frame->stride(VideoFrame::kYPlane),
                     video_frame->data(VideoFrame::kUPlane),
                     video_frame->stride(VideoFrame::kUPlane),
                     video_frame->data(VideoFrame::kVPlane),
                     video_frame->stride(VideoFrame::kVPlane),
                     render_buffer_,
                     video_frame->stride(VideoFrame::kYPlane) * 4,
                     video_frame->coded_size().width(),
                     video_frame->coded_size().height());

  // Place image in window.
  XShmPutImage(display_,
               window_,
               gc_,
               image_,
               0,
               0,
               0,
               0,
               video_frame->coded_size().width(),
               video_frame->coded_size().height(),
               true);

  // Very important for the image to update properly!
  XSync(display_, false);
}

}  // namespace test
}  // namespace cast
}  // namespace media