1 /*
2   Licensed to the Apache Software Foundation (ASF) under one
3   or more contributor license agreements.  See the NOTICE file
4   distributed with this work for additional information
5   regarding copyright ownership.  The ASF licenses this file
6   to you under the Apache License, Version 2.0 (the
7   "License"); you may not use this file except in compliance
8   with the License.  You may obtain a copy of the License at
9 
10   http://www.apache.org/licenses/LICENSE-2.0
11 
12   Unless required by applicable law or agreed to in writing, software
13   distributed under the License is distributed on an "AS IS" BASIS,
14   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   See the License for the specific language governing permissions and
16   limitations under the License.
17 */
18 
19 #pragma once
20 
21 #include <cstring>
22 #include <cstdlib>
23 #include <cstdio>
24 #include <unistd.h>
25 #include <cinttypes>
26 
27 #include <ts/ts.h>
28 #include <ts/experimental.h>
29 #include <ts/remap.h>
30 #include "mp4_meta.h"
31 
32 class IOHandle
33 {
34 public:
IOHandle()35   IOHandle(){};
36 
~IOHandle()37   ~IOHandle()
38   {
39     if (reader) {
40       TSIOBufferReaderFree(reader);
41       reader = nullptr;
42     }
43 
44     if (buffer) {
45       TSIOBufferDestroy(buffer);
46       buffer = nullptr;
47     }
48   }
49 
50 public:
51   TSVIO vio               = nullptr;
52   TSIOBuffer buffer       = nullptr;
53   TSIOBufferReader reader = nullptr;
54 };
55 
56 class Mp4TransformContext
57 {
58 public:
Mp4TransformContext(float offset,int64_t cl)59   Mp4TransformContext(float offset, int64_t cl)
60     : total(0), tail(0), pos(0), content_length(0), meta_length(0), parse_over(false), raw_transform(false)
61   {
62     res_buffer = TSIOBufferCreate();
63     res_reader = TSIOBufferReaderAlloc(res_buffer);
64     dup_reader = TSIOBufferReaderAlloc(res_buffer);
65 
66     mm.start = offset * 1000;
67     mm.cl    = cl;
68   }
69 
~Mp4TransformContext()70   ~Mp4TransformContext()
71   {
72     if (res_reader) {
73       TSIOBufferReaderFree(res_reader);
74     }
75 
76     if (dup_reader) {
77       TSIOBufferReaderFree(dup_reader);
78     }
79 
80     if (res_buffer) {
81       TSIOBufferDestroy(res_buffer);
82     }
83   }
84 
85 public:
86   IOHandle output;
87   Mp4Meta mm;
88   int64_t total;
89   int64_t tail;
90   int64_t pos;
91   int64_t content_length;
92   int64_t meta_length;
93 
94   TSIOBuffer res_buffer;
95   TSIOBufferReader res_reader;
96   TSIOBufferReader dup_reader;
97 
98   bool parse_over;
99   bool raw_transform;
100 };
101 
102 class Mp4Context
103 {
104 public:
Mp4Context(float s)105   Mp4Context(float s) : start(s), cl(0), mtc(nullptr), transform_added(false){};
106 
~Mp4Context()107   ~Mp4Context()
108   {
109     if (mtc) {
110       delete mtc;
111       mtc = nullptr;
112     }
113   }
114 
115 public:
116   float start;
117   int64_t cl;
118 
119   Mp4TransformContext *mtc;
120 
121   bool transform_added;
122 };
123