Using Supplemental Enhancement Information (SEI)

更新时间:
复制 MD 格式

The Real-Time Communication (RTC) SDK lets you send and receive Supplemental Enhancement Information (SEI). This topic describes how to use SEI.

Scenarios

  • You can use SEI to pass timestamps, calculate end-to-end network latency, or synchronize data with your other services.
  • You can use SEI to pass bit control information. You can pass 8 bytes (64 bits) of data. Each bit or group of bits can represent control information, which you can use to transmit instructions for your services.

Sample code

The following code examples show how to use SEI to pass a timestamp.

  • iOS and Mac
    • Sender:
      NSTimeInterval now = [[NSDate dateWithTimeIntervalSinceNow:0] timeIntervalSince1970];
      long long lNow = CFSwapInt64BigToHost((long long)(now * 1000));
      NSData * timeData = [NSData dataWithBytes:&lNow length:sizeof(lNow)];
      [self.engine sendMediaExtensionMsg:timeData repeatCount:5];
    • Receiver:
      - (void)onMediaExtensionMsgReceived:(NSString *)uid message:(NSData *)data {
          long long receivedLongLongValue = 0;
          [data getBytes:&receivedLongLongValue length:8];
          long long sentTime= CFSwapInt64BigToHost(receivedLongLongValue);
          NSTimeInterval now = [[NSDate dateWithTimeIntervalSinceNow:0] timeIntervalSince1970];
          long long nowTime= (long long)(now * 1000);
        NSLog(@"%@", [NSString stringWithFormat:@"uid: %@, now: %lld, sent: %lld, difference: %lld", uid, nowTime, sentTime, (nowTime-sentTime)];)}
  • Android
    • Sender:
      long lNow =System.currentTimeMillis();
      byte b[] = new byte[8];
      ByteBuffer buf = ByteBuffer.wrap(b);
      buf.putLong(l);
      buf.flip();
      mAliRtcEngine.sendMediaExtensionMsg(buf.array(), 5);
    • Receiver:
      public void onMediaExtensionMsgReceived(String uid, byte[] message) {
        super.onMediaExtensionMsgReceived(uid, message);
        ByteBuffer buffer = ByteBuffer.allocate(8);
        buffer.put(message, 0, message.length);
        buffer.flip();
        long sentTime =buffer.getLong();
      }
  • Windows
    • Sender:
      char* long2charArr(long long num)
      {
        char* arr = new char[8];
        int x = 56;
        for (int i = 7; i >= 0; i--)
        {
          long long temp = num << x;
          arr[i] = temp >> 56;
          x -= 8;
        }
        return arr;
      }
      
      SYSTEMTIME tmSys;
      GetLocalTime(&tmSys);
      CTime ctmNow(tmSys);
      long long lNow = __int64(ctmNow.GetTime()) * 1000 + tmSys.wMilliseconds;
      char* cNow = long2charArr(lNow);
      mpEngine->sendMediaExtensionMsg((unsigned char*)cNow, 8, 5);
      delete cNow;
    • Receiver:
      long long charArr2long(char* p)
      {
        long long value = 0;
        value = (((long long)p[0] << 56 & 0xFF00000000000000L) |
        ((long long)p[1] << 48 & 0xFF000000000000L) |
        ((long long)p[2] << 40 & 0xFF0000000000L) |
        ((long long)p[3] << 32 & 0xFF00000000L) |
        ((long long)p[4] << 24 & 0xFF000000L) |
        ((long long)p[5] << 16 & 0xFF0000L) |
        ((long long)p[6] << 8 & 0xFF00L) |
        ((long long)p[7] & 0xFFL));
        return value;
      }
      
      void CTutorialDlg::onMediaExtensionMsgReceived(const AliRtc::String &uid, unsigned char* message, int size)
      {
        long long sentTime = charArr2long((char*)message);
      }

Notes

Because SEI messages reuse the audio and video data tunnel, you must control the frequency and length of your custom messages. Note the following limits:
  • To avoid affecting media data transmission quality, the custom message body is limited to 8 bytes. You can use this to transmit information such as timestamps and bit control information.
  • You can send a maximum of 30 messages per second.
  • repeatCount parameter of the sendMediaExtensionMsg function sets the message redundancy. If the value is greater than 1, the message is sent multiple times to prevent message loss caused by network packet loss. Other users in the channel will receive the same message multiple times. You must remove the duplicate messages.
  • Subscribers in the channel also receive the custom messages during bypass live streaming.
  • The H5 client does not support sending or receiving SEI messages.