blob: c2704393779bc6c75002524ae0dcffb110e757bc [file] [log] [blame]
perkj@webrtc.org96e4db92015-02-13 12:46:51 +00001/*
2 * libjingle
3 * Copyright 2015 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 */
28
29#ifndef TALK_APP_WEBRTC_JAVA_JNI_ANDROIDVIDEOCAPTURER_JNI_H_
30#define TALK_APP_WEBRTC_JAVA_JNI_ANDROIDVIDEOCAPTURER_JNI_H_
31
32#include <string>
33
34#include "talk/app/webrtc/androidvideocapturer.h"
35#include "talk/app/webrtc/java/jni/jni_helpers.h"
perkj@webrtc.org112f1272015-02-25 09:20:07 +000036#include "webrtc/base/asyncinvoker.h"
Magnus Jedvertc464f502015-08-25 23:22:08 +020037#include "webrtc/base/criticalsection.h"
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000038#include "webrtc/base/thread_checker.h"
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000039
40namespace webrtc_jni {
41
42// AndroidVideoCapturerJni implements AndroidVideoCapturerDelegate.
43// The purpose of the delegate is to hide the JNI specifics from the C++ only
44// AndroidVideoCapturer.
45class AndroidVideoCapturerJni : public webrtc::AndroidVideoCapturerDelegate {
46 public:
47 static int SetAndroidObjects(JNIEnv* jni, jobject appliction_context);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000048
perkj@webrtc.org112f1272015-02-25 09:20:07 +000049 // Creates a new instance of AndroidVideoCapturerJni. Returns a nullptr if
50 // it can't be created. This happens if |device_name| is invalid.
Per33544192015-04-02 12:30:51 +020051 static rtc::scoped_refptr<AndroidVideoCapturerJni> Create(
perkj@webrtc.org112f1272015-02-25 09:20:07 +000052 JNIEnv* jni,
53 jobject j_video_capture, // Instance of VideoCapturerAndroid
54 jstring device_name); // Name of the camera to use.
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000055
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000056 void Start(int width, int height, int framerate,
57 webrtc::AndroidVideoCapturer* capturer) override;
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000058 void Stop() override;
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000059
60 std::string GetSupportedFormats() override;
61
perkj@webrtc.org112f1272015-02-25 09:20:07 +000062 // Called from VideoCapturerAndroid::NativeObserver on a Java thread.
63 void OnCapturerStarted(bool success);
64 void OnIncomingFrame(void* video_frame,
65 int length,
magjedb69ab792015-07-22 02:32:00 -070066 int width,
67 int height,
perkj@webrtc.org112f1272015-02-25 09:20:07 +000068 int rotation,
69 int64 time_stamp);
Åsa Persson2b679252015-06-15 09:53:05 +020070 void OnOutputFormatRequest(int width, int height, int fps);
Per33544192015-04-02 12:30:51 +020071protected:
72 AndroidVideoCapturerJni(JNIEnv* jni, jobject j_video_capturer);
73 ~AndroidVideoCapturerJni();
perkj@webrtc.org112f1272015-02-25 09:20:07 +000074
75private:
perkj@webrtc.org112f1272015-02-25 09:20:07 +000076 bool Init(jstring device_name);
Magnus Jedvertc464f502015-08-25 23:22:08 +020077 void ReturnBuffer(int64 time_stamp);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000078 JNIEnv* jni();
79
Magnus Jedvertc464f502015-08-25 23:22:08 +020080 // Helper function to make safe asynchronous calls to |capturer_|. The calls
81 // are not guaranteed to be delivered.
82 template <typename... Args>
83 void AsyncCapturerInvoke(
84 const char* method_name,
85 void (webrtc::AndroidVideoCapturer::*method)(Args...),
86 Args... args);
87
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000088 const ScopedGlobalRef<jobject> j_capturer_global_;
89 const ScopedGlobalRef<jclass> j_video_capturer_class_;
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000090 const ScopedGlobalRef<jclass> j_observer_class_;
perkj@webrtc.org96e4db92015-02-13 12:46:51 +000091
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000092 rtc::ThreadChecker thread_checker_;
93
perkj@webrtc.org112f1272015-02-25 09:20:07 +000094 // |capturer| is a guaranteed to be a valid pointer between a call to
95 // AndroidVideoCapturerDelegate::Start
96 // until AndroidVideoCapturerDelegate::Stop.
Magnus Jedvertc464f502015-08-25 23:22:08 +020097 rtc::CriticalSection capturer_lock_;
98 webrtc::AndroidVideoCapturer* capturer_ GUARDED_BY(capturer_lock_);
99 // |invoker_| is used to communicate with |capturer_| on the thread Start() is
100 // called on.
101 rtc::scoped_ptr<rtc::GuardedAsyncInvoker> invoker_ GUARDED_BY(capturer_lock_);
perkj@webrtc.org3db042e2015-02-19 08:43:38 +0000102
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000103 static jobject application_context_;
104
henrikg3c089d72015-09-16 05:37:44 -0700105 RTC_DISALLOW_COPY_AND_ASSIGN(AndroidVideoCapturerJni);
perkj@webrtc.org96e4db92015-02-13 12:46:51 +0000106};
107
108} // namespace webrtc_jni
109
110#endif // TALK_APP_WEBRTC_JAVA_JNI_ANDROIDVIDEOCAPTURER_JNI_H_