perkj@webrtc.org | 83bc721 | 2015-02-11 11:26:56 +0000 | [diff] [blame] | 1 | /* |
| 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 | #include "talk/app/webrtc/androidvideocapturer.h" |
| 28 | |
| 29 | #include "talk/media/webrtc/webrtcvideoframe.h" |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 30 | #include "webrtc/base/bind.h" |
| 31 | #include "webrtc/base/callback.h" |
perkj@webrtc.org | 83bc721 | 2015-02-11 11:26:56 +0000 | [diff] [blame] | 32 | #include "webrtc/base/common.h" |
| 33 | #include "webrtc/base/json.h" |
| 34 | #include "webrtc/base/timeutils.h" |
| 35 | #include "webrtc/base/thread.h" |
Henrik Boström | 09a9ea8 | 2015-04-17 17:31:53 +0200 | [diff] [blame] | 36 | #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" |
perkj@webrtc.org | 83bc721 | 2015-02-11 11:26:56 +0000 | [diff] [blame] | 37 | |
| 38 | namespace webrtc { |
| 39 | |
| 40 | using cricket::WebRtcVideoFrame; |
| 41 | using rtc::scoped_ptr; |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 42 | using rtc::scoped_refptr; |
perkj@webrtc.org | 83bc721 | 2015-02-11 11:26:56 +0000 | [diff] [blame] | 43 | |
| 44 | // An implementation of cricket::VideoFrameFactory for frames that are not |
| 45 | // guaranteed to outlive the created cricket::VideoFrame. |
| 46 | // A frame is injected using UpdateCapturedFrame, and converted into a |
| 47 | // cricket::VideoFrame with |
| 48 | // CreateAliasedFrame. UpdateCapturedFrame should be called before |
| 49 | // CreateAliasedFrame for every frame. |
| 50 | class AndroidVideoCapturer::FrameFactory : public cricket::VideoFrameFactory { |
| 51 | public: |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 52 | FrameFactory(int width, |
| 53 | int height, |
| 54 | const scoped_refptr<AndroidVideoCapturerDelegate>& delegate) |
| 55 | : start_time_(rtc::TimeNanos()), delegate_(delegate) { |
perkj@webrtc.org | 83bc721 | 2015-02-11 11:26:56 +0000 | [diff] [blame] | 56 | // Create a CapturedFrame that only contains header information, not the |
| 57 | // actual pixel data. |
| 58 | captured_frame_.width = width; |
| 59 | captured_frame_.height = height; |
| 60 | captured_frame_.pixel_height = 1; |
| 61 | captured_frame_.pixel_width = 1; |
| 62 | captured_frame_.rotation = 0; |
| 63 | captured_frame_.data = NULL; |
| 64 | captured_frame_.data_size = cricket::CapturedFrame::kUnknownDataSize; |
| 65 | captured_frame_.fourcc = static_cast<uint32>(cricket::FOURCC_ANY); |
| 66 | } |
| 67 | |
perkj@webrtc.org | 112f127 | 2015-02-25 09:20:07 +0000 | [diff] [blame] | 68 | void UpdateCapturedFrame(void* frame_data, |
perkj@webrtc.org | 83bc721 | 2015-02-11 11:26:56 +0000 | [diff] [blame] | 69 | int length, |
| 70 | int rotation, |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 71 | int64 time_stamp_in_ns) { |
perkj@webrtc.org | 2ad3bb1 | 2015-02-23 11:14:57 +0000 | [diff] [blame] | 72 | captured_frame_.fourcc = static_cast<uint32>(cricket::FOURCC_YV12); |
perkj@webrtc.org | 83bc721 | 2015-02-11 11:26:56 +0000 | [diff] [blame] | 73 | captured_frame_.data = frame_data; |
| 74 | captured_frame_.elapsed_time = rtc::TimeNanos() - start_time_; |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 75 | captured_frame_.time_stamp = time_stamp_in_ns; |
perkj@webrtc.org | 83bc721 | 2015-02-11 11:26:56 +0000 | [diff] [blame] | 76 | captured_frame_.rotation = rotation; |
| 77 | captured_frame_.data_size = length; |
| 78 | } |
| 79 | |
| 80 | const cricket::CapturedFrame* GetCapturedFrame() const { |
| 81 | return &captured_frame_; |
| 82 | } |
| 83 | |
| 84 | cricket::VideoFrame* CreateAliasedFrame( |
| 85 | const cricket::CapturedFrame* captured_frame, |
| 86 | int dst_width, |
| 87 | int dst_height) const override { |
| 88 | // This override of CreateAliasedFrame creates a copy of the frame since |
| 89 | // |captured_frame_.data| is only guaranteed to be valid during the scope |
| 90 | // of |AndroidVideoCapturer::OnIncomingFrame_w|. |
| 91 | // Check that captured_frame is actually our frame. |
Alex Glaznev | 2f5be9a | 2015-05-19 10:56:32 -0700 | [diff] [blame] | 92 | CHECK(captured_frame == &captured_frame_); |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 93 | |
| 94 | if (!apply_rotation_ || captured_frame->rotation == kVideoRotation_0) { |
Alex Glaznev | 2f5be9a | 2015-05-19 10:56:32 -0700 | [diff] [blame] | 95 | CHECK(captured_frame->fourcc == cricket::FOURCC_YV12); |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 96 | const uint8_t* y_plane = static_cast<uint8_t*>(captured_frame_.data); |
Henrik Boström | 09a9ea8 | 2015-04-17 17:31:53 +0200 | [diff] [blame] | 97 | |
| 98 | // Android guarantees that the stride is a multiple of 16. |
| 99 | // http://developer.android.com/reference/android/hardware/Camera.Parameters.html#setPreviewFormat%28int%29 |
| 100 | int y_stride; |
| 101 | int uv_stride; |
| 102 | webrtc::Calc16ByteAlignedStride(captured_frame->width, &y_stride, |
| 103 | &uv_stride); |
| 104 | const uint8_t* v_plane = y_plane + y_stride * captured_frame->height; |
| 105 | const uint8_t* u_plane = |
| 106 | v_plane + uv_stride * webrtc::AlignInt(captured_frame->height, 2) / 2; |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 107 | |
| 108 | // Create a WrappedI420Buffer and bind the |no_longer_used| callback |
| 109 | // to the static method ReturnFrame. The |delegate_| is bound as an |
| 110 | // argument which means that the callback will hold a reference to |
| 111 | // |delegate_|. |
| 112 | rtc::scoped_refptr<WrappedI420Buffer> buffer( |
| 113 | new rtc::RefCountedObject<webrtc::WrappedI420Buffer>( |
| 114 | dst_width, dst_height, captured_frame->width, |
| 115 | captured_frame->height, y_plane, y_stride, u_plane, uv_stride, |
| 116 | v_plane, uv_stride, |
| 117 | rtc::Bind(&AndroidVideoCapturer::FrameFactory::ReturnFrame, |
| 118 | delegate_, |
| 119 | captured_frame->time_stamp))); |
| 120 | return new WebRtcVideoFrame( |
| 121 | buffer, captured_frame->elapsed_time, |
| 122 | captured_frame->time_stamp, captured_frame->GetRotation()); |
| 123 | } |
| 124 | |
perkj@webrtc.org | 83bc721 | 2015-02-11 11:26:56 +0000 | [diff] [blame] | 125 | scoped_ptr<WebRtcVideoFrame> frame(new WebRtcVideoFrame()); |
perkj@webrtc.org | 1d82813 | 2015-03-03 06:44:06 +0000 | [diff] [blame] | 126 | frame->Init(captured_frame, dst_width, dst_height, apply_rotation_); |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 127 | // frame->Init copies the data in |captured_frame| so it is safe to return |
| 128 | // the buffer immediately. |
| 129 | delegate_->ReturnBuffer(captured_frame->time_stamp); |
perkj@webrtc.org | 83bc721 | 2015-02-11 11:26:56 +0000 | [diff] [blame] | 130 | return frame.release(); |
| 131 | } |
| 132 | |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 133 | static void ReturnFrame(scoped_refptr<AndroidVideoCapturerDelegate> delegate, |
| 134 | int64 time_stamp) { |
| 135 | delegate->ReturnBuffer(time_stamp); |
| 136 | } |
| 137 | |
perkj@webrtc.org | 83bc721 | 2015-02-11 11:26:56 +0000 | [diff] [blame] | 138 | private: |
| 139 | uint64 start_time_; |
| 140 | cricket::CapturedFrame captured_frame_; |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 141 | scoped_refptr<AndroidVideoCapturerDelegate> delegate_; |
perkj@webrtc.org | 83bc721 | 2015-02-11 11:26:56 +0000 | [diff] [blame] | 142 | }; |
| 143 | |
| 144 | AndroidVideoCapturer::AndroidVideoCapturer( |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 145 | const rtc::scoped_refptr<AndroidVideoCapturerDelegate>& delegate) |
perkj@webrtc.org | 83bc721 | 2015-02-11 11:26:56 +0000 | [diff] [blame] | 146 | : running_(false), |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 147 | delegate_(delegate), |
perkj@webrtc.org | 8f605e8 | 2015-02-17 13:53:56 +0000 | [diff] [blame] | 148 | frame_factory_(NULL), |
perkj@webrtc.org | 3db042e | 2015-02-19 08:43:38 +0000 | [diff] [blame] | 149 | current_state_(cricket::CS_STOPPED) { |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 150 | thread_checker_.DetachFromThread(); |
perkj@webrtc.org | 83bc721 | 2015-02-11 11:26:56 +0000 | [diff] [blame] | 151 | std::string json_string = delegate_->GetSupportedFormats(); |
| 152 | LOG(LS_INFO) << json_string; |
| 153 | |
| 154 | Json::Value json_values; |
| 155 | Json::Reader reader(Json::Features::strictMode()); |
| 156 | if (!reader.parse(json_string, json_values)) { |
| 157 | LOG(LS_ERROR) << "Failed to parse formats."; |
| 158 | } |
| 159 | |
| 160 | std::vector<cricket::VideoFormat> formats; |
| 161 | for (Json::ArrayIndex i = 0; i < json_values.size(); ++i) { |
| 162 | const Json::Value& json_value = json_values[i]; |
Alex Glaznev | 2f5be9a | 2015-05-19 10:56:32 -0700 | [diff] [blame] | 163 | CHECK(!json_value["width"].isNull() && !json_value["height"].isNull() && |
perkj@webrtc.org | 83bc721 | 2015-02-11 11:26:56 +0000 | [diff] [blame] | 164 | !json_value["framerate"].isNull()); |
| 165 | cricket::VideoFormat format( |
| 166 | json_value["width"].asInt(), |
| 167 | json_value["height"].asInt(), |
| 168 | cricket::VideoFormat::FpsToInterval(json_value["framerate"].asInt()), |
perkj@webrtc.org | 2ad3bb1 | 2015-02-23 11:14:57 +0000 | [diff] [blame] | 169 | cricket::FOURCC_YV12); |
perkj@webrtc.org | 83bc721 | 2015-02-11 11:26:56 +0000 | [diff] [blame] | 170 | formats.push_back(format); |
| 171 | } |
| 172 | SetSupportedFormats(formats); |
| 173 | } |
| 174 | |
| 175 | AndroidVideoCapturer::~AndroidVideoCapturer() { |
Alex Glaznev | 2f5be9a | 2015-05-19 10:56:32 -0700 | [diff] [blame] | 176 | CHECK(!running_); |
perkj@webrtc.org | 83bc721 | 2015-02-11 11:26:56 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | cricket::CaptureState AndroidVideoCapturer::Start( |
| 180 | const cricket::VideoFormat& capture_format) { |
perkj@webrtc.org | 83bc721 | 2015-02-11 11:26:56 +0000 | [diff] [blame] | 181 | LOG(LS_INFO) << " AndroidVideoCapturer::Start w = " << capture_format.width |
| 182 | << " h = " << capture_format.height; |
Alex Glaznev | 2f5be9a | 2015-05-19 10:56:32 -0700 | [diff] [blame] | 183 | CHECK(thread_checker_.CalledOnValidThread()); |
| 184 | CHECK(!running_); |
| 185 | |
perkj@webrtc.org | 83bc721 | 2015-02-11 11:26:56 +0000 | [diff] [blame] | 186 | frame_factory_ = new AndroidVideoCapturer::FrameFactory( |
Per | 3354419 | 2015-04-02 12:30:51 +0200 | [diff] [blame] | 187 | capture_format.width, capture_format.height, delegate_.get()); |
perkj@webrtc.org | 83bc721 | 2015-02-11 11:26:56 +0000 | [diff] [blame] | 188 | set_frame_factory(frame_factory_); |
| 189 | |
| 190 | running_ = true; |
| 191 | delegate_->Start( |
| 192 | capture_format.width, capture_format.height, |
| 193 | cricket::VideoFormat::IntervalToFps(capture_format.interval), this); |
perkj@webrtc.org | 8f605e8 | 2015-02-17 13:53:56 +0000 | [diff] [blame] | 194 | SetCaptureFormat(&capture_format); |
| 195 | current_state_ = cricket::CS_STARTING; |
| 196 | return current_state_; |
perkj@webrtc.org | 83bc721 | 2015-02-11 11:26:56 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | void AndroidVideoCapturer::Stop() { |
perkj@webrtc.org | 83bc721 | 2015-02-11 11:26:56 +0000 | [diff] [blame] | 200 | LOG(LS_INFO) << " AndroidVideoCapturer::Stop "; |
Alex Glaznev | 2f5be9a | 2015-05-19 10:56:32 -0700 | [diff] [blame] | 201 | CHECK(thread_checker_.CalledOnValidThread()); |
| 202 | CHECK(running_); |
perkj@webrtc.org | 83bc721 | 2015-02-11 11:26:56 +0000 | [diff] [blame] | 203 | running_ = false; |
| 204 | SetCaptureFormat(NULL); |
| 205 | |
| 206 | delegate_->Stop(); |
perkj@webrtc.org | 8f605e8 | 2015-02-17 13:53:56 +0000 | [diff] [blame] | 207 | current_state_ = cricket::CS_STOPPED; |
| 208 | SignalStateChange(this, current_state_); |
perkj@webrtc.org | 83bc721 | 2015-02-11 11:26:56 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | bool AndroidVideoCapturer::IsRunning() { |
Alex Glaznev | 2f5be9a | 2015-05-19 10:56:32 -0700 | [diff] [blame] | 212 | CHECK(thread_checker_.CalledOnValidThread()); |
perkj@webrtc.org | 83bc721 | 2015-02-11 11:26:56 +0000 | [diff] [blame] | 213 | return running_; |
| 214 | } |
| 215 | |
| 216 | bool AndroidVideoCapturer::GetPreferredFourccs(std::vector<uint32>* fourccs) { |
Alex Glaznev | 2f5be9a | 2015-05-19 10:56:32 -0700 | [diff] [blame] | 217 | CHECK(thread_checker_.CalledOnValidThread()); |
perkj@webrtc.org | 2ad3bb1 | 2015-02-23 11:14:57 +0000 | [diff] [blame] | 218 | fourccs->push_back(cricket::FOURCC_YV12); |
perkj@webrtc.org | 83bc721 | 2015-02-11 11:26:56 +0000 | [diff] [blame] | 219 | return true; |
| 220 | } |
| 221 | |
| 222 | void AndroidVideoCapturer::OnCapturerStarted(bool success) { |
Alex Glaznev | 2f5be9a | 2015-05-19 10:56:32 -0700 | [diff] [blame] | 223 | CHECK(thread_checker_.CalledOnValidThread()); |
perkj@webrtc.org | 83bc721 | 2015-02-11 11:26:56 +0000 | [diff] [blame] | 224 | cricket::CaptureState new_state = |
| 225 | success ? cricket::CS_RUNNING : cricket::CS_FAILED; |
perkj@webrtc.org | 8f605e8 | 2015-02-17 13:53:56 +0000 | [diff] [blame] | 226 | if (new_state == current_state_) |
| 227 | return; |
| 228 | current_state_ = new_state; |
| 229 | |
| 230 | // TODO(perkj): SetCaptureState can not be used since it posts to |thread_|. |
| 231 | // But |thread_ | is currently just the thread that happened to create the |
| 232 | // cricket::VideoCapturer. |
| 233 | SignalStateChange(this, new_state); |
perkj@webrtc.org | 83bc721 | 2015-02-11 11:26:56 +0000 | [diff] [blame] | 234 | } |
| 235 | |
perkj@webrtc.org | 112f127 | 2015-02-25 09:20:07 +0000 | [diff] [blame] | 236 | void AndroidVideoCapturer::OnIncomingFrame(void* frame_data, |
perkj@webrtc.org | 83bc721 | 2015-02-11 11:26:56 +0000 | [diff] [blame] | 237 | int length, |
| 238 | int rotation, |
| 239 | int64 time_stamp) { |
Alex Glaznev | 2f5be9a | 2015-05-19 10:56:32 -0700 | [diff] [blame] | 240 | CHECK(thread_checker_.CalledOnValidThread()); |
perkj@webrtc.org | 83bc721 | 2015-02-11 11:26:56 +0000 | [diff] [blame] | 241 | frame_factory_->UpdateCapturedFrame(frame_data, length, rotation, time_stamp); |
| 242 | SignalFrameCaptured(this, frame_factory_->GetCapturedFrame()); |
perkj@webrtc.org | 83bc721 | 2015-02-11 11:26:56 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | } // namespace webrtc |