• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <webrtc/OpusPacketizer.h>
18 
19 #include "Utils.h"
20 
21 #include <webrtc/RTPSocketHandler.h>
22 
23 #include <https/SafeCallbackable.h>
24 
25 using namespace android;
26 
OpusPacketizer(std::shared_ptr<RunLoop> runLoop,std::shared_ptr<StreamingSource> audioSource)27 OpusPacketizer::OpusPacketizer(
28         std::shared_ptr<RunLoop> runLoop,
29         std::shared_ptr<StreamingSource> audioSource)
30     : Packetizer(runLoop, audioSource),
31       mFirstInTalkspurt(true) {
32 }
33 
packetize(const std::shared_ptr<SBuffer> & accessUnit,int64_t timeUs)34 void OpusPacketizer::packetize(const std::shared_ptr<SBuffer> &accessUnit, int64_t timeUs) {
35     LOG(VERBOSE) << "Received Opus frame of size " << accessUnit->size();
36 
37     static constexpr uint8_t PT = 98;
38     static constexpr uint32_t SSRC = 0x8badf00d;
39 
40     // XXX Retransmission packets add 2 bytes (for the original seqNum), should
41     // probably reserve that amount in the original packets so we don't exceed
42     // the MTU on retransmission.
43     static const size_t kMaxSRTPPayloadSize =
44         RTPSocketHandler::kMaxUDPPayloadSize - SRTP_MAX_TRAILER_LEN;
45 
46     const uint8_t *audioData = accessUnit->data();
47     size_t size = accessUnit->size();
48 
49     uint32_t rtpTime = ((timeUs - mediaStartTime()) * 48) / 1000;
50 
51     CHECK_LE(12 + size, kMaxSRTPPayloadSize);
52 
53     std::vector<uint8_t> packet(12 + size);
54     uint8_t *data = packet.data();
55 
56     packet[0] = 0x80;
57     packet[1] = PT;
58 
59     if (mFirstInTalkspurt) {
60         packet[1] |= 0x80;  // (M)ark
61         mFirstInTalkspurt = false;
62     }
63 
64     SET_U16(&data[2], 0);  // seqNum
65     SET_U32(&data[4], rtpTime);
66     SET_U32(&data[8], SSRC);
67 
68     memcpy(&data[12], audioData, size);
69 
70     queueRTPDatagram(&packet);
71 }
72 
rtpNow() const73 uint32_t OpusPacketizer::rtpNow() const {
74     return (timeSinceStart() * 48) / 1000;
75 }
76