1 /* 2 * Copyright (C) 2017 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 package com.google.android.exoplayer2.metadata.emsg; 17 18 import com.google.android.exoplayer2.metadata.Metadata; 19 import com.google.android.exoplayer2.metadata.MetadataDecoder; 20 import com.google.android.exoplayer2.metadata.MetadataInputBuffer; 21 import com.google.android.exoplayer2.util.Assertions; 22 import com.google.android.exoplayer2.util.ParsableByteArray; 23 import java.nio.ByteBuffer; 24 import java.util.Arrays; 25 26 /** Decodes data encoded by {@link EventMessageEncoder}. */ 27 public final class EventMessageDecoder implements MetadataDecoder { 28 29 @Override decode(MetadataInputBuffer inputBuffer)30 public Metadata decode(MetadataInputBuffer inputBuffer) { 31 ByteBuffer buffer = Assertions.checkNotNull(inputBuffer.data); 32 Assertions.checkArgument( 33 buffer.position() == 0 && buffer.hasArray() && buffer.arrayOffset() == 0); 34 return new Metadata(decode(new ParsableByteArray(buffer.array(), buffer.limit()))); 35 } 36 decode(ParsableByteArray emsgData)37 public EventMessage decode(ParsableByteArray emsgData) { 38 String schemeIdUri = Assertions.checkNotNull(emsgData.readNullTerminatedString()); 39 String value = Assertions.checkNotNull(emsgData.readNullTerminatedString()); 40 long durationMs = emsgData.readUnsignedInt(); 41 long id = emsgData.readUnsignedInt(); 42 byte[] messageData = 43 Arrays.copyOfRange(emsgData.data, emsgData.getPosition(), emsgData.limit()); 44 return new EventMessage(schemeIdUri, value, durationMs, id, messageData); 45 } 46 } 47