/*---------------------------------------------------------------------- | DumpRtpPackets +---------------------------------------------------------------------*/ static AP4_Result DumpRtpPackets(AP4_HintTrackReader& reader, const char* file_name) { // create the output stream AP4_ByteStream* output; try { output = new AP4_FileByteStream(file_name, AP4_FileByteStream::STREAM_MODE_WRITE); } catch (AP4_Exception) { fprintf(stderr, "ERROR: cannot open output file (%s)\n", file_name); return AP4_FAILURE; } // read the packets from the reader and write them in the output stream AP4_DataBuffer data(1500); AP4_TimeStamp ts; while(AP4_SUCCEEDED(reader.GetNextPacket(data, ts))) { output->Write(data.GetData(), data.GetDataSize()); AP4_Debug("#########\n\tpacket contains %d bytes\n", data.GetDataSize()); AP4_Debug("\tsent at time stamp %d\n\n", ts); } output->Release(); return AP4_SUCCESS; }
/*---------------------------------------------------------------------- | main +---------------------------------------------------------------------*/ int main(int argc, char** argv) { AP4_Result result = AP4_SUCCESS; // parse the command line if (argc != 2) PrintUsageAndExit(); // create the input stream AP4_ByteStream* input; try { input = new AP4_FileByteStream(argv[1], AP4_FileByteStream::STREAM_MODE_READ); } catch (AP4_Exception) { fprintf(stderr, "ERROR: cannot open input file (%s)\n", argv[1]); return 1; } AP4_File* file = new AP4_File(*input); AP4_Movie* movie = file->GetMovie(); if (movie != NULL) { // get a hint track reader AP4_Track* hint_track = movie->GetTrack(AP4_Track::TYPE_HINT, 1); if (hint_track == NULL) { AP4_Debug("No hint track in this movie\n"); return AP4_FAILURE; } AP4_HintTrackReader reader(*hint_track, *movie, 0x01020304); AP4_String rtp_file_name(argv[1]); rtp_file_name += ".rtp"; // display the sdp AP4_String sdp; reader.GetSdpText(sdp); AP4_Debug("sdp:\n%s\n\n", sdp.c_str()); // dump the packet result = DumpRtpPackets(reader, rtp_file_name.c_str()); if (AP4_FAILED(result)) goto bail; } else { AP4_Debug("No movie found in the file\n"); return AP4_FAILURE; } bail: delete file; input->Release(); return result; }
/*---------------------------------------------------------------------- | main +---------------------------------------------------------------------*/ int main(int argc, char** argv) { if (argc < 3) { PrintUsageAndExit(); } // parse command line AP4_Result result; char** args = argv+1; unsigned char key[16]; bool key_option = false; if (!strcmp(*args, "--key")) { if (argc != 5) { fprintf(stderr, "ERROR: invalid command line\n"); return 1; } ++args; if (AP4_ParseHex(*args++, key, 16)) { fprintf(stderr, "ERROR: invalid hex format for key\n"); return 1; } key_option = true; } // create the input stream AP4_ByteStream* input = NULL; result = AP4_FileByteStream::Create(*args++, AP4_FileByteStream::STREAM_MODE_READ, input); if (AP4_FAILED(result)) { fprintf(stderr, "ERROR: cannot open input (%d)\n", result); } // create the output stream AP4_ByteStream* output = NULL; result = AP4_FileByteStream::Create(*args++, AP4_FileByteStream::STREAM_MODE_WRITE, output); if (AP4_FAILED(result)) { fprintf(stderr, "ERROR: cannot open output (%d)\n", result); } // open the file AP4_File* input_file = new AP4_File(*input); // get the movie AP4_SampleDescription* sample_description; AP4_Track* video_track; AP4_Movie* movie = input_file->GetMovie(); if (movie == NULL) { fprintf(stderr, "ERROR: no movie in file\n"); goto end; } // get the video track video_track = movie->GetTrack(AP4_Track::TYPE_VIDEO); if (video_track == NULL) { fprintf(stderr, "ERROR: no video track found\n"); goto end; } // check that the track is of the right type sample_description = video_track->GetSampleDescription(0); if (sample_description == NULL) { fprintf(stderr, "ERROR: unable to parse sample description\n"); goto end; } // show info AP4_Debug("Video Track:\n"); AP4_Debug(" duration: %ld ms\n", video_track->GetDurationMs()); AP4_Debug(" sample count: %ld\n", video_track->GetSampleCount()); switch (sample_description->GetType()) { case AP4_SampleDescription::TYPE_HEVC: WriteSamples(video_track, sample_description, output); break; case AP4_SampleDescription::TYPE_PROTECTED: if (!key_option) { fprintf(stderr, "ERROR: encrypted tracks require a key\n"); goto end; } DecryptAndWriteSamples(video_track, sample_description, key, output); break; default: fprintf(stderr, "ERROR: unsupported sample type\n"); break; } end: delete input_file; input->Release(); output->Release(); return 0; }
/*---------------------------------------------------------------------- | main +---------------------------------------------------------------------*/ int main(int argc, char** argv) { AP4_Result result; if (argc < 2) { PrintUsageAndExit(); } // open the input AP4_ByteStream* input; try { input = new AP4_FileByteStream(argv[1], AP4_FileByteStream::STREAM_MODE_READ); } catch (AP4_Exception&) { AP4_Debug("ERROR: cannot open input (%s)\n", argv[1]); return 1; } // open the output AP4_ByteStream* output = new AP4_FileByteStream( argv[2], AP4_FileByteStream::STREAM_MODE_WRITE); // create a sample table AP4_SyntheticSampleTable* sample_table = new AP4_SyntheticSampleTable(); // create an ADTS parser AP4_AdtsParser parser; bool initialized = false; unsigned int sample_description_index = 0; // read from the input, feed, and get AAC frames AP4_UI32 sample_rate = 0; AP4_Cardinal sample_count = 0; bool eos = false; for(;;) { // try to get a frame AP4_AacFrame frame; result = parser.FindFrame(frame); if (AP4_SUCCEEDED(result)) { AP4_Debug("AAC frame [%06d]: size = %d, %d kHz, %d ch\n", sample_count, frame.m_Info.m_FrameLength, frame.m_Info.m_SamplingFrequency, frame.m_Info.m_ChannelConfiguration); if (!initialized) { initialized = true; // create a sample description for our samples AP4_DataBuffer dsi; unsigned char aac_dsi[2] = {0x12, 0x10}; dsi.SetData(aac_dsi, 2); AP4_MpegAudioSampleDescription* sample_description = new AP4_MpegAudioSampleDescription( AP4_MPEG4_AUDIO_OTI, // object type frame.m_Info.m_SamplingFrequency, 16, // sample size frame.m_Info.m_ChannelConfiguration, &dsi, // decoder info 6144, // buffer size 128000, // max bitrate 128000); // average bitrate sample_description_index = sample_table->AddSampleDescription(sample_description); sample_rate = frame.m_Info.m_SamplingFrequency; } AP4_MemoryByteStream* sample_data = new AP4_MemoryByteStream(frame.m_Info.m_FrameLength); frame.m_Source->ReadBytes(sample_data->GetBuffer(), frame.m_Info.m_FrameLength); printf("%02x %02x %02x %02x\n", sample_data->GetBuffer()[0], sample_data->GetBuffer()[1], sample_data->GetBuffer()[2], sample_data->GetBuffer()[3]); sample_table->AddSample(*sample_data, 0, frame.m_Info.m_FrameLength, sample_description_index); sample_data->Release(); sample_count++; } else { if (eos) break; } // read some data and feed the parser AP4_UI08 input_buffer[4096]; AP4_Size bytes_read = 0; AP4_Size to_read = parser.GetBytesFree(); if (to_read) { if (to_read > sizeof(input_buffer)) to_read = sizeof(input_buffer); result = input->Read(input_buffer, to_read, &bytes_read); if (AP4_SUCCEEDED(result)) { AP4_Size to_feed = bytes_read; result = parser.Feed(input_buffer, &to_feed); if (AP4_FAILED(result)) { AP4_Debug("ERROR: parser.Feed() failed (%d)\n", result); return 1; } } else { if (result == AP4_ERROR_EOS) { eos = true; } } } } // create an audio track AP4_Track* track = new AP4_Track(AP4_Track::TYPE_AUDIO, sample_table, 0, // track id sample_rate, // movie time scale sample_rate, // track time scale sample_count*1024, // track duration "eng", // language 0, 0); // width, height // create a movie AP4_Movie* movie = new AP4_Movie(); // add the track to the movie movie->AddTrack(track); // create a multimedia file AP4_File* file = new AP4_File(movie); // create a writer to write the file AP4_FileWriter* writer = new AP4_FileWriter(*file); // write the file to the output writer->Write(*output); delete writer; delete file; delete output; return 0; }
/*---------------------------------------------------------------------- | main +---------------------------------------------------------------------*/ int main(int argc, char** argv) { if (argc < 2) { PrintUsageAndExit(); } // init the variables AP4_UI32 track_id = 0; AP4_ByteStream* input = NULL; AP4_ByteStream* track_data = NULL; // parse the command line argv++; char* arg; while ((arg = *argv++)) { if (!strcmp(arg, "-track")) { if (argv[0] && argv[1] && argv[2]) { track_id = atoi(argv[0]); if (track_id == 0) PrintUsageAndExit(); track_data = new AP4_FileByteStream(argv[1], AP4_FileByteStream::STREAM_MODE_WRITE); argv += 2; } else { PrintUsageAndExit(); } } else { try { input = new AP4_FileByteStream(arg, AP4_FileByteStream::STREAM_MODE_READ); } catch(AP4_Exception e) { AP4_Debug("ERROR: cannot open input (%d)\n", e.m_Error); return 1; } } } // open the output AP4_ByteStream* output = new AP4_FileByteStream("-stdout", AP4_FileByteStream::STREAM_MODE_WRITE); // create an inspector AP4_PrintInspector inspector(*output); // inspect the atoms one by one AP4_Atom* atom; //for (int i=0; i<1000; i++) { AP4_AtomFactory& atom_factory = AP4_AtomFactory::DefaultFactory; //MyTypeHandler my_type_handler; //atom_factory.AddTypeHandler(&my_type_handler); while (atom_factory.CreateAtomFromStream(*input, atom) == AP4_SUCCESS) { atom->Inspect(inspector); delete atom; } //input->Seek(0); //} // inspect the track data if needed if ((track_id != 0) && (track_data != NULL)) { //DumpTrackData(file, track_id, track_data); } if (input) input->Release(); if (output) output->Release(); if (track_data) track_data->Release(); return 0; }