| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #ifdef HAVE_WEBRTC_VIDEO | |
| 12 #include "webrtc/media/engine/webrtcvideoengine2.h" | 11 #include "webrtc/media/engine/webrtcvideoengine2.h" |
| 13 | 12 |
| 14 #include <algorithm> | 13 #include <algorithm> |
| 15 #include <set> | 14 #include <set> |
| 16 #include <string> | 15 #include <string> |
| 17 | 16 |
| 18 #include "webrtc/base/buffer.h" | 17 #include "webrtc/base/buffer.h" |
| 19 #include "webrtc/base/logging.h" | 18 #include "webrtc/base/logging.h" |
| 20 #include "webrtc/base/stringutils.h" | 19 #include "webrtc/base/stringutils.h" |
| 21 #include "webrtc/base/timeutils.h" | 20 #include "webrtc/base/timeutils.h" |
| (...skipping 941 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 963 if (enable && options) { | 962 if (enable && options) { |
| 964 VideoSendParameters new_params = send_params_; | 963 VideoSendParameters new_params = send_params_; |
| 965 new_params.options.SetAll(*options); | 964 new_params.options.SetAll(*options); |
| 966 SetSendParameters(send_params_); | 965 SetSendParameters(send_params_); |
| 967 } | 966 } |
| 968 return true; | 967 return true; |
| 969 } | 968 } |
| 970 | 969 |
| 971 bool WebRtcVideoChannel2::ValidateSendSsrcAvailability( | 970 bool WebRtcVideoChannel2::ValidateSendSsrcAvailability( |
| 972 const StreamParams& sp) const { | 971 const StreamParams& sp) const { |
| 973 for (uint32_t ssrc: sp.ssrcs) { | 972 for (uint32_t ssrc : sp.ssrcs) { |
| 974 if (send_ssrcs_.find(ssrc) != send_ssrcs_.end()) { | 973 if (send_ssrcs_.find(ssrc) != send_ssrcs_.end()) { |
| 975 LOG(LS_ERROR) << "Send stream with SSRC '" << ssrc << "' already exists."; | 974 LOG(LS_ERROR) << "Send stream with SSRC '" << ssrc << "' already exists."; |
| 976 return false; | 975 return false; |
| 977 } | 976 } |
| 978 } | 977 } |
| 979 return true; | 978 return true; |
| 980 } | 979 } |
| 981 | 980 |
| 982 bool WebRtcVideoChannel2::ValidateReceiveSsrcAvailability( | 981 bool WebRtcVideoChannel2::ValidateReceiveSsrcAvailability( |
| 983 const StreamParams& sp) const { | 982 const StreamParams& sp) const { |
| 984 for (uint32_t ssrc: sp.ssrcs) { | 983 for (uint32_t ssrc : sp.ssrcs) { |
| 985 if (receive_ssrcs_.find(ssrc) != receive_ssrcs_.end()) { | 984 if (receive_ssrcs_.find(ssrc) != receive_ssrcs_.end()) { |
| 986 LOG(LS_ERROR) << "Receive stream with SSRC '" << ssrc | 985 LOG(LS_ERROR) << "Receive stream with SSRC '" << ssrc |
| 987 << "' already exists."; | 986 << "' already exists."; |
| 988 return false; | 987 return false; |
| 989 } | 988 } |
| 990 } | 989 } |
| 991 return true; | 990 return true; |
| 992 } | 991 } |
| 993 | 992 |
| 994 bool WebRtcVideoChannel2::AddSendStream(const StreamParams& sp) { | 993 bool WebRtcVideoChannel2::AddSendStream(const StreamParams& sp) { |
| (...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1280 { | 1279 { |
| 1281 rtc::CritScope stream_lock(&stream_crit_); | 1280 rtc::CritScope stream_lock(&stream_crit_); |
| 1282 if (send_streams_.find(ssrc) == send_streams_.end()) { | 1281 if (send_streams_.find(ssrc) == send_streams_.end()) { |
| 1283 LOG(LS_ERROR) << "No sending stream on ssrc " << ssrc; | 1282 LOG(LS_ERROR) << "No sending stream on ssrc " << ssrc; |
| 1284 return false; | 1283 return false; |
| 1285 } | 1284 } |
| 1286 if (!send_streams_[ssrc]->SetCapturer(capturer)) { | 1285 if (!send_streams_[ssrc]->SetCapturer(capturer)) { |
| 1287 return false; | 1286 return false; |
| 1288 } | 1287 } |
| 1289 } | 1288 } |
| 1290 | |
| 1291 if (capturer) { | |
| 1292 capturer->SetApplyRotation(!ContainsHeaderExtension( | |
| 1293 send_rtp_extensions_, kRtpVideoRotationHeaderExtension)); | |
| 1294 } | |
| 1295 { | 1289 { |
| 1296 rtc::CritScope lock(&capturer_crit_); | 1290 rtc::CritScope lock(&capturer_crit_); |
| 1297 capturers_[ssrc] = capturer; | 1291 capturers_[ssrc] = capturer; |
| 1298 } | 1292 } |
| 1299 return true; | 1293 return true; |
| 1300 } | 1294 } |
| 1301 | 1295 |
| 1302 void WebRtcVideoChannel2::OnPacketReceived( | 1296 void WebRtcVideoChannel2::OnPacketReceived( |
| 1303 rtc::Buffer* packet, | 1297 rtc::Buffer* packet, |
| 1304 const rtc::PacketTime& packet_time) { | 1298 const rtc::PacketTime& packet_time) { |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1550 (width + 1) / 2); | 1544 (width + 1) / 2); |
| 1551 memset(video_frame->buffer(webrtc::kYPlane), 16, | 1545 memset(video_frame->buffer(webrtc::kYPlane), 16, |
| 1552 video_frame->allocated_size(webrtc::kYPlane)); | 1546 video_frame->allocated_size(webrtc::kYPlane)); |
| 1553 memset(video_frame->buffer(webrtc::kUPlane), 128, | 1547 memset(video_frame->buffer(webrtc::kUPlane), 128, |
| 1554 video_frame->allocated_size(webrtc::kUPlane)); | 1548 video_frame->allocated_size(webrtc::kUPlane)); |
| 1555 memset(video_frame->buffer(webrtc::kVPlane), 128, | 1549 memset(video_frame->buffer(webrtc::kVPlane), 128, |
| 1556 video_frame->allocated_size(webrtc::kVPlane)); | 1550 video_frame->allocated_size(webrtc::kVPlane)); |
| 1557 video_frame->set_rotation(rotation); | 1551 video_frame->set_rotation(rotation); |
| 1558 } | 1552 } |
| 1559 | 1553 |
| 1560 void WebRtcVideoChannel2::WebRtcVideoSendStream::InputFrame( | 1554 void WebRtcVideoChannel2::WebRtcVideoSendStream::OnFrame( |
| 1561 VideoCapturer* capturer, | 1555 const VideoFrame& frame) { |
| 1562 const VideoFrame* frame) { | 1556 TRACE_EVENT0("webrtc", "WebRtcVideoSendStream::OnFrame"); |
| 1563 TRACE_EVENT0("webrtc", "WebRtcVideoSendStream::InputFrame"); | 1557 webrtc::VideoFrame video_frame(frame.GetVideoFrameBuffer(), 0, 0, |
| 1564 webrtc::VideoFrame video_frame(frame->GetVideoFrameBuffer(), 0, 0, | 1558 frame.GetVideoRotation()); |
| 1565 frame->GetVideoRotation()); | |
| 1566 rtc::CritScope cs(&lock_); | 1559 rtc::CritScope cs(&lock_); |
| 1567 if (stream_ == NULL) { | 1560 if (stream_ == NULL) { |
| 1568 // Frame input before send codecs are configured, dropping frame. | 1561 // Frame input before send codecs are configured, dropping frame. |
| 1569 return; | 1562 return; |
| 1570 } | 1563 } |
| 1571 | 1564 |
| 1572 // Not sending, abort early to prevent expensive reconfigurations while | 1565 // Not sending, abort early to prevent expensive reconfigurations while |
| 1573 // setting up codecs etc. | 1566 // setting up codecs etc. |
| 1574 if (!sending_) | 1567 if (!sending_) |
| 1575 return; | 1568 return; |
| 1576 | 1569 |
| 1577 if (muted_) { | 1570 if (muted_) { |
| 1578 // Create a black frame to transmit instead. | 1571 // Create a black frame to transmit instead. |
| 1579 CreateBlackFrame(&video_frame, static_cast<int>(frame->GetWidth()), | 1572 CreateBlackFrame(&video_frame, |
| 1580 static_cast<int>(frame->GetHeight()), | 1573 static_cast<int>(frame.GetWidth()), |
| 1581 frame->GetVideoRotation()); | 1574 static_cast<int>(frame.GetHeight()), |
| 1575 video_frame.rotation()); |
| 1582 } | 1576 } |
| 1583 | 1577 |
| 1584 int64_t frame_delta_ms = frame->GetTimeStamp() / rtc::kNumNanosecsPerMillisec; | 1578 int64_t frame_delta_ms = frame.GetTimeStamp() / rtc::kNumNanosecsPerMillisec; |
| 1585 // frame->GetTimeStamp() is essentially a delta, align to webrtc time | 1579 // frame->GetTimeStamp() is essentially a delta, align to webrtc time |
| 1586 if (first_frame_timestamp_ms_ == 0) { | 1580 if (first_frame_timestamp_ms_ == 0) { |
| 1587 first_frame_timestamp_ms_ = rtc::Time() - frame_delta_ms; | 1581 first_frame_timestamp_ms_ = rtc::Time() - frame_delta_ms; |
| 1588 } | 1582 } |
| 1589 | 1583 |
| 1590 last_frame_timestamp_ms_ = first_frame_timestamp_ms_ + frame_delta_ms; | 1584 last_frame_timestamp_ms_ = first_frame_timestamp_ms_ + frame_delta_ms; |
| 1591 video_frame.set_render_time_ms(last_frame_timestamp_ms_); | 1585 video_frame.set_render_time_ms(last_frame_timestamp_ms_); |
| 1592 // Reconfigure codec if necessary. | 1586 // Reconfigure codec if necessary. |
| 1593 SetDimensions( | 1587 SetDimensions(video_frame.width(), video_frame.height(), |
| 1594 video_frame.width(), video_frame.height(), capturer->IsScreencast()); | 1588 capturer_->IsScreencast()); |
| 1595 last_rotation_ = video_frame.rotation(); | 1589 last_rotation_ = video_frame.rotation(); |
| 1596 | 1590 |
| 1597 stream_->Input()->IncomingCapturedFrame(video_frame); | 1591 stream_->Input()->IncomingCapturedFrame(video_frame); |
| 1598 } | 1592 } |
| 1599 | 1593 |
| 1600 bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetCapturer( | 1594 bool WebRtcVideoChannel2::WebRtcVideoSendStream::SetCapturer( |
| 1601 VideoCapturer* capturer) { | 1595 VideoCapturer* capturer) { |
| 1602 TRACE_EVENT0("webrtc", "WebRtcVideoSendStream::SetCapturer"); | 1596 TRACE_EVENT0("webrtc", "WebRtcVideoSendStream::SetCapturer"); |
| 1603 if (!DisconnectCapturer() && capturer == NULL) { | 1597 if (!DisconnectCapturer() && capturer == NULL) { |
| 1604 return false; | 1598 return false; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 1627 last_frame_timestamp_ms_ += 1; | 1621 last_frame_timestamp_ms_ += 1; |
| 1628 black_frame.set_render_time_ms(last_frame_timestamp_ms_); | 1622 black_frame.set_render_time_ms(last_frame_timestamp_ms_); |
| 1629 stream_->Input()->IncomingCapturedFrame(black_frame); | 1623 stream_->Input()->IncomingCapturedFrame(black_frame); |
| 1630 } | 1624 } |
| 1631 | 1625 |
| 1632 capturer_ = NULL; | 1626 capturer_ = NULL; |
| 1633 return true; | 1627 return true; |
| 1634 } | 1628 } |
| 1635 | 1629 |
| 1636 capturer_ = capturer; | 1630 capturer_ = capturer; |
| 1631 capturer_->AddOrUpdateSink(this, sink_wants_); |
| 1637 } | 1632 } |
| 1638 // Lock cannot be held while connecting the capturer to prevent lock-order | |
| 1639 // violations. | |
| 1640 capturer->SignalVideoFrame.connect(this, &WebRtcVideoSendStream::InputFrame); | |
| 1641 return true; | 1633 return true; |
| 1642 } | 1634 } |
| 1643 | 1635 |
| 1644 void WebRtcVideoChannel2::WebRtcVideoSendStream::MuteStream(bool mute) { | 1636 void WebRtcVideoChannel2::WebRtcVideoSendStream::MuteStream(bool mute) { |
| 1645 rtc::CritScope cs(&lock_); | 1637 rtc::CritScope cs(&lock_); |
| 1646 muted_ = mute; | 1638 muted_ = mute; |
| 1647 } | 1639 } |
| 1648 | 1640 |
| 1649 bool WebRtcVideoChannel2::WebRtcVideoSendStream::DisconnectCapturer() { | 1641 bool WebRtcVideoChannel2::WebRtcVideoSendStream::DisconnectCapturer() { |
| 1650 cricket::VideoCapturer* capturer; | 1642 cricket::VideoCapturer* capturer; |
| 1651 { | 1643 { |
| 1652 rtc::CritScope cs(&lock_); | 1644 rtc::CritScope cs(&lock_); |
| 1653 if (capturer_ == NULL) | 1645 if (capturer_ == NULL) |
| 1654 return false; | 1646 return false; |
| 1655 | 1647 |
| 1656 if (capturer_->video_adapter() != nullptr) | 1648 if (capturer_->video_adapter() != nullptr) |
| 1657 old_adapt_changes_ += capturer_->video_adapter()->adaptation_changes(); | 1649 old_adapt_changes_ += capturer_->video_adapter()->adaptation_changes(); |
| 1658 | 1650 |
| 1659 capturer = capturer_; | 1651 capturer = capturer_; |
| 1660 capturer_ = NULL; | 1652 capturer_ = NULL; |
| 1661 } | 1653 } |
| 1662 capturer->SignalVideoFrame.disconnect(this); | 1654 capturer->RemoveSink(this); |
| 1655 |
| 1663 return true; | 1656 return true; |
| 1664 } | 1657 } |
| 1665 | 1658 |
| 1666 const std::vector<uint32_t>& | 1659 const std::vector<uint32_t>& |
| 1667 WebRtcVideoChannel2::WebRtcVideoSendStream::GetSsrcs() const { | 1660 WebRtcVideoChannel2::WebRtcVideoSendStream::GetSsrcs() const { |
| 1668 return ssrcs_; | 1661 return ssrcs_; |
| 1669 } | 1662 } |
| 1670 | 1663 |
| 1671 void WebRtcVideoChannel2::WebRtcVideoSendStream::SetOptions( | 1664 void WebRtcVideoChannel2::WebRtcVideoSendStream::SetOptions( |
| 1672 const VideoOptions& options) { | 1665 const VideoOptions& options) { |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1790 rtc::CritScope cs(&lock_); | 1783 rtc::CritScope cs(&lock_); |
| 1791 // |recreate_stream| means construction-time parameters have changed and the | 1784 // |recreate_stream| means construction-time parameters have changed and the |
| 1792 // sending stream needs to be reset with the new config. | 1785 // sending stream needs to be reset with the new config. |
| 1793 bool recreate_stream = false; | 1786 bool recreate_stream = false; |
| 1794 if (params.rtcp_mode) { | 1787 if (params.rtcp_mode) { |
| 1795 parameters_.config.rtp.rtcp_mode = *params.rtcp_mode; | 1788 parameters_.config.rtp.rtcp_mode = *params.rtcp_mode; |
| 1796 recreate_stream = true; | 1789 recreate_stream = true; |
| 1797 } | 1790 } |
| 1798 if (params.rtp_header_extensions) { | 1791 if (params.rtp_header_extensions) { |
| 1799 parameters_.config.rtp.extensions = *params.rtp_header_extensions; | 1792 parameters_.config.rtp.extensions = *params.rtp_header_extensions; |
| 1793 sink_wants_.rotation_applied = !ContainsHeaderExtension( |
| 1794 *params.rtp_header_extensions, kRtpVideoRotationHeaderExtension); |
| 1800 if (capturer_) { | 1795 if (capturer_) { |
| 1801 capturer_->SetApplyRotation(!ContainsHeaderExtension( | 1796 capturer_->AddOrUpdateSink(this, sink_wants_); |
| 1802 *params.rtp_header_extensions, kRtpVideoRotationHeaderExtension)); | |
| 1803 } | 1797 } |
| 1804 recreate_stream = true; | 1798 recreate_stream = true; |
| 1805 } | 1799 } |
| 1806 if (params.max_bandwidth_bps) { | 1800 if (params.max_bandwidth_bps) { |
| 1807 // Max bitrate has changed, reconfigure encoder settings on the next frame | 1801 // Max bitrate has changed, reconfigure encoder settings on the next frame |
| 1808 // or stream recreation. | 1802 // or stream recreation. |
| 1809 parameters_.max_bitrate_bps = *params.max_bandwidth_bps; | 1803 parameters_.max_bitrate_bps = *params.max_bandwidth_bps; |
| 1810 pending_encoder_reconfiguration_ = true; | 1804 pending_encoder_reconfiguration_ = true; |
| 1811 } | 1805 } |
| 1812 // Set codecs and options. | 1806 // Set codecs and options. |
| (...skipping 688 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2501 rtx_mapping[video_codecs[i].codec.id] != | 2495 rtx_mapping[video_codecs[i].codec.id] != |
| 2502 fec_settings.red_payload_type) { | 2496 fec_settings.red_payload_type) { |
| 2503 video_codecs[i].rtx_payload_type = rtx_mapping[video_codecs[i].codec.id]; | 2497 video_codecs[i].rtx_payload_type = rtx_mapping[video_codecs[i].codec.id]; |
| 2504 } | 2498 } |
| 2505 } | 2499 } |
| 2506 | 2500 |
| 2507 return video_codecs; | 2501 return video_codecs; |
| 2508 } | 2502 } |
| 2509 | 2503 |
| 2510 } // namespace cricket | 2504 } // namespace cricket |
| 2511 | |
| 2512 #endif // HAVE_WEBRTC_VIDEO | |
| OLD | NEW |