1 /*
2  * Copyright (C) 2016 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.scte35;
17 
18 import android.os.Parcel;
19 import com.google.android.exoplayer2.C;
20 import com.google.android.exoplayer2.util.ParsableByteArray;
21 import com.google.android.exoplayer2.util.TimestampAdjuster;
22 
23 /**
24  * Represents a time signal command as defined in SCTE35, Section 9.3.4.
25  */
26 public final class TimeSignalCommand extends SpliceCommand {
27 
28   public final long ptsTime;
29   public final long playbackPositionUs;
30 
TimeSignalCommand(long ptsTime, long playbackPositionUs)31   private TimeSignalCommand(long ptsTime, long playbackPositionUs) {
32     this.ptsTime = ptsTime;
33     this.playbackPositionUs = playbackPositionUs;
34   }
35 
parseFromSection(ParsableByteArray sectionData, long ptsAdjustment, TimestampAdjuster timestampAdjuster)36   /* package */ static TimeSignalCommand parseFromSection(ParsableByteArray sectionData,
37       long ptsAdjustment, TimestampAdjuster timestampAdjuster) {
38     long ptsTime = parseSpliceTime(sectionData, ptsAdjustment);
39     long playbackPositionUs = timestampAdjuster.adjustTsTimestamp(ptsTime);
40     return new TimeSignalCommand(ptsTime, playbackPositionUs);
41   }
42 
43   /**
44    * Parses pts_time from splice_time(), defined in Section 9.4.1. Returns {@link C#TIME_UNSET}, if
45    * time_specified_flag is false.
46    *
47    * @param sectionData The section data from which the pts_time is parsed.
48    * @param ptsAdjustment The pts adjustment provided by the splice info section header.
49    * @return The pts_time defined by splice_time(), or {@link C#TIME_UNSET}, if time_specified_flag
50    *     is false.
51    */
parseSpliceTime(ParsableByteArray sectionData, long ptsAdjustment)52   /* package */ static long parseSpliceTime(ParsableByteArray sectionData, long ptsAdjustment) {
53     long firstByte = sectionData.readUnsignedByte();
54     long ptsTime = C.TIME_UNSET;
55     if ((firstByte & 0x80) != 0 /* time_specified_flag */) {
56       // See SCTE35 9.2.1 for more information about pts adjustment.
57       ptsTime = (firstByte & 0x01) << 32 | sectionData.readUnsignedInt();
58       ptsTime += ptsAdjustment;
59       ptsTime &= 0x1FFFFFFFFL;
60     }
61     return ptsTime;
62   }
63 
64   // Parcelable implementation.
65 
66   @Override
writeToParcel(Parcel dest, int flags)67   public void writeToParcel(Parcel dest, int flags) {
68     dest.writeLong(ptsTime);
69     dest.writeLong(playbackPositionUs);
70   }
71 
72   public static final Creator<TimeSignalCommand> CREATOR =
73       new Creator<TimeSignalCommand>() {
74 
75     @Override
76     public TimeSignalCommand createFromParcel(Parcel in) {
77       return new TimeSignalCommand(in.readLong(), in.readLong());
78     }
79 
80     @Override
81     public TimeSignalCommand[] newArray(int size) {
82       return new TimeSignalCommand[size];
83     }
84 
85   };
86 
87 }
88