blob: cba522480c376e4d97a8f7ceee84a9cef8fc2d4a [file] [log] [blame]
perkj@webrtc.org83bc7212015-02-11 11:26:56 +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 */
Henrik Kjellander15583c12016-02-10 10:53:12 +010027
28#ifndef WEBRTC_API_ANDROIDVIDEOCAPTURER_H_
29#define WEBRTC_API_ANDROIDVIDEOCAPTURER_H_
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000030
31#include <string>
32#include <vector>
33
Per33544192015-04-02 12:30:51 +020034#include "webrtc/base/thread_checker.h"
kjellander6f8ce062015-11-16 13:52:24 -080035#include "webrtc/common_video/include/video_frame_buffer.h"
kjellandera96e2d72016-02-04 23:52:28 -080036#include "webrtc/media/base/videocapturer.h"
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000037
38namespace webrtc {
39
40class AndroidVideoCapturer;
41
Per33544192015-04-02 12:30:51 +020042class AndroidVideoCapturerDelegate : public rtc::RefCountInterface {
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000043 public:
44 virtual ~AndroidVideoCapturerDelegate() {}
45 // Start capturing. The implementation of the delegate must call
46 // AndroidVideoCapturer::OnCapturerStarted with the result of this request.
47 virtual void Start(int width, int height, int framerate,
48 AndroidVideoCapturer* capturer) = 0;
49
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000050 // Stops capturing.
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000051 // The delegate may not call into AndroidVideoCapturer after this call.
perkj@webrtc.org3db042e2015-02-19 08:43:38 +000052 virtual void Stop() = 0;
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000053
54 // Must returns a JSON string "{{width=xxx, height=xxx, framerate = xxx}}"
55 virtual std::string GetSupportedFormats() = 0;
56};
57
58// Android implementation of cricket::VideoCapturer for use with WebRtc
59// PeerConnection.
60class AndroidVideoCapturer : public cricket::VideoCapturer {
61 public:
62 explicit AndroidVideoCapturer(
Per33544192015-04-02 12:30:51 +020063 const rtc::scoped_refptr<AndroidVideoCapturerDelegate>& delegate);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000064 virtual ~AndroidVideoCapturer();
65
perkj@webrtc.org112f1272015-02-25 09:20:07 +000066 // Called from JNI when the capturer has been started.
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000067 void OnCapturerStarted(bool success);
68
perkj@webrtc.org112f1272015-02-25 09:20:07 +000069 // Called from JNI when a new frame has been captured.
Magnus Jedvertc464f502015-08-25 23:22:08 +020070 // Argument |buffer| is intentionally by value, for use with rtc::Bind.
olka30a5b5e2015-10-20 11:04:56 -070071 void OnIncomingFrame(
72 const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& buffer,
73 int rotation,
74 int64_t time_stamp);
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000075
Åsa Persson2b679252015-06-15 09:53:05 +020076 // Called from JNI to request a new video format.
77 void OnOutputFormatRequest(int width, int height, int fps);
78
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000079 AndroidVideoCapturerDelegate* delegate() { return delegate_.get(); }
80
Magnus Jedvert6ec1f922015-08-28 11:40:59 +020081 // cricket::VideoCapturer implementation.
82 bool GetBestCaptureFormat(const cricket::VideoFormat& desired,
83 cricket::VideoFormat* best_format) override;
84
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000085 private:
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000086 // cricket::VideoCapturer implementation.
87 // Video frames will be delivered using
88 // cricket::VideoCapturer::SignalFrameCaptured on the thread that calls Start.
89 cricket::CaptureState Start(
90 const cricket::VideoFormat& capture_format) override;
91 void Stop() override;
92 bool IsRunning() override;
93 bool IsScreencast() const override { return false; }
Peter Boström0c4e06b2015-10-07 12:23:21 +020094 bool GetPreferredFourccs(std::vector<uint32_t>* fourccs) override;
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000095
96 bool running_;
Per33544192015-04-02 12:30:51 +020097 rtc::scoped_refptr<AndroidVideoCapturerDelegate> delegate_;
perkj@webrtc.org83bc7212015-02-11 11:26:56 +000098
Per33544192015-04-02 12:30:51 +020099 rtc::ThreadChecker thread_checker_;
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000100
101 class FrameFactory;
102 FrameFactory* frame_factory_; // Owned by cricket::VideoCapturer.
perkj@webrtc.org8f605e82015-02-17 13:53:56 +0000103
104 cricket::CaptureState current_state_;
perkj@webrtc.org83bc7212015-02-11 11:26:56 +0000105};
106
107} // namespace webrtc
108
Henrik Kjellander15583c12016-02-10 10:53:12 +0100109#endif // WEBRTC_API_ANDROIDVIDEOCAPTURER_H_