1bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org/*
2bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org *  Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org *
4bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org *  Use of this source code is governed by a BSD-style license
5bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org *  that can be found in the LICENSE file in the root of the source
6bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org *  tree. An additional intellectual property rights grant can be found
7bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org *  in the file PATENTS.  All contributing project authors may
8bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
9bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org */
10bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
1125bbc982baeb7ba97c20b90d27828597dc3dc7cffischman@webrtc.org#if !defined(__has_feature) || !__has_feature(objc_arc)
1225bbc982baeb7ba97c20b90d27828597dc3dc7cffischman@webrtc.org#error "This file requires ARC support."
1325bbc982baeb7ba97c20b90d27828597dc3dc7cffischman@webrtc.org#endif
1425bbc982baeb7ba97c20b90d27828597dc3dc7cffischman@webrtc.org
15bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org#include "webrtc/modules/video_render/ios/video_render_ios_gles20.h"
16bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
17bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org#include "webrtc/system_wrappers/interface/event_wrapper.h"
18bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org#include "webrtc/system_wrappers/interface/thread_wrapper.h"
19bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
20bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.orgusing namespace webrtc;
21bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
22bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.orgVideoRenderIosGles20::VideoRenderIosGles20(VideoRenderIosView* view,
23bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org                                           bool full_screen,
24bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org                                           int render_id)
25bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    : gles_crit_sec_(CriticalSectionWrapper::CreateCriticalSection()),
26bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org      screen_update_event_(0),
27bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org      screen_update_thread_(0),
28bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org      view_(view),
29bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org      window_rect_(),
30bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org      window_width_(0),
31bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org      window_height_(0),
32bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org      is_full_screen_(full_screen),
33bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org      agl_channels_(),
34bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org      z_order_to_channel_(),
35bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org      gles_context_([view context]),
36bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org      is_rendering_(true),
37bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org      id_(render_id) {
38bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  screen_update_thread_ = ThreadWrapper::CreateThread(
39bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org      ScreenUpdateThreadProc, this, kRealtimePriority);
40bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  screen_update_event_ = EventWrapper::Create();
41bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  GetWindowRect(window_rect_);
42bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org}
43bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
44bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.orgVideoRenderIosGles20::~VideoRenderIosGles20() {
45bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  // Signal event to exit thread, then delete it
46bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  ThreadWrapper* thread_wrapper = screen_update_thread_;
47bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  screen_update_thread_ = NULL;
48bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
49bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  if (thread_wrapper) {
50bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    thread_wrapper->SetNotAlive();
51bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    screen_update_event_->Set();
52bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    screen_update_event_->StopTimer();
53bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
54bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    if (thread_wrapper->Stop()) {
55bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org      delete thread_wrapper;
56bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    }
57bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    delete screen_update_event_;
58bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    screen_update_event_ = NULL;
59bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    is_rendering_ = FALSE;
60bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  }
61bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
62bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  // Delete all channels
63bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  std::map<int, VideoRenderIosChannel*>::iterator it = agl_channels_.begin();
64bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  while (it != agl_channels_.end()) {
65bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    delete it->second;
66bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    agl_channels_.erase(it);
67bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    it = agl_channels_.begin();
68bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  }
69bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  agl_channels_.clear();
70bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
71bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  // Clean the zOrder map
72bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  std::multimap<int, int>::iterator z_it = z_order_to_channel_.begin();
73bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  while (z_it != z_order_to_channel_.end()) {
74bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    z_order_to_channel_.erase(z_it);
75bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    z_it = z_order_to_channel_.begin();
76bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  }
77bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  z_order_to_channel_.clear();
78bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org}
79bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
80bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.orgint VideoRenderIosGles20::Init() {
81bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  CriticalSectionScoped cs(gles_crit_sec_.get());
82bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
83bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  if (!screen_update_thread_) {
84bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    return -1;
85bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  }
86bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
87bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  if (!view_) {
88bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    view_ = [[VideoRenderIosView alloc] init];
89bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  }
90bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
91bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  if (![view_ createContext]) {
92bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    return -1;
93bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  }
94bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
95bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  unsigned int thread_id;
96bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  screen_update_thread_->Start(thread_id);
97bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
98bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  // Start the event triggering the render process
99bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  unsigned int monitor_freq = 60;
100bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  screen_update_event_->StartTimer(true, 1000 / monitor_freq);
101bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
102bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  window_width_ = window_rect_.right - window_rect_.left;
103bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  window_height_ = window_rect_.bottom - window_rect_.top;
104bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
105bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  return 0;
106bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org}
107bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
108bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.orgVideoRenderIosChannel* VideoRenderIosGles20::CreateEaglChannel(int channel,
109bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org                                                               int z_order,
110bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org                                                               float left,
111bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org                                                               float top,
112bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org                                                               float right,
113bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org                                                               float bottom) {
114bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  CriticalSectionScoped cs(gles_crit_sec_.get());
115bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
116bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  if (HasChannel(channel)) {
117bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    return NULL;
118bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  }
119bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
120bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  VideoRenderIosChannel* new_eagl_channel = new VideoRenderIosChannel(view_);
121bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
122bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  if (new_eagl_channel->SetStreamSettings(z_order, left, top, right, bottom) ==
123bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org      -1) {
124bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    return NULL;
125bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  }
126bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
127bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  agl_channels_[channel] = new_eagl_channel;
128bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  z_order_to_channel_.insert(std::pair<int, int>(z_order, channel));
129bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
130bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  return new_eagl_channel;
131bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org}
132bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
133bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.orgint VideoRenderIosGles20::DeleteEaglChannel(int channel) {
134bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  CriticalSectionScoped cs(gles_crit_sec_.get());
135bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
136bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  std::map<int, VideoRenderIosChannel*>::iterator it;
137bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  it = agl_channels_.find(channel);
138bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  if (it != agl_channels_.end()) {
139bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    delete it->second;
140bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    agl_channels_.erase(it);
141bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  } else {
142bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    return -1;
143bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  }
144bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
145bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  std::multimap<int, int>::iterator z_it = z_order_to_channel_.begin();
146bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  while (z_it != z_order_to_channel_.end()) {
147bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    if (z_it->second == channel) {
148bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org      z_order_to_channel_.erase(z_it);
149bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org      break;
150bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    }
151bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    z_it++;
152bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  }
153bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
154bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  return 0;
155bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org}
156bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
157bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.orgbool VideoRenderIosGles20::HasChannel(int channel) {
158bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  CriticalSectionScoped cs(gles_crit_sec_.get());
159bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
160bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  std::map<int, VideoRenderIosChannel*>::iterator it =
161bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org      agl_channels_.find(channel);
162bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
163bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  if (it != agl_channels_.end()) {
164bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    return true;
165bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  }
166bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
167bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  return false;
168bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org}
169bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
170bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org// Rendering process
171bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.orgbool VideoRenderIosGles20::ScreenUpdateThreadProc(void* obj) {
172bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  return static_cast<VideoRenderIosGles20*>(obj)->ScreenUpdateProcess();
173bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org}
174bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
175bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.orgbool VideoRenderIosGles20::ScreenUpdateProcess() {
176bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  screen_update_event_->Wait(100);
177bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
178bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  CriticalSectionScoped cs(gles_crit_sec_.get());
179bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
180bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  if (!is_rendering_) {
181bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    return false;
182bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  }
183bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
184bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  if (!screen_update_thread_) {
185bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    return false;
186bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  }
187bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
188bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  if (GetWindowRect(window_rect_) == -1) {
189bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    return true;
190bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  }
191bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
192bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  if (window_width_ != (window_rect_.right - window_rect_.left) ||
193bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org      window_height_ != (window_rect_.bottom - window_rect_.top)) {
194bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    window_width_ = window_rect_.right - window_rect_.left;
195bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    window_height_ = window_rect_.bottom - window_rect_.top;
196bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  }
197bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
198bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  // Check if there are any updated buffers
199bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  bool updated = false;
200bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
201bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  std::map<int, VideoRenderIosChannel*>::iterator it = agl_channels_.begin();
202bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  while (it != agl_channels_.end()) {
203bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    VideoRenderIosChannel* agl_channel = it->second;
204bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
205bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    updated = agl_channel->IsUpdated();
206bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    if (updated) {
207bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org      break;
208bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    }
209bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    it++;
210bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  }
211bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
212bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  if (updated) {
213bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    // At least one buffer has been updated, we need to repaint the texture
214bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    // Loop through all channels starting highest zOrder ending with lowest.
215bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    for (std::multimap<int, int>::reverse_iterator r_it =
216bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org             z_order_to_channel_.rbegin();
217bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org         r_it != z_order_to_channel_.rend();
218bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org         r_it++) {
219bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org      int channel_id = r_it->second;
220bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org      std::map<int, VideoRenderIosChannel*>::iterator it =
221bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org          agl_channels_.find(channel_id);
222bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
223bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org      VideoRenderIosChannel* agl_channel = it->second;
224bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
225bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org      agl_channel->RenderOffScreenBuffer();
226bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    }
227bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
228bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    [view_ presentFramebuffer];
229bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  }
230bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
231bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  return true;
232bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org}
233bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
234bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.orgint VideoRenderIosGles20::GetWindowRect(Rect& rect) {
235bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  CriticalSectionScoped cs(gles_crit_sec_.get());
236bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
237bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  if (!view_) {
238bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    return -1;
239bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  }
240bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
2415b7878f96340e0f72cf12ced9fa15f95ab87bd86sjlee@webrtc.org  CGRect bounds = [view_ bounds];
2425b7878f96340e0f72cf12ced9fa15f95ab87bd86sjlee@webrtc.org  rect.top = bounds.origin.y;
2435b7878f96340e0f72cf12ced9fa15f95ab87bd86sjlee@webrtc.org  rect.left = bounds.origin.x;
2445b7878f96340e0f72cf12ced9fa15f95ab87bd86sjlee@webrtc.org  rect.bottom = bounds.size.height + bounds.origin.y;
2455b7878f96340e0f72cf12ced9fa15f95ab87bd86sjlee@webrtc.org  rect.right = bounds.size.width + bounds.origin.x;
246bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
247bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  return 0;
248bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org}
249bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
250bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.orgint VideoRenderIosGles20::ChangeWindow(void* new_window) {
251bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  CriticalSectionScoped cs(gles_crit_sec_.get());
252bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
25325bbc982baeb7ba97c20b90d27828597dc3dc7cffischman@webrtc.org  view_ = (__bridge VideoRenderIosView*)new_window;
254bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
255bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  return 0;
256bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org}
257bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
258bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.orgint VideoRenderIosGles20::ChangeUniqueID(int unique_id) {
259bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  CriticalSectionScoped cs(gles_crit_sec_.get());
260bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
261bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  id_ = unique_id;
262bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
263bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  return 0;
264bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org}
265bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
266bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.orgint VideoRenderIosGles20::StartRender() {
267bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  is_rendering_ = true;
268bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  return 0;
269bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org}
270bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
271bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.orgint VideoRenderIosGles20::StopRender() {
272bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  is_rendering_ = false;
273bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  return 0;
274bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org}
275bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
276bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.orgint VideoRenderIosGles20::GetScreenResolution(uint& screen_width,
277bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org                                              uint& screen_height) {
2785b7878f96340e0f72cf12ced9fa15f95ab87bd86sjlee@webrtc.org  screen_width = [view_ bounds].size.width;
2795b7878f96340e0f72cf12ced9fa15f95ab87bd86sjlee@webrtc.org  screen_height = [view_ bounds].size.height;
280bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  return 0;
281bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org}
282bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
283bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.orgint VideoRenderIosGles20::SetStreamCropping(const uint stream_id,
284bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org                                            const float left,
285bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org                                            const float top,
286bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org                                            const float right,
287bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org                                            const float bottom) {
288bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  // Check if there are any updated buffers
289bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  // bool updated = false;
290bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  uint counter = 0;
291bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
292bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  std::map<int, VideoRenderIosChannel*>::iterator it = agl_channels_.begin();
293bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  while (it != agl_channels_.end()) {
294bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    if (counter == stream_id) {
295bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org      VideoRenderIosChannel* agl_channel = it->second;
296bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org      agl_channel->SetStreamSettings(0, left, top, right, bottom);
297bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    }
298bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    counter++;
299bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org    it++;
300bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  }
301bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org
302bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org  return 0;
303bc375b56d1d38a5b99d5cc726e1da098b5da969dfischman@webrtc.org}
304