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