blob: 5cf52d07f8f8305a564e4d446a9ce10e591413e3 [file] [log] [blame]
Sami Kalliomaki16032122016-07-20 16:13:08 +02001/*
2 * Copyright (c) 2016 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
11#ifndef WEBRTC_API_ANDROIDVIDEOTRACKSOURCE_H_
12#define WEBRTC_API_ANDROIDVIDEOTRACKSOURCE_H_
13
14#include "webrtc/api/android/jni/native_handle_impl.h"
15#include "webrtc/api/android/jni/surfacetexturehelper_jni.h"
16#include "webrtc/api/mediastreaminterface.h"
17#include "webrtc/api/notifier.h"
18#include "webrtc/base/asyncinvoker.h"
19#include "webrtc/base/checks.h"
20#include "webrtc/base/thread_checker.h"
21#include "webrtc/base/timestampaligner.h"
22#include "webrtc/common_video/include/i420_buffer_pool.h"
23#include "webrtc/media/base/videoadapter.h"
24#include "webrtc/media/base/videobroadcaster.h"
25#include "webrtc/media/base/videosinkinterface.h"
Sami Kalliomaki16032122016-07-20 16:13:08 +020026
27namespace webrtc {
28
29class AndroidVideoTrackSource : public Notifier<VideoTrackSourceInterface> {
30 public:
31 AndroidVideoTrackSource(rtc::Thread* signaling_thread,
32 JNIEnv* jni,
arsanyb75f2542016-08-31 18:50:52 -070033 jobject j_egl_context,
34 bool is_screencast = false);
Sami Kalliomaki16032122016-07-20 16:13:08 +020035
arsanyb75f2542016-08-31 18:50:52 -070036 bool is_screencast() const override { return is_screencast_; }
Sami Kalliomaki16032122016-07-20 16:13:08 +020037
38 // Indicates that the encoder should denoise video before encoding it.
39 // If it is not set, the default configuration is used which is different
40 // depending on video codec.
41 rtc::Optional<bool> needs_denoising() const override {
42 return rtc::Optional<bool>(false);
43 }
44
45 // Returns false if no stats are available, e.g, for a remote
46 // source, or a source which has not seen its first frame yet.
47 // Should avoid blocking.
48 bool GetStats(Stats* stats) override;
49
50 // Called by the native capture observer
51 void SetState(SourceState state);
52
53 SourceState state() const override { return state_; }
54
55 bool remote() const override { return false; }
56
57 void AddOrUpdateSink(rtc::VideoSinkInterface<cricket::VideoFrame>* sink,
58 const rtc::VideoSinkWants& wants) override;
59 void RemoveSink(rtc::VideoSinkInterface<cricket::VideoFrame>* sink) override;
60
61 void OnByteBufferFrameCaptured(const void* frame_data,
62 int length,
63 int width,
64 int height,
65 int rotation,
66 int64_t timestamp_ns);
67
68 void OnTextureFrameCaptured(int width,
69 int height,
70 int rotation,
71 int64_t timestamp_ns,
72 const webrtc_jni::NativeHandleImpl& handle);
73
74 void OnOutputFormatRequest(int width, int height, int fps);
75
76 rtc::scoped_refptr<webrtc_jni::SurfaceTextureHelper>
77 surface_texture_helper() {
78 return surface_texture_helper_;
79 }
80
81 private:
82 rtc::Thread* signaling_thread_;
83 rtc::AsyncInvoker invoker_;
84 rtc::ThreadChecker worker_thread_checker_;
85 rtc::ThreadChecker camera_thread_checker_;
86 rtc::CriticalSection stats_crit_;
87 rtc::Optional<Stats> stats_ GUARDED_BY(stats_crit_);
88 SourceState state_;
89 rtc::VideoBroadcaster broadcaster_;
90 rtc::TimestampAligner timestamp_aligner_;
91 cricket::VideoAdapter video_adapter_;
92 rtc::CriticalSection apply_rotation_crit_;
93 bool apply_rotation_ GUARDED_BY(apply_rotation_crit_);
magjed36d38cb2016-09-09 09:09:46 -070094 std::vector<uint8_t> unscaled_uv_planes_;
95 webrtc::I420BufferPool buffer_pool_;
Sami Kalliomaki16032122016-07-20 16:13:08 +020096 rtc::scoped_refptr<webrtc_jni::SurfaceTextureHelper> surface_texture_helper_;
arsanyb75f2542016-08-31 18:50:52 -070097 const bool is_screencast_;
Sami Kalliomaki16032122016-07-20 16:13:08 +020098
99 void OnFrame(const cricket::VideoFrame& frame, int width, int height);
100
101 void OnSinkWantsChanged(const rtc::VideoSinkWants& wants);
102
103 bool AdaptFrame(int width,
104 int height,
105 int64_t camera_time_us,
106 int* out_width,
107 int* out_height,
108 int* crop_width,
109 int* crop_height,
110 int* crop_x,
111 int* crop_y,
112 int64_t* translated_camera_time_us);
113};
114
115} // namespace webrtc
116
117#endif // WEBRTC_API_ANDROIDVIDEOTRACKSOURCE_H_