summaryrefslogtreecommitdiffstats
path: root/content/browser/android/surface_texture_peer_browser_impl.cc
blob: 70dcf1a7ba83d71115b92719e488cd6d6d6c9d25 (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
// Copyright (c) 2012 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 "content/browser/android/surface_texture_peer_browser_impl.h"

#include "content/browser/android/media_player_manager_android.h"
#include "content/browser/renderer_host/render_view_host_impl.h"
#include "content/common/android/surface_callback.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h"
#include "jni/BrowserProcessSurfaceTexture_jni.h"
#include "media/base/android/media_player_bridge.h"

using base::android::MethodID;

namespace content {

// Pass a java surface object to the MediaPlayerBridge object
// identified by render process handle, render view ID and player ID.
static void SetSurfacePeer(
    scoped_refptr<SurfaceTextureBridge> surface_texture_bridge,
    base::ProcessHandle render_process_handle,
    int render_view_id,
    int player_id) {
  int renderer_id = 0;
  RenderProcessHost::iterator it = RenderProcessHost::AllHostsIterator();
  while (!it.IsAtEnd()) {
    if (it.GetCurrentValue()->GetHandle() == render_process_handle) {
      renderer_id = it.GetCurrentValue()->GetID();
      break;
    }
    it.Advance();
  }

  JNIEnv* env = base::android::AttachCurrentThread();
  DCHECK(env);
  if (renderer_id) {
    RenderViewHostImpl* host = RenderViewHostImpl::FromID(
        renderer_id, render_view_id);
    if (host) {
      media::MediaPlayerBridge* player =
          host->media_player_manager()->GetPlayer(player_id);
      if (player &&
          player != host->media_player_manager()->GetFullscreenPlayer()) {
        base::android::ScopedJavaLocalRef<jclass> cls(
            base::android::GetClass(env, "android/view/Surface"));
        jmethodID constructor = MethodID::Get<MethodID::TYPE_INSTANCE>(
            env, cls.obj(), "<init>", "(Landroid/graphics/SurfaceTexture;)V");
        ScopedJavaLocalRef<jobject> j_surface(
            env, env->NewObject(
                cls.obj(), constructor,
                surface_texture_bridge->j_surface_texture().obj()));
        player->SetVideoSurface(j_surface.obj());
        ReleaseSurface(j_surface.obj());
      }
    }
  }
}

SurfaceTexturePeerBrowserImpl::SurfaceTexturePeerBrowserImpl(
    bool player_in_render_process)
    : player_in_render_process_(player_in_render_process) {
}

SurfaceTexturePeerBrowserImpl::~SurfaceTexturePeerBrowserImpl() {
  if (surface_.obj())
    ReleaseSurface(surface_.obj());
}

void SurfaceTexturePeerBrowserImpl::EstablishSurfaceTexturePeer(
    base::ProcessHandle render_process_handle,
    SurfaceTextureTarget type,
    scoped_refptr<SurfaceTextureBridge> surface_texture_bridge,
    int render_view_id,
    int player_id) {
  if (!surface_texture_bridge)
    return;

  JNIEnv* env = base::android::AttachCurrentThread();
  DCHECK(env);
  if (player_in_render_process_) {
    Java_BrowserProcessSurfaceTexture_establishSurfaceTexturePeer(
        env, render_process_handle, type,
        surface_texture_bridge->j_surface_texture().obj(),
        render_view_id, player_id);
  } else {
    BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
        &SetSurfacePeer, surface_texture_bridge, render_process_handle,
        render_view_id, player_id));
  }
}

bool SurfaceTexturePeerBrowserImpl::RegisterBrowserProcessSurfaceTexture(
    JNIEnv* env) {
  return RegisterNativesImpl(env);
}

}  // namespace content